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