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