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