1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
3 * Syscall definitions for NOLIBC (those in man(2))
4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
14 #include <asm/unistd.h>
15 #include <asm/signal.h> /* for SIGCHLD */
16 #include <asm/ioctls.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>
31 /* Syscall return helper for library routines, set errno as -ret when ret is in
32 * range of [-MAX_ERRNO, -1]
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)
38 static __inline__ __attribute__((unused, always_inline))
39 long __sysret(unsigned long ret)
41 if (ret >= (unsigned long)-MAX_ERRNO) {
42 SET_ERRNO(-(long)ret);
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
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.
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.
62 * Each syscall will be defined with the two functions, sorted in alphabetical
63 * order applied to the exported names.
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.
72 * int brk(void *addr);
73 * void *sbrk(intptr_t inc)
76 static __attribute__((unused))
77 void *sys_brk(void *addr)
79 return (void *)my_syscall1(__NR_brk, addr);
82 static __attribute__((unused))
85 return __sysret(sys_brk(addr) ? 0 : -ENOMEM);
88 static __attribute__((unused))
89 void *sbrk(intptr_t inc)
91 /* first call to find current end */
92 void *ret = sys_brk(0);
94 if (ret && sys_brk(ret + inc) == ret + inc)
97 return (void *)__sysret(-ENOMEM);
102 * int chdir(const char *path);
105 static __attribute__((unused))
106 int sys_chdir(const char *path)
108 return my_syscall1(__NR_chdir, path);
111 static __attribute__((unused))
112 int chdir(const char *path)
114 return __sysret(sys_chdir(path));
119 * int chmod(const char *path, mode_t mode);
122 static __attribute__((unused))
123 int sys_chmod(const char *path, mode_t mode)
126 return my_syscall4(__NR_fchmodat, AT_FDCWD, path, mode, 0);
127 #elif defined(__NR_chmod)
128 return my_syscall2(__NR_chmod, path, mode);
134 static __attribute__((unused))
135 int chmod(const char *path, mode_t mode)
137 return __sysret(sys_chmod(path, mode));
142 * int chown(const char *path, uid_t owner, gid_t group);
145 static __attribute__((unused))
146 int sys_chown(const char *path, uid_t owner, gid_t group)
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);
157 static __attribute__((unused))
158 int chown(const char *path, uid_t owner, gid_t group)
160 return __sysret(sys_chown(path, owner, group));
165 * int chroot(const char *path);
168 static __attribute__((unused))
169 int sys_chroot(const char *path)
171 return my_syscall1(__NR_chroot, path);
174 static __attribute__((unused))
175 int chroot(const char *path)
177 return __sysret(sys_chroot(path));
185 static __attribute__((unused))
186 int sys_close(int fd)
188 return my_syscall1(__NR_close, fd);
191 static __attribute__((unused))
194 return __sysret(sys_close(fd));
202 static __attribute__((unused))
205 return my_syscall1(__NR_dup, fd);
208 static __attribute__((unused))
211 return __sysret(sys_dup(fd));
216 * int dup2(int old, int new);
219 static __attribute__((unused))
220 int sys_dup2(int old, int new)
223 return my_syscall3(__NR_dup3, old, new, 0);
224 #elif defined(__NR_dup2)
225 return my_syscall2(__NR_dup2, old, new);
231 static __attribute__((unused))
232 int dup2(int old, int new)
234 return __sysret(sys_dup2(old, new));
239 * int dup3(int old, int new, int flags);
243 static __attribute__((unused))
244 int sys_dup3(int old, int new, int flags)
246 return my_syscall3(__NR_dup3, old, new, flags);
249 static __attribute__((unused))
250 int dup3(int old, int new, int flags)
252 return __sysret(sys_dup3(old, new, flags));
258 * int execve(const char *filename, char *const argv[], char *const envp[]);
261 static __attribute__((unused))
262 int sys_execve(const char *filename, char *const argv[], char *const envp[])
264 return my_syscall3(__NR_execve, filename, argv, envp);
267 static __attribute__((unused))
268 int execve(const char *filename, char *const argv[], char *const envp[])
270 return __sysret(sys_execve(filename, argv, envp));
275 * void exit(int status);
278 static __attribute__((noreturn,unused))
279 void sys_exit(int status)
281 my_syscall1(__NR_exit, status & 255);
282 while(1); /* shut the "noreturn" warnings. */
285 static __attribute__((noreturn,unused))
286 void exit(int status)
297 static __attribute__((unused))
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.
305 return my_syscall5(__NR_clone, SIGCHLD, 0, 0, 0, 0);
306 #elif defined(__NR_fork)
307 return my_syscall0(__NR_fork);
314 static __attribute__((unused))
317 return __sysret(sys_fork());
325 static __attribute__((unused))
326 int sys_fsync(int fd)
328 return my_syscall1(__NR_fsync, fd);
331 static __attribute__((unused))
334 return __sysret(sys_fsync(fd));
339 * int getdents64(int fd, struct linux_dirent64 *dirp, int count);
342 static __attribute__((unused))
343 int sys_getdents64(int fd, struct linux_dirent64 *dirp, int count)
345 return my_syscall3(__NR_getdents64, fd, dirp, count);
348 static __attribute__((unused))
349 int getdents64(int fd, struct linux_dirent64 *dirp, int count)
351 return __sysret(sys_getdents64(fd, dirp, count));
356 * uid_t geteuid(void);
359 static __attribute__((unused))
360 uid_t sys_geteuid(void)
362 #ifdef __NR_geteuid32
363 return my_syscall0(__NR_geteuid32);
365 return my_syscall0(__NR_geteuid);
369 static __attribute__((unused))
372 return sys_geteuid();
377 * pid_t getpgid(pid_t pid);
380 static __attribute__((unused))
381 pid_t sys_getpgid(pid_t pid)
383 return my_syscall1(__NR_getpgid, pid);
386 static __attribute__((unused))
387 pid_t getpgid(pid_t pid)
389 return __sysret(sys_getpgid(pid));
394 * pid_t getpgrp(void);
397 static __attribute__((unused))
398 pid_t sys_getpgrp(void)
400 return sys_getpgid(0);
403 static __attribute__((unused))
406 return sys_getpgrp();
411 * pid_t getpid(void);
414 static __attribute__((unused))
415 pid_t sys_getpid(void)
417 return my_syscall0(__NR_getpid);
420 static __attribute__((unused))
428 * pid_t getppid(void);
431 static __attribute__((unused))
432 pid_t sys_getppid(void)
434 return my_syscall0(__NR_getppid);
437 static __attribute__((unused))
440 return sys_getppid();
445 * pid_t gettid(void);
448 static __attribute__((unused))
449 pid_t sys_gettid(void)
451 return my_syscall0(__NR_gettid);
454 static __attribute__((unused))
460 static unsigned long getauxval(unsigned long key);
463 * int getpagesize(void);
466 static __attribute__((unused))
467 int getpagesize(void)
469 return __sysret(getauxval(AT_PAGESZ) ?: -ENOENT);
474 * int gettimeofday(struct timeval *tv, struct timezone *tz);
477 static __attribute__((unused))
478 int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
480 #ifdef __NR_gettimeofday
481 return my_syscall2(__NR_gettimeofday, tv, tz);
487 static __attribute__((unused))
488 int gettimeofday(struct timeval *tv, struct timezone *tz)
490 return __sysret(sys_gettimeofday(tv, tz));
495 * uid_t getuid(void);
498 static __attribute__((unused))
499 uid_t sys_getuid(void)
502 return my_syscall0(__NR_getuid32);
504 return my_syscall0(__NR_getuid);
508 static __attribute__((unused))
516 * int ioctl(int fd, unsigned long req, void *value);
519 static __attribute__((unused))
520 int sys_ioctl(int fd, unsigned long req, void *value)
522 return my_syscall3(__NR_ioctl, fd, req, value);
525 static __attribute__((unused))
526 int ioctl(int fd, unsigned long req, void *value)
528 return __sysret(sys_ioctl(fd, req, value));
532 * int kill(pid_t pid, int signal);
535 static __attribute__((unused))
536 int sys_kill(pid_t pid, int signal)
538 return my_syscall2(__NR_kill, pid, signal);
541 static __attribute__((unused))
542 int kill(pid_t pid, int signal)
544 return __sysret(sys_kill(pid, signal));
549 * int link(const char *old, const char *new);
552 static __attribute__((unused))
553 int sys_link(const char *old, const char *new)
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);
564 static __attribute__((unused))
565 int link(const char *old, const char *new)
567 return __sysret(sys_link(old, new));
572 * off_t lseek(int fd, off_t offset, int whence);
575 static __attribute__((unused))
576 off_t sys_lseek(int fd, off_t offset, int whence)
579 return my_syscall3(__NR_lseek, fd, offset, whence);
585 static __attribute__((unused))
586 off_t lseek(int fd, off_t offset, int whence)
588 return __sysret(sys_lseek(fd, offset, whence));
593 * int mkdir(const char *path, mode_t mode);
596 static __attribute__((unused))
597 int sys_mkdir(const char *path, mode_t mode)
600 return my_syscall3(__NR_mkdirat, AT_FDCWD, path, mode);
601 #elif defined(__NR_mkdir)
602 return my_syscall2(__NR_mkdir, path, mode);
608 static __attribute__((unused))
609 int mkdir(const char *path, mode_t mode)
611 return __sysret(sys_mkdir(path, mode));
615 * int rmdir(const char *path);
618 static __attribute__((unused))
619 int sys_rmdir(const char *path)
622 return my_syscall1(__NR_rmdir, path);
623 #elif defined(__NR_unlinkat)
624 return my_syscall3(__NR_unlinkat, AT_FDCWD, path, AT_REMOVEDIR);
630 static __attribute__((unused))
631 int rmdir(const char *path)
633 return __sysret(sys_rmdir(path));
638 * int mknod(const char *path, mode_t mode, dev_t dev);
641 static __attribute__((unused))
642 long sys_mknod(const char *path, mode_t mode, dev_t dev)
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);
653 static __attribute__((unused))
654 int mknod(const char *path, mode_t mode, dev_t dev)
656 return __sysret(sys_mknod(path, mode, dev));
660 static __attribute__((unused))
661 void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd,
666 #if defined(__NR_mmap2)
673 return (void *)my_syscall6(n, addr, length, prot, flags, fd, offset);
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
682 static __attribute__((unused))
683 void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
685 return (void *)__sysret((unsigned long)sys_mmap(addr, length, prot, flags, fd, offset));
688 static __attribute__((unused))
689 int sys_munmap(void *addr, size_t length)
691 return my_syscall2(__NR_munmap, addr, length);
694 static __attribute__((unused))
695 int munmap(void *addr, size_t length)
697 return __sysret(sys_munmap(addr, length));
701 * int mount(const char *source, const char *target,
702 * const char *fstype, unsigned long flags,
705 static __attribute__((unused))
706 int sys_mount(const char *src, const char *tgt, const char *fst,
707 unsigned long flags, const void *data)
709 return my_syscall5(__NR_mount, src, tgt, fst, flags, data);
712 static __attribute__((unused))
713 int mount(const char *src, const char *tgt,
714 const char *fst, unsigned long flags,
717 return __sysret(sys_mount(src, tgt, fst, flags, data));
722 * int open(const char *path, int flags[, mode_t mode]);
725 static __attribute__((unused))
726 int sys_open(const char *path, int flags, mode_t mode)
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);
737 static __attribute__((unused))
738 int open(const char *path, int flags, ...)
742 if (flags & O_CREAT) {
745 va_start(args, flags);
746 mode = va_arg(args, int);
750 return __sysret(sys_open(path, flags, mode));
755 * int pipe2(int pipefd[2], int flags);
756 * int pipe(int pipefd[2]);
759 static __attribute__((unused))
760 int sys_pipe2(int pipefd[2], int flags)
762 return my_syscall2(__NR_pipe2, pipefd, flags);
765 static __attribute__((unused))
766 int pipe2(int pipefd[2], int flags)
768 return __sysret(sys_pipe2(pipefd, flags));
771 static __attribute__((unused))
772 int pipe(int pipefd[2])
774 return pipe2(pipefd, 0);
779 * int prctl(int option, unsigned long arg2, unsigned long arg3,
780 * unsigned long arg4, unsigned long arg5);
783 static __attribute__((unused))
784 int sys_prctl(int option, unsigned long arg2, unsigned long arg3,
785 unsigned long arg4, unsigned long arg5)
787 return my_syscall5(__NR_prctl, option, arg2, arg3, arg4, arg5);
790 static __attribute__((unused))
791 int prctl(int option, unsigned long arg2, unsigned long arg3,
792 unsigned long arg4, unsigned long arg5)
794 return __sysret(sys_prctl(option, arg2, arg3, arg4, arg5));
799 * int pivot_root(const char *new, const char *old);
802 static __attribute__((unused))
803 int sys_pivot_root(const char *new, const char *old)
805 return my_syscall2(__NR_pivot_root, new, old);
808 static __attribute__((unused))
809 int pivot_root(const char *new, const char *old)
811 return __sysret(sys_pivot_root(new, old));
816 * int poll(struct pollfd *fds, int nfds, int timeout);
819 static __attribute__((unused))
820 int sys_poll(struct pollfd *fds, int nfds, int timeout)
822 #if defined(__NR_ppoll)
826 t.tv_sec = timeout / 1000;
827 t.tv_nsec = (timeout % 1000) * 1000000;
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);
837 static __attribute__((unused))
838 int poll(struct pollfd *fds, int nfds, int timeout)
840 return __sysret(sys_poll(fds, nfds, timeout));
845 * ssize_t read(int fd, void *buf, size_t count);
848 static __attribute__((unused))
849 ssize_t sys_read(int fd, void *buf, size_t count)
851 return my_syscall3(__NR_read, fd, buf, count);
854 static __attribute__((unused))
855 ssize_t read(int fd, void *buf, size_t count)
857 return __sysret(sys_read(fd, buf, count));
862 * int reboot(int cmd);
863 * <cmd> is among LINUX_REBOOT_CMD_*
866 static __attribute__((unused))
867 ssize_t sys_reboot(int magic1, int magic2, int cmd, void *arg)
869 return my_syscall4(__NR_reboot, magic1, magic2, cmd, arg);
872 static __attribute__((unused))
875 return __sysret(sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0));
880 * int sched_yield(void);
883 static __attribute__((unused))
884 int sys_sched_yield(void)
886 return my_syscall0(__NR_sched_yield);
889 static __attribute__((unused))
890 int sched_yield(void)
892 return __sysret(sys_sched_yield());
897 * int select(int nfds, fd_set *read_fds, fd_set *write_fds,
898 * fd_set *except_fds, struct timeval *timeout);
901 static __attribute__((unused))
902 int sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
904 #if defined(__ARCH_WANT_SYS_OLD_SELECT) && !defined(__NR__newselect)
905 struct sel_arg_struct {
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)
915 t.tv_sec = timeout->tv_sec;
916 t.tv_nsec = timeout->tv_usec * 1000;
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
923 return my_syscall5(__NR__newselect, nfds, rfds, wfds, efds, timeout);
929 static __attribute__((unused))
930 int select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
932 return __sysret(sys_select(nfds, rfds, wfds, efds, timeout));
937 * int setpgid(pid_t pid, pid_t pgid);
940 static __attribute__((unused))
941 int sys_setpgid(pid_t pid, pid_t pgid)
943 return my_syscall2(__NR_setpgid, pid, pgid);
946 static __attribute__((unused))
947 int setpgid(pid_t pid, pid_t pgid)
949 return __sysret(sys_setpgid(pid, pgid));
954 * pid_t setsid(void);
957 static __attribute__((unused))
958 pid_t sys_setsid(void)
960 return my_syscall0(__NR_setsid);
963 static __attribute__((unused))
966 return __sysret(sys_setsid());
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);
974 static __attribute__((unused))
975 int sys_statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
978 return my_syscall5(__NR_statx, fd, path, flags, mask, buf);
984 static __attribute__((unused))
985 int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
987 return __sysret(sys_statx(fd, path, flags, mask, buf));
991 static __attribute__((unused))
992 int stat(const char *path, struct stat *buf)
997 ret = __sysret(sys_statx(AT_FDCWD, path, AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx));
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;
1027 * int symlink(const char *old, const char *new);
1030 static __attribute__((unused))
1031 int sys_symlink(const char *old, const char *new)
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);
1042 static __attribute__((unused))
1043 int symlink(const char *old, const char *new)
1045 return __sysret(sys_symlink(old, new));
1050 * mode_t umask(mode_t mode);
1053 static __attribute__((unused))
1054 mode_t sys_umask(mode_t mode)
1056 return my_syscall1(__NR_umask, mode);
1059 static __attribute__((unused))
1060 mode_t umask(mode_t mode)
1062 return sys_umask(mode);
1067 * int umount2(const char *path, int flags);
1070 static __attribute__((unused))
1071 int sys_umount2(const char *path, int flags)
1073 return my_syscall2(__NR_umount2, path, flags);
1076 static __attribute__((unused))
1077 int umount2(const char *path, int flags)
1079 return __sysret(sys_umount2(path, flags));
1084 * int unlink(const char *path);
1087 static __attribute__((unused))
1088 int sys_unlink(const char *path)
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);
1099 static __attribute__((unused))
1100 int unlink(const char *path)
1102 return __sysret(sys_unlink(path));
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);
1112 static __attribute__((unused))
1113 pid_t sys_wait4(pid_t pid, int *status, int options, struct rusage *rusage)
1116 return my_syscall4(__NR_wait4, pid, status, options, rusage);
1122 static __attribute__((unused))
1123 pid_t wait(int *status)
1125 return __sysret(sys_wait4(-1, status, 0, NULL));
1128 static __attribute__((unused))
1129 pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage)
1131 return __sysret(sys_wait4(pid, status, options, rusage));
1135 static __attribute__((unused))
1136 pid_t waitpid(pid_t pid, int *status, int options)
1138 return __sysret(sys_wait4(pid, status, options, NULL));
1143 * ssize_t write(int fd, const void *buf, size_t count);
1146 static __attribute__((unused))
1147 ssize_t sys_write(int fd, const void *buf, size_t count)
1149 return my_syscall3(__NR_write, fd, buf, count);
1152 static __attribute__((unused))
1153 ssize_t write(int fd, const void *buf, size_t count)
1155 return __sysret(sys_write(fd, buf, count));
1160 * int memfd_create(const char *name, unsigned int flags);
1163 static __attribute__((unused))
1164 int sys_memfd_create(const char *name, unsigned int flags)
1166 return my_syscall2(__NR_memfd_create, name, flags);
1169 static __attribute__((unused))
1170 int memfd_create(const char *name, unsigned int flags)
1172 return __sysret(sys_memfd_create(name, flags));
1175 /* make sure to include all global symbols */
1178 #endif /* _NOLIBC_SYS_H */