9804 hal-set-property should support --direct option
[unleashed.git] / usr / src / cmd / prstat / prfile.c
blobf14937a10cd6fcc1fcc69295569f6d1d766b5aaa
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 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <strings.h>
37 #include "prtable.h"
38 #include "prutil.h"
39 #include "prfile.h"
41 #define FDS_TABLE_SIZE 1024
43 static fd_t *fd_tbl = NULL;
44 static int fd_max;
45 static int fd_cnt;
46 static int fd_cnt_cur;
47 static int fd_cnt_old;
48 static fds_t *fds_tbl[FDS_TABLE_SIZE];
50 void
51 fd_init(int n)
53 fd_max = n;
54 fd_cnt = fd_cnt_cur = fd_cnt_old = 0;
55 fd_tbl = Zalloc(sizeof (fd_t) * n);
56 (void) memset(fds_tbl, 0, sizeof (fds_t *) * FDS_TABLE_SIZE);
59 void
60 fd_exit()
62 if (fd_tbl)
63 free(fd_tbl);
66 void
67 fd_close(fd_t *fdp)
69 if (fdp) {
70 if (fdp->fd_fd >= 0 && fdp->fd_name[0] != '\0') {
71 (void) close(fdp->fd_fd);
72 fd_cnt--;
75 (void) memset(fdp, 0, sizeof (fd_t));
76 fdp->fd_fd = -1;
80 void
81 fd_closeall()
83 fd_t *fdp = fd_tbl;
84 int i;
86 for (i = 0; i < fd_max; i++) {
87 fd_close(fdp);
88 fdp++;
92 static void
93 fd_recycle()
95 fd_t *fdp = fd_tbl;
96 int counter;
97 int i;
99 counter = abs(fd_cnt_old - fd_cnt) + NUM_RESERVED_FD;
101 for (i = 0; i < fd_max; i++, fdp++) {
103 if (fdp->fd_fd == -1)
104 continue; /* skip recycled ones */
106 if (fdp->fd_name[0] != '\0') { /* file has name */
107 (void) close(fdp->fd_fd);
108 fd_cnt--;
109 counter--;
110 fdp->fd_fd = -1;
113 if (counter == 0)
114 break;
118 fd_t *
119 fd_open(char *name, int flags, fd_t *fdp)
121 fd_t *fdp_new;
122 int fd;
124 if (fd_cnt > fd_max - NUM_RESERVED_FD)
125 fd_recycle();
127 if (fdp != NULL) {
128 if ((strcmp(fdp->fd_name, name) == 0) && (fdp->fd_fd >= 0)) {
129 fd_cnt_cur++;
130 return (fdp);
134 again: fd = open(name, flags);
136 if (fd == -1) {
137 if ((errno == EMFILE) || (errno == ENFILE)) {
138 fd_recycle();
139 goto again;
141 fdp_new = NULL;
142 } else {
143 fdp_new = &fd_tbl[fd];
144 fdp_new->fd_fd = fd;
145 fdp_new->fd_flags = flags;
146 (void) strcpy(fdp_new->fd_name, name);
147 fd_cnt++;
148 fd_cnt_cur++;
150 return (fdp_new);
154 fd_getfd(fd_t *fdp)
156 return (fdp->fd_fd);
159 void
160 fd_update()
162 fd_cnt_old = fd_cnt_cur;
163 fd_cnt_cur = 0;
166 fds_t *
167 fds_get(pid_t pid)
169 fds_t *fdsp;
170 int hash = pid % FDS_TABLE_SIZE;
172 for (fdsp = fds_tbl[hash]; fdsp; fdsp = fdsp->fds_next)
173 if (fdsp->fds_pid == pid) /* searching for pid */
174 return (fdsp);
176 fdsp = Zalloc(sizeof (fds_t)); /* adding new if pid was not found */
177 fdsp->fds_pid = pid;
178 fdsp->fds_next = fds_tbl[hash];
179 fds_tbl[hash] = fdsp;
180 return (fdsp);
183 void
184 fds_rm(pid_t pid)
186 fds_t *fds;
187 fds_t *fds_prev = NULL;
188 int hash = pid % FDS_TABLE_SIZE;
190 for (fds = fds_tbl[hash]; fds && fds->fds_pid != pid;
191 fds = fds->fds_next) /* finding pid */
192 fds_prev = fds;
194 if (fds) { /* if pid was found */
196 fd_close(fds->fds_psinfo);
197 fd_close(fds->fds_usage);
198 fd_close(fds->fds_lpsinfo);
199 fd_close(fds->fds_lusage);
201 if (fds_prev)
202 fds_prev->fds_next = fds->fds_next;
203 else
204 fds_tbl[hash] = fds->fds_next;
206 free(fds);