9c07792dd1537ebd845a48f020f1ad833917261d
[musl] / src / process / _Fork.c
1 #include <unistd.h>
2 #include <signal.h>
3 #include "syscall.h"
4 #include "libc.h"
5 #include "lock.h"
6 #include "pthread_impl.h"
7 #include "aio_impl.h"
8 #include "fork_impl.h"
9
10 static void dummy(int x) { }
11 weak_alias(dummy, __aio_atfork);
12
13 void __post_Fork(int ret)
14 {
15         if (!ret) {
16                 pthread_t self = __pthread_self();
17                 self->tid = __syscall(SYS_set_tid_address, &__thread_list_lock);
18                 self->robust_list.off = 0;
19                 self->robust_list.pending = 0;
20                 self->next = self->prev = self;
21                 __thread_list_lock = 0;
22                 libc.threads_minus_1 = 0;
23                 if (libc.need_locks) libc.need_locks = -1;
24         }
25         UNLOCK(__abort_lock);
26         if (!ret) __aio_atfork(1);
27 }
28
29 pid_t _Fork(void)
30 {
31         pid_t ret;
32         sigset_t set;
33         __block_all_sigs(&set);
34         LOCK(__abort_lock);
35 #ifdef SYS_fork
36         ret = __syscall(SYS_fork);
37 #else
38         ret = __syscall(SYS_clone, SIGCHLD, 0);
39 #endif
40         __post_Fork(ret);
41         __restore_sigs(&set);
42         return __syscall_ret(ret);
43 }