1*71db0c75SAndroid Build Coastguard Worker //===-- Linux implementation of fork --------------------------------------===// 2*71db0c75SAndroid Build Coastguard Worker // 3*71db0c75SAndroid Build Coastguard Worker // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*71db0c75SAndroid Build Coastguard Worker // See https://llvm.org/LICENSE.txt for license information. 5*71db0c75SAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*71db0c75SAndroid Build Coastguard Worker // 7*71db0c75SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===// 8*71db0c75SAndroid Build Coastguard Worker 9*71db0c75SAndroid Build Coastguard Worker #include "src/unistd/fork.h" 10*71db0c75SAndroid Build Coastguard Worker 11*71db0c75SAndroid Build Coastguard Worker #include "src/__support/OSUtil/syscall.h" // For internal syscall function. 12*71db0c75SAndroid Build Coastguard Worker #include "src/__support/common.h" 13*71db0c75SAndroid Build Coastguard Worker #include "src/__support/macros/config.h" 14*71db0c75SAndroid Build Coastguard Worker #include "src/__support/threads/fork_callbacks.h" 15*71db0c75SAndroid Build Coastguard Worker #include "src/__support/threads/identifier.h" 16*71db0c75SAndroid Build Coastguard Worker #include "src/__support/threads/thread.h" // For thread self object 17*71db0c75SAndroid Build Coastguard Worker 18*71db0c75SAndroid Build Coastguard Worker #include "src/errno/libc_errno.h" 19*71db0c75SAndroid Build Coastguard Worker #include <signal.h> // For SIGCHLD 20*71db0c75SAndroid Build Coastguard Worker #include <sys/syscall.h> // For syscall numbers. 21*71db0c75SAndroid Build Coastguard Worker 22*71db0c75SAndroid Build Coastguard Worker namespace LIBC_NAMESPACE_DECL { 23*71db0c75SAndroid Build Coastguard Worker 24*71db0c75SAndroid Build Coastguard Worker // The implementation of fork here is very minimal. We will add more 25*71db0c75SAndroid Build Coastguard Worker // functionality and standard compliance in future. 26*71db0c75SAndroid Build Coastguard Worker 27*71db0c75SAndroid Build Coastguard Worker LLVM_LIBC_FUNCTION(pid_t, fork, (void)) { 28*71db0c75SAndroid Build Coastguard Worker invoke_prepare_callbacks(); 29*71db0c75SAndroid Build Coastguard Worker pid_t parent_tid = internal::gettid(); 30*71db0c75SAndroid Build Coastguard Worker // Invalidate parent's tid cache before forking. We cannot do this in child 31*71db0c75SAndroid Build Coastguard Worker // process because in the post-fork instruction windows, there may be a signal 32*71db0c75SAndroid Build Coastguard Worker // handler triggered which may get the wrong tid. 33*71db0c75SAndroid Build Coastguard Worker internal::force_set_tid(0); 34*71db0c75SAndroid Build Coastguard Worker #ifdef SYS_fork 35*71db0c75SAndroid Build Coastguard Worker pid_t ret = syscall_impl<pid_t>(SYS_fork); 36*71db0c75SAndroid Build Coastguard Worker #elif defined(SYS_clone) 37*71db0c75SAndroid Build Coastguard Worker pid_t ret = syscall_impl<pid_t>(SYS_clone, SIGCHLD, 0); 38*71db0c75SAndroid Build Coastguard Worker #else 39*71db0c75SAndroid Build Coastguard Worker #error "fork and clone syscalls not available." 40*71db0c75SAndroid Build Coastguard Worker #endif 41*71db0c75SAndroid Build Coastguard Worker 42*71db0c75SAndroid Build Coastguard Worker if (ret == 0) { 43*71db0c75SAndroid Build Coastguard Worker // Return value is 0 in the child process. 44*71db0c75SAndroid Build Coastguard Worker // The child is created with a single thread whose self object will be a 45*71db0c75SAndroid Build Coastguard Worker // copy of parent process' thread which called fork. So, we have to fix up 46*71db0c75SAndroid Build Coastguard Worker // the child process' self object with the new process' tid. 47*71db0c75SAndroid Build Coastguard Worker internal::force_set_tid(syscall_impl<pid_t>(SYS_gettid)); 48*71db0c75SAndroid Build Coastguard Worker invoke_child_callbacks(); 49*71db0c75SAndroid Build Coastguard Worker return 0; 50*71db0c75SAndroid Build Coastguard Worker } 51*71db0c75SAndroid Build Coastguard Worker 52*71db0c75SAndroid Build Coastguard Worker if (ret < 0) { 53*71db0c75SAndroid Build Coastguard Worker // Error case, a child process was not created. 54*71db0c75SAndroid Build Coastguard Worker libc_errno = static_cast<int>(-ret); 55*71db0c75SAndroid Build Coastguard Worker return -1; 56*71db0c75SAndroid Build Coastguard Worker } 57*71db0c75SAndroid Build Coastguard Worker // recover parent's tid. 58*71db0c75SAndroid Build Coastguard Worker internal::force_set_tid(parent_tid); 59*71db0c75SAndroid Build Coastguard Worker invoke_parent_callbacks(); 60*71db0c75SAndroid Build Coastguard Worker return ret; 61*71db0c75SAndroid Build Coastguard Worker } 62*71db0c75SAndroid Build Coastguard Worker 63*71db0c75SAndroid Build Coastguard Worker } // namespace LIBC_NAMESPACE_DECL 64