1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */ 2 /* 3 * wait definitions for NOLIBC 4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu> 5 */ 6 7 /* make sure to include all global symbols */ 8 #include "../nolibc.h" 9 10 #ifndef _NOLIBC_SYS_WAIT_H 11 #define _NOLIBC_SYS_WAIT_H 12 13 #include "../arch.h" 14 #include "../std.h" 15 #include "../types.h" 16 17 /* 18 * pid_t wait(int *status); 19 * pid_t waitpid(pid_t pid, int *status, int options); 20 * int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options); 21 */ 22 23 static __attribute__((unused)) 24 int sys_waitid(int which, pid_t pid, siginfo_t *infop, int options, struct rusage *rusage) 25 { 26 return my_syscall5(__NR_waitid, which, pid, infop, options, rusage); 27 } 28 29 static __attribute__((unused)) 30 int waitid(int which, pid_t pid, siginfo_t *infop, int options) 31 { 32 return __sysret(sys_waitid(which, pid, infop, options, NULL)); 33 } 34 35 36 static __attribute__((unused)) 37 pid_t waitpid(pid_t pid, int *status, int options) 38 { 39 int idtype, ret; 40 siginfo_t info; 41 pid_t id; 42 43 if (pid == INT_MIN) { 44 SET_ERRNO(ESRCH); 45 return -1; 46 } else if (pid < -1) { 47 idtype = P_PGID; 48 id = -pid; 49 } else if (pid == -1) { 50 idtype = P_ALL; 51 id = 0; 52 } else if (pid == 0) { 53 idtype = P_PGID; 54 id = 0; 55 } else { 56 idtype = P_PID; 57 id = pid; 58 } 59 60 options |= WEXITED; 61 62 ret = waitid(idtype, id, &info, options); 63 if (ret) 64 return -1; 65 66 switch (info.si_code) { 67 case 0: 68 *status = 0; 69 break; 70 case CLD_EXITED: 71 *status = (info.si_status & 0xff) << 8; 72 break; 73 case CLD_KILLED: 74 *status = info.si_status & 0x7f; 75 break; 76 case CLD_DUMPED: 77 *status = (info.si_status & 0x7f) | 0x80; 78 break; 79 case CLD_STOPPED: 80 case CLD_TRAPPED: 81 *status = (info.si_status << 8) + 0x7f; 82 break; 83 case CLD_CONTINUED: 84 *status = 0xffff; 85 break; 86 default: 87 return -1; 88 } 89 90 return info.si_pid; 91 } 92 93 static __attribute__((unused)) 94 pid_t wait(int *status) 95 { 96 return waitpid(-1, status, 0); 97 } 98 99 #endif /* _NOLIBC_SYS_WAIT_H */ 100