* sysdeps/m68k/Makefile (CFLAGS-.oS): Append -fPIC.
[glibc/pb-stable.git] / sysdeps / posix / waitid.c
blob679d97d2034d48ed6766c39618391b00f1a22be6
1 /* Pseudo implementation of waitid.
2 Copyright (C) 1997, 1998, 2002 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 static int
32 do_waitid (idtype_t idtype, id_t id, siginfo_t *infop, int options)
34 pid_t pid, child;
35 int status;
37 switch (idtype)
39 case P_PID:
40 if(id <= 0)
41 goto invalid;
42 pid = (pid_t) id;
43 break;
44 case P_PGID:
45 if (id < 0 || id == 1)
46 goto invalid;
47 pid = (pid_t) -id;
48 break;
49 case P_ALL:
50 pid = -1;
51 break;
52 default:
53 invalid:
54 __set_errno (EINVAL);
55 return -1;
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. */
63 if (infop == NULL)
65 __set_errno (EFAULT);
66 return -1;
69 child = __waitpid (pid, &status, options);
71 if (child == -1)
72 /* `waitpid' set `errno' for us. */
73 return -1;
75 if (child == 0)
77 /* The WHOHANG bit in OPTIONS is set and there are children available
78 but none has a status for us. The XPG docs do not mention this
79 case so we clear the `siginfo_t' struct and return successfully. */
80 infop->si_signo = 0;
81 infop->si_code = 0;
82 return 0;
85 /* Decode the status field and set infop members... */
86 infop->si_signo = SIGCHLD;
87 infop->si_pid = child;
88 infop->si_errno = 0;
90 if (WIFEXITED (status))
92 infop->si_code = CLD_EXITED;
93 infop->si_status = WEXITSTATUS (status);
95 else if (WIFSIGNALED (status))
97 infop->si_code = WCOREDUMP (status) ? CLD_DUMPED : CLD_KILLED;
98 infop->si_status = WTERMSIG (status);
100 else if (WIFSTOPPED (status))
102 infop->si_code = CLD_STOPPED;
103 infop->si_status = WSTOPSIG (status);
105 #ifdef WIFCONTINUED
106 else if (WIFCONTINUED (status))
108 infop->si_code = CLD_CONTINUED;
109 infop->si_status = SIGCONT;
111 #endif
112 else
113 /* Can't happen. */
114 assert (! "What?");
116 return 0;
121 __waitid (idtype, id, infop, options)
122 idtype_t idtype;
123 id_t id;
124 siginfo_t *infop;
125 int options;
127 if (SINGLE_THREAD_P)
128 return do_waitid (idtype, id, infop, options);
130 int oldtype = LIBC_CANCEL_ASYNC ();
132 int result = do_waitid (idtype, id, infop, options);
134 LIBC_CANCEL_RESET (oldtype);
136 return result;
138 weak_alias (__waitid, waitid)
139 strong_alias (__waitid, __libc_waitid)