xref: /dragonfly/usr.bin/pwait/pwait.c (revision 9348a738)
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  * $FreeBSD: head/bin/pwait/pwait.c 245506 2013-01-16 18:15:25Z delphij $
34  */
35 
36 #include <sys/types.h>
37 #include <sys/event.h>
38 #include <sys/time.h>
39 #include <sys/wait.h>
40 
41 #include <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <signal.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <sysexits.h>
49 #include <unistd.h>
50 
51 static void
52 usage(void)
53 {
54 
55 	fprintf(stderr, "usage: pwait [-v] pid ...\n");
56 	exit(EX_USAGE);
57 }
58 
59 /*
60  * pwait - wait for processes to terminate
61  */
62 int
63 main(int argc, char *argv[])
64 {
65 	int kq;
66 	struct kevent *e;
67 	int verbose = 0;
68 	int opt, nleft, n, i, duplicate, status;
69 	long pid;
70 	char *s, *end;
71 
72 	while ((opt = getopt(argc, argv, "v")) != -1) {
73 		switch (opt) {
74 		case 'v':
75 			verbose = 1;
76 			break;
77 		default:
78 			usage();
79 			/* NOTREACHED */
80 		}
81 	}
82 
83 	argc -= optind;
84 	argv += optind;
85 
86 	if (argc == 0)
87 		usage();
88 
89 	kq = kqueue();
90 	if (kq == -1)
91 		err(1, "kqueue");
92 
93 	e = malloc(argc * sizeof(struct kevent));
94 	if (e == NULL)
95 		err(1, "malloc");
96 	nleft = 0;
97 	for (n = 0; n < argc; n++) {
98 		s = argv[n];
99 		if (!strncmp(s, "/proc/", 6)) /* Undocumented Solaris compat */
100 			s += 6;
101 		errno = 0;
102 		pid = strtol(s, &end, 10);
103 		if (pid < 0 || *end != '\0' || errno != 0) {
104 			warnx("%s: bad process id", s);
105 			continue;
106 		}
107 		duplicate = 0;
108 		for (i = 0; i < nleft; i++)
109 			if (e[i].ident == (uintptr_t)pid)
110 				duplicate = 1;
111 		if (!duplicate) {
112 			EV_SET(e + nleft, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT,
113 			    0, NULL);
114 			if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1)
115 				warn("%ld", pid);
116 			else
117 				nleft++;
118 		}
119 	}
120 
121 	while (nleft > 0) {
122 		n = kevent(kq, NULL, 0, e, nleft, NULL);
123 		if (n == -1)
124 			err(1, "kevent");
125 		if (verbose)
126 			for (i = 0; i < n; i++) {
127 				status = e[i].data;
128 				if (WIFEXITED(status))
129 					printf("%ld: exited with status %d.\n",
130 					    (long)e[i].ident,
131 					    WEXITSTATUS(status));
132 				else if (WIFSIGNALED(status))
133 					printf("%ld: killed by signal %d.\n",
134 					    (long)e[i].ident,
135 					    WTERMSIG(status));
136 				else
137 					printf("%ld: terminated.\n",
138 					    (long)e[i].ident);
139 			}
140 		nleft -= n;
141 	}
142 
143 	exit(EX_OK);
144 }
145