xref: /titanic_51/usr/src/cmd/ptools/pwait/pwait.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1994-2000, 2002 Sun Microsystems, Inc.
24  * All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <string.h>
36 #include <dirent.h>
37 #include <errno.h>
38 #include <sys/types.h>
39 #include <stropts.h>
40 #include <poll.h>
41 #include <procfs.h>
42 #include <sys/resource.h>
43 
44 static int count_my_files();
45 static char *command;
46 
47 /* slop to account for extra file descriptors opened by libraries we call */
48 #define	SLOP	5
49 
50 int
51 main(int argc, char **argv)
52 {
53 	unsigned long remain = 0;
54 	struct pollfd *pollfd;
55 	struct pollfd *pfd;
56 	struct rlimit rlim;
57 	char *arg;
58 	unsigned i;
59 	int verbose = 0;
60 
61 	if ((command = strrchr(argv[0], '/')) != NULL)
62 		command++;
63 	else
64 		command = argv[0];
65 
66 	argc--;
67 	argv++;
68 
69 	if (argc > 0 && strcmp(argv[0], "-v") == 0) {
70 		verbose = 1;
71 		argc--;
72 		argv++;
73 	}
74 
75 	if (argc <= 0) {
76 		(void) fprintf(stderr, "usage:\t%s [-v] pid ...\n", command);
77 		(void) fprintf(stderr, "  (wait for processes to terminate)\n");
78 		(void) fprintf(stderr,
79 			"  -v: verbose; report terminations to standard out\n");
80 		return (2);
81 	}
82 
83 	/* make sure we have enough file descriptors */
84 	if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
85 		int nfiles = count_my_files();
86 
87 		if (rlim.rlim_cur < argc + nfiles + SLOP) {
88 			rlim.rlim_cur = argc + nfiles + SLOP;
89 			if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
90 				(void) fprintf(stderr,
91 					"%s: insufficient file descriptors\n",
92 					command);
93 				return (2);
94 			}
95 		}
96 	}
97 
98 	pollfd = (struct pollfd *)malloc(argc*sizeof (struct pollfd));
99 	if (pollfd == NULL) {
100 		perror("malloc");
101 		return (2);
102 	}
103 
104 	for (i = 0; i < argc; i++) {
105 		char psinfofile[100];
106 
107 		arg = argv[i];
108 		if (strchr(arg, '/') != NULL)
109 			(void) strncpy(psinfofile, arg, sizeof (psinfofile));
110 		else {
111 			(void) strcpy(psinfofile, "/proc/");
112 			(void) strncat(psinfofile, arg, sizeof (psinfofile)-6);
113 		}
114 		(void) strncat(psinfofile, "/psinfo",
115 			sizeof (psinfofile)-strlen(psinfofile));
116 
117 		pfd = &pollfd[i];
118 		if ((pfd->fd = open(psinfofile, O_RDONLY)) >= 0) {
119 			remain++;
120 			/*
121 			 * We set POLLPRI to detect system processes.
122 			 * We will get POLLNVAL below for a POLLPRI
123 			 * requested event on a system process.
124 			 */
125 			pfd->events = POLLPRI;
126 			pfd->revents = 0;
127 		} else if (errno == ENOENT) {
128 			(void) fprintf(stderr, "%s: no such process: %s\n",
129 				command, arg);
130 		} else {
131 			perror(arg);
132 		}
133 	}
134 
135 	while (remain != 0) {
136 		while (poll(pollfd, argc, INFTIM) < 0) {
137 			if (errno != EAGAIN) {
138 				perror("poll");
139 				return (2);
140 			}
141 			(void) sleep(2);
142 		}
143 		for (i = 0; i < argc; i++) {
144 			pfd = &pollfd[i];
145 			if (pfd->fd < 0 || (pfd->revents & ~POLLPRI) == 0) {
146 				/*
147 				 * We don't care if a non-system process
148 				 * stopped.  Don't check for that again.
149 				 */
150 				pfd->events = 0;
151 				pfd->revents = 0;
152 				continue;
153 			}
154 
155 			if (verbose) {
156 				arg = argv[i];
157 				if (pfd->revents & POLLHUP) {
158 					psinfo_t psinfo;
159 
160 					if (pread(pfd->fd, &psinfo,
161 					    sizeof (psinfo), (off_t)0)
162 					    == sizeof (psinfo)) {
163 						(void) printf(
164 					"%s: terminated, wait status 0x%.4x\n",
165 							arg, psinfo.pr_wstat);
166 					} else {
167 						(void) printf(
168 						    "%s: terminated\n", arg);
169 					}
170 				}
171 				if (pfd->revents & POLLNVAL)
172 					(void) printf("%s: system process\n",
173 						arg);
174 				if (pfd->revents & ~(POLLPRI|POLLHUP|POLLNVAL))
175 					(void) printf("%s: unknown error\n",
176 						arg);
177 			}
178 
179 			(void) close(pfd->fd);
180 			pfd->fd = -1;
181 			remain--;
182 		}
183 	}
184 
185 	return (0);
186 }
187 
188 /* ARGSUSED1 */
189 static int
190 do_count(void *nofilesp, int fd)
191 {
192 	(*(int *)nofilesp)++;
193 	return (0);
194 }
195 
196 static int
197 count_my_files()
198 {
199 	int nofiles = 0;
200 
201 	(void) fdwalk(do_count, &nofiles);
202 	return (nofiles);
203 }
204