implement _Fork and refactor fork using it

the _Fork interface is defined for future issue of POSIX as the
outcome of Austin Group issue 62, which drops the AS-safety
requirement for fork, and provides an AS-safe replacement that does
not run the registered atfork handlers.
This commit is contained in:
Rich Felker 2020-09-30 13:32:59 -04:00
parent e1e98d869c
commit bd153422f2
3 changed files with 16 additions and 9 deletions

View File

@ -82,6 +82,7 @@ unsigned sleep(unsigned);
int pause(void);
pid_t fork(void);
pid_t _Fork(void);
int execve(const char *, char *const [], char *const []);
int execv(const char *, char *const []);
int execle(const char *, const char *, ...);

View File

@ -1,23 +1,17 @@
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include "syscall.h"
#include "libc.h"
#include "lock.h"
#include "pthread_impl.h"
static void dummy(int x)
{
}
weak_alias(dummy, __fork_handler);
static void dummy(int x) { }
weak_alias(dummy, __aio_atfork);
pid_t fork(void)
pid_t _Fork(void)
{
pid_t ret;
sigset_t set;
__fork_handler(-1);
__block_all_sigs(&set);
__aio_atfork(-1);
LOCK(__abort_lock);
@ -39,6 +33,5 @@ pid_t fork(void)
UNLOCK(__abort_lock);
__aio_atfork(!ret);
__restore_sigs(&set);
__fork_handler(!ret);
return __syscall_ret(ret);
}

13
src/process/fork.c Normal file
View File

@ -0,0 +1,13 @@
#include <unistd.h>
#include "libc.h"
static void dummy(int x) { }
weak_alias(dummy, __fork_handler);
pid_t fork(void)
{
__fork_handler(-1);
pid_t ret = _Fork();
__fork_handler(!ret);
return ret;
}