2 .\" Copyright (c) 2000, Sun Microsystems, Inc. All Rights Reserved.
3 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
4 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
5 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
6 .TH CLOSEFROM 3C "Apr 27, 2000"
8 closefrom, fdwalk \- close or iterate over open file descriptors
14 \fBvoid\fR \fBclosefrom\fR(\fBint\fR \fIlowfd\fR);
19 \fBint\fR \fBfdwalk\fR(\fBint\fR (*\fIfunc\fR)(\fBvoid\fR *, \fBint\fR), \fBvoid *\fR\fIcd\fR);
25 The \fBclosefrom()\fR function calls \fBclose\fR(2) on all open file
26 descriptors greater than or equal to \fIlowfd\fR.
29 The effect of \fBclosefrom\fR(\fIlowfd\fR) is the same as the code
33 #include <sys/resource.h>
37 getrlimit(RLIMIT_NOFILE, &rl);
38 for (i = lowfd; i < rl.rlim_max; i++)
45 except that \fBclose()\fR is called only on file descriptors that are actually
46 open, not on every possible file descriptor greater than or equal to
47 \fIlowfd\fR, and \fBclose()\fR is also called on any open file descriptors
48 greater than or equal to \fBrl.rlim_max\fR (and \fIlowfd\fR), should any exist.
51 The \fBfdwalk()\fR function first makes a list of all currently open file
52 descriptors. Then for each file descriptor in the list, it calls the
53 user-defined function, \fIfunc\fR(\fIcd\fR, \fIfd\fR), passing it the pointer
54 to the callback data, \fIcd\fR, and the value of the file descriptor from the
55 list, \fIfd\fR. The list is processed in file descriptor value order, lowest
59 If \fIfunc\fR() returns a non-zero value, the iteration over the list is
60 terminated and \fBfdwalk()\fR returns the non-zero value returned by
61 \fIfunc\fR(). Otherwise, \fBfdwalk()\fR returns 0 after having called
62 \fIfunc\fR() for every file descriptor in the list.
65 The \fBfdwalk()\fR function can be used for fine-grained control over the
66 closing of file descriptors. For example, the \fBclosefrom()\fR function can
72 close_func(void *lowfdp, int fd)
74 if (fd >= *(int *)lowfdp)
82 (void) fdwalk(close_func, &lowfd);
89 The \fBfdwalk()\fR function can then be used to count the number of open files
94 No return value is defined for \fBclosefrom()\fR. If \fBclose()\fR fails for
95 any of the open file descriptors, the error is ignored and the file descriptors
96 whose \fBclose()\fR operation failed might remain open on return from
100 The \fBfdwalk()\fR function returns the return value of the last call to the
101 callback function \fIfunc\fR(), or 0 if \fIfunc\fR() is never called (no open
106 No errors are defined. The \fBclosefrom()\fR and \fBfdwalk()\fR functions do
107 not set \fBerrno\fR but \fBerrno\fR can be set by \fBclose()\fR or by another
108 function called by the callback function, \fIfunc\fR().
113 \fB\fB/proc/self/fd\fR\fR
116 directory (list of open files)
122 The act of closing all open file descriptors should be performed only as the
123 first action of a daemon process. Closing file descriptors that are in use
124 elsewhere in the current process normally leads to disastrous results.
128 See \fBattributes\fR(5) for descriptions of the following attributes:
136 ATTRIBUTE TYPE ATTRIBUTE VALUE
144 \fBclose\fR(2), \fBgetrlimit\fR(2), \fBproc\fR(4), \fBattributes\fR(5)