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