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