tools/nolibc: fix return type of getpagesize()
[linux-block.git] / tools / include / nolibc / sys.h
1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2 /*
3  * Syscall definitions for NOLIBC (those in man(2))
4  * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
5  */
6
7 #ifndef _NOLIBC_SYS_H
8 #define _NOLIBC_SYS_H
9
10 #include <stdarg.h>
11 #include "std.h"
12
13 /* system includes */
14 #include <asm/unistd.h>
15 #include <asm/signal.h>  /* for SIGCHLD */
16 #include <asm/ioctls.h>
17 #include <asm/mman.h>
18 #include <linux/fs.h>
19 #include <linux/loop.h>
20 #include <linux/time.h>
21 #include <linux/auxvec.h>
22 #include <linux/fcntl.h> /* for O_* and AT_* */
23 #include <linux/stat.h>  /* for statx() */
24 #include <linux/prctl.h>
25
26 #include "arch.h"
27 #include "errno.h"
28 #include "types.h"
29
30
31 /* Syscall return helper for library routines, set errno as -ret when ret is in
32  * range of [-MAX_ERRNO, -1]
33  *
34  * Note, No official reference states the errno range here aligns with musl
35  * (src/internal/syscall_ret.c) and glibc (sysdeps/unix/sysv/linux/sysdep.h)
36  */
37
38 static __inline__ __attribute__((unused, always_inline))
39 long __sysret(unsigned long ret)
40 {
41         if (ret >= (unsigned long)-MAX_ERRNO) {
42                 SET_ERRNO(-(long)ret);
43                 return -1;
44         }
45         return ret;
46 }
47
48 /* Functions in this file only describe syscalls. They're declared static so
49  * that the compiler usually decides to inline them while still being allowed
50  * to pass a pointer to one of their instances. Each syscall exists in two
51  * versions:
52  *   - the "internal" ones, which matches the raw syscall interface at the
53  *     kernel level, which may sometimes slightly differ from the documented
54  *     libc-level ones. For example most of them return either a valid value
55  *     or -errno. All of these are prefixed with "sys_". They may be called
56  *     by non-portable applications if desired.
57  *
58  *   - the "exported" ones, whose interface must closely match the one
59  *     documented in man(2), that applications are supposed to expect. These
60  *     ones rely on the internal ones, and set errno.
61  *
62  * Each syscall will be defined with the two functions, sorted in alphabetical
63  * order applied to the exported names.
64  *
65  * In case of doubt about the relevance of a function here, only those which
66  * set errno should be defined here. Wrappers like those appearing in man(3)
67  * should not be placed here.
68  */
69
70
71 /*
72  * int brk(void *addr);
73  * void *sbrk(intptr_t inc)
74  */
75
76 static __attribute__((unused))
77 void *sys_brk(void *addr)
78 {
79         return (void *)my_syscall1(__NR_brk, addr);
80 }
81
82 static __attribute__((unused))
83 int brk(void *addr)
84 {
85         return __sysret(sys_brk(addr) ? 0 : -ENOMEM);
86 }
87
88 static __attribute__((unused))
89 void *sbrk(intptr_t inc)
90 {
91         /* first call to find current end */
92         void *ret = sys_brk(0);
93
94         if (ret && sys_brk(ret + inc) == ret + inc)
95                 return ret + inc;
96
97         return (void *)__sysret(-ENOMEM);
98 }
99
100
101 /*
102  * int chdir(const char *path);
103  */
104
105 static __attribute__((unused))
106 int sys_chdir(const char *path)
107 {
108         return my_syscall1(__NR_chdir, path);
109 }
110
111 static __attribute__((unused))
112 int chdir(const char *path)
113 {
114         return __sysret(sys_chdir(path));
115 }
116
117
118 /*
119  * int chmod(const char *path, mode_t mode);
120  */
121
122 static __attribute__((unused))
123 int sys_chmod(const char *path, mode_t mode)
124 {
125 #ifdef __NR_fchmodat
126         return my_syscall4(__NR_fchmodat, AT_FDCWD, path, mode, 0);
127 #elif defined(__NR_chmod)
128         return my_syscall2(__NR_chmod, path, mode);
129 #else
130         return -ENOSYS;
131 #endif
132 }
133
134 static __attribute__((unused))
135 int chmod(const char *path, mode_t mode)
136 {
137         return __sysret(sys_chmod(path, mode));
138 }
139
140
141 /*
142  * int chown(const char *path, uid_t owner, gid_t group);
143  */
144
145 static __attribute__((unused))
146 int sys_chown(const char *path, uid_t owner, gid_t group)
147 {
148 #ifdef __NR_fchownat
149         return my_syscall5(__NR_fchownat, AT_FDCWD, path, owner, group, 0);
150 #elif defined(__NR_chown)
151         return my_syscall3(__NR_chown, path, owner, group);
152 #else
153         return -ENOSYS;
154 #endif
155 }
156
157 static __attribute__((unused))
158 int chown(const char *path, uid_t owner, gid_t group)
159 {
160         return __sysret(sys_chown(path, owner, group));
161 }
162
163
164 /*
165  * int chroot(const char *path);
166  */
167
168 static __attribute__((unused))
169 int sys_chroot(const char *path)
170 {
171         return my_syscall1(__NR_chroot, path);
172 }
173
174 static __attribute__((unused))
175 int chroot(const char *path)
176 {
177         return __sysret(sys_chroot(path));
178 }
179
180
181 /*
182  * int close(int fd);
183  */
184
185 static __attribute__((unused))
186 int sys_close(int fd)
187 {
188         return my_syscall1(__NR_close, fd);
189 }
190
191 static __attribute__((unused))
192 int close(int fd)
193 {
194         return __sysret(sys_close(fd));
195 }
196
197
198 /*
199  * int dup(int fd);
200  */
201
202 static __attribute__((unused))
203 int sys_dup(int fd)
204 {
205         return my_syscall1(__NR_dup, fd);
206 }
207
208 static __attribute__((unused))
209 int dup(int fd)
210 {
211         return __sysret(sys_dup(fd));
212 }
213
214
215 /*
216  * int dup2(int old, int new);
217  */
218
219 static __attribute__((unused))
220 int sys_dup2(int old, int new)
221 {
222 #ifdef __NR_dup3
223         return my_syscall3(__NR_dup3, old, new, 0);
224 #elif defined(__NR_dup2)
225         return my_syscall2(__NR_dup2, old, new);
226 #else
227         return -ENOSYS;
228 #endif
229 }
230
231 static __attribute__((unused))
232 int dup2(int old, int new)
233 {
234         return __sysret(sys_dup2(old, new));
235 }
236
237
238 /*
239  * int dup3(int old, int new, int flags);
240  */
241
242 #ifdef __NR_dup3
243 static __attribute__((unused))
244 int sys_dup3(int old, int new, int flags)
245 {
246         return my_syscall3(__NR_dup3, old, new, flags);
247 }
248
249 static __attribute__((unused))
250 int dup3(int old, int new, int flags)
251 {
252         return __sysret(sys_dup3(old, new, flags));
253 }
254 #endif
255
256
257 /*
258  * int execve(const char *filename, char *const argv[], char *const envp[]);
259  */
260
261 static __attribute__((unused))
262 int sys_execve(const char *filename, char *const argv[], char *const envp[])
263 {
264         return my_syscall3(__NR_execve, filename, argv, envp);
265 }
266
267 static __attribute__((unused))
268 int execve(const char *filename, char *const argv[], char *const envp[])
269 {
270         return __sysret(sys_execve(filename, argv, envp));
271 }
272
273
274 /*
275  * void exit(int status);
276  */
277
278 static __attribute__((noreturn,unused))
279 void sys_exit(int status)
280 {
281         my_syscall1(__NR_exit, status & 255);
282         while(1); /* shut the "noreturn" warnings. */
283 }
284
285 static __attribute__((noreturn,unused))
286 void exit(int status)
287 {
288         sys_exit(status);
289 }
290
291
292 /*
293  * pid_t fork(void);
294  */
295
296 #ifndef sys_fork
297 static __attribute__((unused))
298 pid_t sys_fork(void)
299 {
300 #ifdef __NR_clone
301         /* note: some archs only have clone() and not fork(). Different archs
302          * have a different API, but most archs have the flags on first arg and
303          * will not use the rest with no other flag.
304          */
305         return my_syscall5(__NR_clone, SIGCHLD, 0, 0, 0, 0);
306 #elif defined(__NR_fork)
307         return my_syscall0(__NR_fork);
308 #else
309         return -ENOSYS;
310 #endif
311 }
312 #endif
313
314 static __attribute__((unused))
315 pid_t fork(void)
316 {
317         return __sysret(sys_fork());
318 }
319
320
321 /*
322  * int fsync(int fd);
323  */
324
325 static __attribute__((unused))
326 int sys_fsync(int fd)
327 {
328         return my_syscall1(__NR_fsync, fd);
329 }
330
331 static __attribute__((unused))
332 int fsync(int fd)
333 {
334         return __sysret(sys_fsync(fd));
335 }
336
337
338 /*
339  * int getdents64(int fd, struct linux_dirent64 *dirp, int count);
340  */
341
342 static __attribute__((unused))
343 int sys_getdents64(int fd, struct linux_dirent64 *dirp, int count)
344 {
345         return my_syscall3(__NR_getdents64, fd, dirp, count);
346 }
347
348 static __attribute__((unused))
349 int getdents64(int fd, struct linux_dirent64 *dirp, int count)
350 {
351         return __sysret(sys_getdents64(fd, dirp, count));
352 }
353
354
355 /*
356  * uid_t geteuid(void);
357  */
358
359 static __attribute__((unused))
360 uid_t sys_geteuid(void)
361 {
362 #ifdef __NR_geteuid32
363         return my_syscall0(__NR_geteuid32);
364 #else
365         return my_syscall0(__NR_geteuid);
366 #endif
367 }
368
369 static __attribute__((unused))
370 uid_t geteuid(void)
371 {
372         return sys_geteuid();
373 }
374
375
376 /*
377  * pid_t getpgid(pid_t pid);
378  */
379
380 static __attribute__((unused))
381 pid_t sys_getpgid(pid_t pid)
382 {
383         return my_syscall1(__NR_getpgid, pid);
384 }
385
386 static __attribute__((unused))
387 pid_t getpgid(pid_t pid)
388 {
389         return __sysret(sys_getpgid(pid));
390 }
391
392
393 /*
394  * pid_t getpgrp(void);
395  */
396
397 static __attribute__((unused))
398 pid_t sys_getpgrp(void)
399 {
400         return sys_getpgid(0);
401 }
402
403 static __attribute__((unused))
404 pid_t getpgrp(void)
405 {
406         return sys_getpgrp();
407 }
408
409
410 /*
411  * pid_t getpid(void);
412  */
413
414 static __attribute__((unused))
415 pid_t sys_getpid(void)
416 {
417         return my_syscall0(__NR_getpid);
418 }
419
420 static __attribute__((unused))
421 pid_t getpid(void)
422 {
423         return sys_getpid();
424 }
425
426
427 /*
428  * pid_t getppid(void);
429  */
430
431 static __attribute__((unused))
432 pid_t sys_getppid(void)
433 {
434         return my_syscall0(__NR_getppid);
435 }
436
437 static __attribute__((unused))
438 pid_t getppid(void)
439 {
440         return sys_getppid();
441 }
442
443
444 /*
445  * pid_t gettid(void);
446  */
447
448 static __attribute__((unused))
449 pid_t sys_gettid(void)
450 {
451         return my_syscall0(__NR_gettid);
452 }
453
454 static __attribute__((unused))
455 pid_t gettid(void)
456 {
457         return sys_gettid();
458 }
459
460 static unsigned long getauxval(unsigned long key);
461
462 /*
463  * int getpagesize(void);
464  */
465
466 static __attribute__((unused))
467 int getpagesize(void)
468 {
469         return __sysret(getauxval(AT_PAGESZ) ?: -ENOENT);
470 }
471
472
473 /*
474  * int gettimeofday(struct timeval *tv, struct timezone *tz);
475  */
476
477 static __attribute__((unused))
478 int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
479 {
480 #ifdef __NR_gettimeofday
481         return my_syscall2(__NR_gettimeofday, tv, tz);
482 #else
483         return -ENOSYS;
484 #endif
485 }
486
487 static __attribute__((unused))
488 int gettimeofday(struct timeval *tv, struct timezone *tz)
489 {
490         return __sysret(sys_gettimeofday(tv, tz));
491 }
492
493
494 /*
495  * uid_t getuid(void);
496  */
497
498 static __attribute__((unused))
499 uid_t sys_getuid(void)
500 {
501 #ifdef __NR_getuid32
502         return my_syscall0(__NR_getuid32);
503 #else
504         return my_syscall0(__NR_getuid);
505 #endif
506 }
507
508 static __attribute__((unused))
509 uid_t getuid(void)
510 {
511         return sys_getuid();
512 }
513
514
515 /*
516  * int ioctl(int fd, unsigned long req, void *value);
517  */
518
519 static __attribute__((unused))
520 int sys_ioctl(int fd, unsigned long req, void *value)
521 {
522         return my_syscall3(__NR_ioctl, fd, req, value);
523 }
524
525 static __attribute__((unused))
526 int ioctl(int fd, unsigned long req, void *value)
527 {
528         return __sysret(sys_ioctl(fd, req, value));
529 }
530
531 /*
532  * int kill(pid_t pid, int signal);
533  */
534
535 static __attribute__((unused))
536 int sys_kill(pid_t pid, int signal)
537 {
538         return my_syscall2(__NR_kill, pid, signal);
539 }
540
541 static __attribute__((unused))
542 int kill(pid_t pid, int signal)
543 {
544         return __sysret(sys_kill(pid, signal));
545 }
546
547
548 /*
549  * int link(const char *old, const char *new);
550  */
551
552 static __attribute__((unused))
553 int sys_link(const char *old, const char *new)
554 {
555 #ifdef __NR_linkat
556         return my_syscall5(__NR_linkat, AT_FDCWD, old, AT_FDCWD, new, 0);
557 #elif defined(__NR_link)
558         return my_syscall2(__NR_link, old, new);
559 #else
560         return -ENOSYS;
561 #endif
562 }
563
564 static __attribute__((unused))
565 int link(const char *old, const char *new)
566 {
567         return __sysret(sys_link(old, new));
568 }
569
570
571 /*
572  * off_t lseek(int fd, off_t offset, int whence);
573  */
574
575 static __attribute__((unused))
576 off_t sys_lseek(int fd, off_t offset, int whence)
577 {
578 #ifdef __NR_lseek
579         return my_syscall3(__NR_lseek, fd, offset, whence);
580 #else
581         return -ENOSYS;
582 #endif
583 }
584
585 static __attribute__((unused))
586 off_t lseek(int fd, off_t offset, int whence)
587 {
588         return __sysret(sys_lseek(fd, offset, whence));
589 }
590
591
592 /*
593  * int mkdir(const char *path, mode_t mode);
594  */
595
596 static __attribute__((unused))
597 int sys_mkdir(const char *path, mode_t mode)
598 {
599 #ifdef __NR_mkdirat
600         return my_syscall3(__NR_mkdirat, AT_FDCWD, path, mode);
601 #elif defined(__NR_mkdir)
602         return my_syscall2(__NR_mkdir, path, mode);
603 #else
604         return -ENOSYS;
605 #endif
606 }
607
608 static __attribute__((unused))
609 int mkdir(const char *path, mode_t mode)
610 {
611         return __sysret(sys_mkdir(path, mode));
612 }
613
614 /*
615  * int rmdir(const char *path);
616  */
617
618 static __attribute__((unused))
619 int sys_rmdir(const char *path)
620 {
621 #ifdef __NR_rmdir
622         return my_syscall1(__NR_rmdir, path);
623 #elif defined(__NR_unlinkat)
624         return my_syscall3(__NR_unlinkat, AT_FDCWD, path, AT_REMOVEDIR);
625 #else
626         return -ENOSYS;
627 #endif
628 }
629
630 static __attribute__((unused))
631 int rmdir(const char *path)
632 {
633         return __sysret(sys_rmdir(path));
634 }
635
636
637 /*
638  * int mknod(const char *path, mode_t mode, dev_t dev);
639  */
640
641 static __attribute__((unused))
642 long sys_mknod(const char *path, mode_t mode, dev_t dev)
643 {
644 #ifdef __NR_mknodat
645         return my_syscall4(__NR_mknodat, AT_FDCWD, path, mode, dev);
646 #elif defined(__NR_mknod)
647         return my_syscall3(__NR_mknod, path, mode, dev);
648 #else
649         return -ENOSYS;
650 #endif
651 }
652
653 static __attribute__((unused))
654 int mknod(const char *path, mode_t mode, dev_t dev)
655 {
656         return __sysret(sys_mknod(path, mode, dev));
657 }
658
659 #ifndef sys_mmap
660 static __attribute__((unused))
661 void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd,
662                off_t offset)
663 {
664         int n;
665
666 #if defined(__NR_mmap2)
667         n = __NR_mmap2;
668         offset >>= 12;
669 #else
670         n = __NR_mmap;
671 #endif
672
673         return (void *)my_syscall6(n, addr, length, prot, flags, fd, offset);
674 }
675 #endif
676
677 /* Note that on Linux, MAP_FAILED is -1 so we can use the generic __sysret()
678  * which returns -1 upon error and still satisfy user land that checks for
679  * MAP_FAILED.
680  */
681
682 static __attribute__((unused))
683 void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
684 {
685         return (void *)__sysret((unsigned long)sys_mmap(addr, length, prot, flags, fd, offset));
686 }
687
688 static __attribute__((unused))
689 int sys_munmap(void *addr, size_t length)
690 {
691         return my_syscall2(__NR_munmap, addr, length);
692 }
693
694 static __attribute__((unused))
695 int munmap(void *addr, size_t length)
696 {
697         return __sysret(sys_munmap(addr, length));
698 }
699
700 /*
701  * int mount(const char *source, const char *target,
702  *           const char *fstype, unsigned long flags,
703  *           const void *data);
704  */
705 static __attribute__((unused))
706 int sys_mount(const char *src, const char *tgt, const char *fst,
707                      unsigned long flags, const void *data)
708 {
709         return my_syscall5(__NR_mount, src, tgt, fst, flags, data);
710 }
711
712 static __attribute__((unused))
713 int mount(const char *src, const char *tgt,
714           const char *fst, unsigned long flags,
715           const void *data)
716 {
717         return __sysret(sys_mount(src, tgt, fst, flags, data));
718 }
719
720
721 /*
722  * int open(const char *path, int flags[, mode_t mode]);
723  */
724
725 static __attribute__((unused))
726 int sys_open(const char *path, int flags, mode_t mode)
727 {
728 #ifdef __NR_openat
729         return my_syscall4(__NR_openat, AT_FDCWD, path, flags, mode);
730 #elif defined(__NR_open)
731         return my_syscall3(__NR_open, path, flags, mode);
732 #else
733         return -ENOSYS;
734 #endif
735 }
736
737 static __attribute__((unused))
738 int open(const char *path, int flags, ...)
739 {
740         mode_t mode = 0;
741
742         if (flags & O_CREAT) {
743                 va_list args;
744
745                 va_start(args, flags);
746                 mode = va_arg(args, int);
747                 va_end(args);
748         }
749
750         return __sysret(sys_open(path, flags, mode));
751 }
752
753
754 /*
755  * int pipe2(int pipefd[2], int flags);
756  * int pipe(int pipefd[2]);
757  */
758
759 static __attribute__((unused))
760 int sys_pipe2(int pipefd[2], int flags)
761 {
762         return my_syscall2(__NR_pipe2, pipefd, flags);
763 }
764
765 static __attribute__((unused))
766 int pipe2(int pipefd[2], int flags)
767 {
768         return __sysret(sys_pipe2(pipefd, flags));
769 }
770
771 static __attribute__((unused))
772 int pipe(int pipefd[2])
773 {
774         return pipe2(pipefd, 0);
775 }
776
777
778 /*
779  * int prctl(int option, unsigned long arg2, unsigned long arg3,
780  *                       unsigned long arg4, unsigned long arg5);
781  */
782
783 static __attribute__((unused))
784 int sys_prctl(int option, unsigned long arg2, unsigned long arg3,
785                           unsigned long arg4, unsigned long arg5)
786 {
787         return my_syscall5(__NR_prctl, option, arg2, arg3, arg4, arg5);
788 }
789
790 static __attribute__((unused))
791 int prctl(int option, unsigned long arg2, unsigned long arg3,
792                       unsigned long arg4, unsigned long arg5)
793 {
794         return __sysret(sys_prctl(option, arg2, arg3, arg4, arg5));
795 }
796
797
798 /*
799  * int pivot_root(const char *new, const char *old);
800  */
801
802 static __attribute__((unused))
803 int sys_pivot_root(const char *new, const char *old)
804 {
805         return my_syscall2(__NR_pivot_root, new, old);
806 }
807
808 static __attribute__((unused))
809 int pivot_root(const char *new, const char *old)
810 {
811         return __sysret(sys_pivot_root(new, old));
812 }
813
814
815 /*
816  * int poll(struct pollfd *fds, int nfds, int timeout);
817  */
818
819 static __attribute__((unused))
820 int sys_poll(struct pollfd *fds, int nfds, int timeout)
821 {
822 #if defined(__NR_ppoll)
823         struct timespec t;
824
825         if (timeout >= 0) {
826                 t.tv_sec  = timeout / 1000;
827                 t.tv_nsec = (timeout % 1000) * 1000000;
828         }
829         return my_syscall5(__NR_ppoll, fds, nfds, (timeout >= 0) ? &t : NULL, NULL, 0);
830 #elif defined(__NR_poll)
831         return my_syscall3(__NR_poll, fds, nfds, timeout);
832 #else
833         return -ENOSYS;
834 #endif
835 }
836
837 static __attribute__((unused))
838 int poll(struct pollfd *fds, int nfds, int timeout)
839 {
840         return __sysret(sys_poll(fds, nfds, timeout));
841 }
842
843
844 /*
845  * ssize_t read(int fd, void *buf, size_t count);
846  */
847
848 static __attribute__((unused))
849 ssize_t sys_read(int fd, void *buf, size_t count)
850 {
851         return my_syscall3(__NR_read, fd, buf, count);
852 }
853
854 static __attribute__((unused))
855 ssize_t read(int fd, void *buf, size_t count)
856 {
857         return __sysret(sys_read(fd, buf, count));
858 }
859
860
861 /*
862  * int reboot(int cmd);
863  * <cmd> is among LINUX_REBOOT_CMD_*
864  */
865
866 static __attribute__((unused))
867 ssize_t sys_reboot(int magic1, int magic2, int cmd, void *arg)
868 {
869         return my_syscall4(__NR_reboot, magic1, magic2, cmd, arg);
870 }
871
872 static __attribute__((unused))
873 int reboot(int cmd)
874 {
875         return __sysret(sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0));
876 }
877
878
879 /*
880  * int sched_yield(void);
881  */
882
883 static __attribute__((unused))
884 int sys_sched_yield(void)
885 {
886         return my_syscall0(__NR_sched_yield);
887 }
888
889 static __attribute__((unused))
890 int sched_yield(void)
891 {
892         return __sysret(sys_sched_yield());
893 }
894
895
896 /*
897  * int select(int nfds, fd_set *read_fds, fd_set *write_fds,
898  *            fd_set *except_fds, struct timeval *timeout);
899  */
900
901 static __attribute__((unused))
902 int sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
903 {
904 #if defined(__ARCH_WANT_SYS_OLD_SELECT) && !defined(__NR__newselect)
905         struct sel_arg_struct {
906                 unsigned long n;
907                 fd_set *r, *w, *e;
908                 struct timeval *t;
909         } arg = { .n = nfds, .r = rfds, .w = wfds, .e = efds, .t = timeout };
910         return my_syscall1(__NR_select, &arg);
911 #elif defined(__ARCH_WANT_SYS_PSELECT6) && defined(__NR_pselect6)
912         struct timespec t;
913
914         if (timeout) {
915                 t.tv_sec  = timeout->tv_sec;
916                 t.tv_nsec = timeout->tv_usec * 1000;
917         }
918         return my_syscall6(__NR_pselect6, nfds, rfds, wfds, efds, timeout ? &t : NULL, NULL);
919 #elif defined(__NR__newselect) || defined(__NR_select)
920 #ifndef __NR__newselect
921 #define __NR__newselect __NR_select
922 #endif
923         return my_syscall5(__NR__newselect, nfds, rfds, wfds, efds, timeout);
924 #else
925         return -ENOSYS;
926 #endif
927 }
928
929 static __attribute__((unused))
930 int select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
931 {
932         return __sysret(sys_select(nfds, rfds, wfds, efds, timeout));
933 }
934
935
936 /*
937  * int setpgid(pid_t pid, pid_t pgid);
938  */
939
940 static __attribute__((unused))
941 int sys_setpgid(pid_t pid, pid_t pgid)
942 {
943         return my_syscall2(__NR_setpgid, pid, pgid);
944 }
945
946 static __attribute__((unused))
947 int setpgid(pid_t pid, pid_t pgid)
948 {
949         return __sysret(sys_setpgid(pid, pgid));
950 }
951
952
953 /*
954  * pid_t setsid(void);
955  */
956
957 static __attribute__((unused))
958 pid_t sys_setsid(void)
959 {
960         return my_syscall0(__NR_setsid);
961 }
962
963 static __attribute__((unused))
964 pid_t setsid(void)
965 {
966         return __sysret(sys_setsid());
967 }
968
969 /*
970  * int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf);
971  * int stat(const char *path, struct stat *buf);
972  */
973
974 static __attribute__((unused))
975 int sys_statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
976 {
977 #ifdef __NR_statx
978         return my_syscall5(__NR_statx, fd, path, flags, mask, buf);
979 #else
980         return -ENOSYS;
981 #endif
982 }
983
984 static __attribute__((unused))
985 int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
986 {
987         return __sysret(sys_statx(fd, path, flags, mask, buf));
988 }
989
990
991 static __attribute__((unused))
992 int stat(const char *path, struct stat *buf)
993 {
994         struct statx statx;
995         long ret;
996
997         ret = __sysret(sys_statx(AT_FDCWD, path, AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx));
998         if (ret == -1)
999                 return ret;
1000
1001         buf->st_dev          = ((statx.stx_dev_minor & 0xff)
1002                                | (statx.stx_dev_major << 8)
1003                                | ((statx.stx_dev_minor & ~0xff) << 12));
1004         buf->st_ino          = statx.stx_ino;
1005         buf->st_mode         = statx.stx_mode;
1006         buf->st_nlink        = statx.stx_nlink;
1007         buf->st_uid          = statx.stx_uid;
1008         buf->st_gid          = statx.stx_gid;
1009         buf->st_rdev         = ((statx.stx_rdev_minor & 0xff)
1010                                | (statx.stx_rdev_major << 8)
1011                                | ((statx.stx_rdev_minor & ~0xff) << 12));
1012         buf->st_size         = statx.stx_size;
1013         buf->st_blksize      = statx.stx_blksize;
1014         buf->st_blocks       = statx.stx_blocks;
1015         buf->st_atim.tv_sec  = statx.stx_atime.tv_sec;
1016         buf->st_atim.tv_nsec = statx.stx_atime.tv_nsec;
1017         buf->st_mtim.tv_sec  = statx.stx_mtime.tv_sec;
1018         buf->st_mtim.tv_nsec = statx.stx_mtime.tv_nsec;
1019         buf->st_ctim.tv_sec  = statx.stx_ctime.tv_sec;
1020         buf->st_ctim.tv_nsec = statx.stx_ctime.tv_nsec;
1021
1022         return 0;
1023 }
1024
1025
1026 /*
1027  * int symlink(const char *old, const char *new);
1028  */
1029
1030 static __attribute__((unused))
1031 int sys_symlink(const char *old, const char *new)
1032 {
1033 #ifdef __NR_symlinkat
1034         return my_syscall3(__NR_symlinkat, old, AT_FDCWD, new);
1035 #elif defined(__NR_symlink)
1036         return my_syscall2(__NR_symlink, old, new);
1037 #else
1038         return -ENOSYS;
1039 #endif
1040 }
1041
1042 static __attribute__((unused))
1043 int symlink(const char *old, const char *new)
1044 {
1045         return __sysret(sys_symlink(old, new));
1046 }
1047
1048
1049 /*
1050  * mode_t umask(mode_t mode);
1051  */
1052
1053 static __attribute__((unused))
1054 mode_t sys_umask(mode_t mode)
1055 {
1056         return my_syscall1(__NR_umask, mode);
1057 }
1058
1059 static __attribute__((unused))
1060 mode_t umask(mode_t mode)
1061 {
1062         return sys_umask(mode);
1063 }
1064
1065
1066 /*
1067  * int umount2(const char *path, int flags);
1068  */
1069
1070 static __attribute__((unused))
1071 int sys_umount2(const char *path, int flags)
1072 {
1073         return my_syscall2(__NR_umount2, path, flags);
1074 }
1075
1076 static __attribute__((unused))
1077 int umount2(const char *path, int flags)
1078 {
1079         return __sysret(sys_umount2(path, flags));
1080 }
1081
1082
1083 /*
1084  * int unlink(const char *path);
1085  */
1086
1087 static __attribute__((unused))
1088 int sys_unlink(const char *path)
1089 {
1090 #ifdef __NR_unlinkat
1091         return my_syscall3(__NR_unlinkat, AT_FDCWD, path, 0);
1092 #elif defined(__NR_unlink)
1093         return my_syscall1(__NR_unlink, path);
1094 #else
1095         return -ENOSYS;
1096 #endif
1097 }
1098
1099 static __attribute__((unused))
1100 int unlink(const char *path)
1101 {
1102         return __sysret(sys_unlink(path));
1103 }
1104
1105
1106 /*
1107  * pid_t wait(int *status);
1108  * pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage);
1109  * pid_t waitpid(pid_t pid, int *status, int options);
1110  */
1111
1112 static __attribute__((unused))
1113 pid_t sys_wait4(pid_t pid, int *status, int options, struct rusage *rusage)
1114 {
1115 #ifdef __NR_wait4
1116         return my_syscall4(__NR_wait4, pid, status, options, rusage);
1117 #else
1118         return -ENOSYS;
1119 #endif
1120 }
1121
1122 static __attribute__((unused))
1123 pid_t wait(int *status)
1124 {
1125         return __sysret(sys_wait4(-1, status, 0, NULL));
1126 }
1127
1128 static __attribute__((unused))
1129 pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage)
1130 {
1131         return __sysret(sys_wait4(pid, status, options, rusage));
1132 }
1133
1134
1135 static __attribute__((unused))
1136 pid_t waitpid(pid_t pid, int *status, int options)
1137 {
1138         return __sysret(sys_wait4(pid, status, options, NULL));
1139 }
1140
1141
1142 /*
1143  * ssize_t write(int fd, const void *buf, size_t count);
1144  */
1145
1146 static __attribute__((unused))
1147 ssize_t sys_write(int fd, const void *buf, size_t count)
1148 {
1149         return my_syscall3(__NR_write, fd, buf, count);
1150 }
1151
1152 static __attribute__((unused))
1153 ssize_t write(int fd, const void *buf, size_t count)
1154 {
1155         return __sysret(sys_write(fd, buf, count));
1156 }
1157
1158
1159 /*
1160  * int memfd_create(const char *name, unsigned int flags);
1161  */
1162
1163 static __attribute__((unused))
1164 int sys_memfd_create(const char *name, unsigned int flags)
1165 {
1166         return my_syscall2(__NR_memfd_create, name, flags);
1167 }
1168
1169 static __attribute__((unused))
1170 int memfd_create(const char *name, unsigned int flags)
1171 {
1172         return __sysret(sys_memfd_create(name, flags));
1173 }
1174
1175 /* make sure to include all global symbols */
1176 #include "nolibc.h"
1177
1178 #endif /* _NOLIBC_SYS_H */