xref: /netbsd/usr.bin/pwait/pwait.c (revision 337a4b74)
1 /*	$NetBSD: pwait.c,v 1.5 2015/03/04 16:36:12 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2004-2009, Jilles Tjoelker
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with
8  * or without modification, are permitted provided that the
9  * following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above
12  *    copyright notice, this list of conditions and the
13  *    following disclaimer.
14  * 2. Redistributions in binary form must reproduce the
15  *    above copyright notice, this list of conditions and
16  *    the following disclaimer in the documentation and/or
17  *    other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
20  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
21  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33  * OF SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifdef __FBSDID
38 __FBSDID("$FreeBSD: head/bin/pwait/pwait.c 245506 2013-01-16 18:15:25Z delphij $");
39 #endif
40 __RCSID("$NetBSD: pwait.c,v 1.5 2015/03/04 16:36:12 christos Exp $");
41 
42 #include <sys/types.h>
43 #include <sys/event.h>
44 #include <sys/time.h>
45 #include <sys/wait.h>
46 
47 #include <err.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <signal.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <sysexits.h>
55 #include <unistd.h>
56 
57 static __dead void
58 usage(void)
59 {
60 
61 	fprintf(stderr, "Usage: %s [-isv] [-t <timeout>] <pid> ...\n",
62 	    getprogname());
63 	exit(EX_USAGE);
64 }
65 
66 /*
67  * pwait - wait for processes to terminate
68  */
69 int
70 main(int argc, char *argv[])
71 {
72 	int kq;
73 	struct kevent *e;
74 	int verbose = 0, childstatus = 0;
75 	int opt, duplicate, status, immediately = 0;
76 	size_t nleft, n, i;
77 	pid_t pid;
78 	char *s, *end;
79 	double timeout = 0;
80 	struct timespec ts, *tsp;
81 
82 	setprogname(argv[0]);
83 	while ((opt = getopt(argc, argv, "ist:v")) != -1) {
84 		switch (opt) {
85 		case 'i':
86 			immediately = 1;
87 			break;
88 		case 's':
89 			childstatus = 1;
90 			break;
91 		case 't':
92 			timeout = atof(optarg);
93 			if (timeout < 0)
94 				timeout = 0;
95 			break;
96 		case 'v':
97 			verbose = 1;
98 			break;
99 		default:
100 			usage();
101 			/* NOTREACHED */
102 		}
103 	}
104 
105 	argc -= optind;
106 	argv += optind;
107 
108 	if (argc == 0)
109 		usage();
110 
111 	if (timeout != 0) {
112 		ts.tv_sec = (time_t)timeout;
113 		timeout -= (double)ts.tv_sec;
114 		ts.tv_nsec = (long)(timeout * 1000000000L);
115 		while (ts.tv_nsec < 0) {
116 			ts.tv_sec--;
117 			ts.tv_nsec += 1000000000L;
118 		}
119 		tsp = &ts;
120 	} else
121 		tsp = NULL;
122 
123 	kq = kqueue();
124 	if (kq == -1)
125 		err(EXIT_FAILURE, "kqueue");
126 
127 	e = malloc((size_t)argc * sizeof(*e));
128 	if (e == NULL)
129 		err(EXIT_FAILURE, "malloc");
130 	nleft = 0;
131 	for (n = 0; n < (size_t)argc; n++) {
132 		long pidl;
133 		s = argv[n];
134 		if (!strncmp(s, "/proc/", 6)) /* Undocumented Solaris compat */
135 			s += 6;
136 		errno = 0;
137 		pidl = strtol(s, &end, 10);
138 		if (pidl < 0 || *end != '\0' || errno != 0) {
139 			warnx("%s: bad process id", s);
140 			continue;
141 		}
142 		pid = (pid_t)pidl;
143 		duplicate = 0;
144 		for (i = 0; i < nleft; i++)
145 			if (e[i].ident == (uintptr_t)pid)
146 				duplicate = 1;
147 		if (!duplicate) {
148 			EV_SET(e + nleft, (uintptr_t)pid, EVFILT_PROC, EV_ADD,
149 			    NOTE_EXIT, 0, 0);
150 			if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1)
151 				warn("%jd", (intmax_t)pid);
152 			else
153 				nleft++;
154 		}
155 	}
156 
157 	while (nleft > 0) {
158 		int rv;
159 
160 		switch (rv = kevent(kq, NULL, 0, e, nleft, tsp)) {
161 		case 0:
162 			if (verbose)
163 				printf("timed out\n");
164 			if (childstatus)
165 				return 255;
166 			return EX_OK;
167 		case -1:
168 			err(EXIT_FAILURE, "kevent");
169 		default:
170 			n = (size_t)rv;
171 			break;
172 		}
173 
174 		for (i = 0; i < n; i++) {
175 			status = (int)e[i].data;
176 			if (verbose) {
177 				if (WIFEXITED(status))
178 					printf("%ld: exited with status %d.\n",
179 					    (long)e[i].ident,
180 					    WEXITSTATUS(status));
181 				else if (WIFSIGNALED(status))
182 					printf("%ld: killed by signal %d.\n",
183 					    (long)e[i].ident,
184 					    WTERMSIG(status));
185 				else
186 					printf("%ld: terminated.\n",
187 					    (long)e[i].ident);
188 			}
189 			if (childstatus)
190 				return status;
191 		}
192 		nleft -= n;
193 		// n != 0 here, belt-n-suspenders...
194 		if (immediately && n)
195 			break;
196 	}
197 
198 	return EX_OK;
199 }
200