]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bin/pwait/pwait.c
- Be consistent with using sysexits(3) codes.
[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         errx(EX_USAGE, "usage: pwait [-t timeout] [-v] pid ...");
57 }
58
59 /*
60  * pwait - wait for processes to terminate
61  */
62 int
63 main(int argc, char *argv[])
64 {
65         struct itimerval itv;
66         int kq;
67         struct kevent *e;
68         int tflag, verbose;
69         int opt, nleft, n, i, duplicate, status;
70         long pid;
71         char *s, *end;
72         double timeout;
73
74         tflag = verbose = 0;
75         memset(&itv, 0, sizeof(itv));
76         while ((opt = getopt(argc, argv, "t:v")) != -1) {
77                 switch (opt) {
78                 case 't':
79                         tflag = 1;
80                         errno = 0;
81                         timeout = strtod(optarg, &end);
82                         if (end == optarg || errno == ERANGE ||
83                             timeout < 0)
84                                 errx(EX_DATAERR, "timeout value");
85                         switch(*end) {
86                         case 0:
87                         case 's':
88                                 break;
89                         case 'h':
90                                 timeout *= 60;
91                                 /* FALLTHROUGH */
92                         case 'm':
93                                 timeout *= 60;
94                                 break;
95                         default:
96                                 errx(EX_DATAERR, "timeout unit");
97                         }
98                         if (timeout > 100000000L)
99                                 errx(EX_DATAERR, "timeout value");
100                         itv.it_value.tv_sec = (time_t)timeout;
101                         timeout -= (time_t)timeout;
102                         itv.it_value.tv_usec =
103                             (suseconds_t)(timeout * 1000000UL);
104                         break;
105                 case 'v':
106                         verbose = 1;
107                         break;
108                 default:
109                         usage();
110                         /* NOTREACHED */
111                 }
112         }
113
114         argc -= optind;
115         argv += optind;
116
117         if (argc == 0)
118                 usage();
119
120         kq = kqueue();
121         if (kq == -1)
122                 err(EX_OSERR, "kqueue");
123
124         e = malloc((argc + tflag) * sizeof(struct kevent));
125         if (e == NULL)
126                 err(EX_OSERR, "malloc");
127         nleft = 0;
128         for (n = 0; n < argc; n++) {
129                 s = argv[n];
130                 if (!strncmp(s, "/proc/", 6)) /* Undocumented Solaris compat */
131                         s += 6;
132                 errno = 0;
133                 pid = strtol(s, &end, 10);
134                 if (pid < 0 || *end != '\0' || errno != 0) {
135                         warnx("%s: bad process id", s);
136                         continue;
137                 }
138                 duplicate = 0;
139                 for (i = 0; i < nleft; i++)
140                         if (e[i].ident == (uintptr_t)pid)
141                                 duplicate = 1;
142                 if (!duplicate) {
143                         EV_SET(e + nleft, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT,
144                             0, NULL);
145                         if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1)
146                                 warn("%ld", pid);
147                         else
148                                 nleft++;
149                 }
150         }
151
152         if (tflag) {
153                 /*
154                  * Explicitly detect SIGALRM so that an exit status of 124
155                  * can be returned rather than 142.
156                  */
157                 EV_SET(e + nleft, SIGALRM, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
158                 if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1)
159                         err(EX_OSERR, "kevent");
160                 /* Ignore SIGALRM to not interrupt kevent(2). */
161                 signal(SIGALRM, SIG_IGN);
162                 if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
163                         err(EX_OSERR, "setitimer");
164         }
165         while (nleft > 0) {
166                 n = kevent(kq, NULL, 0, e, nleft + tflag, NULL);
167                 if (n == -1)
168                         err(EX_OSERR, "kevent");
169                 for (i = 0; i < n; i++) {
170                         if (e[i].filter == EVFILT_SIGNAL) {
171                                 if (verbose)
172                                         printf("timeout\n");
173                                 exit(124);
174                         }
175                         if (verbose) {
176                                 status = e[i].data;
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                         --nleft;
190                 }
191         }
192
193         exit(EX_OK);
194 }