Update. Old logs are in ChangeLog.7.
[glibc.git] / sysdeps / posix / waitid.c
blob7bb3bbeb4790b84e91619521a60d89a0b0d85ea7
1 /* Pseudo implementation of waitid.
2 Copyright (C) 1997, 1998 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
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>
28 #include <assert.h>
30 int
31 waitid (idtype, id, infop, options)
32 idtype_t idtype;
33 id_t id;
34 siginfo_t *infop;
35 int options;
37 pid_t pid, child;
38 int status;
40 switch (idtype)
42 case P_PID:
43 if(id <= 0)
44 goto invalid;
45 pid = (pid_t) id;
46 break;
47 case P_PGID:
48 if (id < 0 || id == 1)
49 goto invalid;
50 pid = (pid_t) -id;
51 break;
52 case P_ALL:
53 pid = -1;
54 break;
55 default:
56 invalid:
57 __set_errno (EINVAL);
58 return -1;
61 /* Technically we're supposed to return EFAULT if infop is bogus,
62 but that would involve mucking with signals, which is
63 too much hassle. User will have to deal with SIGSEGV/SIGBUS.
64 We just check for a null pointer. */
66 if (infop == NULL)
68 __set_errno (EFAULT);
69 return -1;
72 child = __waitpid (pid, &status, options);
74 if (child == -1)
75 /* `waitpid' set `errno' for us. */
76 return -1;
78 if (child == 0)
80 /* The WHOHANG bit in OPTIONS is set and there are children available
81 but none has a status for us. The XPG docs do not mention this
82 case so we clear the `siginfo_t' struct and return succesfully. */
83 infop->si_signo = 0;
84 infop->si_code = 0;
85 return 0;
88 /* Decode the status field and set infop members... */
89 infop->si_signo = SIGCHLD;
90 infop->si_pid = child;
91 infop->si_errno = 0;
93 if (WIFEXITED (status))
95 infop->si_code = CLD_EXITED;
96 infop->si_status = WEXITSTATUS (status);
98 else if (WIFSIGNALED (status))
100 infop->si_code = WCOREDUMP (status) ? CLD_DUMPED : CLD_KILLED;
101 infop->si_status = WTERMSIG (status);
103 else if (WIFSTOPPED (status))
105 infop->si_code = CLD_STOPPED;
106 infop->si_status = WSTOPSIG (status);
108 #ifdef WIFCONTINUED
109 else if (WIFCONTINUED (status))
111 infop->si_code = CLD_CONTINUED;
112 infop->si_status = SIGCONT;
114 #endif
115 else
116 /* Can't happen. */
117 assert (! "What?");
119 return 0;