xref: /titanic_52/usr/src/cmd/ptools/pwait/pwait.c (revision 004388ebfdfe2ed7dfd2d153a876dfcc22d2c006)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*004388ebScasper  * Common Development and Distribution License (the "License").
6*004388ebScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*004388ebScasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
29*004388ebScasper #include <stdio_ext.h>
307c478bd9Sstevel@tonic-gate #include <ctype.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <fcntl.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <dirent.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <stropts.h>
397c478bd9Sstevel@tonic-gate #include <poll.h>
407c478bd9Sstevel@tonic-gate #include <procfs.h>
417c478bd9Sstevel@tonic-gate #include <sys/resource.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static int count_my_files();
447c478bd9Sstevel@tonic-gate static char *command;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /* slop to account for extra file descriptors opened by libraries we call */
477c478bd9Sstevel@tonic-gate #define	SLOP	5
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate int
507c478bd9Sstevel@tonic-gate main(int argc, char **argv)
517c478bd9Sstevel@tonic-gate {
527c478bd9Sstevel@tonic-gate 	unsigned long remain = 0;
537c478bd9Sstevel@tonic-gate 	struct pollfd *pollfd;
547c478bd9Sstevel@tonic-gate 	struct pollfd *pfd;
557c478bd9Sstevel@tonic-gate 	struct rlimit rlim;
567c478bd9Sstevel@tonic-gate 	char *arg;
577c478bd9Sstevel@tonic-gate 	unsigned i;
587c478bd9Sstevel@tonic-gate 	int verbose = 0;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	if ((command = strrchr(argv[0], '/')) != NULL)
617c478bd9Sstevel@tonic-gate 		command++;
627c478bd9Sstevel@tonic-gate 	else
637c478bd9Sstevel@tonic-gate 		command = argv[0];
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	argc--;
667c478bd9Sstevel@tonic-gate 	argv++;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	if (argc > 0 && strcmp(argv[0], "-v") == 0) {
697c478bd9Sstevel@tonic-gate 		verbose = 1;
707c478bd9Sstevel@tonic-gate 		argc--;
717c478bd9Sstevel@tonic-gate 		argv++;
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	if (argc <= 0) {
757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "usage:\t%s [-v] pid ...\n", command);
767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "  (wait for processes to terminate)\n");
777c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
787c478bd9Sstevel@tonic-gate 			"  -v: verbose; report terminations to standard out\n");
797c478bd9Sstevel@tonic-gate 		return (2);
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	/* make sure we have enough file descriptors */
837c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
847c478bd9Sstevel@tonic-gate 		int nfiles = count_my_files();
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 		if (rlim.rlim_cur < argc + nfiles + SLOP) {
877c478bd9Sstevel@tonic-gate 			rlim.rlim_cur = argc + nfiles + SLOP;
887c478bd9Sstevel@tonic-gate 			if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
897c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
907c478bd9Sstevel@tonic-gate 					"%s: insufficient file descriptors\n",
917c478bd9Sstevel@tonic-gate 					command);
927c478bd9Sstevel@tonic-gate 				return (2);
937c478bd9Sstevel@tonic-gate 			}
947c478bd9Sstevel@tonic-gate 		}
95*004388ebScasper 		(void) enable_extended_FILE_stdio(-1, -1);
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	pollfd = (struct pollfd *)malloc(argc*sizeof (struct pollfd));
997c478bd9Sstevel@tonic-gate 	if (pollfd == NULL) {
1007c478bd9Sstevel@tonic-gate 		perror("malloc");
1017c478bd9Sstevel@tonic-gate 		return (2);
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
1057c478bd9Sstevel@tonic-gate 		char psinfofile[100];
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 		arg = argv[i];
1087c478bd9Sstevel@tonic-gate 		if (strchr(arg, '/') != NULL)
1097c478bd9Sstevel@tonic-gate 			(void) strncpy(psinfofile, arg, sizeof (psinfofile));
1107c478bd9Sstevel@tonic-gate 		else {
1117c478bd9Sstevel@tonic-gate 			(void) strcpy(psinfofile, "/proc/");
1127c478bd9Sstevel@tonic-gate 			(void) strncat(psinfofile, arg, sizeof (psinfofile)-6);
1137c478bd9Sstevel@tonic-gate 		}
1147c478bd9Sstevel@tonic-gate 		(void) strncat(psinfofile, "/psinfo",
1157c478bd9Sstevel@tonic-gate 			sizeof (psinfofile)-strlen(psinfofile));
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 		pfd = &pollfd[i];
1187c478bd9Sstevel@tonic-gate 		if ((pfd->fd = open(psinfofile, O_RDONLY)) >= 0) {
1197c478bd9Sstevel@tonic-gate 			remain++;
1207c478bd9Sstevel@tonic-gate 			/*
1217c478bd9Sstevel@tonic-gate 			 * We set POLLPRI to detect system processes.
1227c478bd9Sstevel@tonic-gate 			 * We will get POLLNVAL below for a POLLPRI
1237c478bd9Sstevel@tonic-gate 			 * requested event on a system process.
1247c478bd9Sstevel@tonic-gate 			 */
1257c478bd9Sstevel@tonic-gate 			pfd->events = POLLPRI;
1267c478bd9Sstevel@tonic-gate 			pfd->revents = 0;
1277c478bd9Sstevel@tonic-gate 		} else if (errno == ENOENT) {
1287c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: no such process: %s\n",
1297c478bd9Sstevel@tonic-gate 				command, arg);
1307c478bd9Sstevel@tonic-gate 		} else {
1317c478bd9Sstevel@tonic-gate 			perror(arg);
1327c478bd9Sstevel@tonic-gate 		}
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	while (remain != 0) {
1367c478bd9Sstevel@tonic-gate 		while (poll(pollfd, argc, INFTIM) < 0) {
1377c478bd9Sstevel@tonic-gate 			if (errno != EAGAIN) {
1387c478bd9Sstevel@tonic-gate 				perror("poll");
1397c478bd9Sstevel@tonic-gate 				return (2);
1407c478bd9Sstevel@tonic-gate 			}
1417c478bd9Sstevel@tonic-gate 			(void) sleep(2);
1427c478bd9Sstevel@tonic-gate 		}
1437c478bd9Sstevel@tonic-gate 		for (i = 0; i < argc; i++) {
1447c478bd9Sstevel@tonic-gate 			pfd = &pollfd[i];
1457c478bd9Sstevel@tonic-gate 			if (pfd->fd < 0 || (pfd->revents & ~POLLPRI) == 0) {
1467c478bd9Sstevel@tonic-gate 				/*
1477c478bd9Sstevel@tonic-gate 				 * We don't care if a non-system process
1487c478bd9Sstevel@tonic-gate 				 * stopped.  Don't check for that again.
1497c478bd9Sstevel@tonic-gate 				 */
1507c478bd9Sstevel@tonic-gate 				pfd->events = 0;
1517c478bd9Sstevel@tonic-gate 				pfd->revents = 0;
1527c478bd9Sstevel@tonic-gate 				continue;
1537c478bd9Sstevel@tonic-gate 			}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 			if (verbose) {
1567c478bd9Sstevel@tonic-gate 				arg = argv[i];
1577c478bd9Sstevel@tonic-gate 				if (pfd->revents & POLLHUP) {
1587c478bd9Sstevel@tonic-gate 					psinfo_t psinfo;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 					if (pread(pfd->fd, &psinfo,
1617c478bd9Sstevel@tonic-gate 					    sizeof (psinfo), (off_t)0)
1627c478bd9Sstevel@tonic-gate 					    == sizeof (psinfo)) {
1637c478bd9Sstevel@tonic-gate 						(void) printf(
1647c478bd9Sstevel@tonic-gate 					"%s: terminated, wait status 0x%.4x\n",
1657c478bd9Sstevel@tonic-gate 							arg, psinfo.pr_wstat);
1667c478bd9Sstevel@tonic-gate 					} else {
1677c478bd9Sstevel@tonic-gate 						(void) printf(
1687c478bd9Sstevel@tonic-gate 						    "%s: terminated\n", arg);
1697c478bd9Sstevel@tonic-gate 					}
1707c478bd9Sstevel@tonic-gate 				}
1717c478bd9Sstevel@tonic-gate 				if (pfd->revents & POLLNVAL)
1727c478bd9Sstevel@tonic-gate 					(void) printf("%s: system process\n",
1737c478bd9Sstevel@tonic-gate 						arg);
1747c478bd9Sstevel@tonic-gate 				if (pfd->revents & ~(POLLPRI|POLLHUP|POLLNVAL))
1757c478bd9Sstevel@tonic-gate 					(void) printf("%s: unknown error\n",
1767c478bd9Sstevel@tonic-gate 						arg);
1777c478bd9Sstevel@tonic-gate 			}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 			(void) close(pfd->fd);
1807c478bd9Sstevel@tonic-gate 			pfd->fd = -1;
1817c478bd9Sstevel@tonic-gate 			remain--;
1827c478bd9Sstevel@tonic-gate 		}
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	return (0);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
1897c478bd9Sstevel@tonic-gate static int
1907c478bd9Sstevel@tonic-gate do_count(void *nofilesp, int fd)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	(*(int *)nofilesp)++;
1937c478bd9Sstevel@tonic-gate 	return (0);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate static int
1977c478bd9Sstevel@tonic-gate count_my_files()
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	int nofiles = 0;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	(void) fdwalk(do_count, &nofiles);
2027c478bd9Sstevel@tonic-gate 	return (nofiles);
2037c478bd9Sstevel@tonic-gate }
204