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