fix fork of processes with active async io contexts
[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 weak_alias(dummy, __aio_atfork);
14
15 pid_t fork(void)
16 {
17         pid_t ret;
18         sigset_t set;
19         __fork_handler(-1);
20         __block_all_sigs(&set);
21         __aio_atfork(-1);
22 #ifdef SYS_fork
23         ret = __syscall(SYS_fork);
24 #else
25         ret = __syscall(SYS_clone, SIGCHLD, 0);
26 #endif
27         if (!ret) {
28                 pthread_t self = __pthread_self();
29                 self->tid = __syscall(SYS_gettid);
30                 self->robust_list.off = 0;
31                 self->robust_list.pending = 0;
32                 self->next = self->prev = self;
33                 __thread_list_lock = 0;
34                 libc.threads_minus_1 = 0;
35                 if (libc.need_locks) libc.need_locks = -1;
36         }
37         __aio_atfork(!ret);
38         __restore_sigs(&set);
39         __fork_handler(!ret);
40         return __syscall_ret(ret);
41 }