chiark / gitweb /
c801939d1945356ef0ff46e856e1f6d5c9346f43
[elogind.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 <errno.h>
12 #include <sched.h>
13 #include <sys/syscall.h>
14
15 #include "log.h"
16 #include "macro.h"
17
18 /**
19  * raw_clone() - uses clone to create a new process with clone flags
20  * @flags: Flags to pass to the clone system call
21  *
22  * Uses the clone system call to create a new process with the cloning flags and termination signal passed in the flags
23  * parameter. Opposed to glibc's clone funtion, using this function does not set up a separate stack for the child, but
24  * relies on copy-on-write semantics on the one stack at a common virtual address, just as fork does.
25  *
26  * To obtain copy-on-write semantics, flags must not contain CLONE_VM, and thus CLONE_THREAD and CLONE_SIGHAND (which
27  * require CLONE_VM) are not usable.
28  *
29  * Additionally, as this function does not pass the ptid, newtls and ctid parameters to the kernel, flags must not
30  * contain CLONE_PARENT_SETTID, CLONE_CHILD_SETTID, CLONE_CHILD_CLEARTID or CLONE_SETTLS.
31  *
32  * Returns: 0 in the child process and the child process id in the parent.
33  */
34 static inline pid_t raw_clone(unsigned long flags) {
35         pid_t ret;
36
37         assert((flags & (CLONE_VM|CLONE_PARENT_SETTID|CLONE_CHILD_SETTID|
38                          CLONE_CHILD_CLEARTID|CLONE_SETTLS)) == 0);
39 #if defined(__s390x__) || defined(__s390__) || defined(__CRIS__)
40         /* On s390/s390x and cris the order of the first and second arguments
41          * of the raw clone() system call is reversed. */
42         ret = (pid_t) syscall(__NR_clone, NULL, flags);
43 #elif defined(__sparc__)
44         {
45                 /**
46                  * sparc always returns the other process id in %o0, and
47                  * a boolean flag whether this is the child or the parent in
48                  * %o1. Inline assembly is needed to get the flag returned
49                  * in %o1.
50                  */
51                 int in_child, child_pid, error;
52
53                 asm volatile("mov %3, %%g1\n\t"
54                              "mov %4, %%o0\n\t"
55                              "mov 0 , %%o1\n\t"
56 #if defined(__arch64__)
57                              "t 0x6d\n\t"
58 #else
59                              "t 0x10\n\t"
60 #endif
61                              "addx %%g0, 0, %2\n\t"
62                              "mov %%o1, %0\n\t"
63                              "mov %%o0, %1" :
64                              "=r"(in_child), "=r"(child_pid), "=r"(error) :
65                              "i"(__NR_clone), "r"(flags) :
66                              "%o1", "%o0", "%g1" "cc" );
67
68                 if (error) {
69                         errno = child_pid;
70                         ret = -1;
71                 } else
72                         ret = in_child ? 0 : child_pid;
73         }
74 #else
75         ret = (pid_t) syscall(__NR_clone, flags, NULL);
76 #endif
77
78         if (ret == 0)
79                 reset_cached_pid();
80
81         return ret;
82 }