]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/raw-clone.h
Merge pull request #8417 from brauner/2018-03-09/add_bind_mount_fallback_to_private_d...
[thirdparty/systemd.git] / src / basic / raw-clone.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5 This file is part of systemd.
6
7 Copyright 2010 Lennart Poettering
8 Copyright 2016 Michael Karcher
9 ***/
10
11 #include <sched.h>
12 #include <sys/syscall.h>
13
14 #include "log.h"
15 #include "macro.h"
16
17 /**
18 * raw_clone() - uses clone to create a new process with clone flags
19 * @flags: Flags to pass to the clone system call
20 *
21 * Uses the clone system call to create a new process with the cloning flags and termination signal passed in the flags
22 * parameter. Opposed to glibc's clone funtion, using this function does not set up a separate stack for the child, but
23 * relies on copy-on-write semantics on the one stack at a common virtual address, just as fork does.
24 *
25 * To obtain copy-on-write semantics, flags must not contain CLONE_VM, and thus CLONE_THREAD and CLONE_SIGHAND (which
26 * require CLONE_VM) are not usable.
27 *
28 * Additionally, as this function does not pass the ptid, newtls and ctid parameters to the kernel, flags must not
29 * contain CLONE_PARENT_SETTID, CLONE_CHILD_SETTID, CLONE_CHILD_CLEARTID or CLONE_SETTLS.
30 *
31 * Returns: 0 in the child process and the child process id in the parent.
32 */
33 static inline pid_t raw_clone(unsigned long flags) {
34 pid_t ret;
35
36 assert((flags & (CLONE_VM|CLONE_PARENT_SETTID|CLONE_CHILD_SETTID|
37 CLONE_CHILD_CLEARTID|CLONE_SETTLS)) == 0);
38 #if defined(__s390x__) || defined(__s390__) || defined(__CRIS__)
39 /* On s390/s390x and cris the order of the first and second arguments
40 * of the raw clone() system call is reversed. */
41 ret = (pid_t) syscall(__NR_clone, NULL, flags);
42 #elif defined(__sparc__) && defined(__arch64__)
43 {
44 /**
45 * sparc64 always returns the other process id in %o0, and
46 * a boolean flag whether this is the child or the parent in
47 * %o1. Inline assembly is needed to get the flag returned
48 * in %o1.
49 */
50 int in_child, child_pid;
51
52 asm volatile("mov %2, %%g1\n\t"
53 "mov %3, %%o0\n\t"
54 "mov 0 , %%o1\n\t"
55 "t 0x6d\n\t"
56 "mov %%o1, %0\n\t"
57 "mov %%o0, %1" :
58 "=r"(in_child), "=r"(child_pid) :
59 "i"(__NR_clone), "r"(flags) :
60 "%o1", "%o0", "%g1" );
61
62 ret = in_child ? 0 : child_pid;
63 }
64 #else
65 ret = (pid_t) syscall(__NR_clone, flags, NULL);
66 #endif
67
68 if (ret == 0)
69 reset_cached_pid();
70
71 return ret;
72 }