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