github.com/afumu/libc@v0.0.6/musl/src/process/fork.c (about)

     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 (!ret) {
    26  		pthread_t self = __pthread_self();
    27  		self->tid = __syscall(SYS_gettid);
    28  		self->robust_list.off = 0;
    29  		self->robust_list.pending = 0;
    30  		self->next = self->prev = self;
    31  		__thread_list_lock = 0;
    32  		libc.threads_minus_1 = 0;
    33  		if (libc.need_locks) libc.need_locks = -1;
    34  	}
    35  	__restore_sigs(&set);
    36  	__fork_handler(!ret);
    37  	return __syscall_ret(ret);
    38  }