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 /* 616 * int mknod(const char *path, mode_t mode, dev_t dev); 617 */ 618 619 static __attribute__((unused)) 620 long sys_mknod(const char *path, mode_t mode, dev_t dev) 621 { 622 #ifdef __NR_mknodat 623 return my_syscall4(__NR_mknodat, AT_FDCWD, path, mode, dev); 624 #elif defined(__NR_mknod) 625 return my_syscall3(__NR_mknod, path, mode, dev); 626 #else 627 return -ENOSYS; 628 #endif 629 } 630 631 static __attribute__((unused)) 632 int mknod(const char *path, mode_t mode, dev_t dev) 633 { 634 return __sysret(sys_mknod(path, mode, dev)); 635 } 636 637 #ifndef sys_mmap 638 static __attribute__((unused)) 639 void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd, 640 off_t offset) 641 { 642 int n; 643 644 #if defined(__NR_mmap2) 645 n = __NR_mmap2; 646 offset >>= 12; 647 #else 648 n = __NR_mmap; 649 #endif 650 651 return (void *)my_syscall6(n, addr, length, prot, flags, fd, offset); 652 } 653 #endif 654 655 /* Note that on Linux, MAP_FAILED is -1 so we can use the generic __sysret() 656 * which returns -1 upon error and still satisfy user land that checks for 657 * MAP_FAILED. 658 */ 659 660 static __attribute__((unused)) 661 void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) 662 { 663 return (void *)__sysret((unsigned long)sys_mmap(addr, length, prot, flags, fd, offset)); 664 } 665 666 static __attribute__((unused)) 667 int sys_munmap(void *addr, size_t length) 668 { 669 return my_syscall2(__NR_munmap, addr, length); 670 } 671 672 static __attribute__((unused)) 673 int munmap(void *addr, size_t length) 674 { 675 return __sysret(sys_munmap(addr, length)); 676 } 677 678 /* 679 * int mount(const char *source, const char *target, 680 * const char *fstype, unsigned long flags, 681 * const void *data); 682 */ 683 static __attribute__((unused)) 684 int sys_mount(const char *src, const char *tgt, const char *fst, 685 unsigned long flags, const void *data) 686 { 687 return my_syscall5(__NR_mount, src, tgt, fst, flags, data); 688 } 689 690 static __attribute__((unused)) 691 int mount(const char *src, const char *tgt, 692 const char *fst, unsigned long flags, 693 const void *data) 694 { 695 return __sysret(sys_mount(src, tgt, fst, flags, data)); 696 } 697 698 699 /* 700 * int open(const char *path, int flags[, mode_t mode]); 701 */ 702 703 static __attribute__((unused)) 704 int sys_open(const char *path, int flags, mode_t mode) 705 { 706 #ifdef __NR_openat 707 return my_syscall4(__NR_openat, AT_FDCWD, path, flags, mode); 708 #elif defined(__NR_open) 709 return my_syscall3(__NR_open, path, flags, mode); 710 #else 711 return -ENOSYS; 712 #endif 713 } 714 715 static __attribute__((unused)) 716 int open(const char *path, int flags, ...) 717 { 718 mode_t mode = 0; 719 int ret; 720 721 if (flags & O_CREAT) { 722 va_list args; 723 724 va_start(args, flags); 725 mode = va_arg(args, int); 726 va_end(args); 727 } 728 729 return __sysret(sys_open(path, flags, mode)); 730 } 731 732 733 /* 734 * int prctl(int option, unsigned long arg2, unsigned long arg3, 735 * unsigned long arg4, unsigned long arg5); 736 */ 737 738 static __attribute__((unused)) 739 int sys_prctl(int option, unsigned long arg2, unsigned long arg3, 740 unsigned long arg4, unsigned long arg5) 741 { 742 return my_syscall5(__NR_prctl, option, arg2, arg3, arg4, arg5); 743 } 744 745 static __attribute__((unused)) 746 int prctl(int option, unsigned long arg2, unsigned long arg3, 747 unsigned long arg4, unsigned long arg5) 748 { 749 return __sysret(sys_prctl(option, arg2, arg3, arg4, arg5)); 750 } 751 752 753 /* 754 * int pivot_root(const char *new, const char *old); 755 */ 756 757 static __attribute__((unused)) 758 int sys_pivot_root(const char *new, const char *old) 759 { 760 return my_syscall2(__NR_pivot_root, new, old); 761 } 762 763 static __attribute__((unused)) 764 int pivot_root(const char *new, const char *old) 765 { 766 return __sysret(sys_pivot_root(new, old)); 767 } 768 769 770 /* 771 * int poll(struct pollfd *fds, int nfds, int timeout); 772 */ 773 774 static __attribute__((unused)) 775 int sys_poll(struct pollfd *fds, int nfds, int timeout) 776 { 777 #if defined(__NR_ppoll) 778 struct timespec t; 779 780 if (timeout >= 0) { 781 t.tv_sec = timeout / 1000; 782 t.tv_nsec = (timeout % 1000) * 1000000; 783 } 784 return my_syscall5(__NR_ppoll, fds, nfds, (timeout >= 0) ? &t : NULL, NULL, 0); 785 #elif defined(__NR_poll) 786 return my_syscall3(__NR_poll, fds, nfds, timeout); 787 #else 788 return -ENOSYS; 789 #endif 790 } 791 792 static __attribute__((unused)) 793 int poll(struct pollfd *fds, int nfds, int timeout) 794 { 795 return __sysret(sys_poll(fds, nfds, timeout)); 796 } 797 798 799 /* 800 * ssize_t read(int fd, void *buf, size_t count); 801 */ 802 803 static __attribute__((unused)) 804 ssize_t sys_read(int fd, void *buf, size_t count) 805 { 806 return my_syscall3(__NR_read, fd, buf, count); 807 } 808 809 static __attribute__((unused)) 810 ssize_t read(int fd, void *buf, size_t count) 811 { 812 return __sysret(sys_read(fd, buf, count)); 813 } 814 815 816 /* 817 * int reboot(int cmd); 818 * <cmd> is among LINUX_REBOOT_CMD_* 819 */ 820 821 static __attribute__((unused)) 822 ssize_t sys_reboot(int magic1, int magic2, int cmd, void *arg) 823 { 824 return my_syscall4(__NR_reboot, magic1, magic2, cmd, arg); 825 } 826 827 static __attribute__((unused)) 828 int reboot(int cmd) 829 { 830 return __sysret(sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0)); 831 } 832 833 834 /* 835 * int sched_yield(void); 836 */ 837 838 static __attribute__((unused)) 839 int sys_sched_yield(void) 840 { 841 return my_syscall0(__NR_sched_yield); 842 } 843 844 static __attribute__((unused)) 845 int sched_yield(void) 846 { 847 return __sysret(sys_sched_yield()); 848 } 849 850 851 /* 852 * int select(int nfds, fd_set *read_fds, fd_set *write_fds, 853 * fd_set *except_fds, struct timeval *timeout); 854 */ 855 856 static __attribute__((unused)) 857 int sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout) 858 { 859 #if defined(__ARCH_WANT_SYS_OLD_SELECT) && !defined(__NR__newselect) 860 struct sel_arg_struct { 861 unsigned long n; 862 fd_set *r, *w, *e; 863 struct timeval *t; 864 } arg = { .n = nfds, .r = rfds, .w = wfds, .e = efds, .t = timeout }; 865 return my_syscall1(__NR_select, &arg); 866 #elif defined(__ARCH_WANT_SYS_PSELECT6) && defined(__NR_pselect6) 867 struct timespec t; 868 869 if (timeout) { 870 t.tv_sec = timeout->tv_sec; 871 t.tv_nsec = timeout->tv_usec * 1000; 872 } 873 return my_syscall6(__NR_pselect6, nfds, rfds, wfds, efds, timeout ? &t : NULL, NULL); 874 #elif defined(__NR__newselect) || defined(__NR_select) 875 #ifndef __NR__newselect 876 #define __NR__newselect __NR_select 877 #endif 878 return my_syscall5(__NR__newselect, nfds, rfds, wfds, efds, timeout); 879 #else 880 return -ENOSYS; 881 #endif 882 } 883 884 static __attribute__((unused)) 885 int select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout) 886 { 887 return __sysret(sys_select(nfds, rfds, wfds, efds, timeout)); 888 } 889 890 891 /* 892 * int setpgid(pid_t pid, pid_t pgid); 893 */ 894 895 static __attribute__((unused)) 896 int sys_setpgid(pid_t pid, pid_t pgid) 897 { 898 return my_syscall2(__NR_setpgid, pid, pgid); 899 } 900 901 static __attribute__((unused)) 902 int setpgid(pid_t pid, pid_t pgid) 903 { 904 return __sysret(sys_setpgid(pid, pgid)); 905 } 906 907 908 /* 909 * pid_t setsid(void); 910 */ 911 912 static __attribute__((unused)) 913 pid_t sys_setsid(void) 914 { 915 return my_syscall0(__NR_setsid); 916 } 917 918 static __attribute__((unused)) 919 pid_t setsid(void) 920 { 921 return __sysret(sys_setsid()); 922 } 923 924 #if defined(__NR_statx) 925 /* 926 * int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf); 927 */ 928 929 static __attribute__((unused)) 930 int sys_statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf) 931 { 932 return my_syscall5(__NR_statx, fd, path, flags, mask, buf); 933 } 934 935 static __attribute__((unused)) 936 int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf) 937 { 938 return __sysret(sys_statx(fd, path, flags, mask, buf)); 939 } 940 #endif 941 942 /* 943 * int stat(const char *path, struct stat *buf); 944 * Warning: the struct stat's layout is arch-dependent. 945 */ 946 947 #if defined(__NR_statx) && !defined(__NR_newfstatat) && !defined(__NR_stat) 948 /* 949 * Maybe we can just use statx() when available for all architectures? 950 */ 951 static __attribute__((unused)) 952 int sys_stat(const char *path, struct stat *buf) 953 { 954 struct statx statx; 955 long ret; 956 957 ret = sys_statx(AT_FDCWD, path, AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx); 958 buf->st_dev = ((statx.stx_dev_minor & 0xff) 959 | (statx.stx_dev_major << 8) 960 | ((statx.stx_dev_minor & ~0xff) << 12)); 961 buf->st_ino = statx.stx_ino; 962 buf->st_mode = statx.stx_mode; 963 buf->st_nlink = statx.stx_nlink; 964 buf->st_uid = statx.stx_uid; 965 buf->st_gid = statx.stx_gid; 966 buf->st_rdev = ((statx.stx_rdev_minor & 0xff) 967 | (statx.stx_rdev_major << 8) 968 | ((statx.stx_rdev_minor & ~0xff) << 12)); 969 buf->st_size = statx.stx_size; 970 buf->st_blksize = statx.stx_blksize; 971 buf->st_blocks = statx.stx_blocks; 972 buf->st_atim.tv_sec = statx.stx_atime.tv_sec; 973 buf->st_atim.tv_nsec = statx.stx_atime.tv_nsec; 974 buf->st_mtim.tv_sec = statx.stx_mtime.tv_sec; 975 buf->st_mtim.tv_nsec = statx.stx_mtime.tv_nsec; 976 buf->st_ctim.tv_sec = statx.stx_ctime.tv_sec; 977 buf->st_ctim.tv_nsec = statx.stx_ctime.tv_nsec; 978 return ret; 979 } 980 #else 981 static __attribute__((unused)) 982 int sys_stat(const char *path, struct stat *buf) 983 { 984 struct sys_stat_struct stat; 985 long ret; 986 987 #ifdef __NR_newfstatat 988 /* only solution for arm64 */ 989 ret = my_syscall4(__NR_newfstatat, AT_FDCWD, path, &stat, 0); 990 #elif defined(__NR_stat) 991 ret = my_syscall2(__NR_stat, path, &stat); 992 #else 993 return -ENOSYS; 994 #endif 995 buf->st_dev = stat.st_dev; 996 buf->st_ino = stat.st_ino; 997 buf->st_mode = stat.st_mode; 998 buf->st_nlink = stat.st_nlink; 999 buf->st_uid = stat.st_uid; 1000 buf->st_gid = stat.st_gid; 1001 buf->st_rdev = stat.st_rdev; 1002 buf->st_size = stat.st_size; 1003 buf->st_blksize = stat.st_blksize; 1004 buf->st_blocks = stat.st_blocks; 1005 buf->st_atim.tv_sec = stat.st_atime; 1006 buf->st_atim.tv_nsec = stat.st_atime_nsec; 1007 buf->st_mtim.tv_sec = stat.st_mtime; 1008 buf->st_mtim.tv_nsec = stat.st_mtime_nsec; 1009 buf->st_ctim.tv_sec = stat.st_ctime; 1010 buf->st_ctim.tv_nsec = stat.st_ctime_nsec; 1011 return ret; 1012 } 1013 #endif 1014 1015 static __attribute__((unused)) 1016 int stat(const char *path, struct stat *buf) 1017 { 1018 return __sysret(sys_stat(path, buf)); 1019 } 1020 1021 1022 /* 1023 * int symlink(const char *old, const char *new); 1024 */ 1025 1026 static __attribute__((unused)) 1027 int sys_symlink(const char *old, const char *new) 1028 { 1029 #ifdef __NR_symlinkat 1030 return my_syscall3(__NR_symlinkat, old, AT_FDCWD, new); 1031 #elif defined(__NR_symlink) 1032 return my_syscall2(__NR_symlink, old, new); 1033 #else 1034 return -ENOSYS; 1035 #endif 1036 } 1037 1038 static __attribute__((unused)) 1039 int symlink(const char *old, const char *new) 1040 { 1041 return __sysret(sys_symlink(old, new)); 1042 } 1043 1044 1045 /* 1046 * mode_t umask(mode_t mode); 1047 */ 1048 1049 static __attribute__((unused)) 1050 mode_t sys_umask(mode_t mode) 1051 { 1052 return my_syscall1(__NR_umask, mode); 1053 } 1054 1055 static __attribute__((unused)) 1056 mode_t umask(mode_t mode) 1057 { 1058 return sys_umask(mode); 1059 } 1060 1061 1062 /* 1063 * int umount2(const char *path, int flags); 1064 */ 1065 1066 static __attribute__((unused)) 1067 int sys_umount2(const char *path, int flags) 1068 { 1069 return my_syscall2(__NR_umount2, path, flags); 1070 } 1071 1072 static __attribute__((unused)) 1073 int umount2(const char *path, int flags) 1074 { 1075 return __sysret(sys_umount2(path, flags)); 1076 } 1077 1078 1079 /* 1080 * int unlink(const char *path); 1081 */ 1082 1083 static __attribute__((unused)) 1084 int sys_unlink(const char *path) 1085 { 1086 #ifdef __NR_unlinkat 1087 return my_syscall3(__NR_unlinkat, AT_FDCWD, path, 0); 1088 #elif defined(__NR_unlink) 1089 return my_syscall1(__NR_unlink, path); 1090 #else 1091 return -ENOSYS; 1092 #endif 1093 } 1094 1095 static __attribute__((unused)) 1096 int unlink(const char *path) 1097 { 1098 return __sysret(sys_unlink(path)); 1099 } 1100 1101 1102 /* 1103 * pid_t wait(int *status); 1104 * pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage); 1105 * pid_t waitpid(pid_t pid, int *status, int options); 1106 */ 1107 1108 static __attribute__((unused)) 1109 pid_t sys_wait4(pid_t pid, int *status, int options, struct rusage *rusage) 1110 { 1111 #ifdef __NR_wait4 1112 return my_syscall4(__NR_wait4, pid, status, options, rusage); 1113 #else 1114 return -ENOSYS; 1115 #endif 1116 } 1117 1118 static __attribute__((unused)) 1119 pid_t wait(int *status) 1120 { 1121 return __sysret(sys_wait4(-1, status, 0, NULL)); 1122 } 1123 1124 static __attribute__((unused)) 1125 pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage) 1126 { 1127 return __sysret(sys_wait4(pid, status, options, rusage)); 1128 } 1129 1130 1131 static __attribute__((unused)) 1132 pid_t waitpid(pid_t pid, int *status, int options) 1133 { 1134 return __sysret(sys_wait4(pid, status, options, NULL)); 1135 } 1136 1137 1138 /* 1139 * ssize_t write(int fd, const void *buf, size_t count); 1140 */ 1141 1142 static __attribute__((unused)) 1143 ssize_t sys_write(int fd, const void *buf, size_t count) 1144 { 1145 return my_syscall3(__NR_write, fd, buf, count); 1146 } 1147 1148 static __attribute__((unused)) 1149 ssize_t write(int fd, const void *buf, size_t count) 1150 { 1151 return __sysret(sys_write(fd, buf, count)); 1152 } 1153 1154 1155 /* 1156 * int memfd_create(const char *name, unsigned int flags); 1157 */ 1158 1159 static __attribute__((unused)) 1160 int sys_memfd_create(const char *name, unsigned int flags) 1161 { 1162 return my_syscall2(__NR_memfd_create, name, flags); 1163 } 1164 1165 static __attribute__((unused)) 1166 int memfd_create(const char *name, unsigned int flags) 1167 { 1168 return __sysret(sys_memfd_create(name, flags)); 1169 } 1170 1171 /* make sure to include all global symbols */ 1172 #include "nolibc.h" 1173 1174 #endif /* _NOLIBC_SYS_H */ 1175