]>
Commit | Line | Data |
---|---|---|
13be2733 CB |
1 | /* liblxcapi |
2 | * | |
3 | * Copyright © 2018 Christian Brauner <christian.brauner@ubuntu.com>. | |
4 | * Copyright © 2018 Canonical Ltd. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2, as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 | */ | |
19 | ||
20 | #ifndef __LXC_RAW_SYSCALL_H | |
21 | #define __LXC_RAW_SYSCALL_H | |
22 | ||
23 | #ifndef _GNU_SOURCE | |
24 | #define _GNU_SOURCE 1 | |
25 | #endif | |
38e5c2db | 26 | #include <sched.h> |
13be2733 CB |
27 | #include <stdio.h> |
28 | #include <stdlib.h> | |
d9bb2fba | 29 | #include <signal.h> |
38e5c2db | 30 | #include <sys/syscall.h> |
d7b58715 | 31 | #include <unistd.h> |
38e5c2db | 32 | |
a59440be CB |
33 | /* clone */ |
34 | #ifndef CLONE_PIDFD | |
35 | #define CLONE_PIDFD 0x00001000 | |
36 | #endif | |
37 | ||
38e5c2db CB |
38 | /* |
39 | * lxc_raw_clone() - create a new process | |
40 | * | |
41 | * - fork() behavior: | |
42 | * This function returns 0 in the child and > 0 in the parent. | |
43 | * | |
44 | * - copy-on-write: | |
45 | * This function does not allocate a new stack and relies on copy-on-write | |
46 | * semantics. | |
47 | * | |
48 | * - supports subset of ClONE_* flags: | |
49 | * lxc_raw_clone() intentionally only supports a subset of the flags available | |
50 | * to the actual system call. Please refer to the implementation what flags | |
51 | * cannot be used. Also, please don't assume that just because a flag isn't | |
52 | * explicitly checked for as being unsupported that it is supported. If in | |
53 | * doubt or not sufficiently familiar with process creation in the kernel and | |
54 | * interactions with libcs this function should be used. | |
55 | * | |
56 | * - no pthread_atfork() handlers: | |
57 | * This function circumvents - as much as this this is possible - any libc | |
58 | * wrappers and thus does not run any pthread_atfork() handlers. Make sure | |
59 | * that this is safe to do in the context you are trying to call this | |
60 | * function. | |
61 | * | |
62 | * - must call lxc_raw_getpid(): | |
63 | * The child must use lxc_raw_getpid() to retrieve its pid. | |
64 | */ | |
a59440be | 65 | extern pid_t lxc_raw_clone(unsigned long flags, int *pidfd); |
38e5c2db CB |
66 | |
67 | /* | |
68 | * lxc_raw_clone_cb() - create a new process | |
69 | * | |
70 | * - non-fork() behavior: | |
71 | * Function does return pid of the child or -1 on error. Pass in a callback | |
72 | * function via the "fn" argument that gets executed in the child process. | |
73 | * The "args" argument is passed to "fn". | |
74 | * | |
75 | * All other comments that apply to lxc_raw_clone() apply to lxc_raw_clone_cb() | |
76 | * as well. | |
77 | */ | |
a59440be CB |
78 | extern pid_t lxc_raw_clone_cb(int (*fn)(void *), void *args, |
79 | unsigned long flags, int *pidfd); | |
13be2733 CB |
80 | |
81 | extern int lxc_raw_execveat(int dirfd, const char *pathname, char *const argv[], | |
82 | char *const envp[], int flags); | |
83 | ||
d7b58715 CB |
84 | /* |
85 | * Because of older glibc's pid cache (up to 2.25) whenever clone() is called | |
86 | * the child must must retrieve it's own pid via lxc_raw_getpid(). | |
87 | */ | |
88 | static inline pid_t lxc_raw_getpid(void) | |
89 | { | |
90 | return (pid_t)syscall(SYS_getpid); | |
91 | } | |
92 | ||
07a50156 CB |
93 | static inline pid_t lxc_raw_gettid(void) |
94 | { | |
95 | #ifdef __NR_gettid | |
96 | return syscall(__NR_gettid); | |
97 | #else | |
98 | return lxc_raw_getpid(); | |
99 | #endif | |
100 | } | |
101 | ||
d9bb2fba CB |
102 | extern int lxc_raw_pidfd_send_signal(int pidfd, int sig, siginfo_t *info, |
103 | unsigned int flags); | |
104 | ||
13be2733 | 105 | #endif /* __LXC_RAW_SYSCALL_H */ |