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