10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*1914Scasper * Common Development and Distribution License (the "License").
6*1914Scasper * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*1914Scasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <stdio.h>
29*1914Scasper #include <stdio_ext.h>
300Sstevel@tonic-gate #include <ctype.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate #include <fcntl.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <dirent.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <stropts.h>
390Sstevel@tonic-gate #include <poll.h>
400Sstevel@tonic-gate #include <procfs.h>
410Sstevel@tonic-gate #include <sys/resource.h>
420Sstevel@tonic-gate
430Sstevel@tonic-gate static int count_my_files();
440Sstevel@tonic-gate static char *command;
450Sstevel@tonic-gate
460Sstevel@tonic-gate /* slop to account for extra file descriptors opened by libraries we call */
470Sstevel@tonic-gate #define SLOP 5
480Sstevel@tonic-gate
490Sstevel@tonic-gate int
main(int argc,char ** argv)500Sstevel@tonic-gate main(int argc, char **argv)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate unsigned long remain = 0;
530Sstevel@tonic-gate struct pollfd *pollfd;
540Sstevel@tonic-gate struct pollfd *pfd;
550Sstevel@tonic-gate struct rlimit rlim;
560Sstevel@tonic-gate char *arg;
570Sstevel@tonic-gate unsigned i;
580Sstevel@tonic-gate int verbose = 0;
590Sstevel@tonic-gate
600Sstevel@tonic-gate if ((command = strrchr(argv[0], '/')) != NULL)
610Sstevel@tonic-gate command++;
620Sstevel@tonic-gate else
630Sstevel@tonic-gate command = argv[0];
640Sstevel@tonic-gate
650Sstevel@tonic-gate argc--;
660Sstevel@tonic-gate argv++;
670Sstevel@tonic-gate
680Sstevel@tonic-gate if (argc > 0 && strcmp(argv[0], "-v") == 0) {
690Sstevel@tonic-gate verbose = 1;
700Sstevel@tonic-gate argc--;
710Sstevel@tonic-gate argv++;
720Sstevel@tonic-gate }
730Sstevel@tonic-gate
740Sstevel@tonic-gate if (argc <= 0) {
750Sstevel@tonic-gate (void) fprintf(stderr, "usage:\t%s [-v] pid ...\n", command);
760Sstevel@tonic-gate (void) fprintf(stderr, " (wait for processes to terminate)\n");
770Sstevel@tonic-gate (void) fprintf(stderr,
780Sstevel@tonic-gate " -v: verbose; report terminations to standard out\n");
790Sstevel@tonic-gate return (2);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate
820Sstevel@tonic-gate /* make sure we have enough file descriptors */
830Sstevel@tonic-gate if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
840Sstevel@tonic-gate int nfiles = count_my_files();
850Sstevel@tonic-gate
860Sstevel@tonic-gate if (rlim.rlim_cur < argc + nfiles + SLOP) {
870Sstevel@tonic-gate rlim.rlim_cur = argc + nfiles + SLOP;
880Sstevel@tonic-gate if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
890Sstevel@tonic-gate (void) fprintf(stderr,
900Sstevel@tonic-gate "%s: insufficient file descriptors\n",
910Sstevel@tonic-gate command);
920Sstevel@tonic-gate return (2);
930Sstevel@tonic-gate }
940Sstevel@tonic-gate }
95*1914Scasper (void) enable_extended_FILE_stdio(-1, -1);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate
980Sstevel@tonic-gate pollfd = (struct pollfd *)malloc(argc*sizeof (struct pollfd));
990Sstevel@tonic-gate if (pollfd == NULL) {
1000Sstevel@tonic-gate perror("malloc");
1010Sstevel@tonic-gate return (2);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate for (i = 0; i < argc; i++) {
1050Sstevel@tonic-gate char psinfofile[100];
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate arg = argv[i];
1080Sstevel@tonic-gate if (strchr(arg, '/') != NULL)
1090Sstevel@tonic-gate (void) strncpy(psinfofile, arg, sizeof (psinfofile));
1100Sstevel@tonic-gate else {
1110Sstevel@tonic-gate (void) strcpy(psinfofile, "/proc/");
1120Sstevel@tonic-gate (void) strncat(psinfofile, arg, sizeof (psinfofile)-6);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate (void) strncat(psinfofile, "/psinfo",
1150Sstevel@tonic-gate sizeof (psinfofile)-strlen(psinfofile));
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate pfd = &pollfd[i];
1180Sstevel@tonic-gate if ((pfd->fd = open(psinfofile, O_RDONLY)) >= 0) {
1190Sstevel@tonic-gate remain++;
1200Sstevel@tonic-gate /*
1210Sstevel@tonic-gate * We set POLLPRI to detect system processes.
1220Sstevel@tonic-gate * We will get POLLNVAL below for a POLLPRI
1230Sstevel@tonic-gate * requested event on a system process.
1240Sstevel@tonic-gate */
1250Sstevel@tonic-gate pfd->events = POLLPRI;
1260Sstevel@tonic-gate pfd->revents = 0;
1270Sstevel@tonic-gate } else if (errno == ENOENT) {
1280Sstevel@tonic-gate (void) fprintf(stderr, "%s: no such process: %s\n",
1290Sstevel@tonic-gate command, arg);
1300Sstevel@tonic-gate } else {
1310Sstevel@tonic-gate perror(arg);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate while (remain != 0) {
1360Sstevel@tonic-gate while (poll(pollfd, argc, INFTIM) < 0) {
1370Sstevel@tonic-gate if (errno != EAGAIN) {
1380Sstevel@tonic-gate perror("poll");
1390Sstevel@tonic-gate return (2);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate (void) sleep(2);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate for (i = 0; i < argc; i++) {
1440Sstevel@tonic-gate pfd = &pollfd[i];
1450Sstevel@tonic-gate if (pfd->fd < 0 || (pfd->revents & ~POLLPRI) == 0) {
1460Sstevel@tonic-gate /*
1470Sstevel@tonic-gate * We don't care if a non-system process
1480Sstevel@tonic-gate * stopped. Don't check for that again.
1490Sstevel@tonic-gate */
1500Sstevel@tonic-gate pfd->events = 0;
1510Sstevel@tonic-gate pfd->revents = 0;
1520Sstevel@tonic-gate continue;
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate if (verbose) {
1560Sstevel@tonic-gate arg = argv[i];
1570Sstevel@tonic-gate if (pfd->revents & POLLHUP) {
1580Sstevel@tonic-gate psinfo_t psinfo;
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate if (pread(pfd->fd, &psinfo,
1610Sstevel@tonic-gate sizeof (psinfo), (off_t)0)
1620Sstevel@tonic-gate == sizeof (psinfo)) {
1630Sstevel@tonic-gate (void) printf(
1640Sstevel@tonic-gate "%s: terminated, wait status 0x%.4x\n",
1650Sstevel@tonic-gate arg, psinfo.pr_wstat);
1660Sstevel@tonic-gate } else {
1670Sstevel@tonic-gate (void) printf(
1680Sstevel@tonic-gate "%s: terminated\n", arg);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate if (pfd->revents & POLLNVAL)
1720Sstevel@tonic-gate (void) printf("%s: system process\n",
1730Sstevel@tonic-gate arg);
1740Sstevel@tonic-gate if (pfd->revents & ~(POLLPRI|POLLHUP|POLLNVAL))
1750Sstevel@tonic-gate (void) printf("%s: unknown error\n",
1760Sstevel@tonic-gate arg);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate (void) close(pfd->fd);
1800Sstevel@tonic-gate pfd->fd = -1;
1810Sstevel@tonic-gate remain--;
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate return (0);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate /* ARGSUSED1 */
1890Sstevel@tonic-gate static int
do_count(void * nofilesp,int fd)1900Sstevel@tonic-gate do_count(void *nofilesp, int fd)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate (*(int *)nofilesp)++;
1930Sstevel@tonic-gate return (0);
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate static int
count_my_files()1970Sstevel@tonic-gate count_my_files()
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate int nofiles = 0;
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate (void) fdwalk(do_count, &nofiles);
2020Sstevel@tonic-gate return (nofiles);
2030Sstevel@tonic-gate }
204