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