1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
3 * Syscall definitions for NOLIBC (those in man(2))
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: takes the syscall value in argument and checks for an
32 * error in it. This may only be used with signed returns (int or long), but
33 * not with pointers. An error is any value < 0. When an error is encountered,
34 * -ret is set into errno and -1 is returned. Otherwise the returned value is
35 * passed as-is with its type preserved.
38 #define __sysret(arg) \
40 __typeof__(arg) __sysret_arg = (arg); \
41 (__sysret_arg < 0) /* error ? */ \
42 ? (({ SET_ERRNO(-__sysret_arg); }), -1) /* ret -1 with errno = -arg */ \
43 : __sysret_arg; /* return original value */ \
47 /* Functions in this file only describe syscalls. They're declared static so
48 * that the compiler usually decides to inline them while still being allowed
49 * to pass a pointer to one of their instances. Each syscall exists in two
51 * - the "internal" ones, which matches the raw syscall interface at the
52 * kernel level, which may sometimes slightly differ from the documented
53 * libc-level ones. For example most of them return either a valid value
54 * or -errno. All of these are prefixed with "sys_". They may be called
55 * by non-portable applications if desired.
57 * - the "exported" ones, whose interface must closely match the one
58 * documented in man(2), that applications are supposed to expect. These
59 * ones rely on the internal ones, and set errno.
61 * Each syscall will be defined with the two functions, sorted in alphabetical
62 * order applied to the exported names.
64 * In case of doubt about the relevance of a function here, only those which
65 * set errno should be defined here. Wrappers like those appearing in man(3)
66 * should not be placed here.
71 * int brk(void *addr);
72 * void *sbrk(intptr_t inc)
75 static __attribute__((unused))
76 void *sys_brk(void *addr)
78 return (void *)my_syscall1(__NR_brk, addr);
81 static __attribute__((unused))
84 void *ret = sys_brk(addr);
93 static __attribute__((unused))
94 void *sbrk(intptr_t inc)
96 /* first call to find current end */
97 void *ret = sys_brk(0);
99 if (ret && sys_brk(ret + inc) == ret + inc)
108 * int chdir(const char *path);
111 static __attribute__((unused))
112 int sys_chdir(const char *path)
114 return my_syscall1(__NR_chdir, path);
117 static __attribute__((unused))
118 int chdir(const char *path)
120 return __sysret(sys_chdir(path));
125 * int chmod(const char *path, mode_t mode);
128 static __attribute__((unused))
129 int sys_chmod(const char *path, mode_t mode)
132 return my_syscall4(__NR_fchmodat, AT_FDCWD, path, mode, 0);
133 #elif defined(__NR_chmod)
134 return my_syscall2(__NR_chmod, path, mode);
140 static __attribute__((unused))
141 int chmod(const char *path, mode_t mode)
143 return __sysret(sys_chmod(path, mode));
148 * int chown(const char *path, uid_t owner, gid_t group);
151 static __attribute__((unused))
152 int sys_chown(const char *path, uid_t owner, gid_t group)
155 return my_syscall5(__NR_fchownat, AT_FDCWD, path, owner, group, 0);
156 #elif defined(__NR_chown)
157 return my_syscall3(__NR_chown, path, owner, group);
163 static __attribute__((unused))
164 int chown(const char *path, uid_t owner, gid_t group)
166 return __sysret(sys_chown(path, owner, group));
171 * int chroot(const char *path);
174 static __attribute__((unused))
175 int sys_chroot(const char *path)
177 return my_syscall1(__NR_chroot, path);
180 static __attribute__((unused))
181 int chroot(const char *path)
183 return __sysret(sys_chroot(path));
191 static __attribute__((unused))
192 int sys_close(int fd)
194 return my_syscall1(__NR_close, fd);
197 static __attribute__((unused))
200 return __sysret(sys_close(fd));
208 static __attribute__((unused))
211 return my_syscall1(__NR_dup, fd);
214 static __attribute__((unused))
217 return __sysret(sys_dup(fd));
222 * int dup2(int old, int new);
225 static __attribute__((unused))
226 int sys_dup2(int old, int new)
229 return my_syscall3(__NR_dup3, old, new, 0);
230 #elif defined(__NR_dup2)
231 return my_syscall2(__NR_dup2, old, new);
237 static __attribute__((unused))
238 int dup2(int old, int new)
240 return __sysret(sys_dup2(old, new));
245 * int dup3(int old, int new, int flags);
249 static __attribute__((unused))
250 int sys_dup3(int old, int new, int flags)
252 return my_syscall3(__NR_dup3, old, new, flags);
255 static __attribute__((unused))
256 int dup3(int old, int new, int flags)
258 return __sysret(sys_dup3(old, new, flags));
264 * int execve(const char *filename, char *const argv[], char *const envp[]);
267 static __attribute__((unused))
268 int sys_execve(const char *filename, char *const argv[], char *const envp[])
270 return my_syscall3(__NR_execve, filename, argv, envp);
273 static __attribute__((unused))
274 int execve(const char *filename, char *const argv[], char *const envp[])
276 return __sysret(sys_execve(filename, argv, envp));
281 * void exit(int status);
284 static __attribute__((noreturn,unused))
285 void sys_exit(int status)
287 my_syscall1(__NR_exit, status & 255);
288 while(1); /* shut the "noreturn" warnings. */
291 static __attribute__((noreturn,unused))
292 void exit(int status)
303 static __attribute__((unused))
307 /* note: some archs only have clone() and not fork(). Different archs
308 * have a different API, but most archs have the flags on first arg and
309 * will not use the rest with no other flag.
311 return my_syscall5(__NR_clone, SIGCHLD, 0, 0, 0, 0);
312 #elif defined(__NR_fork)
313 return my_syscall0(__NR_fork);
320 static __attribute__((unused))
323 return __sysret(sys_fork());
331 static __attribute__((unused))
332 int sys_fsync(int fd)
334 return my_syscall1(__NR_fsync, fd);
337 static __attribute__((unused))
340 return __sysret(sys_fsync(fd));
345 * int getdents64(int fd, struct linux_dirent64 *dirp, int count);
348 static __attribute__((unused))
349 int sys_getdents64(int fd, struct linux_dirent64 *dirp, int count)
351 return my_syscall3(__NR_getdents64, fd, dirp, count);
354 static __attribute__((unused))
355 int getdents64(int fd, struct linux_dirent64 *dirp, int count)
357 return __sysret(sys_getdents64(fd, dirp, count));
362 * uid_t geteuid(void);
365 static __attribute__((unused))
366 uid_t sys_geteuid(void)
368 #ifdef __NR_geteuid32
369 return my_syscall0(__NR_geteuid32);
371 return my_syscall0(__NR_geteuid);
375 static __attribute__((unused))
378 return sys_geteuid();
383 * pid_t getpgid(pid_t pid);
386 static __attribute__((unused))
387 pid_t sys_getpgid(pid_t pid)
389 return my_syscall1(__NR_getpgid, pid);
392 static __attribute__((unused))
393 pid_t getpgid(pid_t pid)
395 return __sysret(sys_getpgid(pid));
400 * pid_t getpgrp(void);
403 static __attribute__((unused))
404 pid_t sys_getpgrp(void)
406 return sys_getpgid(0);
409 static __attribute__((unused))
412 return sys_getpgrp();
417 * pid_t getpid(void);
420 static __attribute__((unused))
421 pid_t sys_getpid(void)
423 return my_syscall0(__NR_getpid);
426 static __attribute__((unused))
434 * pid_t getppid(void);
437 static __attribute__((unused))
438 pid_t sys_getppid(void)
440 return my_syscall0(__NR_getppid);
443 static __attribute__((unused))
446 return sys_getppid();
451 * pid_t gettid(void);
454 static __attribute__((unused))
455 pid_t sys_gettid(void)
457 return my_syscall0(__NR_gettid);
460 static __attribute__((unused))
466 static unsigned long getauxval(unsigned long key);
469 * int getpagesize(void);
472 static __attribute__((unused))
473 int getpagesize(void)
475 return __sysret((int)getauxval(AT_PAGESZ) ?: -ENOENT);
480 * int gettimeofday(struct timeval *tv, struct timezone *tz);
483 static __attribute__((unused))
484 int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
486 #ifdef __NR_gettimeofday
487 return my_syscall2(__NR_gettimeofday, tv, tz);
493 static __attribute__((unused))
494 int gettimeofday(struct timeval *tv, struct timezone *tz)
496 return __sysret(sys_gettimeofday(tv, tz));
501 * uid_t getuid(void);
504 static __attribute__((unused))
505 uid_t sys_getuid(void)
508 return my_syscall0(__NR_getuid32);
510 return my_syscall0(__NR_getuid);
514 static __attribute__((unused))
522 * int ioctl(int fd, unsigned long req, void *value);
525 static __attribute__((unused))
526 int sys_ioctl(int fd, unsigned long req, void *value)
528 return my_syscall3(__NR_ioctl, fd, req, value);
531 static __attribute__((unused))
532 int ioctl(int fd, unsigned long req, void *value)
534 return __sysret(sys_ioctl(fd, req, value));
538 * int kill(pid_t pid, int signal);
541 static __attribute__((unused))
542 int sys_kill(pid_t pid, int signal)
544 return my_syscall2(__NR_kill, pid, signal);
547 static __attribute__((unused))
548 int kill(pid_t pid, int signal)
550 return __sysret(sys_kill(pid, signal));
555 * int link(const char *old, const char *new);
558 static __attribute__((unused))
559 int sys_link(const char *old, const char *new)
562 return my_syscall5(__NR_linkat, AT_FDCWD, old, AT_FDCWD, new, 0);
563 #elif defined(__NR_link)
564 return my_syscall2(__NR_link, old, new);
570 static __attribute__((unused))
571 int link(const char *old, const char *new)
573 return __sysret(sys_link(old, new));
578 * off_t lseek(int fd, off_t offset, int whence);
581 static __attribute__((unused))
582 off_t sys_lseek(int fd, off_t offset, int whence)
585 return my_syscall3(__NR_lseek, fd, offset, whence);
591 static __attribute__((unused))
592 off_t lseek(int fd, off_t offset, int whence)
594 return __sysret(sys_lseek(fd, offset, whence));
599 * int mkdir(const char *path, mode_t mode);
602 static __attribute__((unused))
603 int sys_mkdir(const char *path, mode_t mode)
606 return my_syscall3(__NR_mkdirat, AT_FDCWD, path, mode);
607 #elif defined(__NR_mkdir)
608 return my_syscall2(__NR_mkdir, path, mode);
614 static __attribute__((unused))
615 int mkdir(const char *path, mode_t mode)
617 return __sysret(sys_mkdir(path, mode));
621 * int rmdir(const char *path);
624 static __attribute__((unused))
625 int sys_rmdir(const char *path)
628 return my_syscall1(__NR_rmdir, path);
629 #elif defined(__NR_unlinkat)
630 return my_syscall3(__NR_unlinkat, AT_FDCWD, path, AT_REMOVEDIR);
636 static __attribute__((unused))
637 int rmdir(const char *path)
639 return __sysret(sys_rmdir(path));
644 * int mknod(const char *path, mode_t mode, dev_t dev);
647 static __attribute__((unused))
648 long sys_mknod(const char *path, mode_t mode, dev_t dev)
651 return my_syscall4(__NR_mknodat, AT_FDCWD, path, mode, dev);
652 #elif defined(__NR_mknod)
653 return my_syscall3(__NR_mknod, path, mode, dev);
659 static __attribute__((unused))
660 int mknod(const char *path, mode_t mode, dev_t dev)
662 return __sysret(sys_mknod(path, mode, dev));
666 static __attribute__((unused))
667 void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd,
672 #if defined(__NR_mmap2)
679 return (void *)my_syscall6(n, addr, length, prot, flags, fd, offset);
683 /* Note that on Linux, MAP_FAILED is -1 so we can use the generic __sysret()
684 * which returns -1 upon error and still satisfy user land that checks for
688 static __attribute__((unused))
689 void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
691 void *ret = sys_mmap(addr, length, prot, flags, fd, offset);
693 if ((unsigned long)ret >= -4095UL) {
694 SET_ERRNO(-(long)ret);
700 static __attribute__((unused))
701 int sys_munmap(void *addr, size_t length)
703 return my_syscall2(__NR_munmap, addr, length);
706 static __attribute__((unused))
707 int munmap(void *addr, size_t length)
709 return __sysret(sys_munmap(addr, length));
713 * int mount(const char *source, const char *target,
714 * const char *fstype, unsigned long flags,
717 static __attribute__((unused))
718 int sys_mount(const char *src, const char *tgt, const char *fst,
719 unsigned long flags, const void *data)
721 return my_syscall5(__NR_mount, src, tgt, fst, flags, data);
724 static __attribute__((unused))
725 int mount(const char *src, const char *tgt,
726 const char *fst, unsigned long flags,
729 return __sysret(sys_mount(src, tgt, fst, flags, data));
734 * int open(const char *path, int flags[, mode_t mode]);
737 static __attribute__((unused))
738 int sys_open(const char *path, int flags, mode_t mode)
741 return my_syscall4(__NR_openat, AT_FDCWD, path, flags, mode);
742 #elif defined(__NR_open)
743 return my_syscall3(__NR_open, path, flags, mode);
749 static __attribute__((unused))
750 int open(const char *path, int flags, ...)
754 if (flags & O_CREAT) {
757 va_start(args, flags);
758 mode = va_arg(args, int);
762 return __sysret(sys_open(path, flags, mode));
767 * int pipe2(int pipefd[2], int flags);
768 * int pipe(int pipefd[2]);
771 static __attribute__((unused))
772 int sys_pipe2(int pipefd[2], int flags)
774 return my_syscall2(__NR_pipe2, pipefd, flags);
777 static __attribute__((unused))
778 int pipe2(int pipefd[2], int flags)
780 return __sysret(sys_pipe2(pipefd, flags));
783 static __attribute__((unused))
784 int pipe(int pipefd[2])
786 return pipe2(pipefd, 0);
791 * int prctl(int option, unsigned long arg2, unsigned long arg3,
792 * unsigned long arg4, unsigned long arg5);
795 static __attribute__((unused))
796 int sys_prctl(int option, unsigned long arg2, unsigned long arg3,
797 unsigned long arg4, unsigned long arg5)
799 return my_syscall5(__NR_prctl, option, arg2, arg3, arg4, arg5);
802 static __attribute__((unused))
803 int prctl(int option, unsigned long arg2, unsigned long arg3,
804 unsigned long arg4, unsigned long arg5)
806 return __sysret(sys_prctl(option, arg2, arg3, arg4, arg5));
811 * int pivot_root(const char *new, const char *old);
814 static __attribute__((unused))
815 int sys_pivot_root(const char *new, const char *old)
817 return my_syscall2(__NR_pivot_root, new, old);
820 static __attribute__((unused))
821 int pivot_root(const char *new, const char *old)
823 return __sysret(sys_pivot_root(new, old));
828 * int poll(struct pollfd *fds, int nfds, int timeout);
831 static __attribute__((unused))
832 int sys_poll(struct pollfd *fds, int nfds, int timeout)
834 #if defined(__NR_ppoll)
838 t.tv_sec = timeout / 1000;
839 t.tv_nsec = (timeout % 1000) * 1000000;
841 return my_syscall5(__NR_ppoll, fds, nfds, (timeout >= 0) ? &t : NULL, NULL, 0);
842 #elif defined(__NR_poll)
843 return my_syscall3(__NR_poll, fds, nfds, timeout);
849 static __attribute__((unused))
850 int poll(struct pollfd *fds, int nfds, int timeout)
852 return __sysret(sys_poll(fds, nfds, timeout));
857 * ssize_t read(int fd, void *buf, size_t count);
860 static __attribute__((unused))
861 ssize_t sys_read(int fd, void *buf, size_t count)
863 return my_syscall3(__NR_read, fd, buf, count);
866 static __attribute__((unused))
867 ssize_t read(int fd, void *buf, size_t count)
869 return __sysret(sys_read(fd, buf, count));
874 * int reboot(int cmd);
875 * <cmd> is among LINUX_REBOOT_CMD_*
878 static __attribute__((unused))
879 ssize_t sys_reboot(int magic1, int magic2, int cmd, void *arg)
881 return my_syscall4(__NR_reboot, magic1, magic2, cmd, arg);
884 static __attribute__((unused))
887 return __sysret(sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0));
892 * int sched_yield(void);
895 static __attribute__((unused))
896 int sys_sched_yield(void)
898 return my_syscall0(__NR_sched_yield);
901 static __attribute__((unused))
902 int sched_yield(void)
904 return __sysret(sys_sched_yield());
909 * int select(int nfds, fd_set *read_fds, fd_set *write_fds,
910 * fd_set *except_fds, struct timeval *timeout);
913 static __attribute__((unused))
914 int sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
916 #if defined(__ARCH_WANT_SYS_OLD_SELECT) && !defined(__NR__newselect)
917 struct sel_arg_struct {
921 } arg = { .n = nfds, .r = rfds, .w = wfds, .e = efds, .t = timeout };
922 return my_syscall1(__NR_select, &arg);
923 #elif defined(__ARCH_WANT_SYS_PSELECT6) && defined(__NR_pselect6)
927 t.tv_sec = timeout->tv_sec;
928 t.tv_nsec = timeout->tv_usec * 1000;
930 return my_syscall6(__NR_pselect6, nfds, rfds, wfds, efds, timeout ? &t : NULL, NULL);
931 #elif defined(__NR__newselect) || defined(__NR_select)
932 #ifndef __NR__newselect
933 #define __NR__newselect __NR_select
935 return my_syscall5(__NR__newselect, nfds, rfds, wfds, efds, timeout);
941 static __attribute__((unused))
942 int select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
944 return __sysret(sys_select(nfds, rfds, wfds, efds, timeout));
949 * int setpgid(pid_t pid, pid_t pgid);
952 static __attribute__((unused))
953 int sys_setpgid(pid_t pid, pid_t pgid)
955 return my_syscall2(__NR_setpgid, pid, pgid);
958 static __attribute__((unused))
959 int setpgid(pid_t pid, pid_t pgid)
961 return __sysret(sys_setpgid(pid, pgid));
966 * pid_t setsid(void);
969 static __attribute__((unused))
970 pid_t sys_setsid(void)
972 return my_syscall0(__NR_setsid);
975 static __attribute__((unused))
978 return __sysret(sys_setsid());
982 * int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf);
983 * int stat(const char *path, struct stat *buf);
986 static __attribute__((unused))
987 int sys_statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
990 return my_syscall5(__NR_statx, fd, path, flags, mask, buf);
996 static __attribute__((unused))
997 int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
999 return __sysret(sys_statx(fd, path, flags, mask, buf));
1003 static __attribute__((unused))
1004 int stat(const char *path, struct stat *buf)
1009 ret = __sysret(sys_statx(AT_FDCWD, path, AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx));
1013 buf->st_dev = ((statx.stx_dev_minor & 0xff)
1014 | (statx.stx_dev_major << 8)
1015 | ((statx.stx_dev_minor & ~0xff) << 12));
1016 buf->st_ino = statx.stx_ino;
1017 buf->st_mode = statx.stx_mode;
1018 buf->st_nlink = statx.stx_nlink;
1019 buf->st_uid = statx.stx_uid;
1020 buf->st_gid = statx.stx_gid;
1021 buf->st_rdev = ((statx.stx_rdev_minor & 0xff)
1022 | (statx.stx_rdev_major << 8)
1023 | ((statx.stx_rdev_minor & ~0xff) << 12));
1024 buf->st_size = statx.stx_size;
1025 buf->st_blksize = statx.stx_blksize;
1026 buf->st_blocks = statx.stx_blocks;
1027 buf->st_atim.tv_sec = statx.stx_atime.tv_sec;
1028 buf->st_atim.tv_nsec = statx.stx_atime.tv_nsec;
1029 buf->st_mtim.tv_sec = statx.stx_mtime.tv_sec;
1030 buf->st_mtim.tv_nsec = statx.stx_mtime.tv_nsec;
1031 buf->st_ctim.tv_sec = statx.stx_ctime.tv_sec;
1032 buf->st_ctim.tv_nsec = statx.stx_ctime.tv_nsec;
1039 * int symlink(const char *old, const char *new);
1042 static __attribute__((unused))
1043 int sys_symlink(const char *old, const char *new)
1045 #ifdef __NR_symlinkat
1046 return my_syscall3(__NR_symlinkat, old, AT_FDCWD, new);
1047 #elif defined(__NR_symlink)
1048 return my_syscall2(__NR_symlink, old, new);
1054 static __attribute__((unused))
1055 int symlink(const char *old, const char *new)
1057 return __sysret(sys_symlink(old, new));
1062 * mode_t umask(mode_t mode);
1065 static __attribute__((unused))
1066 mode_t sys_umask(mode_t mode)
1068 return my_syscall1(__NR_umask, mode);
1071 static __attribute__((unused))
1072 mode_t umask(mode_t mode)
1074 return sys_umask(mode);
1079 * int umount2(const char *path, int flags);
1082 static __attribute__((unused))
1083 int sys_umount2(const char *path, int flags)
1085 return my_syscall2(__NR_umount2, path, flags);
1088 static __attribute__((unused))
1089 int umount2(const char *path, int flags)
1091 return __sysret(sys_umount2(path, flags));
1096 * int unlink(const char *path);
1099 static __attribute__((unused))
1100 int sys_unlink(const char *path)
1102 #ifdef __NR_unlinkat
1103 return my_syscall3(__NR_unlinkat, AT_FDCWD, path, 0);
1104 #elif defined(__NR_unlink)
1105 return my_syscall1(__NR_unlink, path);
1111 static __attribute__((unused))
1112 int unlink(const char *path)
1114 return __sysret(sys_unlink(path));
1119 * pid_t wait(int *status);
1120 * pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage);
1121 * pid_t waitpid(pid_t pid, int *status, int options);
1124 static __attribute__((unused))
1125 pid_t sys_wait4(pid_t pid, int *status, int options, struct rusage *rusage)
1128 return my_syscall4(__NR_wait4, pid, status, options, rusage);
1134 static __attribute__((unused))
1135 pid_t wait(int *status)
1137 return __sysret(sys_wait4(-1, status, 0, NULL));
1140 static __attribute__((unused))
1141 pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage)
1143 return __sysret(sys_wait4(pid, status, options, rusage));
1147 static __attribute__((unused))
1148 pid_t waitpid(pid_t pid, int *status, int options)
1150 return __sysret(sys_wait4(pid, status, options, NULL));
1155 * ssize_t write(int fd, const void *buf, size_t count);
1158 static __attribute__((unused))
1159 ssize_t sys_write(int fd, const void *buf, size_t count)
1161 return my_syscall3(__NR_write, fd, buf, count);
1164 static __attribute__((unused))
1165 ssize_t write(int fd, const void *buf, size_t count)
1167 return __sysret(sys_write(fd, buf, count));
1172 * int memfd_create(const char *name, unsigned int flags);
1175 static __attribute__((unused))
1176 int sys_memfd_create(const char *name, unsigned int flags)
1178 return my_syscall2(__NR_memfd_create, name, flags);
1181 static __attribute__((unused))
1182 int memfd_create(const char *name, unsigned int flags)
1184 return __sysret(sys_memfd_create(name, flags));
1187 /* make sure to include all global symbols */
1190 #endif /* _NOLIBC_SYS_H */