]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/process_utils.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #ifndef __LXC_PROCESS_UTILS_H
4 #define __LXC_PROCESS_UTILS_H
14 #include <sys/syscall.h>
18 #define CSIGNAL 0x000000ff /* signal mask to be sent at exit */
22 #define CLONE_VM 0x00000100 /* set if VM shared between processes */
26 #define CLONE_FS 0x00000200 /* set if fs info shared between processes */
30 #define CLONE_FILES 0x00000400 /* set if open files shared between processes */
34 #define CLONE_SIGHAND 0x00000800 /* set if signal handlers and blocked signals shared */
38 #define CLONE_PIDFD 0x00001000 /* set if a pidfd should be placed in parent */
42 #define CLONE_PTRACE 0x00002000 /* set if we want to let tracing continue on the child too */
46 #define CLONE_VFORK 0x00004000 /* set if the parent wants the child to wake it up on mm_release */
50 #define CLONE_PARENT 0x00008000 /* set if we want to have the same parent as the cloner */
54 #define CLONE_THREAD 0x00010000 /* Same thread group? */
58 #define CLONE_NEWNS 0x00020000 /* New mount namespace group */
62 #define CLONE_SYSVSEM 0x00040000 /* share system V SEM_UNDO semantics */
66 #define CLONE_SETTLS 0x00080000 /* create a new TLS for the child */
69 #ifndef CLONE_PARENT_SETTID
70 #define CLONE_PARENT_SETTID 0x00100000 /* set the TID in the parent */
73 #ifndef CLONE_CHILD_CLEARTID
74 #define CLONE_CHILD_CLEARTID 0x00200000 /* clear the TID in the child */
77 #ifndef CLONE_DETACHED
78 #define CLONE_DETACHED 0x00400000 /* Unused, ignored */
81 #ifndef CLONE_UNTRACED
82 #define CLONE_UNTRACED 0x00800000 /* set if the tracing process can't force CLONE_PTRACE on this clone */
85 #ifndef CLONE_CHILD_SETTID
86 #define CLONE_CHILD_SETTID 0x01000000 /* set the TID in the child */
89 #ifndef CLONE_NEWCGROUP
90 #define CLONE_NEWCGROUP 0x02000000 /* New cgroup namespace */
94 #define CLONE_NEWUTS 0x04000000 /* New utsname namespace */
98 #define CLONE_NEWIPC 0x08000000 /* New ipc namespace */
101 #ifndef CLONE_NEWUSER
102 #define CLONE_NEWUSER 0x10000000 /* New user namespace */
106 #define CLONE_NEWPID 0x20000000 /* New pid namespace */
110 #define CLONE_NEWNET 0x40000000 /* New network namespace */
114 #define CLONE_IO 0x80000000 /* Clone io context */
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. */
122 #ifndef CLONE_INTO_CGROUP
123 #define CLONE_INTO_CGROUP 0x200000000ULL /* Clone into a specific cgroup given the right permissions. */
127 * cloning flags intersect with CSIGNAL so can be used with unshare and clone3
130 #ifndef CLONE_NEWTIME
131 #define CLONE_NEWTIME 0x00000080 /* New time namespace */
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
, ...);
144 int clone(int (*fn
)(void *), void *child_stack
, int flags
, void *arg
, ...
145 /* pid_t *ptid, struct user_desc *tls, pid_t *ctid */);
149 * lxc_clone() - create a new process
152 * This function allocates a new stack the size of page and passes it to the
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.
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}.
167 * - should call lxc_raw_getpid():
168 * The child should use lxc_raw_getpid() to retrieve its pid.
170 extern pid_t
lxc_clone(int (*fn
)(void *), void *arg
, int flags
, int *pidfd
);
174 * lxc_raw_clone() - create a new process
177 * This function returns 0 in the child and > 0 in the parent.
180 * This function does not allocate a new stack and relies on copy-on-write
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.
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
197 * - must call lxc_raw_getpid():
198 * The child must use lxc_raw_getpid() to retrieve its pid.
200 extern pid_t
lxc_raw_clone(unsigned long flags
, int *pidfd
);
203 * lxc_raw_clone_cb() - create a new process
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".
210 * All other comments that apply to lxc_raw_clone() apply to lxc_raw_clone_cb()
213 extern pid_t
lxc_raw_clone_cb(int (*fn
)(void *), void *args
,
214 unsigned long flags
, int *pidfd
);
216 #ifndef HAVE_EXECVEAT
217 static inline int execveat(int dirfd
, const char *pathname
, char *const argv
[],
218 char *const envp
[], int flags
)
220 return syscall(__NR_execveat
, dirfd
, pathname
, argv
, envp
, flags
);
223 extern int execveat(int dirfd
, const char *pathname
, char *const argv
[],
224 char *const envp
[], int flags
);
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().
231 static inline pid_t
lxc_raw_getpid(void)
233 return (pid_t
)syscall(SYS_getpid
);
236 static inline pid_t
lxc_raw_gettid(void)
239 return syscall(__NR_gettid
);
241 return lxc_raw_getpid();
245 extern int lxc_raw_pidfd_send_signal(int pidfd
, int sig
, siginfo_t
*info
,
248 #endif /* __LXC_PROCESS_UTILS_H */