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