1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 /* Missing glibc definitions to access certain kernel APIs */
5
6 #include <errno.h>
7 #include <fcntl.h>
8 #if HAVE_LINUX_TIME_TYPES_H
9 /* This header defines __kernel_timespec for us, but is only available since Linux 5.1, hence conditionally
10 * include this. */
11 #include <linux/time_types.h>
12 #endif
13 #include <signal.h>
14 #include <sys/syscall.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <unistd.h>
18
19 #ifdef ARCH_MIPS
20 #include <asm/sgidefs.h>
21 #endif
22
23 #include "macro.h"
24 #include "missing_keyctl.h"
25 #include "missing_stat.h"
26 #include "missing_syscall_def.h"
27
28 /* linux/kcmp.h */
29 #ifndef KCMP_FILE /* 3f4994cfc15f38a3159c6e3a4b3ab2e1481a6b02 (3.19) */
30 #define KCMP_FILE 0
31 #endif
32
33 /* ======================================================================= */
34
35 #if !HAVE_PIVOT_ROOT
missing_pivot_root(const char * new_root,const char * put_old)36 static inline int missing_pivot_root(const char *new_root, const char *put_old) {
37 return syscall(__NR_pivot_root, new_root, put_old);
38 }
39
40 # define pivot_root missing_pivot_root
41 #endif
42
43 /* ======================================================================= */
44
45 #if !HAVE_IOPRIO_GET
missing_ioprio_get(int which,int who)46 static inline int missing_ioprio_get(int which, int who) {
47 return syscall(__NR_ioprio_get, which, who);
48 }
49
50 # define ioprio_get missing_ioprio_get
51 #endif
52
53 /* ======================================================================= */
54
55 #if !HAVE_IOPRIO_SET
missing_ioprio_set(int which,int who,int ioprio)56 static inline int missing_ioprio_set(int which, int who, int ioprio) {
57 return syscall(__NR_ioprio_set, which, who, ioprio);
58 }
59
60 # define ioprio_set missing_ioprio_set
61 #endif
62
63 /* ======================================================================= */
64
65 #if !HAVE_MEMFD_CREATE
missing_memfd_create(const char * name,unsigned int flags)66 static inline int missing_memfd_create(const char *name, unsigned int flags) {
67 # ifdef __NR_memfd_create
68 return syscall(__NR_memfd_create, name, flags);
69 # else
70 errno = ENOSYS;
71 return -1;
72 # endif
73 }
74
75 # define memfd_create missing_memfd_create
76 #endif
77
78 /* ======================================================================= */
79
80 #if !HAVE_GETRANDOM
81 /* glibc says getrandom() returns ssize_t */
missing_getrandom(void * buffer,size_t count,unsigned flags)82 static inline ssize_t missing_getrandom(void *buffer, size_t count, unsigned flags) {
83 # ifdef __NR_getrandom
84 return syscall(__NR_getrandom, buffer, count, flags);
85 # else
86 errno = ENOSYS;
87 return -1;
88 # endif
89 }
90
91 # define getrandom missing_getrandom
92 #endif
93
94 /* ======================================================================= */
95
96 /* The syscall has been defined since forever, but the glibc wrapper was missing. */
97 #if !HAVE_GETTID
missing_gettid(void)98 static inline pid_t missing_gettid(void) {
99 # if defined __NR_gettid && __NR_gettid >= 0
100 return (pid_t) syscall(__NR_gettid);
101 # else
102 # error "__NR_gettid not defined"
103 # endif
104 }
105
106 # define gettid missing_gettid
107 #endif
108
109 /* ======================================================================= */
110
111 #if !HAVE_NAME_TO_HANDLE_AT
112 struct file_handle {
113 unsigned int handle_bytes;
114 int handle_type;
115 unsigned char f_handle[0];
116 };
117
missing_name_to_handle_at(int fd,const char * name,struct file_handle * handle,int * mnt_id,int flags)118 static inline int missing_name_to_handle_at(int fd, const char *name, struct file_handle *handle, int *mnt_id, int flags) {
119 # ifdef __NR_name_to_handle_at
120 return syscall(__NR_name_to_handle_at, fd, name, handle, mnt_id, flags);
121 # else
122 errno = ENOSYS;
123 return -1;
124 # endif
125 }
126
127 # define name_to_handle_at missing_name_to_handle_at
128 #endif
129
130 /* ======================================================================= */
131
132 #if !HAVE_SETNS
missing_setns(int fd,int nstype)133 static inline int missing_setns(int fd, int nstype) {
134 # ifdef __NR_setns
135 return syscall(__NR_setns, fd, nstype);
136 # else
137 errno = ENOSYS;
138 return -1;
139 # endif
140 }
141
142 # define setns missing_setns
143 #endif
144
145 /* ======================================================================= */
146
raw_getpid(void)147 static inline pid_t raw_getpid(void) {
148 #if defined(__alpha__)
149 return (pid_t) syscall(__NR_getxpid);
150 #else
151 return (pid_t) syscall(__NR_getpid);
152 #endif
153 }
154
155 /* ======================================================================= */
156
157 #if !HAVE_RENAMEAT2
missing_renameat2(int oldfd,const char * oldname,int newfd,const char * newname,unsigned flags)158 static inline int missing_renameat2(int oldfd, const char *oldname, int newfd, const char *newname, unsigned flags) {
159 # ifdef __NR_renameat2
160 return syscall(__NR_renameat2, oldfd, oldname, newfd, newname, flags);
161 # else
162 errno = ENOSYS;
163 return -1;
164 # endif
165 }
166
167 # define renameat2 missing_renameat2
168 #endif
169
170 /* ======================================================================= */
171
172 #if !HAVE_KCMP
missing_kcmp(pid_t pid1,pid_t pid2,int type,unsigned long idx1,unsigned long idx2)173 static inline int missing_kcmp(pid_t pid1, pid_t pid2, int type, unsigned long idx1, unsigned long idx2) {
174 # if defined __NR_kcmp && __NR_kcmp >= 0
175 return syscall(__NR_kcmp, pid1, pid2, type, idx1, idx2);
176 # else
177 errno = ENOSYS;
178 return -1;
179 # endif
180 }
181
182 # define kcmp missing_kcmp
183 #endif
184
185 /* ======================================================================= */
186
187 #if !HAVE_KEYCTL
missing_keyctl(int cmd,unsigned long arg2,unsigned long arg3,unsigned long arg4,unsigned long arg5)188 static inline long missing_keyctl(int cmd, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5) {
189 # if defined __NR_keyctl && __NR_keyctl >= 0
190 return syscall(__NR_keyctl, cmd, arg2, arg3, arg4, arg5);
191 # else
192 errno = ENOSYS;
193 return -1;
194 # endif
195
196 # define keyctl missing_keyctl
197 }
198
missing_add_key(const char * type,const char * description,const void * payload,size_t plen,key_serial_t ringid)199 static inline key_serial_t missing_add_key(const char *type, const char *description, const void *payload, size_t plen, key_serial_t ringid) {
200 # if defined __NR_add_key && __NR_add_key >= 0
201 return syscall(__NR_add_key, type, description, payload, plen, ringid);
202 # else
203 errno = ENOSYS;
204 return -1;
205 # endif
206
207 # define add_key missing_add_key
208 }
209
missing_request_key(const char * type,const char * description,const char * callout_info,key_serial_t destringid)210 static inline key_serial_t missing_request_key(const char *type, const char *description, const char * callout_info, key_serial_t destringid) {
211 # if defined __NR_request_key && __NR_request_key >= 0
212 return syscall(__NR_request_key, type, description, callout_info, destringid);
213 # else
214 errno = ENOSYS;
215 return -1;
216 # endif
217
218 # define request_key missing_request_key
219 }
220 #endif
221
222 /* ======================================================================= */
223
224 #if !HAVE_COPY_FILE_RANGE
missing_copy_file_range(int fd_in,loff_t * off_in,int fd_out,loff_t * off_out,size_t len,unsigned int flags)225 static inline ssize_t missing_copy_file_range(int fd_in, loff_t *off_in,
226 int fd_out, loff_t *off_out,
227 size_t len,
228 unsigned int flags) {
229 # ifdef __NR_copy_file_range
230 return syscall(__NR_copy_file_range, fd_in, off_in, fd_out, off_out, len, flags);
231 # else
232 errno = ENOSYS;
233 return -1;
234 # endif
235 }
236
237 # define copy_file_range missing_copy_file_range
238 #endif
239
240 /* ======================================================================= */
241
242 #if !HAVE_BPF
243 union bpf_attr;
244
missing_bpf(int cmd,union bpf_attr * attr,size_t size)245 static inline int missing_bpf(int cmd, union bpf_attr *attr, size_t size) {
246 #ifdef __NR_bpf
247 return (int) syscall(__NR_bpf, cmd, attr, size);
248 #else
249 errno = ENOSYS;
250 return -1;
251 #endif
252 }
253
254 # define bpf missing_bpf
255 #endif
256
257 /* ======================================================================= */
258
259 #if !HAVE_STATX
260 struct statx;
261
missing_statx(int dfd,const char * filename,unsigned flags,unsigned int mask,struct statx * buffer)262 static inline ssize_t missing_statx(int dfd, const char *filename, unsigned flags, unsigned int mask, struct statx *buffer) {
263 # ifdef __NR_statx
264 return syscall(__NR_statx, dfd, filename, flags, mask, buffer);
265 # else
266 errno = ENOSYS;
267 return -1;
268 # endif
269 }
270 #endif
271
272 /* This typedef is supposed to be always defined. */
273 typedef struct statx struct_statx;
274
275 #if !HAVE_STATX
276 # define statx(dfd, filename, flags, mask, buffer) missing_statx(dfd, filename, flags, mask, buffer)
277 #endif
278
279 /* ======================================================================= */
280
281 #if !HAVE_SET_MEMPOLICY
282 enum {
283 MPOL_DEFAULT,
284 MPOL_PREFERRED,
285 MPOL_BIND,
286 MPOL_INTERLEAVE,
287 MPOL_LOCAL,
288 };
289
missing_set_mempolicy(int mode,const unsigned long * nodemask,unsigned long maxnode)290 static inline long missing_set_mempolicy(int mode, const unsigned long *nodemask,
291 unsigned long maxnode) {
292 long i;
293 # if defined __NR_set_mempolicy && __NR_set_mempolicy >= 0
294 i = syscall(__NR_set_mempolicy, mode, nodemask, maxnode);
295 # else
296 errno = ENOSYS;
297 i = -1;
298 # endif
299 return i;
300 }
301
302 # define set_mempolicy missing_set_mempolicy
303 #endif
304
305 #if !HAVE_GET_MEMPOLICY
missing_get_mempolicy(int * mode,unsigned long * nodemask,unsigned long maxnode,void * addr,unsigned long flags)306 static inline long missing_get_mempolicy(int *mode, unsigned long *nodemask,
307 unsigned long maxnode, void *addr,
308 unsigned long flags) {
309 long i;
310 # if defined __NR_get_mempolicy && __NR_get_mempolicy >= 0
311 i = syscall(__NR_get_mempolicy, mode, nodemask, maxnode, addr, flags);
312 # else
313 errno = ENOSYS;
314 i = -1;
315 # endif
316 return i;
317 }
318
319 # define get_mempolicy missing_get_mempolicy
320 #endif
321
322 /* ======================================================================= */
323
324 #if !HAVE_PIDFD_SEND_SIGNAL
missing_pidfd_send_signal(int fd,int sig,siginfo_t * info,unsigned flags)325 static inline int missing_pidfd_send_signal(int fd, int sig, siginfo_t *info, unsigned flags) {
326 # ifdef __NR_pidfd_send_signal
327 return syscall(__NR_pidfd_send_signal, fd, sig, info, flags);
328 # else
329 errno = ENOSYS;
330 return -1;
331 # endif
332 }
333
334 # define pidfd_send_signal missing_pidfd_send_signal
335 #endif
336
337 #if !HAVE_PIDFD_OPEN
missing_pidfd_open(pid_t pid,unsigned flags)338 static inline int missing_pidfd_open(pid_t pid, unsigned flags) {
339 # ifdef __NR_pidfd_open
340 return syscall(__NR_pidfd_open, pid, flags);
341 # else
342 errno = ENOSYS;
343 return -1;
344 # endif
345 }
346
347 # define pidfd_open missing_pidfd_open
348 #endif
349
350 /* ======================================================================= */
351
352 #if !HAVE_RT_SIGQUEUEINFO
missing_rt_sigqueueinfo(pid_t tgid,int sig,siginfo_t * info)353 static inline int missing_rt_sigqueueinfo(pid_t tgid, int sig, siginfo_t *info) {
354 # if defined __NR_rt_sigqueueinfo && __NR_rt_sigqueueinfo >= 0
355 return syscall(__NR_rt_sigqueueinfo, tgid, sig, info);
356 # else
357 # error "__NR_rt_sigqueueinfo not defined"
358 # endif
359 }
360
361 # define rt_sigqueueinfo missing_rt_sigqueueinfo
362 #endif
363
364 /* ======================================================================= */
365
366 #if !HAVE_EXECVEAT
missing_execveat(int dirfd,const char * pathname,char * const argv[],char * const envp[],int flags)367 static inline int missing_execveat(int dirfd, const char *pathname,
368 char *const argv[], char *const envp[],
369 int flags) {
370 # if defined __NR_execveat && __NR_execveat >= 0
371 return syscall(__NR_execveat, dirfd, pathname, argv, envp, flags);
372 # else
373 errno = ENOSYS;
374 return -1;
375 # endif
376 }
377
378 # undef AT_EMPTY_PATH
379 # define AT_EMPTY_PATH 0x1000
380 # define execveat missing_execveat
381 #endif
382
383 /* ======================================================================= */
384
385 #if !HAVE_CLOSE_RANGE
missing_close_range(int first_fd,int end_fd,unsigned flags)386 static inline int missing_close_range(int first_fd, int end_fd, unsigned flags) {
387 # ifdef __NR_close_range
388 /* Kernel-side the syscall expects fds as unsigned integers (just like close() actually), while
389 * userspace exclusively uses signed integers for fds. We don't know just yet how glibc is going to
390 * wrap this syscall, but let's assume it's going to be similar to what they do for close(),
391 * i.e. make the same unsigned → signed type change from the raw kernel syscall compared to the
392 * userspace wrapper. There's only one caveat for this: unlike for close() there's the special
393 * UINT_MAX fd value for the 'end_fd' argument. Let's safely map that to -1 here. And let's refuse
394 * any other negative values. */
395 if ((first_fd < 0) || (end_fd < 0 && end_fd != -1)) {
396 errno = -EBADF;
397 return -1;
398 }
399
400 return syscall(__NR_close_range,
401 (unsigned) first_fd,
402 end_fd == -1 ? UINT_MAX : (unsigned) end_fd, /* Of course, the compiler should figure out that this is the identity mapping IRL */
403 flags);
404 # else
405 errno = ENOSYS;
406 return -1;
407 # endif
408 }
409
410 # define close_range missing_close_range
411 #endif
412
413 /* ======================================================================= */
414
415 #if !HAVE_EPOLL_PWAIT2
416
417 /* Defined to be equivalent to the kernel's _NSIG_WORDS, i.e. the size of the array of longs that is
418 * encapsulated by sigset_t. */
419 #define KERNEL_NSIG_WORDS (64 / (sizeof(long) * 8))
420 #define KERNEL_NSIG_BYTES (KERNEL_NSIG_WORDS * sizeof(long))
421
422 struct epoll_event;
423
missing_epoll_pwait2(int fd,struct epoll_event * events,int maxevents,const struct timespec * timeout,const sigset_t * sigset)424 static inline int missing_epoll_pwait2(
425 int fd,
426 struct epoll_event *events,
427 int maxevents,
428 const struct timespec *timeout,
429 const sigset_t *sigset) {
430
431 # if defined(__NR_epoll_pwait2) && HAVE_LINUX_TIME_TYPES_H
432 if (timeout) {
433 /* Convert from userspace timespec to kernel timespec */
434 struct __kernel_timespec ts = {
435 .tv_sec = timeout->tv_sec,
436 .tv_nsec = timeout->tv_nsec,
437 };
438
439 return syscall(__NR_epoll_pwait2, fd, events, maxevents, &ts, sigset, sigset ? KERNEL_NSIG_BYTES : 0);
440 } else
441 return syscall(__NR_epoll_pwait2, fd, events, maxevents, NULL, sigset, sigset ? KERNEL_NSIG_BYTES : 0);
442 # else
443 errno = ENOSYS;
444 return -1;
445 # endif
446 }
447
448 # define epoll_pwait2 missing_epoll_pwait2
449 #endif
450
451 /* ======================================================================= */
452
453 #if !HAVE_MOUNT_SETATTR
454
455 #if !HAVE_STRUCT_MOUNT_ATTR
456 struct mount_attr {
457 uint64_t attr_set;
458 uint64_t attr_clr;
459 uint64_t propagation;
460 uint64_t userns_fd;
461 };
462 #else
463 struct mount_attr;
464 #endif
465
466 #ifndef MOUNT_ATTR_RDONLY
467 #define MOUNT_ATTR_RDONLY 0x00000001 /* Mount read-only */
468 #endif
469
470 #ifndef MOUNT_ATTR_NOSUID
471 #define MOUNT_ATTR_NOSUID 0x00000002 /* Ignore suid and sgid bits */
472 #endif
473
474 #ifndef MOUNT_ATTR_NODEV
475 #define MOUNT_ATTR_NODEV 0x00000004 /* Disallow access to device special files */
476 #endif
477
478 #ifndef MOUNT_ATTR_NOEXEC
479 #define MOUNT_ATTR_NOEXEC 0x00000008 /* Disallow program execution */
480 #endif
481
482 #ifndef MOUNT_ATTR__ATIME
483 #define MOUNT_ATTR__ATIME 0x00000070 /* Setting on how atime should be updated */
484 #endif
485
486 #ifndef MOUNT_ATTR_RELATIME
487 #define MOUNT_ATTR_RELATIME 0x00000000 /* - Update atime relative to mtime/ctime. */
488 #endif
489
490 #ifndef MOUNT_ATTR_NOATIME
491 #define MOUNT_ATTR_NOATIME 0x00000010 /* - Do not update access times. */
492 #endif
493
494 #ifndef MOUNT_ATTR_STRICTATIME
495 #define MOUNT_ATTR_STRICTATIME 0x00000020 /* - Always perform atime updates */
496 #endif
497
498 #ifndef MOUNT_ATTR_NODIRATIME
499 #define MOUNT_ATTR_NODIRATIME 0x00000080 /* Do not update directory access times */
500 #endif
501
502 #ifndef MOUNT_ATTR_IDMAP
503 #define MOUNT_ATTR_IDMAP 0x00100000 /* Idmap mount to @userns_fd in struct mount_attr. */
504 #endif
505
506 #ifndef MOUNT_ATTR_NOSYMFOLLOW
507 #define MOUNT_ATTR_NOSYMFOLLOW 0x00200000 /* Do not follow symlinks */
508 #endif
509
510 #ifndef MOUNT_ATTR_SIZE_VER0
511 #define MOUNT_ATTR_SIZE_VER0 32 /* sizeof first published struct */
512 #endif
513
514 #ifndef AT_RECURSIVE
515 #define AT_RECURSIVE 0x8000
516 #endif
517
missing_mount_setattr(int dfd,const char * path,unsigned flags,struct mount_attr * attr,size_t size)518 static inline int missing_mount_setattr(
519 int dfd,
520 const char *path,
521 unsigned flags,
522 struct mount_attr *attr,
523 size_t size) {
524
525 # if defined __NR_mount_setattr && __NR_mount_setattr >= 0
526 return syscall(__NR_mount_setattr, dfd, path, flags, attr, size);
527 # else
528 errno = ENOSYS;
529 return -1;
530 # endif
531 }
532
533 # define mount_setattr missing_mount_setattr
534 #endif
535
536 /* ======================================================================= */
537
538 #if !HAVE_OPEN_TREE
539
540 #ifndef OPEN_TREE_CLONE
541 #define OPEN_TREE_CLONE 1
542 #endif
543
544 #ifndef OPEN_TREE_CLOEXEC
545 #define OPEN_TREE_CLOEXEC O_CLOEXEC
546 #endif
547
missing_open_tree(int dfd,const char * filename,unsigned flags)548 static inline int missing_open_tree(
549 int dfd,
550 const char *filename,
551 unsigned flags) {
552
553 # if defined __NR_open_tree && __NR_open_tree >= 0
554 return syscall(__NR_open_tree, dfd, filename, flags);
555 # else
556 errno = ENOSYS;
557 return -1;
558 # endif
559 }
560
561 # define open_tree missing_open_tree
562 #endif
563
564 /* ======================================================================= */
565
566 #if !HAVE_MOVE_MOUNT
567
568 #ifndef MOVE_MOUNT_F_EMPTY_PATH
569 #define MOVE_MOUNT_F_EMPTY_PATH 0x00000004 /* Empty from path permitted */
570 #endif
571
572 #ifndef MOVE_MOUNT_T_EMPTY_PATH
573 #define MOVE_MOUNT_T_EMPTY_PATH 0x00000040 /* Empty to path permitted */
574 #endif
575
missing_move_mount(int from_dfd,const char * from_pathname,int to_dfd,const char * to_pathname,unsigned flags)576 static inline int missing_move_mount(
577 int from_dfd,
578 const char *from_pathname,
579 int to_dfd,
580 const char *to_pathname,
581 unsigned flags) {
582
583 # if defined __NR_move_mount && __NR_move_mount >= 0
584 return syscall(__NR_move_mount, from_dfd, from_pathname, to_dfd, to_pathname, flags);
585 # else
586 errno = ENOSYS;
587 return -1;
588 # endif
589 }
590
591 # define move_mount missing_move_mount
592 #endif
593
594 /* ======================================================================= */
595
596 #if !HAVE_GETDENTS64
597
missing_getdents64(int fd,void * buffer,size_t length)598 static inline ssize_t missing_getdents64(int fd, void *buffer, size_t length) {
599 # if defined __NR_getdents64 && __NR_getdents64 >= 0
600 return syscall(__NR_getdents64, fd, buffer, length);
601 # else
602 errno = ENOSYS;
603 return -1;
604 # endif
605 }
606
607 # define getdents64 missing_getdents64
608 #endif
609