]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/syscall_wrappers.h
github: Update for main branch
[mirror_lxc.git] / src / lxc / syscall_wrappers.h
1 /* liblxcapi
2 *
3 * Copyright © 2018 Christian Brauner <christian.brauner@ubuntu.com>.
4 * Copyright © 2018 Canonical Ltd.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #ifndef __LXC_SYSCALL_WRAPPER_H
22 #define __LXC_SYSCALL_WRAPPER_H
23
24 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE 1
26 #endif
27 #include <asm/unistd.h>
28 #include <errno.h>
29 #include <linux/keyctl.h>
30 #include <sched.h>
31 #include <stdint.h>
32 #include <sys/syscall.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35
36 #include "config.h"
37
38 #ifdef HAVE_LINUX_MEMFD_H
39 #include <linux/memfd.h>
40 #endif
41
42 #ifdef HAVE_SYS_SIGNALFD_H
43 #include <sys/signalfd.h>
44 #endif
45
46 typedef int32_t key_serial_t;
47
48 #if !HAVE_KEYCTL
49 static inline long __keyctl(int cmd, unsigned long arg2, unsigned long arg3,
50 unsigned long arg4, unsigned long arg5)
51 {
52 #ifdef __NR_keyctl
53 return syscall(__NR_keyctl, cmd, arg2, arg3, arg4, arg5);
54 #else
55 errno = ENOSYS;
56 return -1;
57 #endif
58 }
59 #define keyctl __keyctl
60 #endif
61
62 #ifndef F_LINUX_SPECIFIC_BASE
63 #define F_LINUX_SPECIFIC_BASE 1024
64 #endif
65 #ifndef F_ADD_SEALS
66 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
67 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
68 #endif
69 #ifndef F_SEAL_SEAL
70 #define F_SEAL_SEAL 0x0001
71 #define F_SEAL_SHRINK 0x0002
72 #define F_SEAL_GROW 0x0004
73 #define F_SEAL_WRITE 0x0008
74 #endif
75
76 #ifndef HAVE_MEMFD_CREATE
77 static inline int memfd_create(const char *name, unsigned int flags) {
78 #ifndef __NR_memfd_create
79 #if defined __i386__
80 #define __NR_memfd_create 356
81 #elif defined __x86_64__
82 #define __NR_memfd_create 319
83 #elif defined __arm__
84 #define __NR_memfd_create 385
85 #elif defined __aarch64__
86 #define __NR_memfd_create 279
87 #elif defined __s390__
88 #define __NR_memfd_create 350
89 #elif defined __powerpc__
90 #define __NR_memfd_create 360
91 #elif defined __sparc__
92 #define __NR_memfd_create 348
93 #elif defined __blackfin__
94 #define __NR_memfd_create 390
95 #elif defined __ia64__
96 #define __NR_memfd_create 1340
97 #elif defined _MIPS_SIM
98 #if _MIPS_SIM == _MIPS_SIM_ABI32
99 #define __NR_memfd_create 4354
100 #endif
101 #if _MIPS_SIM == _MIPS_SIM_NABI32
102 #define __NR_memfd_create 6318
103 #endif
104 #if _MIPS_SIM == _MIPS_SIM_ABI64
105 #define __NR_memfd_create 5314
106 #endif
107 #endif
108 #endif
109 #ifdef __NR_memfd_create
110 return syscall(__NR_memfd_create, name, flags);
111 #else
112 errno = ENOSYS;
113 return -1;
114 #endif
115 }
116 #else
117 extern int memfd_create(const char *name, unsigned int flags);
118 #endif
119
120 #if !HAVE_PIVOT_ROOT
121 static int pivot_root(const char *new_root, const char *put_old)
122 {
123 #ifdef __NR_pivot_root
124 return syscall(__NR_pivot_root, new_root, put_old);
125 #else
126 errno = ENOSYS;
127 return -1;
128 #endif
129 }
130 #else
131 extern int pivot_root(const char *new_root, const char *put_old);
132 #endif
133
134 #if !defined(__NR_setns) && !defined(__NR_set_ns)
135 #if defined(__x86_64__)
136 #define __NR_setns 308
137 #elif defined(__i386__)
138 #define __NR_setns 346
139 #elif defined(__arm__)
140 #define __NR_setns 375
141 #elif defined(__aarch64__)
142 #define __NR_setns 375
143 #elif defined(__powerpc__)
144 #define __NR_setns 350
145 #elif defined(__s390__)
146 #define __NR_setns 339
147 #endif
148 #endif
149
150 /* Define sethostname() if missing from the C library */
151 #ifndef HAVE_SETHOSTNAME
152 static inline int sethostname(const char *name, size_t len)
153 {
154 #ifdef __NR_sethostname
155 return syscall(__NR_sethostname, name, len);
156 #else
157 errno = ENOSYS;
158 return -1;
159 #endif
160 }
161 #endif
162
163 /* Define setns() if missing from the C library */
164 #ifndef HAVE_SETNS
165 static inline int setns(int fd, int nstype)
166 {
167 #ifdef __NR_setns
168 return syscall(__NR_setns, fd, nstype);
169 #elif defined(__NR_set_ns)
170 return syscall(__NR_set_ns, fd, nstype);
171 #else
172 errno = ENOSYS;
173 return -1;
174 #endif
175 }
176 #endif
177
178 #ifndef HAVE_SYS_SIGNALFD_H
179 struct signalfd_siginfo {
180 uint32_t ssi_signo;
181 int32_t ssi_errno;
182 int32_t ssi_code;
183 uint32_t ssi_pid;
184 uint32_t ssi_uid;
185 int32_t ssi_fd;
186 uint32_t ssi_tid;
187 uint32_t ssi_band;
188 uint32_t ssi_overrun;
189 uint32_t ssi_trapno;
190 int32_t ssi_status;
191 int32_t ssi_int;
192 uint64_t ssi_ptr;
193 uint64_t ssi_utime;
194 uint64_t ssi_stime;
195 uint64_t ssi_addr;
196 uint8_t __pad[48];
197 };
198
199 #ifndef __NR_signalfd4
200 /* assume kernel headers are too old */
201 #if __i386__
202 #define __NR_signalfd4 327
203 #elif __x86_64__
204 #define __NR_signalfd4 289
205 #elif __powerpc__
206 #define __NR_signalfd4 313
207 #elif __s390x__
208 #define __NR_signalfd4 322
209 #elif __arm__
210 #define __NR_signalfd4 355
211 #elif __mips__ && _MIPS_SIM == _ABIO32
212 #define __NR_signalfd4 4324
213 #elif __mips__ && _MIPS_SIM == _ABI64
214 #define __NR_signalfd4 5283
215 #elif __mips__ && _MIPS_SIM == _ABIN32
216 #define __NR_signalfd4 6287
217 #endif
218 #endif
219
220 #ifndef __NR_signalfd
221 /* assume kernel headers are too old */
222 #if __i386__
223 #define __NR_signalfd 321
224 #elif __x86_64__
225 #define __NR_signalfd 282
226 #elif __powerpc__
227 #define __NR_signalfd 305
228 #elif __s390x__
229 #define __NR_signalfd 316
230 #elif __arm__
231 #define __NR_signalfd 349
232 #elif __mips__ && _MIPS_SIM == _ABIO32
233 #define __NR_signalfd 4317
234 #elif __mips__ && _MIPS_SIM == _ABI64
235 #define __NR_signalfd 5276
236 #elif __mips__ && _MIPS_SIM == _ABIN32
237 #define __NR_signalfd 6280
238 #endif
239 #endif
240
241 static inline int signalfd(int fd, const sigset_t *mask, int flags)
242 {
243 int retval;
244
245 retval = syscall(__NR_signalfd4, fd, mask, _NSIG / 8, flags);
246 if (errno == ENOSYS && flags == 0)
247 retval = syscall(__NR_signalfd, fd, mask, _NSIG / 8);
248
249 return retval;
250 }
251 #endif
252
253 /* Define unshare() if missing from the C library */
254 #ifndef HAVE_UNSHARE
255 static inline int unshare(int flags)
256 {
257 #ifdef __NR_unshare
258 return syscall(__NR_unshare, flags);
259 #else
260 errno = ENOSYS;
261 return -1;
262 #endif
263 }
264 #else
265 extern int unshare(int);
266 #endif
267
268 #endif /* __LXC_SYSCALL_WRAPPER_H */