fix reintroduction of errno clobbering by atfork handlers

commit bd153422f2 reintroduced the bug
fixed in c21051e90c by refactoring the
__syscall_ret into _Fork where it once again runs before the atfork
handlers are called. since _Fork is a public interface that sets
errno, this can't be fixed the way it was fixed last time without
making new internal interfaces. instead, just save errno, and restore
it only on error to ensure that a value of 0 is never restored.
This commit is contained in:
Rich Felker 2020-10-26 18:06:18 -04:00
parent 2d0bbe6c78
commit 3437e478ba

View file

@ -1,4 +1,5 @@
#include <unistd.h>
#include <errno.h>
#include "libc.h"
static void dummy(int x) { }
@ -8,6 +9,8 @@ pid_t fork(void)
{
__fork_handler(-1);
pid_t ret = _Fork();
int errno_save = errno;
__fork_handler(!ret);
if (ret<0) errno = errno_save;
return ret;
}