]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/process_utils.h
Merge pull request #4062 from stgraber/master
[mirror_lxc.git] / src / lxc / process_utils.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef __LXC_PROCESS_UTILS_H
4 #define __LXC_PROCESS_UTILS_H
5
6 #include "config.h"
7
8 #include <linux/sched.h>
9 #include <sched.h>
10 #include <signal.h>
11 #include <stdbool.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/syscall.h>
16 #include <unistd.h>
17
18 #include "compiler.h"
19 #include "syscall_numbers.h"
20
21 #ifndef CSIGNAL
22 #define CSIGNAL 0x000000ff /* signal mask to be sent at exit */
23 #endif
24
25 #ifndef CLONE_VM
26 #define CLONE_VM 0x00000100 /* set if VM shared between processes */
27 #endif
28
29 #ifndef CLONE_FS
30 #define CLONE_FS 0x00000200 /* set if fs info shared between processes */
31 #endif
32
33 #ifndef CLONE_FILES
34 #define CLONE_FILES 0x00000400 /* set if open files shared between processes */
35 #endif
36
37 #ifndef CLONE_SIGHAND
38 #define CLONE_SIGHAND 0x00000800 /* set if signal handlers and blocked signals shared */
39 #endif
40
41 #ifndef CLONE_PIDFD
42 #define CLONE_PIDFD 0x00001000 /* set if a pidfd should be placed in parent */
43 #endif
44
45 #ifndef CLONE_PTRACE
46 #define CLONE_PTRACE 0x00002000 /* set if we want to let tracing continue on the child too */
47 #endif
48
49 #ifndef CLONE_VFORK
50 #define CLONE_VFORK 0x00004000 /* set if the parent wants the child to wake it up on mm_release */
51 #endif
52
53 #ifndef CLONE_PARENT
54 #define CLONE_PARENT 0x00008000 /* set if we want to have the same parent as the cloner */
55 #endif
56
57 #ifndef CLONE_THREAD
58 #define CLONE_THREAD 0x00010000 /* Same thread group? */
59 #endif
60
61 #ifndef CLONE_NEWNS
62 #define CLONE_NEWNS 0x00020000 /* New mount namespace group */
63 #endif
64
65 #ifndef CLONE_SYSVSEM
66 #define CLONE_SYSVSEM 0x00040000 /* share system V SEM_UNDO semantics */
67 #endif
68
69 #ifndef CLONE_SETTLS
70 #define CLONE_SETTLS 0x00080000 /* create a new TLS for the child */
71 #endif
72
73 #ifndef CLONE_PARENT_SETTID
74 #define CLONE_PARENT_SETTID 0x00100000 /* set the TID in the parent */
75 #endif
76
77 #ifndef CLONE_CHILD_CLEARTID
78 #define CLONE_CHILD_CLEARTID 0x00200000 /* clear the TID in the child */
79 #endif
80
81 #ifndef CLONE_DETACHED
82 #define CLONE_DETACHED 0x00400000 /* Unused, ignored */
83 #endif
84
85 #ifndef CLONE_UNTRACED
86 #define CLONE_UNTRACED 0x00800000 /* set if the tracing process can't force CLONE_PTRACE on this clone */
87 #endif
88
89 #ifndef CLONE_CHILD_SETTID
90 #define CLONE_CHILD_SETTID 0x01000000 /* set the TID in the child */
91 #endif
92
93 #ifndef CLONE_NEWCGROUP
94 #define CLONE_NEWCGROUP 0x02000000 /* New cgroup namespace */
95 #endif
96
97 #ifndef CLONE_NEWUTS
98 #define CLONE_NEWUTS 0x04000000 /* New utsname namespace */
99 #endif
100
101 #ifndef CLONE_NEWIPC
102 #define CLONE_NEWIPC 0x08000000 /* New ipc namespace */
103 #endif
104
105 #ifndef CLONE_NEWUSER
106 #define CLONE_NEWUSER 0x10000000 /* New user namespace */
107 #endif
108
109 #ifndef CLONE_NEWPID
110 #define CLONE_NEWPID 0x20000000 /* New pid namespace */
111 #endif
112
113 #ifndef CLONE_NEWNET
114 #define CLONE_NEWNET 0x40000000 /* New network namespace */
115 #endif
116
117 #ifndef CLONE_IO
118 #define CLONE_IO 0x80000000 /* Clone io context */
119 #endif
120
121 /* Flags for the clone3() syscall. */
122 #ifndef CLONE_CLEAR_SIGHAND
123 #define CLONE_CLEAR_SIGHAND 0x100000000ULL /* Clear any signal handler and reset to SIG_DFL. */
124 #endif
125
126 #ifndef CLONE_INTO_CGROUP
127 #define CLONE_INTO_CGROUP 0x200000000ULL /* Clone into a specific cgroup given the right permissions. */
128 #endif
129
130 /*
131 * cloning flags intersect with CSIGNAL so can be used with unshare and clone3
132 * syscalls only:
133 */
134 #ifndef CLONE_NEWTIME
135 #define CLONE_NEWTIME 0x00000080 /* New time namespace */
136 #endif
137
138 /* waitid */
139 #ifndef P_PIDFD
140 #define P_PIDFD 3
141 #endif
142
143 #ifndef CLONE_ARGS_SIZE_VER0
144 #define CLONE_ARGS_SIZE_VER0 64 /* sizeof first published struct */
145 #endif
146
147 #ifndef CLONE_ARGS_SIZE_VER1
148 #define CLONE_ARGS_SIZE_VER1 80 /* sizeof second published struct */
149 #endif
150
151 #ifndef CLONE_ARGS_SIZE_VER2
152 #define CLONE_ARGS_SIZE_VER2 88 /* sizeof third published struct */
153 #endif
154
155 #ifndef ptr_to_u64
156 #define ptr_to_u64(ptr) ((__u64)((uintptr_t)(ptr)))
157 #endif
158 #ifndef u64_to_ptr
159 #define u64_to_ptr(x) ((void *)(uintptr_t)x)
160 #endif
161
162 struct lxc_clone_args {
163 __aligned_u64 flags;
164 __aligned_u64 pidfd;
165 __aligned_u64 child_tid;
166 __aligned_u64 parent_tid;
167 __aligned_u64 exit_signal;
168 __aligned_u64 stack;
169 __aligned_u64 stack_size;
170 __aligned_u64 tls;
171 __aligned_u64 set_tid;
172 __aligned_u64 set_tid_size;
173 __aligned_u64 cgroup;
174 };
175
176 __returns_twice static inline pid_t lxc_clone3(struct lxc_clone_args *args, size_t size)
177 {
178 return syscall(__NR_clone3, args, size);
179 }
180
181 #if defined(__ia64__)
182 int __clone2(int (*__fn)(void *__arg), void *__child_stack_base,
183 size_t __child_stack_size, int __flags, void *__arg, ...);
184 #else
185 int clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ...
186 /* pid_t *ptid, struct user_desc *tls, pid_t *ctid */);
187 #endif
188
189 /**
190 * lxc_clone() - create a new process
191 *
192 * - allocate stack:
193 * This function allocates a new stack the size of page and passes it to the
194 * kernel.
195 *
196 * - support all CLONE_*flags:
197 * This function supports all CLONE_* flags. If in doubt or not sufficiently
198 * familiar with process creation in the kernel and interactions with libcs
199 * this function should be used.
200 *
201 * - pthread_atfork() handlers depending on libc:
202 * Whether this function runs pthread_atfork() handlers depends on the
203 * corresponding libc wrapper. glibc currently does not run pthread_atfork()
204 * handlers but does not guarantee that they are not. Other libcs might or
205 * might not run pthread_atfork() handlers. If you require guarantees please
206 * refer to the lxc_raw_clone*() functions in process_utils.{c,h}.
207 *
208 * - should call lxc_raw_getpid():
209 * The child should use lxc_raw_getpid() to retrieve its pid.
210 */
211 __hidden extern pid_t lxc_clone(int (*fn)(void *), void *arg, int flags, int *pidfd);
212
213
214 /*
215 * lxc_raw_clone() - create a new process
216 *
217 * - fork() behavior:
218 * This function returns 0 in the child and > 0 in the parent.
219 *
220 * - copy-on-write:
221 * This function does not allocate a new stack and relies on copy-on-write
222 * semantics.
223 *
224 * - supports subset of ClONE_* flags:
225 * lxc_raw_clone() intentionally only supports a subset of the flags available
226 * to the actual system call. Please refer to the implementation what flags
227 * cannot be used. Also, please don't assume that just because a flag isn't
228 * explicitly checked for as being unsupported that it is supported. If in
229 * doubt or not sufficiently familiar with process creation in the kernel and
230 * interactions with libcs this function should be used.
231 *
232 * - no pthread_atfork() handlers:
233 * This function circumvents - as much as this this is possible - any libc
234 * wrappers and thus does not run any pthread_atfork() handlers. Make sure
235 * that this is safe to do in the context you are trying to call this
236 * function.
237 *
238 * - must call lxc_raw_getpid():
239 * The child must use lxc_raw_getpid() to retrieve its pid.
240 */
241 __hidden extern pid_t lxc_raw_clone(unsigned long flags, int *pidfd);
242 __hidden extern pid_t lxc_raw_legacy_clone(unsigned long flags, int *pidfd);
243
244 /*
245 * lxc_raw_clone_cb() - create a new process
246 *
247 * - non-fork() behavior:
248 * Function does return pid of the child or -1 on error. Pass in a callback
249 * function via the "fn" argument that gets executed in the child process.
250 * The "args" argument is passed to "fn".
251 *
252 * All other comments that apply to lxc_raw_clone() apply to lxc_raw_clone_cb()
253 * as well.
254 */
255 __hidden extern pid_t lxc_raw_clone_cb(int (*fn)(void *), void *args, unsigned long flags,
256 int *pidfd);
257
258 #if !HAVE_EXECVEAT
259 static inline int execveat(int dirfd, const char *pathname, char *const argv[],
260 char *const envp[], int flags)
261 {
262 return syscall(__NR_execveat, dirfd, pathname, argv, envp, flags);
263 }
264 #else
265 extern int execveat(int dirfd, const char *pathname, char *const argv[],
266 char *const envp[], int flags);
267 #endif
268
269 /*
270 * Because of older glibc's pid cache (up to 2.25) whenever clone() is called
271 * the child must must retrieve it's own pid via lxc_raw_getpid().
272 */
273 static inline pid_t lxc_raw_getpid(void)
274 {
275 return (pid_t)syscall(SYS_getpid);
276 }
277
278 static inline pid_t lxc_raw_gettid(void)
279 {
280 #if __NR_gettid > 0
281 return syscall(__NR_gettid);
282 #else
283 return lxc_raw_getpid();
284 #endif
285 }
286
287 __hidden extern int lxc_raw_pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
288 unsigned int flags);
289
290 static inline const char *signal_name(int sig)
291 {
292 const char *s;
293
294 #if HAVE_SIGDESCR_NP
295 s = sigdescr_np(sig);
296 #else
297 s = "UNSUPPORTED";
298 #endif
299 return s ?: "INVALID_SIGNAL_NUMBER";
300 }
301
302 #endif /* __LXC_PROCESS_UTILS_H */