• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef __LITEOS__
9 #include "proc_xid_impl.h"
10 #endif
11 #include "fork_impl.h"
12 
dummy(int x)13 static void dummy(int x) { }
14 weak_alias(dummy, __aio_atfork);
15 
__post_Fork(int ret)16 void __post_Fork(int ret)
17 {
18 	if (!ret) {
19 		pthread_t self = __pthread_self();
20 #ifdef __LITEOS_A__
21 		self->tid = __syscall(SYS_gettid);
22 		self->pid = __syscall(SYS_getpid);
23 #else
24 		self->tid = __syscall(SYS_set_tid_address, &__thread_list_lock);
25 		self->pid = self->tid;
26 #endif
27 		self->proc_tid = -1;
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 #ifndef __LITEOS__
34 		__clear_proc_pid();
35 		// Setting the child process fdsan is invalid,
36 		// preventing the child process from closing all fds and causing large batch errors
37 		fdsan_set_error_level(FDSAN_ERROR_LEVEL_DISABLED);
38 #endif
39 		if (libc.need_locks) libc.need_locks = -1;
40 #ifdef __LITEOS_A__
41 		libc.exit = 0;
42 		signal(SIGSYS, arm_do_signal);
43 #endif
44 	}
45 	UNLOCK(__abort_lock);
46 	if (!ret) __aio_atfork(1);
47 }
48 
_Fork(void)49 pid_t _Fork(void)
50 {
51 	pid_t ret;
52 	sigset_t set;
53 
54 	__block_all_sigs(&set);
55 	LOCK(__abort_lock);
56 
57 #ifdef SYS_fork
58 	ret = __syscall(SYS_fork);
59 #else
60 	ret = __syscall(SYS_clone, SIGCHLD, 0);
61 #endif
62 
63 	__post_Fork(ret);
64 	__restore_sigs(&set);
65 
66 	return __syscall_ret(ret);
67 }
68