]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/missing_syscall.h
blockdev-util: add correct API for detecting if block device has partition scanning...
[thirdparty/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 <errno.h>
7 #include <fcntl.h>
8 #include <signal.h>
9 #include <sys/syscall.h>
10 #include <sys/types.h>
11 #include <sys/wait.h>
12 #include <unistd.h>
13
14 #ifdef ARCH_MIPS
15 #include <asm/sgidefs.h>
16 #endif
17
18 #include "missing_keyctl.h"
19 #include "missing_stat.h"
20
21 /* linux/kcmp.h */
22 #ifndef KCMP_FILE /* 3f4994cfc15f38a3159c6e3a4b3ab2e1481a6b02 (3.19) */
23 #define KCMP_FILE 0
24 #endif
25
26 #if !HAVE_PIVOT_ROOT
27 static inline int missing_pivot_root(const char *new_root, const char *put_old) {
28 return syscall(__NR_pivot_root, new_root, put_old);
29 }
30
31 # define pivot_root missing_pivot_root
32 #endif
33
34 /* ======================================================================= */
35
36 #if !HAVE_MEMFD_CREATE
37 /* may be (invalid) negative number due to libseccomp, see PR 13319 */
38 # if ! (defined __NR_memfd_create && __NR_memfd_create >= 0)
39 # if defined __NR_memfd_create
40 # undef __NR_memfd_create
41 # endif
42 # if defined __x86_64__
43 # define __NR_memfd_create 319
44 # elif defined __arm__
45 # define __NR_memfd_create 385
46 # elif defined __aarch64__
47 # define __NR_memfd_create 279
48 # elif defined __s390__
49 # define __NR_memfd_create 350
50 # elif defined _MIPS_SIM
51 # if _MIPS_SIM == _MIPS_SIM_ABI32
52 # define __NR_memfd_create 4354
53 # endif
54 # if _MIPS_SIM == _MIPS_SIM_NABI32
55 # define __NR_memfd_create 6318
56 # endif
57 # if _MIPS_SIM == _MIPS_SIM_ABI64
58 # define __NR_memfd_create 5314
59 # endif
60 # elif defined __i386__
61 # define __NR_memfd_create 356
62 # elif defined __arc__
63 # define __NR_memfd_create 279
64 # else
65 # warning "__NR_memfd_create unknown for your architecture"
66 # endif
67 # endif
68
69 static inline int missing_memfd_create(const char *name, unsigned int flags) {
70 # ifdef __NR_memfd_create
71 return syscall(__NR_memfd_create, name, flags);
72 # else
73 errno = ENOSYS;
74 return -1;
75 # endif
76 }
77
78 # define memfd_create missing_memfd_create
79 #endif
80
81 /* ======================================================================= */
82
83 #if !HAVE_GETRANDOM
84 /* may be (invalid) negative number due to libseccomp, see PR 13319 */
85 # if ! (defined __NR_getrandom && __NR_getrandom >= 0)
86 # if defined __NR_getrandom
87 # undef __NR_getrandom
88 # endif
89 # if defined __x86_64__
90 # define __NR_getrandom 318
91 # elif defined(__i386__)
92 # define __NR_getrandom 355
93 # elif defined(__arm__)
94 # define __NR_getrandom 384
95 # elif defined(__aarch64__)
96 # define __NR_getrandom 278
97 # elif defined(__ia64__)
98 # define __NR_getrandom 1339
99 # elif defined(__m68k__)
100 # define __NR_getrandom 352
101 # elif defined(__s390x__)
102 # define __NR_getrandom 349
103 # elif defined(__powerpc__)
104 # define __NR_getrandom 359
105 # elif defined _MIPS_SIM
106 # if _MIPS_SIM == _MIPS_SIM_ABI32
107 # define __NR_getrandom 4353
108 # endif
109 # if _MIPS_SIM == _MIPS_SIM_NABI32
110 # define __NR_getrandom 6317
111 # endif
112 # if _MIPS_SIM == _MIPS_SIM_ABI64
113 # define __NR_getrandom 5313
114 # endif
115 # elif defined(__arc__)
116 # define __NR_getrandom 278
117 # else
118 # warning "__NR_getrandom unknown for your architecture"
119 # endif
120 # endif
121
122 static inline int missing_getrandom(void *buffer, size_t count, unsigned flags) {
123 # ifdef __NR_getrandom
124 return syscall(__NR_getrandom, buffer, count, flags);
125 # else
126 errno = ENOSYS;
127 return -1;
128 # endif
129 }
130
131 # define getrandom missing_getrandom
132 #endif
133
134 /* ======================================================================= */
135
136 #if !HAVE_GETTID
137 static inline pid_t missing_gettid(void) {
138 return (pid_t) syscall(__NR_gettid);
139 }
140
141 # define gettid missing_gettid
142 #endif
143
144 /* ======================================================================= */
145
146 #if !HAVE_NAME_TO_HANDLE_AT
147 /* may be (invalid) negative number due to libseccomp, see PR 13319 */
148 # if ! (defined __NR_name_to_handle_at && __NR_name_to_handle_at >= 0)
149 # if defined __NR_name_to_handle_at
150 # undef __NR_name_to_handle_at
151 # endif
152 # if defined(__x86_64__)
153 # define __NR_name_to_handle_at 303
154 # elif defined(__i386__)
155 # define __NR_name_to_handle_at 341
156 # elif defined(__arm__)
157 # define __NR_name_to_handle_at 370
158 # elif defined(__powerpc__)
159 # define __NR_name_to_handle_at 345
160 # elif defined(__arc__)
161 # define __NR_name_to_handle_at 264
162 # else
163 # error "__NR_name_to_handle_at is not defined"
164 # endif
165 # endif
166
167 struct file_handle {
168 unsigned int handle_bytes;
169 int handle_type;
170 unsigned char f_handle[0];
171 };
172
173 static inline int missing_name_to_handle_at(int fd, const char *name, struct file_handle *handle, int *mnt_id, int flags) {
174 # ifdef __NR_name_to_handle_at
175 return syscall(__NR_name_to_handle_at, fd, name, handle, mnt_id, flags);
176 # else
177 errno = ENOSYS;
178 return -1;
179 # endif
180 }
181
182 # define name_to_handle_at missing_name_to_handle_at
183 #endif
184
185 /* ======================================================================= */
186
187 #if !HAVE_SETNS
188 /* may be (invalid) negative number due to libseccomp, see PR 13319 */
189 # if ! (defined __NR_setns && __NR_setns >= 0)
190 # if defined __NR_setns
191 # undef __NR_setns
192 # endif
193 # if defined(__x86_64__)
194 # define __NR_setns 308
195 # elif defined(__i386__)
196 # define __NR_setns 346
197 # elif defined(__arc__)
198 # define __NR_setns 268
199 # else
200 # error "__NR_setns is not defined"
201 # endif
202 # endif
203
204 static inline int missing_setns(int fd, int nstype) {
205 # ifdef __NR_setns
206 return syscall(__NR_setns, fd, nstype);
207 # else
208 errno = ENOSYS;
209 return -1;
210 # endif
211 }
212
213 # define setns missing_setns
214 #endif
215
216 /* ======================================================================= */
217
218 static inline pid_t raw_getpid(void) {
219 #if defined(__alpha__)
220 return (pid_t) syscall(__NR_getxpid);
221 #else
222 return (pid_t) syscall(__NR_getpid);
223 #endif
224 }
225
226 /* ======================================================================= */
227
228 #if !HAVE_RENAMEAT2
229 /* may be (invalid) negative number due to libseccomp, see PR 13319 */
230 # if ! (defined __NR_renameat2 && __NR_renameat2 >= 0)
231 # if defined __NR_renameat2
232 # undef __NR_renameat2
233 # endif
234 # if defined __x86_64__
235 # define __NR_renameat2 316
236 # elif defined __arm__
237 # define __NR_renameat2 382
238 # elif defined __aarch64__
239 # define __NR_renameat2 276
240 # elif defined _MIPS_SIM
241 # if _MIPS_SIM == _MIPS_SIM_ABI32
242 # define __NR_renameat2 4351
243 # endif
244 # if _MIPS_SIM == _MIPS_SIM_NABI32
245 # define __NR_renameat2 6315
246 # endif
247 # if _MIPS_SIM == _MIPS_SIM_ABI64
248 # define __NR_renameat2 5311
249 # endif
250 # elif defined __i386__
251 # define __NR_renameat2 353
252 # elif defined __powerpc64__
253 # define __NR_renameat2 357
254 # elif defined __s390__ || defined __s390x__
255 # define __NR_renameat2 347
256 # elif defined __arc__
257 # define __NR_renameat2 276
258 # else
259 # warning "__NR_renameat2 unknown for your architecture"
260 # endif
261 # endif
262
263 static inline int missing_renameat2(int oldfd, const char *oldname, int newfd, const char *newname, unsigned flags) {
264 # ifdef __NR_renameat2
265 return syscall(__NR_renameat2, oldfd, oldname, newfd, newname, flags);
266 # else
267 errno = ENOSYS;
268 return -1;
269 # endif
270 }
271
272 # define renameat2 missing_renameat2
273 #endif
274
275 /* ======================================================================= */
276
277 #if !HAVE_KCMP
278 static inline int missing_kcmp(pid_t pid1, pid_t pid2, int type, unsigned long idx1, unsigned long idx2) {
279 # if defined __NR_kcmp && __NR_kcmp >= 0
280 return syscall(__NR_kcmp, pid1, pid2, type, idx1, idx2);
281 # else
282 errno = ENOSYS;
283 return -1;
284 # endif
285 }
286
287 # define kcmp missing_kcmp
288 #endif
289
290 /* ======================================================================= */
291
292 #if !HAVE_KEYCTL
293 static inline long missing_keyctl(int cmd, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5) {
294 # if defined __NR_keyctl && __NR_keyctl >= 0
295 return syscall(__NR_keyctl, cmd, arg2, arg3, arg4, arg5);
296 # else
297 errno = ENOSYS;
298 return -1;
299 # endif
300
301 # define keyctl missing_keyctl
302 }
303
304 static inline key_serial_t missing_add_key(const char *type, const char *description, const void *payload, size_t plen, key_serial_t ringid) {
305 # if defined __NR_add_key && __NR_add_key >= 0
306 return syscall(__NR_add_key, type, description, payload, plen, ringid);
307 # else
308 errno = ENOSYS;
309 return -1;
310 # endif
311
312 # define add_key missing_add_key
313 }
314
315 static inline key_serial_t missing_request_key(const char *type, const char *description, const char * callout_info, key_serial_t destringid) {
316 # if defined __NR_request_key && __NR_request_key >= 0
317 return syscall(__NR_request_key, type, description, callout_info, destringid);
318 # else
319 errno = ENOSYS;
320 return -1;
321 # endif
322
323 # define request_key missing_request_key
324 }
325 #endif
326
327 /* ======================================================================= */
328
329 #if !HAVE_COPY_FILE_RANGE
330 /* may be (invalid) negative number due to libseccomp, see PR 13319 */
331 # if ! (defined __NR_copy_file_range && __NR_copy_file_range >= 0)
332 # if defined __NR_copy_file_range
333 # undef __NR_copy_file_range
334 # endif
335 # if defined(__x86_64__)
336 # define __NR_copy_file_range 326
337 # elif defined(__i386__)
338 # define __NR_copy_file_range 377
339 # elif defined __s390__
340 # define __NR_copy_file_range 375
341 # elif defined __arm__
342 # define __NR_copy_file_range 391
343 # elif defined __aarch64__
344 # define __NR_copy_file_range 285
345 # elif defined __powerpc__
346 # define __NR_copy_file_range 379
347 # elif defined __arc__
348 # define __NR_copy_file_range 285
349 # else
350 # warning "__NR_copy_file_range not defined for your architecture"
351 # endif
352 # endif
353
354 static inline ssize_t missing_copy_file_range(int fd_in, loff_t *off_in,
355 int fd_out, loff_t *off_out,
356 size_t len,
357 unsigned int flags) {
358 # ifdef __NR_copy_file_range
359 return syscall(__NR_copy_file_range, fd_in, off_in, fd_out, off_out, len, flags);
360 # else
361 errno = ENOSYS;
362 return -1;
363 # endif
364 }
365
366 # define copy_file_range missing_copy_file_range
367 #endif
368
369 /* ======================================================================= */
370
371 #if !HAVE_BPF
372 /* may be (invalid) negative number due to libseccomp, see PR 13319 */
373 # if ! (defined __NR_bpf && __NR_bpf >= 0)
374 # if defined __NR_bpf
375 # undef __NR_bpf
376 # endif
377 # if defined __i386__
378 # define __NR_bpf 357
379 # elif defined __x86_64__
380 # define __NR_bpf 321
381 # elif defined __aarch64__
382 # define __NR_bpf 280
383 # elif defined __arm__
384 # define __NR_bpf 386
385 # elif defined __sparc__
386 # define __NR_bpf 349
387 # elif defined __s390__
388 # define __NR_bpf 351
389 # elif defined __tilegx__
390 # define __NR_bpf 280
391 # else
392 # warning "__NR_bpf not defined for your architecture"
393 # endif
394 # endif
395
396 union bpf_attr;
397
398 static inline int missing_bpf(int cmd, union bpf_attr *attr, size_t size) {
399 #ifdef __NR_bpf
400 return (int) syscall(__NR_bpf, cmd, attr, size);
401 #else
402 errno = ENOSYS;
403 return -1;
404 #endif
405 }
406
407 # define bpf missing_bpf
408 #endif
409
410 /* ======================================================================= */
411
412 #ifndef __IGNORE_pkey_mprotect
413 /* may be (invalid) negative number due to libseccomp, see PR 13319 */
414 # if ! (defined __NR_pkey_mprotect && __NR_pkey_mprotect >= 0)
415 # if defined __NR_pkey_mprotect
416 # undef __NR_pkey_mprotect
417 # endif
418 # if defined __i386__
419 # define __NR_pkey_mprotect 380
420 # elif defined __x86_64__
421 # define __NR_pkey_mprotect 329
422 # elif defined __arm__
423 # define __NR_pkey_mprotect 394
424 # elif defined __aarch64__
425 # define __NR_pkey_mprotect 394
426 # elif defined __powerpc__
427 # define __NR_pkey_mprotect 386
428 # elif defined __s390__
429 # define __NR_pkey_mprotect 384
430 # elif defined _MIPS_SIM
431 # if _MIPS_SIM == _MIPS_SIM_ABI32
432 # define __NR_pkey_mprotect 4363
433 # endif
434 # if _MIPS_SIM == _MIPS_SIM_NABI32
435 # define __NR_pkey_mprotect 6327
436 # endif
437 # if _MIPS_SIM == _MIPS_SIM_ABI64
438 # define __NR_pkey_mprotect 5323
439 # endif
440 # else
441 # warning "__NR_pkey_mprotect not defined for your architecture"
442 # endif
443 # endif
444 #endif
445
446 /* ======================================================================= */
447
448 #if !HAVE_STATX
449 /* may be (invalid) negative number due to libseccomp, see PR 13319 */
450 # if ! (defined __NR_statx && __NR_statx >= 0)
451 # if defined __NR_statx
452 # undef __NR_statx
453 # endif
454 # if defined __aarch64__ || defined __arm__
455 # define __NR_statx 397
456 # elif defined __alpha__
457 # define __NR_statx 522
458 # elif defined __i386__ || defined __powerpc64__
459 # define __NR_statx 383
460 # elif defined __sparc__
461 # define __NR_statx 360
462 # elif defined __x86_64__
463 # define __NR_statx 332
464 # else
465 # warning "__NR_statx not defined for your architecture"
466 # endif
467 # endif
468
469 struct statx;
470 #endif
471
472 /* This typedef is supposed to be always defined. */
473 typedef struct statx struct_statx;
474
475 #if !HAVE_STATX
476 static inline ssize_t missing_statx(int dfd, const char *filename, unsigned flags, unsigned int mask, struct statx *buffer) {
477 # ifdef __NR_statx
478 return syscall(__NR_statx, dfd, filename, flags, mask, buffer);
479 # else
480 errno = ENOSYS;
481 return -1;
482 # endif
483 }
484
485 # define statx missing_statx
486 #endif
487
488 #if !HAVE_SET_MEMPOLICY
489
490 enum {
491 MPOL_DEFAULT,
492 MPOL_PREFERRED,
493 MPOL_BIND,
494 MPOL_INTERLEAVE,
495 MPOL_LOCAL,
496 };
497
498 static inline long missing_set_mempolicy(int mode, const unsigned long *nodemask,
499 unsigned long maxnode) {
500 long i;
501 # if defined __NR_set_mempolicy && __NR_set_mempolicy >= 0
502 i = syscall(__NR_set_mempolicy, mode, nodemask, maxnode);
503 # else
504 errno = ENOSYS;
505 i = -1;
506 # endif
507 return i;
508 }
509
510 # define set_mempolicy missing_set_mempolicy
511 #endif
512
513 #if !HAVE_GET_MEMPOLICY
514 static inline long missing_get_mempolicy(int *mode, unsigned long *nodemask,
515 unsigned long maxnode, void *addr,
516 unsigned long flags) {
517 long i;
518 # ifdef __NR_get_mempolicy
519 i = syscall(__NR_get_mempolicy, mode, nodemask, maxnode, addr, flags);
520 # else
521 errno = ENOSYS;
522 i = -1;
523 # endif
524 return i;
525 }
526
527 #define get_mempolicy missing_get_mempolicy
528 #endif
529
530 #if !HAVE_PIDFD_OPEN
531 /* may be (invalid) negative number due to libseccomp, see PR 13319 */
532 # if ! (defined __NR_pidfd_open && __NR_pidfd_open >= 0)
533 # if defined __NR_pidfd_open
534 # undef __NR_pidfd_open
535 # endif
536 # define __NR_pidfd_open 434
537 #endif
538 static inline int pidfd_open(pid_t pid, unsigned flags) {
539 #ifdef __NR_pidfd_open
540 return syscall(__NR_pidfd_open, pid, flags);
541 #else
542 errno = ENOSYS;
543 return -1;
544 #endif
545 }
546 #endif
547
548 #if !HAVE_PIDFD_SEND_SIGNAL
549 /* may be (invalid) negative number due to libseccomp, see PR 13319 */
550 # if ! (defined __NR_pidfd_send_signal && __NR_pidfd_send_signal >= 0)
551 # if defined __NR_pidfd_send_signal
552 # undef __NR_pidfd_send_signal
553 # endif
554 # define __NR_pidfd_send_signal 424
555 #endif
556 static inline int pidfd_send_signal(int fd, int sig, siginfo_t *info, unsigned flags) {
557 #ifdef __NR_pidfd_open
558 return syscall(__NR_pidfd_send_signal, fd, sig, info, flags);
559 #else
560 errno = ENOSYS;
561 return -1;
562 #endif
563 }
564 #endif
565
566 #if !HAVE_RT_SIGQUEUEINFO
567 static inline int rt_sigqueueinfo(pid_t tgid, int sig, siginfo_t *info) {
568 return syscall(__NR_rt_sigqueueinfo, tgid, sig, info);
569 }
570 #endif