2 * Copyright (c) 2004-2009, Jilles Tjoelker
5 * Redistribution and use in source and binary forms, with
6 * or without modification, are permitted provided that the
7 * following conditions are met:
9 * 1. Redistributions of source code must retain the above
10 * copyright notice, this list of conditions and the
11 * following disclaimer.
12 * 2. Redistributions in binary form must reproduce the
13 * above copyright notice, this list of conditions and
14 * the following disclaimer in the documentation and/or
15 * other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/types.h>
38 #include <sys/event.h>
56 fprintf(stderr, "usage: pwait [-t timeout] [-ov] pid ...\n");
61 * pwait - wait for processes to terminate
64 main(int argc, char *argv[])
68 int oflag, tflag, verbose;
69 int i, kq, n, nleft, opt, status;
77 memset(&itv, 0, sizeof(itv));
79 while ((opt = getopt(argc, argv, "ot:v")) != -1) {
87 timeout = strtod(optarg, &end);
88 if (end == optarg || errno == ERANGE || timeout < 0) {
89 errx(EX_DATAERR, "timeout value");
102 errx(EX_DATAERR, "timeout unit");
104 if (timeout > 100000000L) {
105 errx(EX_DATAERR, "timeout value");
107 itv.it_value.tv_sec = (time_t)timeout;
108 timeout -= (time_t)timeout;
109 itv.it_value.tv_usec =
110 (suseconds_t)(timeout * 1000000UL);
130 err(EX_OSERR, "kqueue");
133 e = malloc((argc + tflag) * sizeof(struct kevent));
135 err(EX_OSERR, "malloc");
138 for (n = 0; n < argc; n++) {
140 /* Undocumented Solaris compat */
141 if (!strncmp(s, "/proc/", 6)) {
145 pid = strtol(s, &end, 10);
146 if (pid < 0 || *end != '\0' || errno != 0) {
147 warnx("%s: bad process id", s);
150 for (i = 0; i < nleft; i++) {
151 if (e[i].ident == (uintptr_t)pid) {
159 EV_SET(e + nleft, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL);
160 if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1) {
170 if (nleft > 0 && tflag) {
172 * Explicitly detect SIGALRM so that an exit status of 124
173 * can be returned rather than 142.
175 EV_SET(e + nleft, SIGALRM, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
176 if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1) {
177 err(EX_OSERR, "kevent");
179 /* Ignore SIGALRM to not interrupt kevent(2). */
180 signal(SIGALRM, SIG_IGN);
181 if (setitimer(ITIMER_REAL, &itv, NULL) == -1) {
182 err(EX_OSERR, "setitimer");
186 n = kevent(kq, NULL, 0, e, nleft + tflag, NULL);
188 err(EX_OSERR, "kevent");
190 for (i = 0; i < n; i++) {
191 if (e[i].filter == EVFILT_SIGNAL) {
199 if (WIFEXITED(status)) {
200 printf("%ld: exited with status %d.\n",
202 WEXITSTATUS(status));
203 } else if (WIFSIGNALED(status)) {
204 printf("%ld: killed by signal %d.\n",
208 printf("%ld: terminated.\n",