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