remove xen support
[unleashed/tickless.git] / usr / src / cmd / prstat / prfile.c
blobe1eedd219e4596e53e6fc6f25b94265be9086693
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright (c) 1999 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <strings.h>
35 #include "prtable.h"
36 #include "prutil.h"
37 #include "prfile.h"
39 #define FDS_TABLE_SIZE 1024
41 static fd_t *fd_tbl = NULL;
42 static int fd_max;
43 static int fd_cnt;
44 static int fd_cnt_cur;
45 static int fd_cnt_old;
46 static fds_t *fds_tbl[FDS_TABLE_SIZE];
48 void
49 fd_init(int n)
51 fd_max = n;
52 fd_cnt = fd_cnt_cur = fd_cnt_old = 0;
53 fd_tbl = Zalloc(sizeof (fd_t) * n);
54 (void) memset(fds_tbl, 0, sizeof (fds_t *) * FDS_TABLE_SIZE);
57 void
58 fd_exit()
60 free(fd_tbl);
63 void
64 fd_close(fd_t *fdp)
66 if (fdp) {
67 if (fdp->fd_fd >= 0 && fdp->fd_name[0] != '\0') {
68 (void) close(fdp->fd_fd);
69 fd_cnt--;
72 (void) memset(fdp, 0, sizeof (fd_t));
73 fdp->fd_fd = -1;
77 void
78 fd_closeall()
80 fd_t *fdp = fd_tbl;
81 int i;
83 for (i = 0; i < fd_max; i++) {
84 fd_close(fdp);
85 fdp++;
89 static void
90 fd_recycle()
92 fd_t *fdp = fd_tbl;
93 int counter;
94 int i;
96 counter = abs(fd_cnt_old - fd_cnt) + NUM_RESERVED_FD;
98 for (i = 0; i < fd_max; i++, fdp++) {
100 if (fdp->fd_fd == -1)
101 continue; /* skip recycled ones */
103 if (fdp->fd_name[0] != '\0') { /* file has name */
104 (void) close(fdp->fd_fd);
105 fd_cnt--;
106 counter--;
107 fdp->fd_fd = -1;
110 if (counter == 0)
111 break;
115 fd_t *
116 fd_open(char *name, int flags, fd_t *fdp)
118 fd_t *fdp_new;
119 int fd;
121 if (fd_cnt > fd_max - NUM_RESERVED_FD)
122 fd_recycle();
124 if (fdp != NULL) {
125 if ((strcmp(fdp->fd_name, name) == 0) && (fdp->fd_fd >= 0)) {
126 fd_cnt_cur++;
127 return (fdp);
131 again: fd = open(name, flags);
133 if (fd == -1) {
134 if ((errno == EMFILE) || (errno == ENFILE)) {
135 fd_recycle();
136 goto again;
138 fdp_new = NULL;
139 } else {
140 fdp_new = &fd_tbl[fd];
141 fdp_new->fd_fd = fd;
142 fdp_new->fd_flags = flags;
143 (void) strcpy(fdp_new->fd_name, name);
144 fd_cnt++;
145 fd_cnt_cur++;
147 return (fdp_new);
151 fd_getfd(fd_t *fdp)
153 return (fdp->fd_fd);
156 void
157 fd_update()
159 fd_cnt_old = fd_cnt_cur;
160 fd_cnt_cur = 0;
163 fds_t *
164 fds_get(pid_t pid)
166 fds_t *fdsp;
167 int hash = pid % FDS_TABLE_SIZE;
169 for (fdsp = fds_tbl[hash]; fdsp; fdsp = fdsp->fds_next)
170 if (fdsp->fds_pid == pid) /* searching for pid */
171 return (fdsp);
173 fdsp = Zalloc(sizeof (fds_t)); /* adding new if pid was not found */
174 fdsp->fds_pid = pid;
175 fdsp->fds_next = fds_tbl[hash];
176 fds_tbl[hash] = fdsp;
177 return (fdsp);
180 void
181 fds_rm(pid_t pid)
183 fds_t *fds;
184 fds_t *fds_prev = NULL;
185 int hash = pid % FDS_TABLE_SIZE;
187 for (fds = fds_tbl[hash]; fds && fds->fds_pid != pid;
188 fds = fds->fds_next) /* finding pid */
189 fds_prev = fds;
191 if (fds) { /* if pid was found */
193 fd_close(fds->fds_psinfo);
194 fd_close(fds->fds_usage);
195 fd_close(fds->fds_lpsinfo);
196 fd_close(fds->fds_lusage);
198 if (fds_prev)
199 fds_prev->fds_next = fds->fds_next;
200 else
201 fds_tbl[hash] = fds->fds_next;
203 free(fds);