chiark / gitweb /
tree-wide: drop 'This file is part of systemd' blurb
[elogind.git] / src / basic / raw-clone.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5   Copyright 2010 Lennart Poettering
6   Copyright 2016 Michael Karcher
7 ***/
8
9 //#include <errno.h>
10 #include <sched.h>
11 #include <sys/syscall.h>
12
13 #include "log.h"
14 #include "macro.h"
15
16 /**
17  * raw_clone() - uses clone to create a new process with clone flags
18  * @flags: Flags to pass to the clone system call
19  *
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.
23  *
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.
26  *
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.
29  *
30  * Returns: 0 in the child process and the child process id in the parent.
31  */
32 static inline pid_t raw_clone(unsigned long flags) {
33         pid_t ret;
34
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__)
42         {
43                 /**
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
47                  * in %o1.
48                  */
49                 int in_child, child_pid, error;
50
51                 asm volatile("mov %3, %%g1\n\t"
52                              "mov %4, %%o0\n\t"
53                              "mov 0 , %%o1\n\t"
54 #if defined(__arch64__)
55                              "t 0x6d\n\t"
56 #else
57                              "t 0x10\n\t"
58 #endif
59                              "addx %%g0, 0, %2\n\t"
60                              "mov %%o1, %0\n\t"
61                              "mov %%o0, %1" :
62                              "=r"(in_child), "=r"(child_pid), "=r"(error) :
63                              "i"(__NR_clone), "r"(flags) :
64                              "%o1", "%o0", "%g1" "cc" );
65
66                 if (error) {
67                         errno = child_pid;
68                         ret = -1;
69                 } else
70                         ret = in_child ? 0 : child_pid;
71         }
72 #else
73         ret = (pid_t) syscall(__NR_clone, flags, NULL);
74 #endif
75
76         if (ret == 0)
77                 reset_cached_pid();
78
79         return ret;
80 }