xref: /freebsd/bin/pwait/pwait.c (revision cfe30d02adda7c3b5c76156ac52d50d8cab325d9)
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 [-v] 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 	int kq;
67 	struct kevent *e;
68 	int verbose = 0;
69 	int opt, nleft, n, i, duplicate, status;
70 	long pid;
71 	char *s, *end;
72 
73 	while ((opt = getopt(argc, argv, "v")) != -1) {
74 		switch (opt) {
75 		case 'v':
76 			verbose = 1;
77 			break;
78 		default:
79 			usage();
80 			/* NOTREACHED */
81 		}
82 	}
83 
84 	argc -= optind;
85 	argv += optind;
86 
87 	if (argc == 0)
88 		usage();
89 
90 	kq = kqueue();
91 	if (kq == -1)
92 		err(1, "kqueue");
93 
94 	e = malloc(argc * sizeof(struct kevent));
95 	if (e == NULL)
96 		err(1, "malloc");
97 	nleft = 0;
98 	for (n = 0; n < argc; n++) {
99 		s = argv[n];
100 		if (!strncmp(s, "/proc/", 6)) /* Undocumented Solaris compat */
101 			s += 6;
102 		errno = 0;
103 		pid = strtol(s, &end, 10);
104 		if (pid < 0 || *end != '\0' || errno != 0) {
105 			warnx("%s: bad process id", s);
106 			continue;
107 		}
108 		duplicate = 0;
109 		for (i = 0; i < nleft; i++)
110 			if (e[i].ident == (uintptr_t)pid)
111 				duplicate = 1;
112 		if (!duplicate) {
113 			EV_SET(e + nleft, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT,
114 			    0, NULL);
115 			if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1)
116 				warn("%ld", pid);
117 			else
118 				nleft++;
119 		}
120 	}
121 
122 	while (nleft > 0) {
123 		n = kevent(kq, NULL, 0, e, nleft, NULL);
124 		if (n == -1)
125 			err(1, "kevent");
126 		if (verbose)
127 			for (i = 0; i < n; i++) {
128 				status = e[i].data;
129 				if (WIFEXITED(status))
130 					printf("%ld: exited with status %d.\n",
131 					    (long)e[i].ident,
132 					    WEXITSTATUS(status));
133 				else if (WIFSIGNALED(status))
134 					printf("%ld: killed by signal %d.\n",
135 					    (long)e[i].ident,
136 					    WTERMSIG(status));
137 				else
138 					printf("%ld: terminated.\n",
139 					    (long)e[i].ident);
140 			}
141 		nleft -= n;
142 	}
143 
144 	exit(EX_OK);
145 }
146