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