Debian Codegraph @

src/basic/raw-clone.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 /***
4 Copyright © 2016 Michael Karcher
5 ***/
6
7 #include <errno.h> /* IWYU pragma: keep */
8 #include <sched.h>
9 #include <sys/syscall.h> /* IWYU pragma: keep */
10 #include <unistd.h>
11
12 #include "process-util.h"
13 #include "raw-clone.h"
14
15 /**
16 * raw_clone() - uses clone to create a new process with clone flags
17 * @flags: Flags to pass to the clone system call
18 *
19 * Uses the clone system call to create a new process with the cloning flags and termination signal passed in the flags
20 * parameter. Opposed to glibc's clone function, using this function does not set up a separate stack for the child, but
21 * relies on copy-on-write semantics on the one stack at a common virtual address, just as fork does.
22 *
23 * To obtain copy-on-write semantics, flags must not contain CLONE_VM, and thus CLONE_THREAD and CLONE_SIGHAND
24 * (which require CLONE_VM) are not usable.
25 *
26 * Additionally, as this function does not pass the ptid (pidfd in the case of CLONE_PIDFD), newtls and ctid
27 * parameters to the kernel, flags must not contain CLONE_PARENT_SETTID, CLONE_CHILD_SETTID, CLONE_CHILD_CLEARTID,
28 * CLONE_SETTLS, or CLONE_PIDFD.
29 *
30 * WARNING: 💣 this call (just like glibc's own clone() wrapper) will not synchronize on glibc's malloc
31 * locks, which means they will be in an undefined state in the child if the parent is
32 * threaded. This means: the parent must either never use threads, or the child cannot use memory
33 * allocation itself. This is a major pitfall, hence be careful! 💣
34 *
35 * Returns: 0 in the child process and the child process id in the parent.
36 */
37 pid_t raw_clone(unsigned long flags) {
38 pid_t ret;
39
40 assert((flags & (CLONE_VM|CLONE_PARENT_SETTID|CLONE_CHILD_SETTID|CLONE_CHILD_CLEARTID|CLONE_SETTLS|CLONE_PIDFD)) == 0);
41 #if defined(__s390x__) || defined(__s390__) || defined(__CRIS__)
42 /* On s390/s390x and cris the order of the first and second arguments
43 * of the raw clone() system call is reversed. */
44 ret = (pid_t) syscall(__NR_clone, NULL, flags);
45 #elif defined(__sparc__)
46 {
47 /**
48 * sparc always returns the other process id in %o0, and
49 * a boolean flag whether this is the child or the parent in
50 * %o1. Inline assembly is needed to get the flag returned
51 * in %o1.
52 */
53 int in_child, child_pid, error;
54
55 asm volatile("mov %3, %%g1\n\t"
56 "mov %4, %%o0\n\t"
57 "mov 0 , %%o1\n\t"
58 #if defined(__arch64__)
59 "t 0x6d\n\t"
60 #else
61 "t 0x10\n\t"
62 #endif
63 "addx %%g0, 0, %2\n\t"
64 "mov %%o1, %0\n\t"
65 "mov %%o0, %1" :
66 "=r"(in_child), "=r"(child_pid), "=r"(error) :
67 "i"(__NR_clone), "r"(flags) :
68 "%o1", "%o0", "%g1", "cc" );
69
70 if (error) {
71 errno = child_pid;
72 ret = -1;
73 } else
74 ret = in_child ? 0 : child_pid;
75 }
76 #else
77 ret = (pid_t) syscall(__NR_clone, flags, NULL);
78 #endif
79
80 if (ret == 0)
81 reset_cached_pid();
82
83 return ret;
84 }