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