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