implement _Fork and refactor fork using it
authorRich Felker <dalias@aerifal.cx>
Wed, 30 Sep 2020 17:32:59 +0000 (13:32 -0400)
committerRich Felker <dalias@aerifal.cx>
Thu, 15 Oct 2020 00:27:12 +0000 (20:27 -0400)
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.

include/unistd.h
src/process/_Fork.c
src/process/fork.c [new file with mode: 0644]

index 07584a2..1306402 100644 (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 *, ...);
index 17fb87a..1f41197 100644 (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);
 }
diff --git a/src/process/fork.c b/src/process/fork.c
new file mode 100644 (file)
index 0000000..a12da01
--- /dev/null
@@ -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;
+}