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