1/* Implementation of waitid. Hurd version.
2 Copyright (C) 1997-2024 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
28int
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 return __hurd_fail (EINVAL);
56 }
57
58 /* Technically we're supposed to return EFAULT if infop is bogus,
59 but that would involve mucking with signals, which is
60 too much hassle. User will have to deal with SIGSEGV/SIGBUS.
61 We just check for a null pointer. */
62
63 if (infop == NULL)
64 return __hurd_fail (EFAULT);
65
66 cancel_oldtype = LIBC_CANCEL_ASYNC();
67#if HURD_INTERFACE_VERSION >= 20201227
68 err = __USEPORT_CANCEL (PROC, __proc_waitid (port, pid, options,
69 &status, &sigcode,
70 &ignored, &child));
71 if (err == MIG_BAD_ID || err == EOPNOTSUPP)
72#endif
73 err = __USEPORT_CANCEL (PROC, __proc_wait (port, pid, options,
74 &status, &sigcode,
75 &ignored, &child));
76 LIBC_CANCEL_RESET (cancel_oldtype);
77
78 if (err == EAGAIN)
79 {
80 /* POSIX.1-2008, Technical Corrigendum 1 XSH/TC1-2008/0713 [153] states
81 that if waitid returns because WNOHANG was specified and status is
82 not available for any process specified by idtype and id, then the
83 si_signo and si_pid members of the structure pointed to by infop
84 shall be set to zero. */
85 infop->si_signo = 0;
86 infop->si_code = 0;
87 return 0;
88 }
89
90 if (err != 0)
91 return __hurd_fail (err);
92
93 /* Decode the status field and set infop members... */
94 infop->si_signo = SIGCHLD;
95 infop->si_pid = child;
96 infop->si_errno = 0;
97
98 if (WIFEXITED (status))
99 {
100 infop->si_code = CLD_EXITED;
101 infop->si_status = WEXITSTATUS (status);
102 }
103 else if (WIFSIGNALED (status))
104 {
105 infop->si_code = WCOREDUMP (status) ? CLD_DUMPED : CLD_KILLED;
106 infop->si_status = WTERMSIG (status);
107 }
108 else if (WIFSTOPPED (status))
109 {
110 infop->si_code = CLD_STOPPED;
111 infop->si_status = WSTOPSIG (status);
112 }
113 else if (WIFCONTINUED (status))
114 {
115 infop->si_code = CLD_CONTINUED;
116 infop->si_status = SIGCONT;
117 }
118
119 return 0;
120}
121weak_alias (__waitid, waitid)
122strong_alias (__waitid, __libc_waitid)
123

source code of glibc/sysdeps/mach/hurd/waitid.c