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 (c) 1999 by Sun Microsystems, Inc. 24*7c478bd9Sstevel@tonic-gate * All rights reserved. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 30*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 31*7c478bd9Sstevel@tonic-gate #include <unistd.h> 32*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 33*7c478bd9Sstevel@tonic-gate #include <errno.h> 34*7c478bd9Sstevel@tonic-gate #include <string.h> 35*7c478bd9Sstevel@tonic-gate #include <strings.h> 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate #include "prtable.h" 38*7c478bd9Sstevel@tonic-gate #include "prutil.h" 39*7c478bd9Sstevel@tonic-gate #include "prfile.h" 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate #define FDS_TABLE_SIZE 1024 42*7c478bd9Sstevel@tonic-gate 43*7c478bd9Sstevel@tonic-gate static fd_t *fd_tbl = NULL; 44*7c478bd9Sstevel@tonic-gate static int fd_max; 45*7c478bd9Sstevel@tonic-gate static int fd_cnt; 46*7c478bd9Sstevel@tonic-gate static int fd_cnt_cur; 47*7c478bd9Sstevel@tonic-gate static int fd_cnt_old; 48*7c478bd9Sstevel@tonic-gate static fds_t *fds_tbl[FDS_TABLE_SIZE]; 49*7c478bd9Sstevel@tonic-gate 50*7c478bd9Sstevel@tonic-gate void 51*7c478bd9Sstevel@tonic-gate fd_init(int n) 52*7c478bd9Sstevel@tonic-gate { 53*7c478bd9Sstevel@tonic-gate fd_max = n; 54*7c478bd9Sstevel@tonic-gate fd_cnt = fd_cnt_cur = fd_cnt_old = 0; 55*7c478bd9Sstevel@tonic-gate fd_tbl = Zalloc(sizeof (fd_t) * n); 56*7c478bd9Sstevel@tonic-gate (void) memset(fds_tbl, 0, sizeof (fds_t *) * FDS_TABLE_SIZE); 57*7c478bd9Sstevel@tonic-gate } 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate void 60*7c478bd9Sstevel@tonic-gate fd_exit() 61*7c478bd9Sstevel@tonic-gate { 62*7c478bd9Sstevel@tonic-gate if (fd_tbl) 63*7c478bd9Sstevel@tonic-gate free(fd_tbl); 64*7c478bd9Sstevel@tonic-gate } 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate void 67*7c478bd9Sstevel@tonic-gate fd_close(fd_t *fdp) 68*7c478bd9Sstevel@tonic-gate { 69*7c478bd9Sstevel@tonic-gate if (fdp) { 70*7c478bd9Sstevel@tonic-gate if (fdp->fd_fd >= 0 && fdp->fd_name[0] != '\0') { 71*7c478bd9Sstevel@tonic-gate (void) close(fdp->fd_fd); 72*7c478bd9Sstevel@tonic-gate fd_cnt--; 73*7c478bd9Sstevel@tonic-gate } 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate (void) memset(fdp, 0, sizeof (fd_t)); 76*7c478bd9Sstevel@tonic-gate fdp->fd_fd = -1; 77*7c478bd9Sstevel@tonic-gate } 78*7c478bd9Sstevel@tonic-gate } 79*7c478bd9Sstevel@tonic-gate 80*7c478bd9Sstevel@tonic-gate void 81*7c478bd9Sstevel@tonic-gate fd_closeall() 82*7c478bd9Sstevel@tonic-gate { 83*7c478bd9Sstevel@tonic-gate fd_t *fdp = fd_tbl; 84*7c478bd9Sstevel@tonic-gate int i; 85*7c478bd9Sstevel@tonic-gate 86*7c478bd9Sstevel@tonic-gate for (i = 0; i < fd_max; i++) { 87*7c478bd9Sstevel@tonic-gate fd_close(fdp); 88*7c478bd9Sstevel@tonic-gate fdp++; 89*7c478bd9Sstevel@tonic-gate } 90*7c478bd9Sstevel@tonic-gate } 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate static void 93*7c478bd9Sstevel@tonic-gate fd_recycle() 94*7c478bd9Sstevel@tonic-gate { 95*7c478bd9Sstevel@tonic-gate fd_t *fdp = fd_tbl; 96*7c478bd9Sstevel@tonic-gate int counter; 97*7c478bd9Sstevel@tonic-gate int i; 98*7c478bd9Sstevel@tonic-gate 99*7c478bd9Sstevel@tonic-gate counter = abs(fd_cnt_old - fd_cnt) + NUM_RESERVED_FD; 100*7c478bd9Sstevel@tonic-gate 101*7c478bd9Sstevel@tonic-gate for (i = 0; i < fd_max; i++, fdp++) { 102*7c478bd9Sstevel@tonic-gate 103*7c478bd9Sstevel@tonic-gate if (fdp->fd_fd == -1) 104*7c478bd9Sstevel@tonic-gate continue; /* skip recycled ones */ 105*7c478bd9Sstevel@tonic-gate 106*7c478bd9Sstevel@tonic-gate if (fdp->fd_name[0] != '\0') { /* file has name */ 107*7c478bd9Sstevel@tonic-gate (void) close(fdp->fd_fd); 108*7c478bd9Sstevel@tonic-gate fd_cnt--; 109*7c478bd9Sstevel@tonic-gate counter--; 110*7c478bd9Sstevel@tonic-gate fdp->fd_fd = -1; 111*7c478bd9Sstevel@tonic-gate } 112*7c478bd9Sstevel@tonic-gate 113*7c478bd9Sstevel@tonic-gate if (counter == 0) 114*7c478bd9Sstevel@tonic-gate break; 115*7c478bd9Sstevel@tonic-gate } 116*7c478bd9Sstevel@tonic-gate } 117*7c478bd9Sstevel@tonic-gate 118*7c478bd9Sstevel@tonic-gate fd_t * 119*7c478bd9Sstevel@tonic-gate fd_open(char *name, int flags, fd_t *fdp) 120*7c478bd9Sstevel@tonic-gate { 121*7c478bd9Sstevel@tonic-gate fd_t *fdp_new; 122*7c478bd9Sstevel@tonic-gate int fd; 123*7c478bd9Sstevel@tonic-gate 124*7c478bd9Sstevel@tonic-gate if (fd_cnt > fd_max - NUM_RESERVED_FD) 125*7c478bd9Sstevel@tonic-gate fd_recycle(); 126*7c478bd9Sstevel@tonic-gate 127*7c478bd9Sstevel@tonic-gate if (fdp != NULL) { 128*7c478bd9Sstevel@tonic-gate if ((strcmp(fdp->fd_name, name) == 0) && (fdp->fd_fd >= 0)) { 129*7c478bd9Sstevel@tonic-gate fd_cnt_cur++; 130*7c478bd9Sstevel@tonic-gate return (fdp); 131*7c478bd9Sstevel@tonic-gate } 132*7c478bd9Sstevel@tonic-gate } 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate again: fd = open(name, flags); 135*7c478bd9Sstevel@tonic-gate 136*7c478bd9Sstevel@tonic-gate if (fd == -1) { 137*7c478bd9Sstevel@tonic-gate if ((errno == EMFILE) || (errno == ENFILE)) { 138*7c478bd9Sstevel@tonic-gate fd_recycle(); 139*7c478bd9Sstevel@tonic-gate goto again; 140*7c478bd9Sstevel@tonic-gate } 141*7c478bd9Sstevel@tonic-gate fdp_new = NULL; 142*7c478bd9Sstevel@tonic-gate } else { 143*7c478bd9Sstevel@tonic-gate fdp_new = &fd_tbl[fd]; 144*7c478bd9Sstevel@tonic-gate fdp_new->fd_fd = fd; 145*7c478bd9Sstevel@tonic-gate fdp_new->fd_flags = flags; 146*7c478bd9Sstevel@tonic-gate (void) strcpy(fdp_new->fd_name, name); 147*7c478bd9Sstevel@tonic-gate fd_cnt++; 148*7c478bd9Sstevel@tonic-gate fd_cnt_cur++; 149*7c478bd9Sstevel@tonic-gate } 150*7c478bd9Sstevel@tonic-gate return (fdp_new); 151*7c478bd9Sstevel@tonic-gate } 152*7c478bd9Sstevel@tonic-gate 153*7c478bd9Sstevel@tonic-gate int 154*7c478bd9Sstevel@tonic-gate fd_getfd(fd_t *fdp) 155*7c478bd9Sstevel@tonic-gate { 156*7c478bd9Sstevel@tonic-gate return (fdp->fd_fd); 157*7c478bd9Sstevel@tonic-gate } 158*7c478bd9Sstevel@tonic-gate 159*7c478bd9Sstevel@tonic-gate void 160*7c478bd9Sstevel@tonic-gate fd_update() 161*7c478bd9Sstevel@tonic-gate { 162*7c478bd9Sstevel@tonic-gate fd_cnt_old = fd_cnt_cur; 163*7c478bd9Sstevel@tonic-gate fd_cnt_cur = 0; 164*7c478bd9Sstevel@tonic-gate } 165*7c478bd9Sstevel@tonic-gate 166*7c478bd9Sstevel@tonic-gate fds_t * 167*7c478bd9Sstevel@tonic-gate fds_get(pid_t pid) 168*7c478bd9Sstevel@tonic-gate { 169*7c478bd9Sstevel@tonic-gate fds_t *fdsp; 170*7c478bd9Sstevel@tonic-gate int hash = pid % FDS_TABLE_SIZE; 171*7c478bd9Sstevel@tonic-gate 172*7c478bd9Sstevel@tonic-gate for (fdsp = fds_tbl[hash]; fdsp; fdsp = fdsp->fds_next) 173*7c478bd9Sstevel@tonic-gate if (fdsp->fds_pid == pid) /* searching for pid */ 174*7c478bd9Sstevel@tonic-gate return (fdsp); 175*7c478bd9Sstevel@tonic-gate 176*7c478bd9Sstevel@tonic-gate fdsp = Zalloc(sizeof (fds_t)); /* adding new if pid was not found */ 177*7c478bd9Sstevel@tonic-gate fdsp->fds_pid = pid; 178*7c478bd9Sstevel@tonic-gate fdsp->fds_next = fds_tbl[hash]; 179*7c478bd9Sstevel@tonic-gate fds_tbl[hash] = fdsp; 180*7c478bd9Sstevel@tonic-gate return (fdsp); 181*7c478bd9Sstevel@tonic-gate } 182*7c478bd9Sstevel@tonic-gate 183*7c478bd9Sstevel@tonic-gate void 184*7c478bd9Sstevel@tonic-gate fds_rm(pid_t pid) 185*7c478bd9Sstevel@tonic-gate { 186*7c478bd9Sstevel@tonic-gate fds_t *fds; 187*7c478bd9Sstevel@tonic-gate fds_t *fds_prev = NULL; 188*7c478bd9Sstevel@tonic-gate int hash = pid % FDS_TABLE_SIZE; 189*7c478bd9Sstevel@tonic-gate 190*7c478bd9Sstevel@tonic-gate for (fds = fds_tbl[hash]; fds && fds->fds_pid != pid; 191*7c478bd9Sstevel@tonic-gate fds = fds->fds_next) /* finding pid */ 192*7c478bd9Sstevel@tonic-gate fds_prev = fds; 193*7c478bd9Sstevel@tonic-gate 194*7c478bd9Sstevel@tonic-gate if (fds) { /* if pid was found */ 195*7c478bd9Sstevel@tonic-gate 196*7c478bd9Sstevel@tonic-gate fd_close(fds->fds_psinfo); 197*7c478bd9Sstevel@tonic-gate fd_close(fds->fds_usage); 198*7c478bd9Sstevel@tonic-gate fd_close(fds->fds_lpsinfo); 199*7c478bd9Sstevel@tonic-gate fd_close(fds->fds_lusage); 200*7c478bd9Sstevel@tonic-gate 201*7c478bd9Sstevel@tonic-gate if (fds_prev) 202*7c478bd9Sstevel@tonic-gate fds_prev->fds_next = fds->fds_next; 203*7c478bd9Sstevel@tonic-gate else 204*7c478bd9Sstevel@tonic-gate fds_tbl[hash] = fds->fds_next; 205*7c478bd9Sstevel@tonic-gate 206*7c478bd9Sstevel@tonic-gate free(fds); 207*7c478bd9Sstevel@tonic-gate } 208*7c478bd9Sstevel@tonic-gate } 209