xref: /freebsd/bin/pwait/pwait.c (revision f61e92ca5a23450bc28169bbdd71d7674df98c19)
1 /*-
2  * Copyright (c) 2004-2009, Jilles Tjoelker
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with
6  * or without modification, are permitted provided that the
7  * following conditions are met:
8  *
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.
16  *
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
31  * OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/types.h>
38 #include <sys/event.h>
39 #include <sys/time.h>
40 #include <sys/wait.h>
41 
42 #include <err.h>
43 #include <errno.h>
44 #include <signal.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <sysexits.h>
49 #include <unistd.h>
50 
51 static void
52 usage(void)
53 {
54 
55 	fprintf(stderr, "usage: pwait [-t timeout] [-ov] pid ...\n");
56 	exit(EX_USAGE);
57 }
58 
59 /*
60  * pwait - wait for processes to terminate
61  */
62 int
63 main(int argc, char *argv[])
64 {
65 	struct itimerval itv;
66 	struct kevent *e;
67 	int oflag, tflag, verbose;
68 	int i, kq, n, nleft, opt, status;
69 	long pid;
70 	char *end, *s;
71 	double timeout;
72 
73 	oflag = 0;
74 	tflag = 0;
75 	verbose = 0;
76 	memset(&itv, 0, sizeof(itv));
77 
78 	while ((opt = getopt(argc, argv, "ot:v")) != -1) {
79 		switch (opt) {
80 		case 'o':
81 			oflag = 1;
82 			break;
83 		case 't':
84 			tflag = 1;
85 			errno = 0;
86 			timeout = strtod(optarg, &end);
87 			if (end == optarg || errno == ERANGE || timeout < 0) {
88 				errx(EX_DATAERR, "timeout value");
89 			}
90 			switch(*end) {
91 			case 0:
92 			case 's':
93 				break;
94 			case 'h':
95 				timeout *= 60;
96 				/* FALLTHROUGH */
97 			case 'm':
98 				timeout *= 60;
99 				break;
100 			default:
101 				errx(EX_DATAERR, "timeout unit");
102 			}
103 			if (timeout > 100000000L) {
104 				errx(EX_DATAERR, "timeout value");
105 			}
106 			itv.it_value.tv_sec = (time_t)timeout;
107 			timeout -= (time_t)timeout;
108 			itv.it_value.tv_usec =
109 			    (suseconds_t)(timeout * 1000000UL);
110 			break;
111 		case 'v':
112 			verbose = 1;
113 			break;
114 		default:
115 			usage();
116 			/* NOTREACHED */
117 		}
118 	}
119 
120 	argc -= optind;
121 	argv += optind;
122 
123 	if (argc == 0) {
124 		usage();
125 	}
126 
127 	kq = kqueue();
128 	if (kq == -1) {
129 		err(EX_OSERR, "kqueue");
130 	}
131 
132 	e = malloc((argc + tflag) * sizeof(struct kevent));
133 	if (e == NULL) {
134 		err(EX_OSERR, "malloc");
135 	}
136 	nleft = 0;
137 	for (n = 0; n < argc; n++) {
138 		s = argv[n];
139 		/* Undocumented Solaris compat */
140 		if (!strncmp(s, "/proc/", 6)) {
141 			s += 6;
142 		}
143 		errno = 0;
144 		pid = strtol(s, &end, 10);
145 		if (pid < 0 || *end != '\0' || errno != 0) {
146 			warnx("%s: bad process id", s);
147 			continue;
148 		}
149 		if (pid == getpid()) {
150 			warnx("%s: skipping my own pid", s);
151 			continue;
152 		}
153 		for (i = 0; i < nleft; i++) {
154 			if (e[i].ident == (uintptr_t)pid) {
155 				break;
156 			}
157 		}
158 		if (i < nleft) {
159 			/* Duplicate. */
160 			continue;
161 		}
162 		EV_SET(e + nleft, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL);
163 		if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1) {
164 			warn("%ld", pid);
165 			if (oflag) {
166 				exit(EX_OK);
167 			}
168 		} else {
169 			nleft++;
170 		}
171 	}
172 
173 	if (nleft > 0 && tflag) {
174 		/*
175 		 * Explicitly detect SIGALRM so that an exit status of 124
176 		 * can be returned rather than 142.
177 		 */
178 		EV_SET(e + nleft, SIGALRM, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
179 		if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1) {
180 			err(EX_OSERR, "kevent");
181 		}
182 		/* Ignore SIGALRM to not interrupt kevent(2). */
183 		signal(SIGALRM, SIG_IGN);
184 		if (setitimer(ITIMER_REAL, &itv, NULL) == -1) {
185 			err(EX_OSERR, "setitimer");
186 		}
187 	}
188 	while (nleft > 0) {
189 		n = kevent(kq, NULL, 0, e, nleft + tflag, NULL);
190 		if (n == -1) {
191 			err(EX_OSERR, "kevent");
192 		}
193 		for (i = 0; i < n; i++) {
194 			if (e[i].filter == EVFILT_SIGNAL) {
195 				if (verbose) {
196 					printf("timeout\n");
197 				}
198 				exit(124);
199 			}
200 			if (verbose) {
201 				status = e[i].data;
202 				if (WIFEXITED(status)) {
203 					printf("%ld: exited with status %d.\n",
204 					    (long)e[i].ident,
205 					    WEXITSTATUS(status));
206 				} else if (WIFSIGNALED(status)) {
207 					printf("%ld: killed by signal %d.\n",
208 					    (long)e[i].ident,
209 					    WTERMSIG(status));
210 				} else {
211 					printf("%ld: terminated.\n",
212 					    (long)e[i].ident);
213 				}
214 			}
215 			if (oflag) {
216 				exit(EX_OK);
217 			}
218 			--nleft;
219 		}
220 	}
221 
222 	exit(EX_OK);
223 }
224