xref: /openbmc/linux/tools/include/nolibc/sys.h (revision 3ec38af6)
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  * long getpagesize(void);
464  */
465 
466 static __attribute__((unused))
467 long 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 	int ret;
742 
743 	if (flags & O_CREAT) {
744 		va_list args;
745 
746 		va_start(args, flags);
747 		mode = va_arg(args, int);
748 		va_end(args);
749 	}
750 
751 	return __sysret(sys_open(path, flags, mode));
752 }
753 
754 
755 /*
756  * int pipe2(int pipefd[2], int flags);
757  * int pipe(int pipefd[2]);
758  */
759 
760 static __attribute__((unused))
761 int sys_pipe2(int pipefd[2], int flags)
762 {
763 	return my_syscall2(__NR_pipe2, pipefd, flags);
764 }
765 
766 static __attribute__((unused))
767 int pipe2(int pipefd[2], int flags)
768 {
769 	return __sysret(sys_pipe2(pipefd, flags));
770 }
771 
772 static __attribute__((unused))
773 int pipe(int pipefd[2])
774 {
775 	return pipe2(pipefd, 0);
776 }
777 
778 
779 /*
780  * int prctl(int option, unsigned long arg2, unsigned long arg3,
781  *                       unsigned long arg4, unsigned long arg5);
782  */
783 
784 static __attribute__((unused))
785 int sys_prctl(int option, unsigned long arg2, unsigned long arg3,
786 		          unsigned long arg4, unsigned long arg5)
787 {
788 	return my_syscall5(__NR_prctl, option, arg2, arg3, arg4, arg5);
789 }
790 
791 static __attribute__((unused))
792 int prctl(int option, unsigned long arg2, unsigned long arg3,
793 		      unsigned long arg4, unsigned long arg5)
794 {
795 	return __sysret(sys_prctl(option, arg2, arg3, arg4, arg5));
796 }
797 
798 
799 /*
800  * int pivot_root(const char *new, const char *old);
801  */
802 
803 static __attribute__((unused))
804 int sys_pivot_root(const char *new, const char *old)
805 {
806 	return my_syscall2(__NR_pivot_root, new, old);
807 }
808 
809 static __attribute__((unused))
810 int pivot_root(const char *new, const char *old)
811 {
812 	return __sysret(sys_pivot_root(new, old));
813 }
814 
815 
816 /*
817  * int poll(struct pollfd *fds, int nfds, int timeout);
818  */
819 
820 static __attribute__((unused))
821 int sys_poll(struct pollfd *fds, int nfds, int timeout)
822 {
823 #if defined(__NR_ppoll)
824 	struct timespec t;
825 
826 	if (timeout >= 0) {
827 		t.tv_sec  = timeout / 1000;
828 		t.tv_nsec = (timeout % 1000) * 1000000;
829 	}
830 	return my_syscall5(__NR_ppoll, fds, nfds, (timeout >= 0) ? &t : NULL, NULL, 0);
831 #elif defined(__NR_poll)
832 	return my_syscall3(__NR_poll, fds, nfds, timeout);
833 #else
834 	return -ENOSYS;
835 #endif
836 }
837 
838 static __attribute__((unused))
839 int poll(struct pollfd *fds, int nfds, int timeout)
840 {
841 	return __sysret(sys_poll(fds, nfds, timeout));
842 }
843 
844 
845 /*
846  * ssize_t read(int fd, void *buf, size_t count);
847  */
848 
849 static __attribute__((unused))
850 ssize_t sys_read(int fd, void *buf, size_t count)
851 {
852 	return my_syscall3(__NR_read, fd, buf, count);
853 }
854 
855 static __attribute__((unused))
856 ssize_t read(int fd, void *buf, size_t count)
857 {
858 	return __sysret(sys_read(fd, buf, count));
859 }
860 
861 
862 /*
863  * int reboot(int cmd);
864  * <cmd> is among LINUX_REBOOT_CMD_*
865  */
866 
867 static __attribute__((unused))
868 ssize_t sys_reboot(int magic1, int magic2, int cmd, void *arg)
869 {
870 	return my_syscall4(__NR_reboot, magic1, magic2, cmd, arg);
871 }
872 
873 static __attribute__((unused))
874 int reboot(int cmd)
875 {
876 	return __sysret(sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0));
877 }
878 
879 
880 /*
881  * int sched_yield(void);
882  */
883 
884 static __attribute__((unused))
885 int sys_sched_yield(void)
886 {
887 	return my_syscall0(__NR_sched_yield);
888 }
889 
890 static __attribute__((unused))
891 int sched_yield(void)
892 {
893 	return __sysret(sys_sched_yield());
894 }
895 
896 
897 /*
898  * int select(int nfds, fd_set *read_fds, fd_set *write_fds,
899  *            fd_set *except_fds, struct timeval *timeout);
900  */
901 
902 static __attribute__((unused))
903 int sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
904 {
905 #if defined(__ARCH_WANT_SYS_OLD_SELECT) && !defined(__NR__newselect)
906 	struct sel_arg_struct {
907 		unsigned long n;
908 		fd_set *r, *w, *e;
909 		struct timeval *t;
910 	} arg = { .n = nfds, .r = rfds, .w = wfds, .e = efds, .t = timeout };
911 	return my_syscall1(__NR_select, &arg);
912 #elif defined(__ARCH_WANT_SYS_PSELECT6) && defined(__NR_pselect6)
913 	struct timespec t;
914 
915 	if (timeout) {
916 		t.tv_sec  = timeout->tv_sec;
917 		t.tv_nsec = timeout->tv_usec * 1000;
918 	}
919 	return my_syscall6(__NR_pselect6, nfds, rfds, wfds, efds, timeout ? &t : NULL, NULL);
920 #elif defined(__NR__newselect) || defined(__NR_select)
921 #ifndef __NR__newselect
922 #define __NR__newselect __NR_select
923 #endif
924 	return my_syscall5(__NR__newselect, nfds, rfds, wfds, efds, timeout);
925 #else
926 	return -ENOSYS;
927 #endif
928 }
929 
930 static __attribute__((unused))
931 int select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
932 {
933 	return __sysret(sys_select(nfds, rfds, wfds, efds, timeout));
934 }
935 
936 
937 /*
938  * int setpgid(pid_t pid, pid_t pgid);
939  */
940 
941 static __attribute__((unused))
942 int sys_setpgid(pid_t pid, pid_t pgid)
943 {
944 	return my_syscall2(__NR_setpgid, pid, pgid);
945 }
946 
947 static __attribute__((unused))
948 int setpgid(pid_t pid, pid_t pgid)
949 {
950 	return __sysret(sys_setpgid(pid, pgid));
951 }
952 
953 
954 /*
955  * pid_t setsid(void);
956  */
957 
958 static __attribute__((unused))
959 pid_t sys_setsid(void)
960 {
961 	return my_syscall0(__NR_setsid);
962 }
963 
964 static __attribute__((unused))
965 pid_t setsid(void)
966 {
967 	return __sysret(sys_setsid());
968 }
969 
970 /*
971  * int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf);
972  * int stat(const char *path, struct stat *buf);
973  */
974 
975 static __attribute__((unused))
976 int sys_statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
977 {
978 #ifdef __NR_statx
979 	return my_syscall5(__NR_statx, fd, path, flags, mask, buf);
980 #else
981 	return -ENOSYS;
982 #endif
983 }
984 
985 static __attribute__((unused))
986 int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
987 {
988 	return __sysret(sys_statx(fd, path, flags, mask, buf));
989 }
990 
991 
992 static __attribute__((unused))
993 int stat(const char *path, struct stat *buf)
994 {
995 	struct statx statx;
996 	long ret;
997 
998 	ret = __sysret(sys_statx(AT_FDCWD, path, AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx));
999 	if (ret == -1)
1000 		return ret;
1001 
1002 	buf->st_dev          = ((statx.stx_dev_minor & 0xff)
1003 			       | (statx.stx_dev_major << 8)
1004 			       | ((statx.stx_dev_minor & ~0xff) << 12));
1005 	buf->st_ino          = statx.stx_ino;
1006 	buf->st_mode         = statx.stx_mode;
1007 	buf->st_nlink        = statx.stx_nlink;
1008 	buf->st_uid          = statx.stx_uid;
1009 	buf->st_gid          = statx.stx_gid;
1010 	buf->st_rdev         = ((statx.stx_rdev_minor & 0xff)
1011 			       | (statx.stx_rdev_major << 8)
1012 			       | ((statx.stx_rdev_minor & ~0xff) << 12));
1013 	buf->st_size         = statx.stx_size;
1014 	buf->st_blksize      = statx.stx_blksize;
1015 	buf->st_blocks       = statx.stx_blocks;
1016 	buf->st_atim.tv_sec  = statx.stx_atime.tv_sec;
1017 	buf->st_atim.tv_nsec = statx.stx_atime.tv_nsec;
1018 	buf->st_mtim.tv_sec  = statx.stx_mtime.tv_sec;
1019 	buf->st_mtim.tv_nsec = statx.stx_mtime.tv_nsec;
1020 	buf->st_ctim.tv_sec  = statx.stx_ctime.tv_sec;
1021 	buf->st_ctim.tv_nsec = statx.stx_ctime.tv_nsec;
1022 
1023 	return 0;
1024 }
1025 
1026 
1027 /*
1028  * int symlink(const char *old, const char *new);
1029  */
1030 
1031 static __attribute__((unused))
1032 int sys_symlink(const char *old, const char *new)
1033 {
1034 #ifdef __NR_symlinkat
1035 	return my_syscall3(__NR_symlinkat, old, AT_FDCWD, new);
1036 #elif defined(__NR_symlink)
1037 	return my_syscall2(__NR_symlink, old, new);
1038 #else
1039 	return -ENOSYS;
1040 #endif
1041 }
1042 
1043 static __attribute__((unused))
1044 int symlink(const char *old, const char *new)
1045 {
1046 	return __sysret(sys_symlink(old, new));
1047 }
1048 
1049 
1050 /*
1051  * mode_t umask(mode_t mode);
1052  */
1053 
1054 static __attribute__((unused))
1055 mode_t sys_umask(mode_t mode)
1056 {
1057 	return my_syscall1(__NR_umask, mode);
1058 }
1059 
1060 static __attribute__((unused))
1061 mode_t umask(mode_t mode)
1062 {
1063 	return sys_umask(mode);
1064 }
1065 
1066 
1067 /*
1068  * int umount2(const char *path, int flags);
1069  */
1070 
1071 static __attribute__((unused))
1072 int sys_umount2(const char *path, int flags)
1073 {
1074 	return my_syscall2(__NR_umount2, path, flags);
1075 }
1076 
1077 static __attribute__((unused))
1078 int umount2(const char *path, int flags)
1079 {
1080 	return __sysret(sys_umount2(path, flags));
1081 }
1082 
1083 
1084 /*
1085  * int unlink(const char *path);
1086  */
1087 
1088 static __attribute__((unused))
1089 int sys_unlink(const char *path)
1090 {
1091 #ifdef __NR_unlinkat
1092 	return my_syscall3(__NR_unlinkat, AT_FDCWD, path, 0);
1093 #elif defined(__NR_unlink)
1094 	return my_syscall1(__NR_unlink, path);
1095 #else
1096 	return -ENOSYS;
1097 #endif
1098 }
1099 
1100 static __attribute__((unused))
1101 int unlink(const char *path)
1102 {
1103 	return __sysret(sys_unlink(path));
1104 }
1105 
1106 
1107 /*
1108  * pid_t wait(int *status);
1109  * pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage);
1110  * pid_t waitpid(pid_t pid, int *status, int options);
1111  */
1112 
1113 static __attribute__((unused))
1114 pid_t sys_wait4(pid_t pid, int *status, int options, struct rusage *rusage)
1115 {
1116 #ifdef __NR_wait4
1117 	return my_syscall4(__NR_wait4, pid, status, options, rusage);
1118 #else
1119 	return -ENOSYS;
1120 #endif
1121 }
1122 
1123 static __attribute__((unused))
1124 pid_t wait(int *status)
1125 {
1126 	return __sysret(sys_wait4(-1, status, 0, NULL));
1127 }
1128 
1129 static __attribute__((unused))
1130 pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage)
1131 {
1132 	return __sysret(sys_wait4(pid, status, options, rusage));
1133 }
1134 
1135 
1136 static __attribute__((unused))
1137 pid_t waitpid(pid_t pid, int *status, int options)
1138 {
1139 	return __sysret(sys_wait4(pid, status, options, NULL));
1140 }
1141 
1142 
1143 /*
1144  * ssize_t write(int fd, const void *buf, size_t count);
1145  */
1146 
1147 static __attribute__((unused))
1148 ssize_t sys_write(int fd, const void *buf, size_t count)
1149 {
1150 	return my_syscall3(__NR_write, fd, buf, count);
1151 }
1152 
1153 static __attribute__((unused))
1154 ssize_t write(int fd, const void *buf, size_t count)
1155 {
1156 	return __sysret(sys_write(fd, buf, count));
1157 }
1158 
1159 
1160 /*
1161  * int memfd_create(const char *name, unsigned int flags);
1162  */
1163 
1164 static __attribute__((unused))
1165 int sys_memfd_create(const char *name, unsigned int flags)
1166 {
1167 	return my_syscall2(__NR_memfd_create, name, flags);
1168 }
1169 
1170 static __attribute__((unused))
1171 int memfd_create(const char *name, unsigned int flags)
1172 {
1173 	return __sysret(sys_memfd_create(name, flags));
1174 }
1175 
1176 /* make sure to include all global symbols */
1177 #include "nolibc.h"
1178 
1179 #endif /* _NOLIBC_SYS_H */
1180