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 <[email protected]> 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 53 static __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 88 static __attribute__((unused)) 89 void *sys_brk(void *addr) 90 { 91 return (void *)my_syscall1(__NR_brk, addr); 92 } 93 94 static __attribute__((unused)) 95 int 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 106 static __attribute__((unused)) 107 void *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 124 static __attribute__((unused)) 125 int sys_chdir(const char *path) 126 { 127 return my_syscall1(__NR_chdir, path); 128 } 129 130 static __attribute__((unused)) 131 int 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 141 static __attribute__((unused)) 142 int 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 153 static __attribute__((unused)) 154 int 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 164 static __attribute__((unused)) 165 int 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 176 static __attribute__((unused)) 177 int 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 187 static __attribute__((unused)) 188 int sys_chroot(const char *path) 189 { 190 return my_syscall1(__NR_chroot, path); 191 } 192 193 static __attribute__((unused)) 194 int chroot(const char *path) 195 { 196 return __sysret(sys_chroot(path)); 197 } 198 199 200 /* 201 * int close(int fd); 202 */ 203 204 static __attribute__((unused)) 205 int sys_close(int fd) 206 { 207 return my_syscall1(__NR_close, fd); 208 } 209 210 static __attribute__((unused)) 211 int close(int fd) 212 { 213 return __sysret(sys_close(fd)); 214 } 215 216 217 /* 218 * int dup(int fd); 219 */ 220 221 static __attribute__((unused)) 222 int sys_dup(int fd) 223 { 224 return my_syscall1(__NR_dup, fd); 225 } 226 227 static __attribute__((unused)) 228 int dup(int fd) 229 { 230 return __sysret(sys_dup(fd)); 231 } 232 233 234 /* 235 * int dup2(int old, int new); 236 */ 237 238 static __attribute__((unused)) 239 int 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 250 static __attribute__((unused)) 251 int 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 262 static __attribute__((unused)) 263 int sys_dup3(int old, int new, int flags) 264 { 265 return my_syscall3(__NR_dup3, old, new, flags); 266 } 267 268 static __attribute__((unused)) 269 int 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 280 static __attribute__((unused)) 281 int sys_execve(const char *filename, char *const argv[], char *const envp[]) 282 { 283 return my_syscall3(__NR_execve, filename, argv, envp); 284 } 285 286 static __attribute__((unused)) 287 int 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 297 static __attribute__((noreturn,unused)) 298 void sys_exit(int status) 299 { 300 my_syscall1(__NR_exit, status & 255); 301 while(1); /* shut the "noreturn" warnings. */ 302 } 303 304 static __attribute__((noreturn,unused)) 305 void exit(int status) 306 { 307 sys_exit(status); 308 } 309 310 311 /* 312 * pid_t fork(void); 313 */ 314 315 #ifndef sys_fork 316 static __attribute__((unused)) 317 pid_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 333 static __attribute__((unused)) 334 pid_t fork(void) 335 { 336 return __sysret(sys_fork()); 337 } 338 339 340 /* 341 * int fsync(int fd); 342 */ 343 344 static __attribute__((unused)) 345 int sys_fsync(int fd) 346 { 347 return my_syscall1(__NR_fsync, fd); 348 } 349 350 static __attribute__((unused)) 351 int 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 361 static __attribute__((unused)) 362 int sys_getdents64(int fd, struct linux_dirent64 *dirp, int count) 363 { 364 return my_syscall3(__NR_getdents64, fd, dirp, count); 365 } 366 367 static __attribute__((unused)) 368 int 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 378 static __attribute__((unused)) 379 uid_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 388 static __attribute__((unused)) 389 uid_t geteuid(void) 390 { 391 return sys_geteuid(); 392 } 393 394 395 /* 396 * pid_t getpgid(pid_t pid); 397 */ 398 399 static __attribute__((unused)) 400 pid_t sys_getpgid(pid_t pid) 401 { 402 return my_syscall1(__NR_getpgid, pid); 403 } 404 405 static __attribute__((unused)) 406 pid_t getpgid(pid_t pid) 407 { 408 return __sysret(sys_getpgid(pid)); 409 } 410 411 412 /* 413 * pid_t getpgrp(void); 414 */ 415 416 static __attribute__((unused)) 417 pid_t sys_getpgrp(void) 418 { 419 return sys_getpgid(0); 420 } 421 422 static __attribute__((unused)) 423 pid_t getpgrp(void) 424 { 425 return sys_getpgrp(); 426 } 427 428 429 /* 430 * pid_t getpid(void); 431 */ 432 433 static __attribute__((unused)) 434 pid_t sys_getpid(void) 435 { 436 return my_syscall0(__NR_getpid); 437 } 438 439 static __attribute__((unused)) 440 pid_t getpid(void) 441 { 442 return sys_getpid(); 443 } 444 445 446 /* 447 * pid_t getppid(void); 448 */ 449 450 static __attribute__((unused)) 451 pid_t sys_getppid(void) 452 { 453 return my_syscall0(__NR_getppid); 454 } 455 456 static __attribute__((unused)) 457 pid_t getppid(void) 458 { 459 return sys_getppid(); 460 } 461 462 463 /* 464 * pid_t gettid(void); 465 */ 466 467 static __attribute__((unused)) 468 pid_t sys_gettid(void) 469 { 470 return my_syscall0(__NR_gettid); 471 } 472 473 static __attribute__((unused)) 474 pid_t gettid(void) 475 { 476 return sys_gettid(); 477 } 478 479 static unsigned long getauxval(unsigned long key); 480 481 /* 482 * int getpagesize(void); 483 */ 484 485 static __attribute__((unused)) 486 int 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 496 static __attribute__((unused)) 497 int 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 506 static __attribute__((unused)) 507 int 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 517 static __attribute__((unused)) 518 uid_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 527 static __attribute__((unused)) 528 uid_t getuid(void) 529 { 530 return sys_getuid(); 531 } 532 533 534 /* 535 * int ioctl(int fd, unsigned long cmd, ... arg); 536 */ 537 538 static __attribute__((unused)) 539 long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg) 540 { 541 return my_syscall3(__NR_ioctl, fd, cmd, arg); 542 } 543 544 #define ioctl(fd, cmd, arg) __sysret(sys_ioctl(fd, cmd, (unsigned long)(arg))) 545 546 /* 547 * int kill(pid_t pid, int signal); 548 */ 549 550 static __attribute__((unused)) 551 int sys_kill(pid_t pid, int signal) 552 { 553 return my_syscall2(__NR_kill, pid, signal); 554 } 555 556 static __attribute__((unused)) 557 int kill(pid_t pid, int signal) 558 { 559 return __sysret(sys_kill(pid, signal)); 560 } 561 562 563 /* 564 * int link(const char *old, const char *new); 565 */ 566 567 static __attribute__((unused)) 568 int sys_link(const char *old, const char *new) 569 { 570 #ifdef __NR_linkat 571 return my_syscall5(__NR_linkat, AT_FDCWD, old, AT_FDCWD, new, 0); 572 #elif defined(__NR_link) 573 return my_syscall2(__NR_link, old, new); 574 #else 575 return __nolibc_enosys(__func__, old, new); 576 #endif 577 } 578 579 static __attribute__((unused)) 580 int link(const char *old, const char *new) 581 { 582 return __sysret(sys_link(old, new)); 583 } 584 585 586 /* 587 * off_t lseek(int fd, off_t offset, int whence); 588 */ 589 590 static __attribute__((unused)) 591 off_t sys_lseek(int fd, off_t offset, int whence) 592 { 593 #ifdef __NR_lseek 594 return my_syscall3(__NR_lseek, fd, offset, whence); 595 #else 596 return __nolibc_enosys(__func__, fd, offset, whence); 597 #endif 598 } 599 600 static __attribute__((unused)) 601 int sys_llseek(int fd, unsigned long offset_high, unsigned long offset_low, 602 __kernel_loff_t *result, int whence) 603 { 604 #ifdef __NR_llseek 605 return my_syscall5(__NR_llseek, fd, offset_high, offset_low, result, whence); 606 #else 607 return __nolibc_enosys(__func__, fd, offset_high, offset_low, result, whence); 608 #endif 609 } 610 611 static __attribute__((unused)) 612 off_t lseek(int fd, off_t offset, int whence) 613 { 614 __kernel_loff_t loff = 0; 615 off_t result; 616 int ret; 617 618 result = sys_lseek(fd, offset, whence); 619 if (result == -ENOSYS) { 620 /* Only exists on 32bit where nolibc off_t is also 32bit */ 621 ret = sys_llseek(fd, 0, offset, &loff, whence); 622 if (ret < 0) 623 result = ret; 624 else if (loff != (off_t)loff) 625 result = -EOVERFLOW; 626 else 627 result = loff; 628 } 629 630 return __sysret(result); 631 } 632 633 634 /* 635 * int mkdir(const char *path, mode_t mode); 636 */ 637 638 static __attribute__((unused)) 639 int sys_mkdir(const char *path, mode_t mode) 640 { 641 #ifdef __NR_mkdirat 642 return my_syscall3(__NR_mkdirat, AT_FDCWD, path, mode); 643 #elif defined(__NR_mkdir) 644 return my_syscall2(__NR_mkdir, path, mode); 645 #else 646 return __nolibc_enosys(__func__, path, mode); 647 #endif 648 } 649 650 static __attribute__((unused)) 651 int mkdir(const char *path, mode_t mode) 652 { 653 return __sysret(sys_mkdir(path, mode)); 654 } 655 656 /* 657 * int rmdir(const char *path); 658 */ 659 660 static __attribute__((unused)) 661 int sys_rmdir(const char *path) 662 { 663 #ifdef __NR_rmdir 664 return my_syscall1(__NR_rmdir, path); 665 #elif defined(__NR_unlinkat) 666 return my_syscall3(__NR_unlinkat, AT_FDCWD, path, AT_REMOVEDIR); 667 #else 668 return __nolibc_enosys(__func__, path); 669 #endif 670 } 671 672 static __attribute__((unused)) 673 int rmdir(const char *path) 674 { 675 return __sysret(sys_rmdir(path)); 676 } 677 678 679 /* 680 * int mknod(const char *path, mode_t mode, dev_t dev); 681 */ 682 683 static __attribute__((unused)) 684 long sys_mknod(const char *path, mode_t mode, dev_t dev) 685 { 686 #ifdef __NR_mknodat 687 return my_syscall4(__NR_mknodat, AT_FDCWD, path, mode, dev); 688 #elif defined(__NR_mknod) 689 return my_syscall3(__NR_mknod, path, mode, dev); 690 #else 691 return __nolibc_enosys(__func__, path, mode, dev); 692 #endif 693 } 694 695 static __attribute__((unused)) 696 int mknod(const char *path, mode_t mode, dev_t dev) 697 { 698 return __sysret(sys_mknod(path, mode, dev)); 699 } 700 701 #ifndef sys_mmap 702 static __attribute__((unused)) 703 void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd, 704 off_t offset) 705 { 706 int n; 707 708 #if defined(__NR_mmap2) 709 n = __NR_mmap2; 710 offset >>= 12; 711 #else 712 n = __NR_mmap; 713 #endif 714 715 return (void *)my_syscall6(n, addr, length, prot, flags, fd, offset); 716 } 717 #endif 718 719 /* Note that on Linux, MAP_FAILED is -1 so we can use the generic __sysret() 720 * which returns -1 upon error and still satisfy user land that checks for 721 * MAP_FAILED. 722 */ 723 724 static __attribute__((unused)) 725 void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) 726 { 727 void *ret = sys_mmap(addr, length, prot, flags, fd, offset); 728 729 if ((unsigned long)ret >= -4095UL) { 730 SET_ERRNO(-(long)ret); 731 ret = MAP_FAILED; 732 } 733 return ret; 734 } 735 736 static __attribute__((unused)) 737 int sys_munmap(void *addr, size_t length) 738 { 739 return my_syscall2(__NR_munmap, addr, length); 740 } 741 742 static __attribute__((unused)) 743 int munmap(void *addr, size_t length) 744 { 745 return __sysret(sys_munmap(addr, length)); 746 } 747 748 /* 749 * int mount(const char *source, const char *target, 750 * const char *fstype, unsigned long flags, 751 * const void *data); 752 */ 753 static __attribute__((unused)) 754 int sys_mount(const char *src, const char *tgt, const char *fst, 755 unsigned long flags, const void *data) 756 { 757 return my_syscall5(__NR_mount, src, tgt, fst, flags, data); 758 } 759 760 static __attribute__((unused)) 761 int mount(const char *src, const char *tgt, 762 const char *fst, unsigned long flags, 763 const void *data) 764 { 765 return __sysret(sys_mount(src, tgt, fst, flags, data)); 766 } 767 768 769 /* 770 * int open(const char *path, int flags[, mode_t mode]); 771 */ 772 773 static __attribute__((unused)) 774 int sys_open(const char *path, int flags, mode_t mode) 775 { 776 #ifdef __NR_openat 777 return my_syscall4(__NR_openat, AT_FDCWD, path, flags, mode); 778 #elif defined(__NR_open) 779 return my_syscall3(__NR_open, path, flags, mode); 780 #else 781 return __nolibc_enosys(__func__, path, flags, mode); 782 #endif 783 } 784 785 static __attribute__((unused)) 786 int open(const char *path, int flags, ...) 787 { 788 mode_t mode = 0; 789 790 if (flags & O_CREAT) { 791 va_list args; 792 793 va_start(args, flags); 794 mode = va_arg(args, int); 795 va_end(args); 796 } 797 798 return __sysret(sys_open(path, flags, mode)); 799 } 800 801 802 /* 803 * int pipe2(int pipefd[2], int flags); 804 * int pipe(int pipefd[2]); 805 */ 806 807 static __attribute__((unused)) 808 int sys_pipe2(int pipefd[2], int flags) 809 { 810 return my_syscall2(__NR_pipe2, pipefd, flags); 811 } 812 813 static __attribute__((unused)) 814 int pipe2(int pipefd[2], int flags) 815 { 816 return __sysret(sys_pipe2(pipefd, flags)); 817 } 818 819 static __attribute__((unused)) 820 int pipe(int pipefd[2]) 821 { 822 return pipe2(pipefd, 0); 823 } 824 825 826 /* 827 * int prctl(int option, unsigned long arg2, unsigned long arg3, 828 * unsigned long arg4, unsigned long arg5); 829 */ 830 831 static __attribute__((unused)) 832 int sys_prctl(int option, unsigned long arg2, unsigned long arg3, 833 unsigned long arg4, unsigned long arg5) 834 { 835 return my_syscall5(__NR_prctl, option, arg2, arg3, arg4, arg5); 836 } 837 838 static __attribute__((unused)) 839 int prctl(int option, unsigned long arg2, unsigned long arg3, 840 unsigned long arg4, unsigned long arg5) 841 { 842 return __sysret(sys_prctl(option, arg2, arg3, arg4, arg5)); 843 } 844 845 846 /* 847 * int pivot_root(const char *new, const char *old); 848 */ 849 850 static __attribute__((unused)) 851 int sys_pivot_root(const char *new, const char *old) 852 { 853 return my_syscall2(__NR_pivot_root, new, old); 854 } 855 856 static __attribute__((unused)) 857 int pivot_root(const char *new, const char *old) 858 { 859 return __sysret(sys_pivot_root(new, old)); 860 } 861 862 863 /* 864 * int poll(struct pollfd *fds, int nfds, int timeout); 865 */ 866 867 static __attribute__((unused)) 868 int sys_poll(struct pollfd *fds, int nfds, int timeout) 869 { 870 #if defined(__NR_ppoll) 871 struct timespec t; 872 873 if (timeout >= 0) { 874 t.tv_sec = timeout / 1000; 875 t.tv_nsec = (timeout % 1000) * 1000000; 876 } 877 return my_syscall5(__NR_ppoll, fds, nfds, (timeout >= 0) ? &t : NULL, NULL, 0); 878 #elif defined(__NR_poll) 879 return my_syscall3(__NR_poll, fds, nfds, timeout); 880 #else 881 return __nolibc_enosys(__func__, fds, nfds, timeout); 882 #endif 883 } 884 885 static __attribute__((unused)) 886 int poll(struct pollfd *fds, int nfds, int timeout) 887 { 888 return __sysret(sys_poll(fds, nfds, timeout)); 889 } 890 891 892 /* 893 * ssize_t read(int fd, void *buf, size_t count); 894 */ 895 896 static __attribute__((unused)) 897 ssize_t sys_read(int fd, void *buf, size_t count) 898 { 899 return my_syscall3(__NR_read, fd, buf, count); 900 } 901 902 static __attribute__((unused)) 903 ssize_t read(int fd, void *buf, size_t count) 904 { 905 return __sysret(sys_read(fd, buf, count)); 906 } 907 908 909 /* 910 * int reboot(int cmd); 911 * <cmd> is among LINUX_REBOOT_CMD_* 912 */ 913 914 static __attribute__((unused)) 915 ssize_t sys_reboot(int magic1, int magic2, int cmd, void *arg) 916 { 917 return my_syscall4(__NR_reboot, magic1, magic2, cmd, arg); 918 } 919 920 static __attribute__((unused)) 921 int reboot(int cmd) 922 { 923 return __sysret(sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0)); 924 } 925 926 927 /* 928 * int getrlimit(int resource, struct rlimit *rlim); 929 * int setrlimit(int resource, const struct rlimit *rlim); 930 */ 931 932 static __attribute__((unused)) 933 int sys_prlimit64(pid_t pid, int resource, 934 const struct rlimit64 *new_limit, struct rlimit64 *old_limit) 935 { 936 return my_syscall4(__NR_prlimit64, pid, resource, new_limit, old_limit); 937 } 938 939 static __attribute__((unused)) 940 int getrlimit(int resource, struct rlimit *rlim) 941 { 942 struct rlimit64 rlim64; 943 int ret; 944 945 ret = __sysret(sys_prlimit64(0, resource, NULL, &rlim64)); 946 rlim->rlim_cur = rlim64.rlim_cur; 947 rlim->rlim_max = rlim64.rlim_max; 948 949 return ret; 950 } 951 952 static __attribute__((unused)) 953 int setrlimit(int resource, const struct rlimit *rlim) 954 { 955 struct rlimit64 rlim64 = { 956 .rlim_cur = rlim->rlim_cur, 957 .rlim_max = rlim->rlim_max, 958 }; 959 960 return __sysret(sys_prlimit64(0, resource, &rlim64, NULL)); 961 } 962 963 964 /* 965 * int sched_yield(void); 966 */ 967 968 static __attribute__((unused)) 969 int sys_sched_yield(void) 970 { 971 return my_syscall0(__NR_sched_yield); 972 } 973 974 static __attribute__((unused)) 975 int sched_yield(void) 976 { 977 return __sysret(sys_sched_yield()); 978 } 979 980 981 /* 982 * int select(int nfds, fd_set *read_fds, fd_set *write_fds, 983 * fd_set *except_fds, struct timeval *timeout); 984 */ 985 986 static __attribute__((unused)) 987 int sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout) 988 { 989 #if defined(__ARCH_WANT_SYS_OLD_SELECT) && !defined(__NR__newselect) 990 struct sel_arg_struct { 991 unsigned long n; 992 fd_set *r, *w, *e; 993 struct timeval *t; 994 } arg = { .n = nfds, .r = rfds, .w = wfds, .e = efds, .t = timeout }; 995 return my_syscall1(__NR_select, &arg); 996 #elif defined(__NR__newselect) 997 return my_syscall5(__NR__newselect, nfds, rfds, wfds, efds, timeout); 998 #elif defined(__NR_select) 999 return my_syscall5(__NR_select, nfds, rfds, wfds, efds, timeout); 1000 #elif defined(__NR_pselect6) 1001 struct timespec t; 1002 1003 if (timeout) { 1004 t.tv_sec = timeout->tv_sec; 1005 t.tv_nsec = timeout->tv_usec * 1000; 1006 } 1007 return my_syscall6(__NR_pselect6, nfds, rfds, wfds, efds, timeout ? &t : NULL, NULL); 1008 #else 1009 return __nolibc_enosys(__func__, nfds, rfds, wfds, efds, timeout); 1010 #endif 1011 } 1012 1013 static __attribute__((unused)) 1014 int select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout) 1015 { 1016 return __sysret(sys_select(nfds, rfds, wfds, efds, timeout)); 1017 } 1018 1019 1020 /* 1021 * int setpgid(pid_t pid, pid_t pgid); 1022 */ 1023 1024 static __attribute__((unused)) 1025 int sys_setpgid(pid_t pid, pid_t pgid) 1026 { 1027 return my_syscall2(__NR_setpgid, pid, pgid); 1028 } 1029 1030 static __attribute__((unused)) 1031 int setpgid(pid_t pid, pid_t pgid) 1032 { 1033 return __sysret(sys_setpgid(pid, pgid)); 1034 } 1035 1036 1037 /* 1038 * pid_t setsid(void); 1039 */ 1040 1041 static __attribute__((unused)) 1042 pid_t sys_setsid(void) 1043 { 1044 return my_syscall0(__NR_setsid); 1045 } 1046 1047 static __attribute__((unused)) 1048 pid_t setsid(void) 1049 { 1050 return __sysret(sys_setsid()); 1051 } 1052 1053 /* 1054 * int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf); 1055 * int stat(const char *path, struct stat *buf); 1056 */ 1057 1058 static __attribute__((unused)) 1059 int sys_statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf) 1060 { 1061 #ifdef __NR_statx 1062 return my_syscall5(__NR_statx, fd, path, flags, mask, buf); 1063 #else 1064 return __nolibc_enosys(__func__, fd, path, flags, mask, buf); 1065 #endif 1066 } 1067 1068 static __attribute__((unused)) 1069 int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf) 1070 { 1071 return __sysret(sys_statx(fd, path, flags, mask, buf)); 1072 } 1073 1074 1075 static __attribute__((unused)) 1076 int stat(const char *path, struct stat *buf) 1077 { 1078 struct statx statx; 1079 long ret; 1080 1081 ret = __sysret(sys_statx(AT_FDCWD, path, AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx)); 1082 if (ret == -1) 1083 return ret; 1084 1085 buf->st_dev = ((statx.stx_dev_minor & 0xff) 1086 | (statx.stx_dev_major << 8) 1087 | ((statx.stx_dev_minor & ~0xff) << 12)); 1088 buf->st_ino = statx.stx_ino; 1089 buf->st_mode = statx.stx_mode; 1090 buf->st_nlink = statx.stx_nlink; 1091 buf->st_uid = statx.stx_uid; 1092 buf->st_gid = statx.stx_gid; 1093 buf->st_rdev = ((statx.stx_rdev_minor & 0xff) 1094 | (statx.stx_rdev_major << 8) 1095 | ((statx.stx_rdev_minor & ~0xff) << 12)); 1096 buf->st_size = statx.stx_size; 1097 buf->st_blksize = statx.stx_blksize; 1098 buf->st_blocks = statx.stx_blocks; 1099 buf->st_atim.tv_sec = statx.stx_atime.tv_sec; 1100 buf->st_atim.tv_nsec = statx.stx_atime.tv_nsec; 1101 buf->st_mtim.tv_sec = statx.stx_mtime.tv_sec; 1102 buf->st_mtim.tv_nsec = statx.stx_mtime.tv_nsec; 1103 buf->st_ctim.tv_sec = statx.stx_ctime.tv_sec; 1104 buf->st_ctim.tv_nsec = statx.stx_ctime.tv_nsec; 1105 1106 return 0; 1107 } 1108 1109 1110 /* 1111 * int symlink(const char *old, const char *new); 1112 */ 1113 1114 static __attribute__((unused)) 1115 int sys_symlink(const char *old, const char *new) 1116 { 1117 #ifdef __NR_symlinkat 1118 return my_syscall3(__NR_symlinkat, old, AT_FDCWD, new); 1119 #elif defined(__NR_symlink) 1120 return my_syscall2(__NR_symlink, old, new); 1121 #else 1122 return __nolibc_enosys(__func__, old, new); 1123 #endif 1124 } 1125 1126 static __attribute__((unused)) 1127 int symlink(const char *old, const char *new) 1128 { 1129 return __sysret(sys_symlink(old, new)); 1130 } 1131 1132 1133 /* 1134 * mode_t umask(mode_t mode); 1135 */ 1136 1137 static __attribute__((unused)) 1138 mode_t sys_umask(mode_t mode) 1139 { 1140 return my_syscall1(__NR_umask, mode); 1141 } 1142 1143 static __attribute__((unused)) 1144 mode_t umask(mode_t mode) 1145 { 1146 return sys_umask(mode); 1147 } 1148 1149 1150 /* 1151 * int umount2(const char *path, int flags); 1152 */ 1153 1154 static __attribute__((unused)) 1155 int sys_umount2(const char *path, int flags) 1156 { 1157 return my_syscall2(__NR_umount2, path, flags); 1158 } 1159 1160 static __attribute__((unused)) 1161 int umount2(const char *path, int flags) 1162 { 1163 return __sysret(sys_umount2(path, flags)); 1164 } 1165 1166 1167 /* 1168 * int uname(struct utsname *buf); 1169 */ 1170 1171 struct utsname { 1172 char sysname[65]; 1173 char nodename[65]; 1174 char release[65]; 1175 char version[65]; 1176 char machine[65]; 1177 char domainname[65]; 1178 }; 1179 1180 static __attribute__((unused)) 1181 int sys_uname(struct utsname *buf) 1182 { 1183 return my_syscall1(__NR_uname, buf); 1184 } 1185 1186 static __attribute__((unused)) 1187 int uname(struct utsname *buf) 1188 { 1189 return __sysret(sys_uname(buf)); 1190 } 1191 1192 1193 /* 1194 * int unlink(const char *path); 1195 */ 1196 1197 static __attribute__((unused)) 1198 int sys_unlink(const char *path) 1199 { 1200 #ifdef __NR_unlinkat 1201 return my_syscall3(__NR_unlinkat, AT_FDCWD, path, 0); 1202 #elif defined(__NR_unlink) 1203 return my_syscall1(__NR_unlink, path); 1204 #else 1205 return __nolibc_enosys(__func__, path); 1206 #endif 1207 } 1208 1209 static __attribute__((unused)) 1210 int unlink(const char *path) 1211 { 1212 return __sysret(sys_unlink(path)); 1213 } 1214 1215 1216 /* 1217 * pid_t wait(int *status); 1218 * pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage); 1219 * pid_t waitpid(pid_t pid, int *status, int options); 1220 */ 1221 1222 static __attribute__((unused)) 1223 pid_t sys_wait4(pid_t pid, int *status, int options, struct rusage *rusage) 1224 { 1225 #ifdef __NR_wait4 1226 return my_syscall4(__NR_wait4, pid, status, options, rusage); 1227 #else 1228 return __nolibc_enosys(__func__, pid, status, options, rusage); 1229 #endif 1230 } 1231 1232 static __attribute__((unused)) 1233 pid_t wait(int *status) 1234 { 1235 return __sysret(sys_wait4(-1, status, 0, NULL)); 1236 } 1237 1238 static __attribute__((unused)) 1239 pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage) 1240 { 1241 return __sysret(sys_wait4(pid, status, options, rusage)); 1242 } 1243 1244 1245 static __attribute__((unused)) 1246 pid_t waitpid(pid_t pid, int *status, int options) 1247 { 1248 return __sysret(sys_wait4(pid, status, options, NULL)); 1249 } 1250 1251 1252 /* 1253 * int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options); 1254 */ 1255 1256 static __attribute__((unused)) 1257 int sys_waitid(int which, pid_t pid, siginfo_t *infop, int options, struct rusage *rusage) 1258 { 1259 return my_syscall5(__NR_waitid, which, pid, infop, options, rusage); 1260 } 1261 1262 static __attribute__((unused)) 1263 int waitid(int which, pid_t pid, siginfo_t *infop, int options) 1264 { 1265 return __sysret(sys_waitid(which, pid, infop, options, NULL)); 1266 } 1267 1268 1269 /* 1270 * ssize_t write(int fd, const void *buf, size_t count); 1271 */ 1272 1273 static __attribute__((unused)) 1274 ssize_t sys_write(int fd, const void *buf, size_t count) 1275 { 1276 return my_syscall3(__NR_write, fd, buf, count); 1277 } 1278 1279 static __attribute__((unused)) 1280 ssize_t write(int fd, const void *buf, size_t count) 1281 { 1282 return __sysret(sys_write(fd, buf, count)); 1283 } 1284 1285 1286 /* 1287 * int memfd_create(const char *name, unsigned int flags); 1288 */ 1289 1290 static __attribute__((unused)) 1291 int sys_memfd_create(const char *name, unsigned int flags) 1292 { 1293 return my_syscall2(__NR_memfd_create, name, flags); 1294 } 1295 1296 static __attribute__((unused)) 1297 int memfd_create(const char *name, unsigned int flags) 1298 { 1299 return __sysret(sys_memfd_create(name, flags)); 1300 } 1301 1302 /* make sure to include all global symbols */ 1303 #include "nolibc.h" 1304 1305 #endif /* _NOLIBC_SYS_H */ 1306