xref: /illumos-gate/usr/src/cmd/ptools/pwait/pwait.c (revision b1bc843f030b066c3da149508c52f7306b25b8ff)
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
5004388ebScasper  * Common Development and Distribution License (the "License").
6004388ebScasper  * 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 /*
22004388ebScasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*b1bc843fSSpencer Evans-Cole  * Copyright 2022 Spencer Evans-Cole.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdio.h>
28004388ebScasper #include <stdio_ext.h>
297c478bd9Sstevel@tonic-gate #include <ctype.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <fcntl.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <dirent.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <stropts.h>
387c478bd9Sstevel@tonic-gate #include <poll.h>
397c478bd9Sstevel@tonic-gate #include <procfs.h>
407c478bd9Sstevel@tonic-gate #include <sys/resource.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate static int count_my_files();
437c478bd9Sstevel@tonic-gate static char *command;
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /* slop to account for extra file descriptors opened by libraries we call */
467c478bd9Sstevel@tonic-gate #define	SLOP	5
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)497c478bd9Sstevel@tonic-gate main(int argc, char **argv)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	unsigned long remain = 0;
527c478bd9Sstevel@tonic-gate 	struct pollfd *pollfd;
537c478bd9Sstevel@tonic-gate 	struct pollfd *pfd;
547c478bd9Sstevel@tonic-gate 	struct rlimit rlim;
557c478bd9Sstevel@tonic-gate 	char *arg;
567c478bd9Sstevel@tonic-gate 	unsigned i;
577c478bd9Sstevel@tonic-gate 	int verbose = 0;
58*b1bc843fSSpencer Evans-Cole 	pid_t mypid = getpid();
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 		}
95004388ebScasper 		(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];
108*b1bc843fSSpencer Evans-Cole 		if (mypid == atol(arg)) {
109*b1bc843fSSpencer Evans-Cole 			if (verbose) {
110*b1bc843fSSpencer Evans-Cole 				(void) printf("%s: has the same"
111*b1bc843fSSpencer Evans-Cole 				    " pid as this process\n", arg);
112*b1bc843fSSpencer Evans-Cole 			}
113*b1bc843fSSpencer Evans-Cole 			continue;
114*b1bc843fSSpencer Evans-Cole 		}
1157c478bd9Sstevel@tonic-gate 		if (strchr(arg, '/') != NULL)
1167c478bd9Sstevel@tonic-gate 			(void) strncpy(psinfofile, arg, sizeof (psinfofile));
1177c478bd9Sstevel@tonic-gate 		else {
1187c478bd9Sstevel@tonic-gate 			(void) strcpy(psinfofile, "/proc/");
1197c478bd9Sstevel@tonic-gate 			(void) strncat(psinfofile, arg, sizeof (psinfofile)-6);
1207c478bd9Sstevel@tonic-gate 		}
1217c478bd9Sstevel@tonic-gate 		(void) strncat(psinfofile, "/psinfo",
1227c478bd9Sstevel@tonic-gate 		    sizeof (psinfofile) - strlen(psinfofile));
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 		pfd = &pollfd[i];
1257c478bd9Sstevel@tonic-gate 		if ((pfd->fd = open(psinfofile, O_RDONLY)) >= 0) {
1267c478bd9Sstevel@tonic-gate 			remain++;
1277c478bd9Sstevel@tonic-gate 			/*
1287c478bd9Sstevel@tonic-gate 			 * We set POLLPRI to detect system processes.
1297c478bd9Sstevel@tonic-gate 			 * We will get POLLNVAL below for a POLLPRI
1307c478bd9Sstevel@tonic-gate 			 * requested event on a system process.
1317c478bd9Sstevel@tonic-gate 			 */
1327c478bd9Sstevel@tonic-gate 			pfd->events = POLLPRI;
1337c478bd9Sstevel@tonic-gate 			pfd->revents = 0;
1347c478bd9Sstevel@tonic-gate 		} else if (errno == ENOENT) {
1357c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: no such process: %s\n",
1367c478bd9Sstevel@tonic-gate 			    command, arg);
1377c478bd9Sstevel@tonic-gate 		} else {
1387c478bd9Sstevel@tonic-gate 			perror(arg);
1397c478bd9Sstevel@tonic-gate 		}
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	while (remain != 0) {
1437c478bd9Sstevel@tonic-gate 		while (poll(pollfd, argc, INFTIM) < 0) {
1447c478bd9Sstevel@tonic-gate 			if (errno != EAGAIN) {
1457c478bd9Sstevel@tonic-gate 				perror("poll");
1467c478bd9Sstevel@tonic-gate 				return (2);
1477c478bd9Sstevel@tonic-gate 			}
1487c478bd9Sstevel@tonic-gate 			(void) sleep(2);
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 		for (i = 0; i < argc; i++) {
1517c478bd9Sstevel@tonic-gate 			pfd = &pollfd[i];
1527c478bd9Sstevel@tonic-gate 			if (pfd->fd < 0 || (pfd->revents & ~POLLPRI) == 0) {
1537c478bd9Sstevel@tonic-gate 				/*
1547c478bd9Sstevel@tonic-gate 				 * We don't care if a non-system process
1557c478bd9Sstevel@tonic-gate 				 * stopped.  Don't check for that again.
1567c478bd9Sstevel@tonic-gate 				 */
1577c478bd9Sstevel@tonic-gate 				pfd->events = 0;
1587c478bd9Sstevel@tonic-gate 				pfd->revents = 0;
1597c478bd9Sstevel@tonic-gate 				continue;
1607c478bd9Sstevel@tonic-gate 			}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 			if (verbose) {
1637c478bd9Sstevel@tonic-gate 				arg = argv[i];
1647c478bd9Sstevel@tonic-gate 				if (pfd->revents & POLLHUP) {
1657c478bd9Sstevel@tonic-gate 					psinfo_t psinfo;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 					if (pread(pfd->fd, &psinfo,
1687c478bd9Sstevel@tonic-gate 					    sizeof (psinfo), (off_t)0)
1697c478bd9Sstevel@tonic-gate 					    == sizeof (psinfo)) {
170*b1bc843fSSpencer Evans-Cole 						(void) printf("%s: terminated,"
171*b1bc843fSSpencer Evans-Cole 						    " wait status 0x%.4x\n",
1727c478bd9Sstevel@tonic-gate 						    arg, psinfo.pr_wstat);
1737c478bd9Sstevel@tonic-gate 					} else {
1747c478bd9Sstevel@tonic-gate 						(void) printf(
1757c478bd9Sstevel@tonic-gate 						    "%s: terminated\n", arg);
1767c478bd9Sstevel@tonic-gate 					}
1777c478bd9Sstevel@tonic-gate 				}
1787c478bd9Sstevel@tonic-gate 				if (pfd->revents & POLLNVAL)
1797c478bd9Sstevel@tonic-gate 					(void) printf("%s: system process\n",
1807c478bd9Sstevel@tonic-gate 					    arg);
1817c478bd9Sstevel@tonic-gate 				if (pfd->revents & ~(POLLPRI|POLLHUP|POLLNVAL))
1827c478bd9Sstevel@tonic-gate 					(void) printf("%s: unknown error\n",
1837c478bd9Sstevel@tonic-gate 					    arg);
1847c478bd9Sstevel@tonic-gate 			}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 			(void) close(pfd->fd);
1877c478bd9Sstevel@tonic-gate 			pfd->fd = -1;
1887c478bd9Sstevel@tonic-gate 			remain--;
1897c478bd9Sstevel@tonic-gate 		}
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	return (0);
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
1967c478bd9Sstevel@tonic-gate static int
do_count(void * nofilesp,int fd)1977c478bd9Sstevel@tonic-gate do_count(void *nofilesp, int fd)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	(*(int *)nofilesp)++;
2007c478bd9Sstevel@tonic-gate 	return (0);
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate static int
count_my_files()2047c478bd9Sstevel@tonic-gate count_my_files()
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate 	int nofiles = 0;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	(void) fdwalk(do_count, &nofiles);
2097c478bd9Sstevel@tonic-gate 	return (nofiles);
2107c478bd9Sstevel@tonic-gate }
211