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>
27 /* Functions in this file only describe syscalls. They're declared static so
28 * that the compiler usually decides to inline them while still being allowed
29 * to pass a pointer to one of their instances. Each syscall exists in two
31 * - the "internal" ones, which matches the raw syscall interface at the
32 * kernel level, which may sometimes slightly differ from the documented
33 * libc-level ones. For example most of them return either a valid value
34 * or -errno. All of these are prefixed with "sys_". They may be called
35 * by non-portable applications if desired.
37 * - the "exported" ones, whose interface must closely match the one
38 * documented in man(2), that applications are supposed to expect. These
39 * ones rely on the internal ones, and set errno.
41 * Each syscall will be defined with the two functions, sorted in alphabetical
42 * order applied to the exported names.
44 * In case of doubt about the relevance of a function here, only those which
45 * set errno should be defined here. Wrappers like those appearing in man(3)
46 * should not be placed here.
51 * int brk(void *addr);
52 * void *sbrk(intptr_t inc)
55 static __attribute__((unused))
56 void *sys_brk(void *addr)
58 return (void *)my_syscall1(__NR_brk, addr);
61 static __attribute__((unused))
64 void *ret = sys_brk(addr);
73 static __attribute__((unused))
74 void *sbrk(intptr_t inc)
78 /* first call to find current end */
79 if ((ret = sys_brk(0)) && (sys_brk(ret + inc) == ret + inc))
88 * int chdir(const char *path);
91 static __attribute__((unused))
92 int sys_chdir(const char *path)
94 return my_syscall1(__NR_chdir, path);
97 static __attribute__((unused))
98 int chdir(const char *path)
100 int ret = sys_chdir(path);
111 * int chmod(const char *path, mode_t mode);
114 static __attribute__((unused))
115 int sys_chmod(const char *path, mode_t mode)
118 return my_syscall4(__NR_fchmodat, AT_FDCWD, path, mode, 0);
119 #elif defined(__NR_chmod)
120 return my_syscall2(__NR_chmod, path, mode);
122 #error Neither __NR_fchmodat nor __NR_chmod defined, cannot implement sys_chmod()
126 static __attribute__((unused))
127 int chmod(const char *path, mode_t mode)
129 int ret = sys_chmod(path, mode);
140 * int chown(const char *path, uid_t owner, gid_t group);
143 static __attribute__((unused))
144 int sys_chown(const char *path, uid_t owner, gid_t group)
147 return my_syscall5(__NR_fchownat, AT_FDCWD, path, owner, group, 0);
148 #elif defined(__NR_chown)
149 return my_syscall3(__NR_chown, path, owner, group);
151 #error Neither __NR_fchownat nor __NR_chown defined, cannot implement sys_chown()
155 static __attribute__((unused))
156 int chown(const char *path, uid_t owner, gid_t group)
158 int ret = sys_chown(path, owner, group);
169 * int chroot(const char *path);
172 static __attribute__((unused))
173 int sys_chroot(const char *path)
175 return my_syscall1(__NR_chroot, path);
178 static __attribute__((unused))
179 int chroot(const char *path)
181 int ret = sys_chroot(path);
195 static __attribute__((unused))
196 int sys_close(int fd)
198 return my_syscall1(__NR_close, fd);
201 static __attribute__((unused))
204 int ret = sys_close(fd);
218 static __attribute__((unused))
221 return my_syscall1(__NR_dup, fd);
224 static __attribute__((unused))
227 int ret = sys_dup(fd);
238 * int dup2(int old, int new);
241 static __attribute__((unused))
242 int sys_dup2(int old, int new)
245 return my_syscall3(__NR_dup3, old, new, 0);
246 #elif defined(__NR_dup2)
247 return my_syscall2(__NR_dup2, old, new);
249 #error Neither __NR_dup3 nor __NR_dup2 defined, cannot implement sys_dup2()
253 static __attribute__((unused))
254 int dup2(int old, int new)
256 int ret = sys_dup2(old, new);
267 * int dup3(int old, int new, int flags);
271 static __attribute__((unused))
272 int sys_dup3(int old, int new, int flags)
274 return my_syscall3(__NR_dup3, old, new, flags);
277 static __attribute__((unused))
278 int dup3(int old, int new, int flags)
280 int ret = sys_dup3(old, new, flags);
292 * int execve(const char *filename, char *const argv[], char *const envp[]);
295 static __attribute__((unused))
296 int sys_execve(const char *filename, char *const argv[], char *const envp[])
298 return my_syscall3(__NR_execve, filename, argv, envp);
301 static __attribute__((unused))
302 int execve(const char *filename, char *const argv[], char *const envp[])
304 int ret = sys_execve(filename, argv, envp);
315 * void exit(int status);
318 static __attribute__((noreturn,unused))
319 void sys_exit(int status)
321 my_syscall1(__NR_exit, status & 255);
322 while(1); // shut the "noreturn" warnings.
325 static __attribute__((noreturn,unused))
326 void exit(int status)
336 static __attribute__((unused))
340 /* note: some archs only have clone() and not fork(). Different archs
341 * have a different API, but most archs have the flags on first arg and
342 * will not use the rest with no other flag.
344 return my_syscall5(__NR_clone, SIGCHLD, 0, 0, 0, 0);
345 #elif defined(__NR_fork)
346 return my_syscall0(__NR_fork);
348 #error Neither __NR_clone nor __NR_fork defined, cannot implement sys_fork()
352 static __attribute__((unused))
355 pid_t ret = sys_fork();
369 static __attribute__((unused))
370 int sys_fsync(int fd)
372 return my_syscall1(__NR_fsync, fd);
375 static __attribute__((unused))
378 int ret = sys_fsync(fd);
389 * int getdents64(int fd, struct linux_dirent64 *dirp, int count);
392 static __attribute__((unused))
393 int sys_getdents64(int fd, struct linux_dirent64 *dirp, int count)
395 return my_syscall3(__NR_getdents64, fd, dirp, count);
398 static __attribute__((unused))
399 int getdents64(int fd, struct linux_dirent64 *dirp, int count)
401 int ret = sys_getdents64(fd, dirp, count);
412 * pid_t getpgid(pid_t pid);
415 static __attribute__((unused))
416 pid_t sys_getpgid(pid_t pid)
418 return my_syscall1(__NR_getpgid, pid);
421 static __attribute__((unused))
422 pid_t getpgid(pid_t pid)
424 pid_t ret = sys_getpgid(pid);
435 * pid_t getpgrp(void);
438 static __attribute__((unused))
439 pid_t sys_getpgrp(void)
441 return sys_getpgid(0);
444 static __attribute__((unused))
447 return sys_getpgrp();
452 * pid_t getpid(void);
455 static __attribute__((unused))
456 pid_t sys_getpid(void)
458 return my_syscall0(__NR_getpid);
461 static __attribute__((unused))
469 * pid_t getppid(void);
472 static __attribute__((unused))
473 pid_t sys_getppid(void)
475 return my_syscall0(__NR_getppid);
478 static __attribute__((unused))
481 return sys_getppid();
486 * pid_t gettid(void);
489 static __attribute__((unused))
490 pid_t sys_gettid(void)
492 return my_syscall0(__NR_gettid);
495 static __attribute__((unused))
503 * int gettimeofday(struct timeval *tv, struct timezone *tz);
506 static __attribute__((unused))
507 int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
509 return my_syscall2(__NR_gettimeofday, tv, tz);
512 static __attribute__((unused))
513 int gettimeofday(struct timeval *tv, struct timezone *tz)
515 int ret = sys_gettimeofday(tv, tz);
526 * int ioctl(int fd, unsigned long req, void *value);
529 static __attribute__((unused))
530 int sys_ioctl(int fd, unsigned long req, void *value)
532 return my_syscall3(__NR_ioctl, fd, req, value);
535 static __attribute__((unused))
536 int ioctl(int fd, unsigned long req, void *value)
538 int ret = sys_ioctl(fd, req, value);
548 * int kill(pid_t pid, int signal);
551 static __attribute__((unused))
552 int sys_kill(pid_t pid, int signal)
554 return my_syscall2(__NR_kill, pid, signal);
557 static __attribute__((unused))
558 int kill(pid_t pid, int signal)
560 int ret = sys_kill(pid, signal);
571 * int link(const char *old, const char *new);
574 static __attribute__((unused))
575 int sys_link(const char *old, const char *new)
578 return my_syscall5(__NR_linkat, AT_FDCWD, old, AT_FDCWD, new, 0);
579 #elif defined(__NR_link)
580 return my_syscall2(__NR_link, old, new);
582 #error Neither __NR_linkat nor __NR_link defined, cannot implement sys_link()
586 static __attribute__((unused))
587 int link(const char *old, const char *new)
589 int ret = sys_link(old, new);
600 * off_t lseek(int fd, off_t offset, int whence);
603 static __attribute__((unused))
604 off_t sys_lseek(int fd, off_t offset, int whence)
606 return my_syscall3(__NR_lseek, fd, offset, whence);
609 static __attribute__((unused))
610 off_t lseek(int fd, off_t offset, int whence)
612 off_t ret = sys_lseek(fd, offset, whence);
623 * int mkdir(const char *path, mode_t mode);
626 static __attribute__((unused))
627 int sys_mkdir(const char *path, mode_t mode)
630 return my_syscall3(__NR_mkdirat, AT_FDCWD, path, mode);
631 #elif defined(__NR_mkdir)
632 return my_syscall2(__NR_mkdir, path, mode);
634 #error Neither __NR_mkdirat nor __NR_mkdir defined, cannot implement sys_mkdir()
638 static __attribute__((unused))
639 int mkdir(const char *path, mode_t mode)
641 int ret = sys_mkdir(path, mode);
652 * int mknod(const char *path, mode_t mode, dev_t dev);
655 static __attribute__((unused))
656 long sys_mknod(const char *path, mode_t mode, dev_t dev)
659 return my_syscall4(__NR_mknodat, AT_FDCWD, path, mode, dev);
660 #elif defined(__NR_mknod)
661 return my_syscall3(__NR_mknod, path, mode, dev);
663 #error Neither __NR_mknodat nor __NR_mknod defined, cannot implement sys_mknod()
667 static __attribute__((unused))
668 int mknod(const char *path, mode_t mode, dev_t dev)
670 int ret = sys_mknod(path, mode, dev);
680 #define MAP_SHARED 0x01 /* Share changes */
681 #define MAP_PRIVATE 0x02 /* Changes are private */
682 #define MAP_SHARED_VALIDATE 0x03 /* share + validate extension flags */
686 #define MAP_FAILED ((void *)-1)
689 static __attribute__((unused))
690 void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd,
694 /* Function not implemented. */
695 return (void *)-ENOSYS;
700 #if defined(__NR_mmap2)
707 return (void *)my_syscall6(n, addr, length, prot, flags, fd, offset);
711 static __attribute__((unused))
712 void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
714 void *ret = sys_mmap(addr, length, prot, flags, fd, offset);
716 if ((unsigned long)ret >= -4095UL) {
717 SET_ERRNO(-(long)ret);
723 static __attribute__((unused))
724 int sys_munmap(void *addr, size_t length)
726 return my_syscall2(__NR_munmap, addr, length);
729 static __attribute__((unused))
730 int munmap(void *addr, size_t length)
732 int ret = sys_munmap(addr, length);
742 * int mount(const char *source, const char *target,
743 * const char *fstype, unsigned long flags,
746 static __attribute__((unused))
747 int sys_mount(const char *src, const char *tgt, const char *fst,
748 unsigned long flags, const void *data)
750 return my_syscall5(__NR_mount, src, tgt, fst, flags, data);
753 static __attribute__((unused))
754 int mount(const char *src, const char *tgt,
755 const char *fst, unsigned long flags,
758 int ret = sys_mount(src, tgt, fst, flags, data);
769 * int open(const char *path, int flags[, mode_t mode]);
772 static __attribute__((unused))
773 int sys_open(const char *path, int flags, mode_t mode)
776 return my_syscall4(__NR_openat, AT_FDCWD, path, flags, mode);
777 #elif defined(__NR_open)
778 return my_syscall3(__NR_open, path, flags, mode);
780 #error Neither __NR_openat nor __NR_open defined, cannot implement sys_open()
784 static __attribute__((unused))
785 int open(const char *path, int flags, ...)
790 if (flags & O_CREAT) {
793 va_start(args, flags);
794 mode = va_arg(args, mode_t);
798 ret = sys_open(path, flags, mode);
809 * int pivot_root(const char *new, const char *old);
812 static __attribute__((unused))
813 int sys_pivot_root(const char *new, const char *old)
815 return my_syscall2(__NR_pivot_root, new, old);
818 static __attribute__((unused))
819 int pivot_root(const char *new, const char *old)
821 int ret = sys_pivot_root(new, old);
832 * int poll(struct pollfd *fds, int nfds, int timeout);
835 static __attribute__((unused))
836 int sys_poll(struct pollfd *fds, int nfds, int timeout)
838 #if defined(__NR_ppoll)
842 t.tv_sec = timeout / 1000;
843 t.tv_nsec = (timeout % 1000) * 1000000;
845 return my_syscall4(__NR_ppoll, fds, nfds, (timeout >= 0) ? &t : NULL, NULL);
846 #elif defined(__NR_poll)
847 return my_syscall3(__NR_poll, fds, nfds, timeout);
849 #error Neither __NR_ppoll nor __NR_poll defined, cannot implement sys_poll()
853 static __attribute__((unused))
854 int poll(struct pollfd *fds, int nfds, int timeout)
856 int ret = sys_poll(fds, nfds, timeout);
867 * ssize_t read(int fd, void *buf, size_t count);
870 static __attribute__((unused))
871 ssize_t sys_read(int fd, void *buf, size_t count)
873 return my_syscall3(__NR_read, fd, buf, count);
876 static __attribute__((unused))
877 ssize_t read(int fd, void *buf, size_t count)
879 ssize_t ret = sys_read(fd, buf, count);
890 * int reboot(int cmd);
891 * <cmd> is among LINUX_REBOOT_CMD_*
894 static __attribute__((unused))
895 ssize_t sys_reboot(int magic1, int magic2, int cmd, void *arg)
897 return my_syscall4(__NR_reboot, magic1, magic2, cmd, arg);
900 static __attribute__((unused))
903 int ret = sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0);
914 * int sched_yield(void);
917 static __attribute__((unused))
918 int sys_sched_yield(void)
920 return my_syscall0(__NR_sched_yield);
923 static __attribute__((unused))
924 int sched_yield(void)
926 int ret = sys_sched_yield();
937 * int select(int nfds, fd_set *read_fds, fd_set *write_fds,
938 * fd_set *except_fds, struct timeval *timeout);
941 static __attribute__((unused))
942 int sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
944 #if defined(__ARCH_WANT_SYS_OLD_SELECT) && !defined(__NR__newselect)
945 struct sel_arg_struct {
949 } arg = { .n = nfds, .r = rfds, .w = wfds, .e = efds, .t = timeout };
950 return my_syscall1(__NR_select, &arg);
951 #elif defined(__ARCH_WANT_SYS_PSELECT6) && defined(__NR_pselect6)
955 t.tv_sec = timeout->tv_sec;
956 t.tv_nsec = timeout->tv_usec * 1000;
958 return my_syscall6(__NR_pselect6, nfds, rfds, wfds, efds, timeout ? &t : NULL, NULL);
959 #elif defined(__NR__newselect) || defined(__NR_select)
960 #ifndef __NR__newselect
961 #define __NR__newselect __NR_select
963 return my_syscall5(__NR__newselect, nfds, rfds, wfds, efds, timeout);
965 #error None of __NR_select, __NR_pselect6, nor __NR__newselect defined, cannot implement sys_select()
969 static __attribute__((unused))
970 int select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
972 int ret = sys_select(nfds, rfds, wfds, efds, timeout);
983 * int setpgid(pid_t pid, pid_t pgid);
986 static __attribute__((unused))
987 int sys_setpgid(pid_t pid, pid_t pgid)
989 return my_syscall2(__NR_setpgid, pid, pgid);
992 static __attribute__((unused))
993 int setpgid(pid_t pid, pid_t pgid)
995 int ret = sys_setpgid(pid, pgid);
1006 * pid_t setsid(void);
1009 static __attribute__((unused))
1010 pid_t sys_setsid(void)
1012 return my_syscall0(__NR_setsid);
1015 static __attribute__((unused))
1018 pid_t ret = sys_setsid();
1029 * int stat(const char *path, struct stat *buf);
1030 * Warning: the struct stat's layout is arch-dependent.
1033 static __attribute__((unused))
1034 int sys_stat(const char *path, struct stat *buf)
1036 struct sys_stat_struct stat;
1039 #ifdef __NR_newfstatat
1040 /* only solution for arm64 */
1041 ret = my_syscall4(__NR_newfstatat, AT_FDCWD, path, &stat, 0);
1042 #elif defined(__NR_stat)
1043 ret = my_syscall2(__NR_stat, path, &stat);
1045 #error Neither __NR_newfstatat nor __NR_stat defined, cannot implement sys_stat()
1047 buf->st_dev = stat.st_dev;
1048 buf->st_ino = stat.st_ino;
1049 buf->st_mode = stat.st_mode;
1050 buf->st_nlink = stat.st_nlink;
1051 buf->st_uid = stat.st_uid;
1052 buf->st_gid = stat.st_gid;
1053 buf->st_rdev = stat.st_rdev;
1054 buf->st_size = stat.st_size;
1055 buf->st_blksize = stat.st_blksize;
1056 buf->st_blocks = stat.st_blocks;
1057 buf->st_atime = stat.st_atime;
1058 buf->st_mtime = stat.st_mtime;
1059 buf->st_ctime = stat.st_ctime;
1063 static __attribute__((unused))
1064 int stat(const char *path, struct stat *buf)
1066 int ret = sys_stat(path, buf);
1077 * int symlink(const char *old, const char *new);
1080 static __attribute__((unused))
1081 int sys_symlink(const char *old, const char *new)
1083 #ifdef __NR_symlinkat
1084 return my_syscall3(__NR_symlinkat, old, AT_FDCWD, new);
1085 #elif defined(__NR_symlink)
1086 return my_syscall2(__NR_symlink, old, new);
1088 #error Neither __NR_symlinkat nor __NR_symlink defined, cannot implement sys_symlink()
1092 static __attribute__((unused))
1093 int symlink(const char *old, const char *new)
1095 int ret = sys_symlink(old, new);
1106 * mode_t umask(mode_t mode);
1109 static __attribute__((unused))
1110 mode_t sys_umask(mode_t mode)
1112 return my_syscall1(__NR_umask, mode);
1115 static __attribute__((unused))
1116 mode_t umask(mode_t mode)
1118 return sys_umask(mode);
1123 * int umount2(const char *path, int flags);
1126 static __attribute__((unused))
1127 int sys_umount2(const char *path, int flags)
1129 return my_syscall2(__NR_umount2, path, flags);
1132 static __attribute__((unused))
1133 int umount2(const char *path, int flags)
1135 int ret = sys_umount2(path, flags);
1146 * int unlink(const char *path);
1149 static __attribute__((unused))
1150 int sys_unlink(const char *path)
1152 #ifdef __NR_unlinkat
1153 return my_syscall3(__NR_unlinkat, AT_FDCWD, path, 0);
1154 #elif defined(__NR_unlink)
1155 return my_syscall1(__NR_unlink, path);
1157 #error Neither __NR_unlinkat nor __NR_unlink defined, cannot implement sys_unlink()
1161 static __attribute__((unused))
1162 int unlink(const char *path)
1164 int ret = sys_unlink(path);
1175 * pid_t wait(int *status);
1176 * pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage);
1177 * pid_t waitpid(pid_t pid, int *status, int options);
1180 static __attribute__((unused))
1181 pid_t sys_wait4(pid_t pid, int *status, int options, struct rusage *rusage)
1183 return my_syscall4(__NR_wait4, pid, status, options, rusage);
1186 static __attribute__((unused))
1187 pid_t wait(int *status)
1189 pid_t ret = sys_wait4(-1, status, 0, NULL);
1198 static __attribute__((unused))
1199 pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage)
1201 pid_t ret = sys_wait4(pid, status, options, rusage);
1211 static __attribute__((unused))
1212 pid_t waitpid(pid_t pid, int *status, int options)
1214 pid_t ret = sys_wait4(pid, status, options, NULL);
1225 * ssize_t write(int fd, const void *buf, size_t count);
1228 static __attribute__((unused))
1229 ssize_t sys_write(int fd, const void *buf, size_t count)
1231 return my_syscall3(__NR_write, fd, buf, count);
1234 static __attribute__((unused))
1235 ssize_t write(int fd, const void *buf, size_t count)
1237 ssize_t ret = sys_write(fd, buf, count);
1246 /* make sure to include all global symbols */
1249 #endif /* _NOLIBC_SYS_H */