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