* sysdeps/mach/hurd/i386/tls.h (THREAD_DTV): Changed type of _DTV
[glibc.git] / sysdeps / posix / waitid.c
blob7d65e42fdc5c258a7fe3dfa58d6eddccb094ed52
1 /* Pseudo implementation of waitid.
2 Copyright (C) 1997, 1998, 2002, 2003, 2004 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.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <assert.h>
22 #include <errno.h>
23 #include <signal.h>
24 #define __need_NULL
25 #include <stddef.h>
26 #include <sys/wait.h>
27 #include <sys/types.h>
28 #include <sysdep-cancel.h>
31 #ifdef DO_WAITID
32 # define OUR_WAITID DO_WAITID
33 #elif !defined NO_DO_WAITID
34 # define OUR_WAITID do_waitid
35 #endif
37 #ifdef OUR_WAITID
38 static int
39 OUR_WAITID (idtype_t idtype, id_t id, siginfo_t *infop, int options)
41 pid_t pid, child;
42 int status;
44 switch (idtype)
46 case P_PID:
47 if(id <= 0)
48 goto invalid;
49 pid = (pid_t) id;
50 break;
51 case P_PGID:
52 if (id < 0 || id == 1)
53 goto invalid;
54 pid = (pid_t) -id;
55 break;
56 case P_ALL:
57 pid = -1;
58 break;
59 default:
60 invalid:
61 __set_errno (EINVAL);
62 return -1;
65 /* Technically we're supposed to return EFAULT if infop is bogus,
66 but that would involve mucking with signals, which is
67 too much hassle. User will have to deal with SIGSEGV/SIGBUS.
68 We just check for a null pointer. */
70 if (infop == NULL)
72 __set_errno (EFAULT);
73 return -1;
76 /* This emulation using waitpid cannot support the waitid modes in which
77 we do not reap the child, or match only stopped and not dead children. */
78 if (0
79 #ifdef WNOWAIT
80 || (options & WNOWAIT)
81 #endif
82 #ifdef WEXITED
83 || ((options & (WEXITED|WSTOPPED|WCONTINUED))
84 != (WEXITED | (options & WUNTRACED)))
85 #endif
88 __set_errno (ENOTSUP);
89 return -1;
92 /* Note the waitid() is a cancellation point. But since we call
93 waitpid() which itself is a cancellation point we do not have
94 to do anything here. */
95 child = __waitpid (pid, &status,
96 options
97 #ifdef WEXITED
98 &~ WEXITED
99 #endif
102 if (child == -1)
103 /* `waitpid' set `errno' for us. */
104 return -1;
106 if (child == 0)
108 /* The WHOHANG bit in OPTIONS is set and there are children available
109 but none has a status for us. The XPG docs do not mention this
110 case so we clear the `siginfo_t' struct and return successfully. */
111 infop->si_signo = 0;
112 infop->si_code = 0;
113 return 0;
116 /* Decode the status field and set infop members... */
117 infop->si_signo = SIGCHLD;
118 infop->si_pid = child;
119 infop->si_errno = 0;
121 if (WIFEXITED (status))
123 infop->si_code = CLD_EXITED;
124 infop->si_status = WEXITSTATUS (status);
126 else if (WIFSIGNALED (status))
128 infop->si_code = WCOREDUMP (status) ? CLD_DUMPED : CLD_KILLED;
129 infop->si_status = WTERMSIG (status);
131 else if (WIFSTOPPED (status))
133 infop->si_code = CLD_STOPPED;
134 infop->si_status = WSTOPSIG (status);
136 #ifdef WIFCONTINUED
137 else if (WIFCONTINUED (status))
139 infop->si_code = CLD_CONTINUED;
140 infop->si_status = SIGCONT;
142 #endif
143 else
144 /* Can't happen. */
145 assert (! "What?");
147 return 0;
149 #endif
153 __waitid (idtype, id, infop, options)
154 idtype_t idtype;
155 id_t id;
156 siginfo_t *infop;
157 int options;
159 if (SINGLE_THREAD_P)
160 return do_waitid (idtype, id, infop, options);
162 int oldtype = LIBC_CANCEL_ASYNC ();
164 int result = do_waitid (idtype, id, infop, options);
166 LIBC_CANCEL_RESET (oldtype);
168 return result;
170 weak_alias (__waitid, waitid)
171 strong_alias (__waitid, __libc_waitid)