| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Pseudo implementation of waitid. | 
|  | 2 | Copyright (C) 1997-2016 Free Software Foundation, Inc. | 
|  | 3 | This file is part of the GNU C Library. | 
|  | 4 | Contributed by Zack Weinberg <zack@rabi.phys.columbia.edu>, 1997. | 
|  | 5 |  | 
|  | 6 | The GNU C Library is free software; you can redistribute it and/or | 
|  | 7 | modify it under the terms of the GNU Lesser General Public | 
|  | 8 | License as published by the Free Software Foundation; either | 
|  | 9 | version 2.1 of the License, or (at your option) any later version. | 
|  | 10 |  | 
|  | 11 | The GNU C Library is distributed in the hope that it will be useful, | 
|  | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|  | 14 | Lesser General Public License for more details. | 
|  | 15 |  | 
|  | 16 | You should have received a copy of the GNU Lesser General Public | 
|  | 17 | License along with the GNU C Library; if not, see | 
|  | 18 | <http://www.gnu.org/licenses/>.  */ | 
|  | 19 |  | 
|  | 20 | #include <assert.h> | 
|  | 21 | #include <errno.h> | 
|  | 22 | #include <signal.h> | 
|  | 23 | #define __need_NULL | 
|  | 24 | #include <stddef.h> | 
|  | 25 | #include <sys/wait.h> | 
|  | 26 | #include <sys/types.h> | 
|  | 27 | #include <sysdep-cancel.h> | 
|  | 28 |  | 
|  | 29 |  | 
|  | 30 | #ifdef DO_WAITID | 
|  | 31 | # define OUR_WAITID DO_WAITID | 
|  | 32 | #elif !defined NO_DO_WAITID | 
|  | 33 | # define OUR_WAITID do_waitid | 
|  | 34 | #endif | 
|  | 35 |  | 
|  | 36 | #ifdef OUR_WAITID | 
|  | 37 | static int | 
|  | 38 | OUR_WAITID (idtype_t idtype, id_t id, siginfo_t *infop, int options) | 
|  | 39 | { | 
|  | 40 | pid_t pid, child; | 
|  | 41 | int status; | 
|  | 42 |  | 
|  | 43 | switch (idtype) | 
|  | 44 | { | 
|  | 45 | case P_PID: | 
|  | 46 | if(id <= 0) | 
|  | 47 | goto invalid; | 
|  | 48 | pid = (pid_t) id; | 
|  | 49 | break; | 
|  | 50 | case P_PGID: | 
|  | 51 | if (id < 0 || id == 1) | 
|  | 52 | goto invalid; | 
|  | 53 | pid = (pid_t) -id; | 
|  | 54 | break; | 
|  | 55 | case P_ALL: | 
|  | 56 | pid = -1; | 
|  | 57 | break; | 
|  | 58 | default: | 
|  | 59 | invalid: | 
|  | 60 | __set_errno (EINVAL); | 
|  | 61 | return -1; | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | /* Technically we're supposed to return EFAULT if infop is bogus, | 
|  | 65 | but that would involve mucking with signals, which is | 
|  | 66 | too much hassle.  User will have to deal with SIGSEGV/SIGBUS. | 
|  | 67 | We just check for a null pointer. */ | 
|  | 68 |  | 
|  | 69 | if (infop == NULL) | 
|  | 70 | { | 
|  | 71 | __set_errno (EFAULT); | 
|  | 72 | return -1; | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | /* This emulation using waitpid cannot support the waitid modes in which | 
|  | 76 | we do not reap the child, or match only stopped and not dead children.  */ | 
|  | 77 | if (0 | 
|  | 78 | #ifdef WNOWAIT | 
|  | 79 | || (options & WNOWAIT) | 
|  | 80 | #endif | 
|  | 81 | #ifdef WEXITED | 
|  | 82 | || ((options & (WEXITED|WSTOPPED|WCONTINUED)) | 
|  | 83 | != (WEXITED | (options & WUNTRACED))) | 
|  | 84 | #endif | 
|  | 85 | ) | 
|  | 86 | { | 
|  | 87 | __set_errno (ENOTSUP); | 
|  | 88 | return -1; | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | /* Note the waitid() is a cancellation point.  But since we call | 
|  | 92 | waitpid() which itself is a cancellation point we do not have | 
|  | 93 | to do anything here.  */ | 
|  | 94 | child = __waitpid (pid, &status, | 
|  | 95 | options | 
|  | 96 | #ifdef WEXITED | 
|  | 97 | &~ WEXITED | 
|  | 98 | #endif | 
|  | 99 | ); | 
|  | 100 |  | 
|  | 101 | if (child == -1) | 
|  | 102 | /* `waitpid' set `errno' for us.  */ | 
|  | 103 | return -1; | 
|  | 104 |  | 
|  | 105 | if (child == 0) | 
|  | 106 | { | 
|  | 107 | /* The WHOHANG bit in OPTIONS is set and there are children available | 
|  | 108 | but none has a status for us.  The XPG docs do not mention this | 
|  | 109 | case so we clear the `siginfo_t' struct and return successfully.  */ | 
|  | 110 | infop->si_signo = 0; | 
|  | 111 | infop->si_code = 0; | 
|  | 112 | return 0; | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | /* Decode the status field and set infop members... */ | 
|  | 116 | infop->si_signo = SIGCHLD; | 
|  | 117 | infop->si_pid = child; | 
|  | 118 | infop->si_errno = 0; | 
|  | 119 |  | 
|  | 120 | if (WIFEXITED (status)) | 
|  | 121 | { | 
|  | 122 | infop->si_code = CLD_EXITED; | 
|  | 123 | infop->si_status = WEXITSTATUS (status); | 
|  | 124 | } | 
|  | 125 | else if (WIFSIGNALED (status)) | 
|  | 126 | { | 
|  | 127 | infop->si_code = WCOREDUMP (status) ? CLD_DUMPED : CLD_KILLED; | 
|  | 128 | infop->si_status = WTERMSIG (status); | 
|  | 129 | } | 
|  | 130 | else if (WIFSTOPPED (status)) | 
|  | 131 | { | 
|  | 132 | infop->si_code = CLD_STOPPED; | 
|  | 133 | infop->si_status = WSTOPSIG (status); | 
|  | 134 | } | 
|  | 135 | #ifdef WIFCONTINUED | 
|  | 136 | else if (WIFCONTINUED (status)) | 
|  | 137 | { | 
|  | 138 | infop->si_code = CLD_CONTINUED; | 
|  | 139 | infop->si_status = SIGCONT; | 
|  | 140 | } | 
|  | 141 | #endif | 
|  | 142 | else | 
|  | 143 | /* Can't happen. */ | 
|  | 144 | assert (! "What?"); | 
|  | 145 |  | 
|  | 146 | return 0; | 
|  | 147 | } | 
|  | 148 | #endif | 
|  | 149 |  | 
|  | 150 |  | 
|  | 151 | int | 
|  | 152 | __waitid (idtype_t idtype, id_t id, siginfo_t *infop, int options) | 
|  | 153 | { | 
|  | 154 | if (SINGLE_THREAD_P) | 
|  | 155 | return do_waitid (idtype, id, infop, options); | 
|  | 156 |  | 
|  | 157 | int oldtype = LIBC_CANCEL_ASYNC (); | 
|  | 158 |  | 
|  | 159 | int result = do_waitid (idtype, id, infop, options); | 
|  | 160 |  | 
|  | 161 | LIBC_CANCEL_RESET (oldtype); | 
|  | 162 |  | 
|  | 163 | return result; | 
|  | 164 | } | 
|  | 165 | weak_alias (__waitid, waitid) | 
|  | 166 | strong_alias (__waitid, __libc_waitid) |