]> git.proxmox.com Git - systemd.git/blob - src/basic/missing_syscall.h
New upstream version 240
[systemd.git] / src / basic / missing_syscall.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /* Missing glibc definitions to access certain kernel APIs */
5
6 #include <fcntl.h>
7 #include <sys/syscall.h>
8 #include <sys/types.h>
9 #include <unistd.h>
10
11 #ifdef ARCH_MIPS
12 #include <asm/sgidefs.h>
13 #endif
14
15 #include "missing_keyctl.h"
16 #include "missing_stat.h"
17
18 /* linux/kcmp.h */
19 #ifndef KCMP_FILE /* 3f4994cfc15f38a3159c6e3a4b3ab2e1481a6b02 (3.19) */
20 #define KCMP_FILE 0
21 #endif
22
23 #if !HAVE_PIVOT_ROOT
24 static inline int missing_pivot_root(const char *new_root, const char *put_old) {
25 return syscall(__NR_pivot_root, new_root, put_old);
26 }
27
28 # define pivot_root missing_pivot_root
29 #endif
30
31 /* ======================================================================= */
32
33 #if !HAVE_MEMFD_CREATE
34 # ifndef __NR_memfd_create
35 # if defined __x86_64__
36 # define __NR_memfd_create 319
37 # elif defined __arm__
38 # define __NR_memfd_create 385
39 # elif defined __aarch64__
40 # define __NR_memfd_create 279
41 # elif defined __s390__
42 # define __NR_memfd_create 350
43 # elif defined _MIPS_SIM
44 # if _MIPS_SIM == _MIPS_SIM_ABI32
45 # define __NR_memfd_create 4354
46 # endif
47 # if _MIPS_SIM == _MIPS_SIM_NABI32
48 # define __NR_memfd_create 6318
49 # endif
50 # if _MIPS_SIM == _MIPS_SIM_ABI64
51 # define __NR_memfd_create 5314
52 # endif
53 # elif defined __i386__
54 # define __NR_memfd_create 356
55 # elif defined __arc__
56 # define __NR_memfd_create 279
57 # else
58 # warning "__NR_memfd_create unknown for your architecture"
59 # endif
60 # endif
61
62 static inline int missing_memfd_create(const char *name, unsigned int flags) {
63 # ifdef __NR_memfd_create
64 return syscall(__NR_memfd_create, name, flags);
65 # else
66 errno = ENOSYS;
67 return -1;
68 # endif
69 }
70
71 # define memfd_create missing_memfd_create
72 #endif
73
74 /* ======================================================================= */
75
76 #if !HAVE_GETRANDOM
77 # ifndef __NR_getrandom
78 # if defined __x86_64__
79 # define __NR_getrandom 318
80 # elif defined(__i386__)
81 # define __NR_getrandom 355
82 # elif defined(__arm__)
83 # define __NR_getrandom 384
84 # elif defined(__aarch64__)
85 # define __NR_getrandom 278
86 # elif defined(__ia64__)
87 # define __NR_getrandom 1339
88 # elif defined(__m68k__)
89 # define __NR_getrandom 352
90 # elif defined(__s390x__)
91 # define __NR_getrandom 349
92 # elif defined(__powerpc__)
93 # define __NR_getrandom 359
94 # elif defined _MIPS_SIM
95 # if _MIPS_SIM == _MIPS_SIM_ABI32
96 # define __NR_getrandom 4353
97 # endif
98 # if _MIPS_SIM == _MIPS_SIM_NABI32
99 # define __NR_getrandom 6317
100 # endif
101 # if _MIPS_SIM == _MIPS_SIM_ABI64
102 # define __NR_getrandom 5313
103 # endif
104 # elif defined(__arc__)
105 # define __NR_getrandom 278
106 # else
107 # warning "__NR_getrandom unknown for your architecture"
108 # endif
109 # endif
110
111 static inline int missing_getrandom(void *buffer, size_t count, unsigned flags) {
112 # ifdef __NR_getrandom
113 return syscall(__NR_getrandom, buffer, count, flags);
114 # else
115 errno = ENOSYS;
116 return -1;
117 # endif
118 }
119
120 # define getrandom missing_getrandom
121 #endif
122
123 /* ======================================================================= */
124
125 #if !HAVE_GETTID
126 static inline pid_t missing_gettid(void) {
127 return (pid_t) syscall(__NR_gettid);
128 }
129
130 # define gettid missing_gettid
131 #endif
132
133 /* ======================================================================= */
134
135 #if !HAVE_NAME_TO_HANDLE_AT
136 # ifndef __NR_name_to_handle_at
137 # if defined(__x86_64__)
138 # define __NR_name_to_handle_at 303
139 # elif defined(__i386__)
140 # define __NR_name_to_handle_at 341
141 # elif defined(__arm__)
142 # define __NR_name_to_handle_at 370
143 # elif defined(__powerpc__)
144 # define __NR_name_to_handle_at 345
145 # elif defined(__arc__)
146 # define __NR_name_to_handle_at 264
147 # else
148 # error "__NR_name_to_handle_at is not defined"
149 # endif
150 # endif
151
152 struct file_handle {
153 unsigned int handle_bytes;
154 int handle_type;
155 unsigned char f_handle[0];
156 };
157
158 static inline int missing_name_to_handle_at(int fd, const char *name, struct file_handle *handle, int *mnt_id, int flags) {
159 # ifdef __NR_name_to_handle_at
160 return syscall(__NR_name_to_handle_at, fd, name, handle, mnt_id, flags);
161 # else
162 errno = ENOSYS;
163 return -1;
164 # endif
165 }
166
167 # define name_to_handle_at missing_name_to_handle_at
168 #endif
169
170 /* ======================================================================= */
171
172 #if !HAVE_SETNS
173 # ifndef __NR_setns
174 # if defined(__x86_64__)
175 # define __NR_setns 308
176 # elif defined(__i386__)
177 # define __NR_setns 346
178 # elif defined(__arc__)
179 # define __NR_setns 268
180 # else
181 # error "__NR_setns is not defined"
182 # endif
183 # endif
184
185 static inline int missing_setns(int fd, int nstype) {
186 # ifdef __NR_setns
187 return syscall(__NR_setns, fd, nstype);
188 # else
189 errno = ENOSYS;
190 return -1;
191 # endif
192 }
193
194 # define setns missing_setns
195 #endif
196
197 /* ======================================================================= */
198
199 static inline pid_t raw_getpid(void) {
200 #if defined(__alpha__)
201 return (pid_t) syscall(__NR_getxpid);
202 #else
203 return (pid_t) syscall(__NR_getpid);
204 #endif
205 }
206
207 /* ======================================================================= */
208
209 #if !HAVE_RENAMEAT2
210 # ifndef __NR_renameat2
211 # if defined __x86_64__
212 # define __NR_renameat2 316
213 # elif defined __arm__
214 # define __NR_renameat2 382
215 # elif defined __aarch64__
216 # define __NR_renameat2 276
217 # elif defined _MIPS_SIM
218 # if _MIPS_SIM == _MIPS_SIM_ABI32
219 # define __NR_renameat2 4351
220 # endif
221 # if _MIPS_SIM == _MIPS_SIM_NABI32
222 # define __NR_renameat2 6315
223 # endif
224 # if _MIPS_SIM == _MIPS_SIM_ABI64
225 # define __NR_renameat2 5311
226 # endif
227 # elif defined __i386__
228 # define __NR_renameat2 353
229 # elif defined __powerpc64__
230 # define __NR_renameat2 357
231 # elif defined __s390__ || defined __s390x__
232 # define __NR_renameat2 347
233 # elif defined __arc__
234 # define __NR_renameat2 276
235 # else
236 # warning "__NR_renameat2 unknown for your architecture"
237 # endif
238 # endif
239
240 static inline int missing_renameat2(int oldfd, const char *oldname, int newfd, const char *newname, unsigned flags) {
241 # ifdef __NR_renameat2
242 return syscall(__NR_renameat2, oldfd, oldname, newfd, newname, flags);
243 # else
244 errno = ENOSYS;
245 return -1;
246 # endif
247 }
248
249 # define renameat2 missing_renameat2
250 #endif
251
252 /* ======================================================================= */
253
254 #if !HAVE_KCMP
255 static inline int missing_kcmp(pid_t pid1, pid_t pid2, int type, unsigned long idx1, unsigned long idx2) {
256 # ifdef __NR_kcmp
257 return syscall(__NR_kcmp, pid1, pid2, type, idx1, idx2);
258 # else
259 errno = ENOSYS;
260 return -1;
261 # endif
262 }
263
264 # define kcmp missing_kcmp
265 #endif
266
267 /* ======================================================================= */
268
269 #if !HAVE_KEYCTL
270 static inline long missing_keyctl(int cmd, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5) {
271 # ifdef __NR_keyctl
272 return syscall(__NR_keyctl, cmd, arg2, arg3, arg4, arg5);
273 # else
274 errno = ENOSYS;
275 return -1;
276 # endif
277
278 # define keyctl missing_keyctl
279 }
280
281 static inline key_serial_t missing_add_key(const char *type, const char *description, const void *payload, size_t plen, key_serial_t ringid) {
282 # ifdef __NR_add_key
283 return syscall(__NR_add_key, type, description, payload, plen, ringid);
284 # else
285 errno = ENOSYS;
286 return -1;
287 # endif
288
289 # define add_key missing_add_key
290 }
291
292 static inline key_serial_t missing_request_key(const char *type, const char *description, const char * callout_info, key_serial_t destringid) {
293 # ifdef __NR_request_key
294 return syscall(__NR_request_key, type, description, callout_info, destringid);
295 # else
296 errno = ENOSYS;
297 return -1;
298 # endif
299
300 # define request_key missing_request_key
301 }
302 #endif
303
304 /* ======================================================================= */
305
306 #if !HAVE_COPY_FILE_RANGE
307 # ifndef __NR_copy_file_range
308 # if defined(__x86_64__)
309 # define __NR_copy_file_range 326
310 # elif defined(__i386__)
311 # define __NR_copy_file_range 377
312 # elif defined __s390__
313 # define __NR_copy_file_range 375
314 # elif defined __arm__
315 # define __NR_copy_file_range 391
316 # elif defined __aarch64__
317 # define __NR_copy_file_range 285
318 # elif defined __powerpc__
319 # define __NR_copy_file_range 379
320 # elif defined __arc__
321 # define __NR_copy_file_range 285
322 # else
323 # warning "__NR_copy_file_range not defined for your architecture"
324 # endif
325 # endif
326
327 static inline ssize_t missing_copy_file_range(int fd_in, loff_t *off_in,
328 int fd_out, loff_t *off_out,
329 size_t len,
330 unsigned int flags) {
331 # ifdef __NR_copy_file_range
332 return syscall(__NR_copy_file_range, fd_in, off_in, fd_out, off_out, len, flags);
333 # else
334 errno = ENOSYS;
335 return -1;
336 # endif
337 }
338
339 # define copy_file_range missing_copy_file_range
340 #endif
341
342 /* ======================================================================= */
343
344 #if !HAVE_BPF
345 # ifndef __NR_bpf
346 # if defined __i386__
347 # define __NR_bpf 357
348 # elif defined __x86_64__
349 # define __NR_bpf 321
350 # elif defined __aarch64__
351 # define __NR_bpf 280
352 # elif defined __arm__
353 # define __NR_bpf 386
354 # elif defined __sparc__
355 # define __NR_bpf 349
356 # elif defined __s390__
357 # define __NR_bpf 351
358 # elif defined __tilegx__
359 # define __NR_bpf 280
360 # else
361 # warning "__NR_bpf not defined for your architecture"
362 # endif
363 # endif
364
365 union bpf_attr;
366
367 static inline int missing_bpf(int cmd, union bpf_attr *attr, size_t size) {
368 #ifdef __NR_bpf
369 return (int) syscall(__NR_bpf, cmd, attr, size);
370 #else
371 errno = ENOSYS;
372 return -1;
373 #endif
374 }
375
376 # define bpf missing_bpf
377 #endif
378
379 /* ======================================================================= */
380
381 #ifndef __IGNORE_pkey_mprotect
382 # ifndef __NR_pkey_mprotect
383 # if defined __i386__
384 # define __NR_pkey_mprotect 380
385 # elif defined __x86_64__
386 # define __NR_pkey_mprotect 329
387 # elif defined __arm__
388 # define __NR_pkey_mprotect 394
389 # elif defined __aarch64__
390 # define __NR_pkey_mprotect 394
391 # elif defined __powerpc__
392 # define __NR_pkey_mprotect 386
393 # elif defined _MIPS_SIM
394 # if _MIPS_SIM == _MIPS_SIM_ABI32
395 # define __NR_pkey_mprotect 4363
396 # endif
397 # if _MIPS_SIM == _MIPS_SIM_NABI32
398 # define __NR_pkey_mprotect 6327
399 # endif
400 # if _MIPS_SIM == _MIPS_SIM_ABI64
401 # define __NR_pkey_mprotect 5323
402 # endif
403 # else
404 # warning "__NR_pkey_mprotect not defined for your architecture"
405 # endif
406 # endif
407 #endif
408
409 /* ======================================================================= */
410
411 #if !HAVE_STATX
412 # ifndef __NR_statx
413 # if defined __aarch64__ || defined __arm__
414 # define __NR_statx 397
415 # elif defined __alpha__
416 # define __NR_statx 522
417 # elif defined __i386__ || defined __powerpc64__
418 # define __NR_statx 383
419 # elif defined __sparc__
420 # define __NR_statx 360
421 # elif defined __x86_64__
422 # define __NR_statx 332
423 # else
424 # warning "__NR_statx not defined for your architecture"
425 # endif
426 # endif
427
428 struct statx;
429 #endif
430
431 /* This typedef is supposed to be always defined. */
432 typedef struct statx struct_statx;
433
434 #if !HAVE_STATX
435 static inline ssize_t missing_statx(int dfd, const char *filename, unsigned flags, unsigned int mask, struct statx *buffer) {
436 # ifdef __NR_statx
437 return syscall(__NR_statx, dfd, filename, flags, mask, buffer);
438 # else
439 errno = ENOSYS;
440 return -1;
441 # endif
442 }
443
444 # define statx missing_statx
445 #endif