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