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 2003 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
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 /*
30*0Sstevel@tonic-gate * File descriptor usage
31*0Sstevel@tonic-gate *
32*0Sstevel@tonic-gate * The number of processes that can be effectively managed is limited to less
33*0Sstevel@tonic-gate * than half the number of descriptors available: one for each process's
34*0Sstevel@tonic-gate * psinfo, the other its pagedata. When managing more processes, file
35*0Sstevel@tonic-gate * descriptors are revoked as needed, in such a way as to maximize the
36*0Sstevel@tonic-gate * distribution of descriptors to pagedata which will be useful in meeting a
37*0Sstevel@tonic-gate * cap without paging out the process's working set, while retaining some
38*0Sstevel@tonic-gate * benefit from caching psinfo descriptors, and leaving enough available for
39*0Sstevel@tonic-gate * use by external consumers, such as are needed for project enumeration or
40*0Sstevel@tonic-gate * configuration file reading.
41*0Sstevel@tonic-gate *
42*0Sstevel@tonic-gate * Revokable file descriptors are opened and associated with a callback
43*0Sstevel@tonic-gate * function which can be invoked to revoke them later. pagedata and psinfo
44*0Sstevel@tonic-gate * descriptors are differentiated for the purposes of preferring pagedata over
45*0Sstevel@tonic-gate * psinfo, which effectively places the performance of rcapd behind the
46*0Sstevel@tonic-gate * importance of making good page selections. The one exception is that one
47*0Sstevel@tonic-gate * psinfo descriptor is guaranteed a place at any time, for the benefit of
48*0Sstevel@tonic-gate * psinfo updates of a presently currently-scanned process. Descriptors are
49*0Sstevel@tonic-gate * otherwise revoked in LIFO order.
50*0Sstevel@tonic-gate */
51*0Sstevel@tonic-gate
52*0Sstevel@tonic-gate #include <sys/types.h>
53*0Sstevel@tonic-gate #include <stdlib.h>
54*0Sstevel@tonic-gate #include <errno.h>
55*0Sstevel@tonic-gate #include <fcntl.h>
56*0Sstevel@tonic-gate #include <limits.h>
57*0Sstevel@tonic-gate #include <strings.h>
58*0Sstevel@tonic-gate #include <unistd.h>
59*0Sstevel@tonic-gate #include "rcapd_rfd.h"
60*0Sstevel@tonic-gate #include "utils.h"
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gate static rfd_t *tail; /* tail of global list */
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gate static int rfd_revoke_next(rfd_class_t);
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate /*
67*0Sstevel@tonic-gate * Return the previous rfd_t of the given class, starting at (and including)
68*0Sstevel@tonic-gate * the given rfd_t.
69*0Sstevel@tonic-gate */
70*0Sstevel@tonic-gate static rfd_t *
rfd_find_prev_class(rfd_t * rfd,rfd_class_t class)71*0Sstevel@tonic-gate rfd_find_prev_class(rfd_t *rfd, rfd_class_t class)
72*0Sstevel@tonic-gate {
73*0Sstevel@tonic-gate while (rfd != NULL && rfd->rfd_class != class)
74*0Sstevel@tonic-gate rfd = rfd->rfd_prev;
75*0Sstevel@tonic-gate return (rfd);
76*0Sstevel@tonic-gate }
77*0Sstevel@tonic-gate
78*0Sstevel@tonic-gate /*
79*0Sstevel@tonic-gate * Revoke and free the given rfd_t, returning as close does.
80*0Sstevel@tonic-gate */
81*0Sstevel@tonic-gate static int
rfd_revoke_fd(rfd_t * rfd)82*0Sstevel@tonic-gate rfd_revoke_fd(rfd_t *rfd)
83*0Sstevel@tonic-gate {
84*0Sstevel@tonic-gate if (rfd->rfd_revoke != NULL)
85*0Sstevel@tonic-gate rfd->rfd_revoke(rfd);
86*0Sstevel@tonic-gate return (rfd_close(rfd->rfd_fd));
87*0Sstevel@tonic-gate }
88*0Sstevel@tonic-gate
89*0Sstevel@tonic-gate /*
90*0Sstevel@tonic-gate * Revoke the next file descriptor according to the above constraints. Return
91*0Sstevel@tonic-gate * nonzero if there are none to revoke.
92*0Sstevel@tonic-gate */
93*0Sstevel@tonic-gate static int
rfd_revoke_next(rfd_class_t class)94*0Sstevel@tonic-gate rfd_revoke_next(rfd_class_t class)
95*0Sstevel@tonic-gate {
96*0Sstevel@tonic-gate rfd_t *rfd = NULL;
97*0Sstevel@tonic-gate
98*0Sstevel@tonic-gate if (tail == NULL) {
99*0Sstevel@tonic-gate debug("nothing to revoke\n");
100*0Sstevel@tonic-gate return (-1);
101*0Sstevel@tonic-gate }
102*0Sstevel@tonic-gate
103*0Sstevel@tonic-gate /*
104*0Sstevel@tonic-gate * RESERVED-clsas descriptors are all equivalent and may not be revoked
105*0Sstevel@tonic-gate * to satisfy another request of the same clsas. rfd_reserve() uses
106*0Sstevel@tonic-gate * this to reserve descriptors by first allocating, then closing, these
107*0Sstevel@tonic-gate * descriptors.
108*0Sstevel@tonic-gate */
109*0Sstevel@tonic-gate if (class != RFD_RESERVED)
110*0Sstevel@tonic-gate rfd = rfd_find_prev_class(tail, RFD_RESERVED);
111*0Sstevel@tonic-gate
112*0Sstevel@tonic-gate /*
113*0Sstevel@tonic-gate * Next try psinfo descriptors, leaving at least one open. Revoke the
114*0Sstevel@tonic-gate * second-last psinfo descriptor, if possible.
115*0Sstevel@tonic-gate */
116*0Sstevel@tonic-gate if (rfd == NULL) {
117*0Sstevel@tonic-gate rfd = rfd_find_prev_class(tail, RFD_PSINFO);
118*0Sstevel@tonic-gate if (rfd != NULL)
119*0Sstevel@tonic-gate rfd = rfd->rfd_prev_class;
120*0Sstevel@tonic-gate }
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gate /*
123*0Sstevel@tonic-gate * Otherwise, revoke the last descriptor allocated, taking the same
124*0Sstevel@tonic-gate * care as above that it is not reserved, if the reserved kind is
125*0Sstevel@tonic-gate * sought.
126*0Sstevel@tonic-gate */
127*0Sstevel@tonic-gate if (rfd == NULL) {
128*0Sstevel@tonic-gate rfd = tail;
129*0Sstevel@tonic-gate while (rfd != NULL && class == RFD_RESERVED && rfd->rfd_class ==
130*0Sstevel@tonic-gate RFD_RESERVED)
131*0Sstevel@tonic-gate rfd = rfd->rfd_prev;
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate if (rfd != NULL)
135*0Sstevel@tonic-gate return (rfd_revoke_fd(rfd));
136*0Sstevel@tonic-gate
137*0Sstevel@tonic-gate /*
138*0Sstevel@tonic-gate * Nothing but reserved-class descriptors are revocable, while a
139*0Sstevel@tonic-gate * reserved- class descriptor was sought.
140*0Sstevel@tonic-gate */
141*0Sstevel@tonic-gate return (-1);
142*0Sstevel@tonic-gate }
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gate /*
145*0Sstevel@tonic-gate * Opens a file of the given class, which can later be revoked with the given
146*0Sstevel@tonic-gate * callback. Returns as open does. The callback should reset any state that
147*0Sstevel@tonic-gate * this caller establishes after the open, but should not close the descriptor,
148*0Sstevel@tonic-gate * which will be done when the caller explicitly does so with rfd_close(), or
149*0Sstevel@tonic-gate * the descriptor is revoked with rfd_revoke().
150*0Sstevel@tonic-gate */
151*0Sstevel@tonic-gate int
rfd_open(char * name,int revoke_ok,rfd_class_t class,void (* revoke)(struct rfd *),void * data,int oflag,mode_t mode)152*0Sstevel@tonic-gate rfd_open(char *name, int revoke_ok, rfd_class_t class,
153*0Sstevel@tonic-gate void(*revoke)(struct rfd *), void *data, int oflag, mode_t mode)
154*0Sstevel@tonic-gate {
155*0Sstevel@tonic-gate int fd;
156*0Sstevel@tonic-gate rfd_t *rfd;
157*0Sstevel@tonic-gate
158*0Sstevel@tonic-gate while ((fd = open(name, oflag, mode)) == -1 && (errno == ENFILE ||
159*0Sstevel@tonic-gate errno == EMFILE)) {
160*0Sstevel@tonic-gate if (revoke_ok) {
161*0Sstevel@tonic-gate if (rfd_revoke_next(class) != 0)
162*0Sstevel@tonic-gate return (-1);
163*0Sstevel@tonic-gate } else
164*0Sstevel@tonic-gate break;
165*0Sstevel@tonic-gate }
166*0Sstevel@tonic-gate
167*0Sstevel@tonic-gate if (fd != -1) {
168*0Sstevel@tonic-gate /*
169*0Sstevel@tonic-gate * Create rfd_t and link into list.
170*0Sstevel@tonic-gate */
171*0Sstevel@tonic-gate rfd = malloc(sizeof (*rfd));
172*0Sstevel@tonic-gate if (rfd == NULL) {
173*0Sstevel@tonic-gate (void) close(fd);
174*0Sstevel@tonic-gate return (-1);
175*0Sstevel@tonic-gate }
176*0Sstevel@tonic-gate (void) bzero(rfd, sizeof (*rfd));
177*0Sstevel@tonic-gate rfd->rfd_fd = fd;
178*0Sstevel@tonic-gate rfd->rfd_class = class;
179*0Sstevel@tonic-gate rfd->rfd_revoke = revoke;
180*0Sstevel@tonic-gate rfd->rfd_data = data;
181*0Sstevel@tonic-gate if (tail != NULL)
182*0Sstevel@tonic-gate rfd->rfd_prev_class = rfd_find_prev_class(tail, class);
183*0Sstevel@tonic-gate else
184*0Sstevel@tonic-gate rfd->rfd_prev_class = tail;
185*0Sstevel@tonic-gate rfd->rfd_prev = tail;
186*0Sstevel@tonic-gate if (tail != NULL)
187*0Sstevel@tonic-gate tail->rfd_next = rfd;
188*0Sstevel@tonic-gate tail = rfd;
189*0Sstevel@tonic-gate }
190*0Sstevel@tonic-gate
191*0Sstevel@tonic-gate return (fd);
192*0Sstevel@tonic-gate }
193*0Sstevel@tonic-gate
194*0Sstevel@tonic-gate /*
195*0Sstevel@tonic-gate * Close a given file descriptor, and return as close() does.
196*0Sstevel@tonic-gate */
197*0Sstevel@tonic-gate int
rfd_close(int fd)198*0Sstevel@tonic-gate rfd_close(int fd)
199*0Sstevel@tonic-gate {
200*0Sstevel@tonic-gate rfd_t *nextclass;
201*0Sstevel@tonic-gate rfd_t *rfdprev;
202*0Sstevel@tonic-gate rfd_t *rfd;
203*0Sstevel@tonic-gate #ifdef DEBUG
204*0Sstevel@tonic-gate int freed = 0;
205*0Sstevel@tonic-gate #endif /* DEBUG */
206*0Sstevel@tonic-gate
207*0Sstevel@tonic-gate rfd = tail;
208*0Sstevel@tonic-gate while (rfd != NULL) {
209*0Sstevel@tonic-gate rfdprev = rfd->rfd_prev;
210*0Sstevel@tonic-gate if (rfd->rfd_fd == fd) {
211*0Sstevel@tonic-gate if (rfd->rfd_prev != NULL)
212*0Sstevel@tonic-gate rfd->rfd_prev->rfd_next = rfd->rfd_next;
213*0Sstevel@tonic-gate if (rfd->rfd_next != NULL)
214*0Sstevel@tonic-gate rfd->rfd_next->rfd_prev = rfd->rfd_prev;
215*0Sstevel@tonic-gate if (tail == rfd)
216*0Sstevel@tonic-gate tail = rfd->rfd_prev;
217*0Sstevel@tonic-gate for (nextclass = rfd->rfd_next; nextclass != NULL;
218*0Sstevel@tonic-gate nextclass = nextclass->rfd_next)
219*0Sstevel@tonic-gate if (nextclass->rfd_class == rfd->rfd_class) {
220*0Sstevel@tonic-gate nextclass->rfd_prev_class =
221*0Sstevel@tonic-gate rfd->rfd_prev_class;
222*0Sstevel@tonic-gate break;
223*0Sstevel@tonic-gate }
224*0Sstevel@tonic-gate free(rfd);
225*0Sstevel@tonic-gate #ifdef DEBUG
226*0Sstevel@tonic-gate freed = 1;
227*0Sstevel@tonic-gate #endif /* DEBUG */
228*0Sstevel@tonic-gate break;
229*0Sstevel@tonic-gate }
230*0Sstevel@tonic-gate rfd = rfdprev;
231*0Sstevel@tonic-gate }
232*0Sstevel@tonic-gate ASSERT(freed == 1);
233*0Sstevel@tonic-gate return (close(fd));
234*0Sstevel@tonic-gate }
235*0Sstevel@tonic-gate
236*0Sstevel@tonic-gate /*
237*0Sstevel@tonic-gate * Makes sure at least n descriptors are available. Returns nonzero if
238*0Sstevel@tonic-gate * successful.
239*0Sstevel@tonic-gate */
240*0Sstevel@tonic-gate int
rfd_reserve(int n)241*0Sstevel@tonic-gate rfd_reserve(int n)
242*0Sstevel@tonic-gate {
243*0Sstevel@tonic-gate int i;
244*0Sstevel@tonic-gate int fd = 0;
245*0Sstevel@tonic-gate rfd_t *otail = NULL;
246*0Sstevel@tonic-gate rfd_t *rfdnext;
247*0Sstevel@tonic-gate
248*0Sstevel@tonic-gate for (i = 0; i < n && fd >= 0; i++) {
249*0Sstevel@tonic-gate /*
250*0Sstevel@tonic-gate * rfd_open() will append as many RFD_RESERVED-clsas
251*0Sstevel@tonic-gate * descriptors to the current tail as are requested, revoking
252*0Sstevel@tonic-gate * non-RFD_RESERVED-class descriptors until nothing else can be
253*0Sstevel@tonic-gate * revoked or the reservation is met.
254*0Sstevel@tonic-gate */
255*0Sstevel@tonic-gate fd = rfd_open("/dev/null", 1, RFD_RESERVED, NULL, NULL,
256*0Sstevel@tonic-gate O_RDONLY, 0);
257*0Sstevel@tonic-gate if (otail == NULL)
258*0Sstevel@tonic-gate otail = tail;
259*0Sstevel@tonic-gate }
260*0Sstevel@tonic-gate
261*0Sstevel@tonic-gate if (fd == -1)
262*0Sstevel@tonic-gate debug("couldn't allocate %d descriptors\n", n);
263*0Sstevel@tonic-gate
264*0Sstevel@tonic-gate while (otail != NULL) {
265*0Sstevel@tonic-gate rfdnext = otail->rfd_next;
266*0Sstevel@tonic-gate (void) rfd_close(otail->rfd_fd);
267*0Sstevel@tonic-gate otail = rfdnext;
268*0Sstevel@tonic-gate }
269*0Sstevel@tonic-gate
270*0Sstevel@tonic-gate return (fd != -1);
271*0Sstevel@tonic-gate }
272