rename fork source file
[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 "lock.h"
7 #include "pthread_impl.h"
8
9 static void dummy(int x)
10 {
11 }
12
13 weak_alias(dummy, __fork_handler);
14 weak_alias(dummy, __aio_atfork);
15
16 pid_t fork(void)
17 {
18         pid_t ret;
19         sigset_t set;
20         __fork_handler(-1);
21         __block_all_sigs(&set);
22         __aio_atfork(-1);
23         LOCK(__abort_lock);
24 #ifdef SYS_fork
25         ret = __syscall(SYS_fork);
26 #else
27         ret = __syscall(SYS_clone, SIGCHLD, 0);
28 #endif
29         if (!ret) {
30                 pthread_t self = __pthread_self();
31                 self->tid = __syscall(SYS_gettid);
32                 self->robust_list.off = 0;
33                 self->robust_list.pending = 0;
34                 self->next = self->prev = self;
35                 __thread_list_lock = 0;
36                 libc.threads_minus_1 = 0;
37                 if (libc.need_locks) libc.need_locks = -1;
38         }
39         UNLOCK(__abort_lock);
40         __aio_atfork(!ret);
41         __restore_sigs(&set);
42         __fork_handler(!ret);
43         return __syscall_ret(ret);
44 }