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