]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/raw-clone.h
83d5ff7c215f03b54615563178d677990e6ea1fe
1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 Copyright 2010 Lennart Poettering
6 Copyright 2016 Michael Karcher
11 #include <sys/syscall.h>
17 * raw_clone() - uses clone to create a new process with clone flags
18 * @flags: Flags to pass to the clone system call
20 * Uses the clone system call to create a new process with the cloning flags and termination signal passed in the flags
21 * parameter. Opposed to glibc's clone funtion, using this function does not set up a separate stack for the child, but
22 * relies on copy-on-write semantics on the one stack at a common virtual address, just as fork does.
24 * To obtain copy-on-write semantics, flags must not contain CLONE_VM, and thus CLONE_THREAD and CLONE_SIGHAND (which
25 * require CLONE_VM) are not usable.
27 * Additionally, as this function does not pass the ptid, newtls and ctid parameters to the kernel, flags must not
28 * contain CLONE_PARENT_SETTID, CLONE_CHILD_SETTID, CLONE_CHILD_CLEARTID or CLONE_SETTLS.
30 * Returns: 0 in the child process and the child process id in the parent.
32 static inline pid_t
raw_clone(unsigned long flags
) {
35 assert((flags
& (CLONE_VM
|CLONE_PARENT_SETTID
|CLONE_CHILD_SETTID
|
36 CLONE_CHILD_CLEARTID
|CLONE_SETTLS
)) == 0);
37 #if defined(__s390x__) || defined(__s390__) || defined(__CRIS__)
38 /* On s390/s390x and cris the order of the first and second arguments
39 * of the raw clone() system call is reversed. */
40 ret
= (pid_t
) syscall(__NR_clone
, NULL
, flags
);
41 #elif defined(__sparc__)
44 * sparc always returns the other process id in %o0, and
45 * a boolean flag whether this is the child or the parent in
46 * %o1. Inline assembly is needed to get the flag returned
49 int in_child
, child_pid
, error
;
51 asm volatile("mov %3, %%g1\n\t"
54 #if defined(__arch64__)
59 "addx %%g0, 0, %2\n\t"
62 "=r"(in_child
), "=r"(child_pid
), "=r"(error
) :
63 "i"(__NR_clone
), "r"(flags
) :
64 "%o1", "%o0", "%g1" "cc" );
70 ret
= in_child
? 0 : child_pid
;
73 ret
= (pid_t
) syscall(__NR_clone
, flags
, NULL
);