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