support linux kernel apis (new archs) with old syscalls removed
[musl] / src / process / fork.c
1 #include <unistd.h>
2 #include <string.h>
3 #include <signal.h>
4 #include "syscall.h"
5 #include "libc.h"
6 #include "pthread_impl.h"
7
8 static void dummy(int x)
9 {
10 }
11
12 weak_alias(dummy, __fork_handler);
13
14 pid_t fork(void)
15 {
16         pid_t ret;
17         sigset_t set;
18         __fork_handler(-1);
19         __block_all_sigs(&set);
20 #ifdef SYS_fork
21         ret = syscall(SYS_fork);
22 #else
23         ret = syscall(SYS_clone, SIGCHLD, 0);
24 #endif
25         if (libc.has_thread_pointer && !ret) {
26                 pthread_t self = __pthread_self();
27                 self->tid = self->pid = __syscall(SYS_getpid);
28                 memset(&self->robust_list, 0, sizeof self->robust_list);
29                 libc.threads_minus_1 = 0;
30         }
31         __restore_sigs(&set);
32         __fork_handler(!ret);
33         return ret;
34 }