1 /* Implementation of waitid. Hurd version.
2 Copyright (C) 1997-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <errno.h>
20 #include <sys/types.h>
21 #include <sys/wait.h>
22 #include <stddef.h>
23 #include <hurd.h>
24 #include <hurd/port.h>
25 #include <hurd/version.h>
26 #include <sysdep-cancel.h>
27
28 int
__waitid(idtype_t idtype,id_t id,siginfo_t * infop,int options)29 __waitid (idtype_t idtype, id_t id, siginfo_t *infop, int options)
30 {
31 struct rusage ignored;
32 error_t err;
33 pid_t pid, child;
34 int sigcode;
35 int status;
36 int cancel_oldtype;
37
38 switch (idtype)
39 {
40 case P_PID:
41 if (id <= 0)
42 goto invalid;
43 pid = (pid_t) id;
44 break;
45 case P_PGID:
46 if (id < 0 || id == 1)
47 goto invalid;
48 pid = (pid_t) -id;
49 break;
50 case P_ALL:
51 pid = -1;
52 break;
53 default:
54 invalid:
55 __set_errno (EINVAL);
56 return -1;
57 }
58
59 /* Technically we're supposed to return EFAULT if infop is bogus,
60 but that would involve mucking with signals, which is
61 too much hassle. User will have to deal with SIGSEGV/SIGBUS.
62 We just check for a null pointer. */
63
64 if (infop == NULL)
65 {
66 __set_errno (EFAULT);
67 return -1;
68 }
69
70 cancel_oldtype = LIBC_CANCEL_ASYNC();
71 #if HURD_INTERFACE_VERSION >= 20201227
72 err = __USEPORT_CANCEL (PROC, __proc_waitid (port, pid, options,
73 &status, &sigcode,
74 &ignored, &child));
75 if (err == MIG_BAD_ID || err == EOPNOTSUPP)
76 #endif
77 err = __USEPORT_CANCEL (PROC, __proc_wait (port, pid, options,
78 &status, &sigcode,
79 &ignored, &child));
80 LIBC_CANCEL_RESET (cancel_oldtype);
81
82 if (err == EAGAIN)
83 {
84 /* POSIX.1-2008, Technical Corrigendum 1 XSH/TC1-2008/0713 [153] states
85 that if waitid returns because WNOHANG was specified and status is
86 not available for any process specified by idtype and id, then the
87 si_signo and si_pid members of the structure pointed to by infop
88 shall be set to zero. */
89 infop->si_signo = 0;
90 infop->si_code = 0;
91 return 0;
92 }
93
94 if (err != 0)
95 return __hurd_fail (err);
96
97 /* Decode the status field and set infop members... */
98 infop->si_signo = SIGCHLD;
99 infop->si_pid = child;
100 infop->si_errno = 0;
101
102 if (WIFEXITED (status))
103 {
104 infop->si_code = CLD_EXITED;
105 infop->si_status = WEXITSTATUS (status);
106 }
107 else if (WIFSIGNALED (status))
108 {
109 infop->si_code = WCOREDUMP (status) ? CLD_DUMPED : CLD_KILLED;
110 infop->si_status = WTERMSIG (status);
111 }
112 else if (WIFSTOPPED (status))
113 {
114 infop->si_code = CLD_STOPPED;
115 infop->si_status = WSTOPSIG (status);
116 }
117 else if (WIFCONTINUED (status))
118 {
119 infop->si_code = CLD_CONTINUED;
120 infop->si_status = SIGCONT;
121 }
122
123 return 0;
124 }
125 weak_alias (__waitid, waitid)
126 strong_alias (__waitid, __libc_waitid)
127