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