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 if (status) 69 *status = 0; 70 break; 71 case CLD_EXITED: 72 if (status) 73 *status = (info.si_status & 0xff) << 8; 74 break; 75 case CLD_KILLED: 76 if (status) 77 *status = info.si_status & 0x7f; 78 break; 79 case CLD_DUMPED: 80 if (status) 81 *status = (info.si_status & 0x7f) | 0x80; 82 break; 83 case CLD_STOPPED: 84 case CLD_TRAPPED: 85 if (status) 86 *status = (info.si_status << 8) + 0x7f; 87 break; 88 case CLD_CONTINUED: 89 if (status) 90 *status = 0xffff; 91 break; 92 default: 93 return -1; 94 } 95 96 return info.si_pid; 97 } 98 99 static __attribute__((unused)) 100 pid_t wait(int *status) 101 { 102 return waitpid(-1, status, 0); 103 } 104 105 #endif /* _NOLIBC_SYS_WAIT_H */ 106