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