xref: /netbsd/usr.bin/pwait/pwait.c (revision bda13abd)
1 /*	$NetBSD: pwait.c,v 1.4 2015/03/04 13:31:53 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.4 2015/03/04 13:31:53 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 [-sv] [-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;
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, "st:v")) != -1) {
84 		switch (opt) {
85 		case 's':
86 			childstatus = 1;
87 			break;
88 		case 't':
89 			timeout = atof(optarg);
90 			if (timeout < 0)
91 				timeout = 0;
92 			break;
93 		case 'v':
94 			verbose = 1;
95 			break;
96 		default:
97 			usage();
98 			/* NOTREACHED */
99 		}
100 	}
101 
102 	argc -= optind;
103 	argv += optind;
104 
105 	if (argc == 0)
106 		usage();
107 
108 	if (timeout != 0) {
109 		ts.tv_sec = (time_t)timeout;
110 		timeout -= (double)ts.tv_sec;
111 		ts.tv_nsec = (long)(timeout * 1000000000L);
112 		while (ts.tv_nsec < 0) {
113 			ts.tv_sec--;
114 			ts.tv_nsec += 1000000000L;
115 		}
116 		tsp = &ts;
117 	} else
118 		tsp = NULL;
119 
120 	kq = kqueue();
121 	if (kq == -1)
122 		err(EXIT_FAILURE, "kqueue");
123 
124 	e = malloc((size_t)argc * sizeof(*e));
125 	if (e == NULL)
126 		err(EXIT_FAILURE, "malloc");
127 	nleft = 0;
128 	for (n = 0; n < (size_t)argc; n++) {
129 		long pidl;
130 		s = argv[n];
131 		if (!strncmp(s, "/proc/", 6)) /* Undocumented Solaris compat */
132 			s += 6;
133 		errno = 0;
134 		pidl = strtol(s, &end, 10);
135 		if (pidl < 0 || *end != '\0' || errno != 0) {
136 			warnx("%s: bad process id", s);
137 			continue;
138 		}
139 		pid = (pid_t)pidl;
140 		duplicate = 0;
141 		for (i = 0; i < nleft; i++)
142 			if (e[i].ident == (uintptr_t)pid)
143 				duplicate = 1;
144 		if (!duplicate) {
145 			EV_SET(e + nleft, (uintptr_t)pid, EVFILT_PROC, EV_ADD,
146 			    NOTE_EXIT, 0, 0);
147 			if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1)
148 				warn("%jd", (intmax_t)pid);
149 			else
150 				nleft++;
151 		}
152 	}
153 
154 	while (nleft > 0) {
155 		int rv;
156 
157 		switch (rv = kevent(kq, NULL, 0, e, nleft, tsp)) {
158 		case 0:
159 			if (verbose)
160 				printf("timed out\n");
161 			if (childstatus)
162 				return 255;
163 			return EX_OK;
164 		case -1:
165 			err(EXIT_FAILURE, "kevent");
166 		default:
167 			n = (size_t)rv;
168 			break;
169 		}
170 
171 		for (i = 0; i < n; i++) {
172 			status = (int)e[i].data;
173 			if (verbose) {
174 				if (WIFEXITED(status))
175 					printf("%ld: exited with status %d.\n",
176 					    (long)e[i].ident,
177 					    WEXITSTATUS(status));
178 				else if (WIFSIGNALED(status))
179 					printf("%ld: killed by signal %d.\n",
180 					    (long)e[i].ident,
181 					    WTERMSIG(status));
182 				else
183 					printf("%ld: terminated.\n",
184 					    (long)e[i].ident);
185 			}
186 			if (childstatus)
187 				return status;
188 		}
189 		nleft -= n;
190 	}
191 
192 	return EX_OK;
193 }
194