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