]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mach/hurd/waitid.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / sysdeps / mach / hurd / waitid.c
1 /* Implementation of waitid. Hurd version.
2 Copyright (C) 1997-2021 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 <https://www.gnu.org/licenses/>. */
19
20 #include <errno.h>
21 #include <sys/types.h>
22 #include <sys/wait.h>
23 #include <stddef.h>
24 #include <hurd.h>
25 #include <hurd/port.h>
26 #include <hurd/version.h>
27 #include <sysdep-cancel.h>
28
29 int
30 __waitid (idtype_t idtype, id_t id, siginfo_t *infop, int options)
31 {
32 struct rusage ignored;
33 error_t err;
34 pid_t pid, child;
35 int sigcode;
36 int status;
37 int cancel_oldtype;
38
39 switch (idtype)
40 {
41 case P_PID:
42 if (id <= 0)
43 goto invalid;
44 pid = (pid_t) id;
45 break;
46 case P_PGID:
47 if (id < 0 || id == 1)
48 goto invalid;
49 pid = (pid_t) -id;
50 break;
51 case P_ALL:
52 pid = -1;
53 break;
54 default:
55 invalid:
56 __set_errno (EINVAL);
57 return -1;
58 }
59
60 /* Technically we're supposed to return EFAULT if infop is bogus,
61 but that would involve mucking with signals, which is
62 too much hassle. User will have to deal with SIGSEGV/SIGBUS.
63 We just check for a null pointer. */
64
65 if (infop == NULL)
66 {
67 __set_errno (EFAULT);
68 return -1;
69 }
70
71 cancel_oldtype = LIBC_CANCEL_ASYNC();
72 #if HURD_INTERFACE_VERSION >= 20201227
73 err = __USEPORT_CANCEL (PROC, __proc_waitid (port, pid, options,
74 &status, &sigcode,
75 &ignored, &child));
76 if (err == MIG_BAD_ID || err == EOPNOTSUPP)
77 #endif
78 err = __USEPORT_CANCEL (PROC, __proc_wait (port, pid, options,
79 &status, &sigcode,
80 &ignored, &child));
81 LIBC_CANCEL_RESET (cancel_oldtype);
82
83 if (err == EAGAIN)
84 {
85 /* POSIX.1-2008, Technical Corrigendum 1 XSH/TC1-2008/0713 [153] states
86 that if waitid returns because WNOHANG was specified and status is
87 not available for any process specified by idtype and id, then the
88 si_signo and si_pid members of the structure pointed to by infop
89 shall be set to zero. */
90 infop->si_signo = 0;
91 infop->si_code = 0;
92 return 0;
93 }
94
95 if (err != 0)
96 return __hurd_fail (err);
97
98 /* Decode the status field and set infop members... */
99 infop->si_signo = SIGCHLD;
100 infop->si_pid = child;
101 infop->si_errno = 0;
102
103 if (WIFEXITED (status))
104 {
105 infop->si_code = CLD_EXITED;
106 infop->si_status = WEXITSTATUS (status);
107 }
108 else if (WIFSIGNALED (status))
109 {
110 infop->si_code = WCOREDUMP (status) ? CLD_DUMPED : CLD_KILLED;
111 infop->si_status = WTERMSIG (status);
112 }
113 else if (WIFSTOPPED (status))
114 {
115 infop->si_code = CLD_STOPPED;
116 infop->si_status = WSTOPSIG (status);
117 }
118 else if (WIFCONTINUED (status))
119 {
120 infop->si_code = CLD_CONTINUED;
121 infop->si_status = SIGCONT;
122 }
123
124 return 0;
125 }
126 weak_alias (__waitid, waitid)
127 strong_alias (__waitid, __libc_waitid)