1closefrom(3C)            Standard C Library Functions            closefrom(3C)
2
3
4

NAME

6       closefrom, fdwalk - close or iterate over open file descriptors
7

SYNOPSIS

9       #include <stdlib.h>
10
11       void closefrom(int lowfd);
12
13
14       int fdwalk(int (*func)(void *, int), void *cd);
15
16

DESCRIPTION

18       The  closefrom()  function  calls close(2) on all open file descriptors
19       greater than or equal to lowfd.
20
21
22       The effect of closefrom(lowfd) is the same as the code
23
24         #include <sys/resource.h>
25         struct rlimit rl;
26         int i;
27
28         getrlimit(RLIMIT_NOFILE, &rl);
29         for (i = lowfd; i < rl.rlim_max; i++)
30              (void) close(i);
31
32
33
34       except that close() is called only on file descriptors that  are  actu‐
35       ally  open, not on every possible file descriptor greater than or equal
36       to lowfd, and close() is also  called  on  any  open  file  descriptors
37       greater than or equal to rl.rlim_max (and lowfd), should any exist.
38
39
40       The  fdwalk()  function  first  makes a list of all currently open file
41       descriptors. Then for each file descriptor in the list,  it  calls  the
42       user-defined  function,  func(cd,  fd),  passing  it the pointer to the
43       callback data, cd, and the value of the file descriptor from the  list,
44       fd.   The  list  is  processed  in  file descriptor value order, lowest
45       numeric value first.
46
47
48       If func() returns a non-zero value, the iteration over the list is ter‐
49       minated  and  fdwalk()  returns  the non-zero value returned by func().
50       Otherwise, fdwalk() returns 0 after having called func() for every file
51       descriptor in the list.
52
53
54       The  fdwalk()  function  can  be used for fine-grained control over the
55       closing of file descriptors.  For example, the closefrom() function can
56       be implemented as:
57
58         static int
59         close_func(void *lowfdp, int fd)
60         {
61              if (fd >= *(int *)lowfdp)
62                   (void) close(fd);
63              return (0);
64         }
65
66         void
67         closefrom(int lowfd)
68         {
69              (void) fdwalk(close_func, &lowfd);
70         }
71
72
73
74       The  fdwalk()  function  can  then  be used to count the number of open
75       files in the process.
76

RETURN VALUES

78       No return value is defined for closefrom(). If close() fails for any of
79       the  open  file descriptors, the error is ignored and the file descrip‐
80       tors whose close() operation failed might remain open  on  return  from
81       closefrom().
82
83
84       The  fdwalk() function returns the return value of the last call to the
85       callback function func(), or 0 if  func()  is  never  called  (no  open
86       files).
87

ERRORS

89       No  errors  are  defined. The closefrom() and fdwalk() functions do not
90       set errno but errno can be set by close() or by another function called
91       by the callback function, func().
92

FILES

94       /proc/self/fd    directory (list of open files)
95
96

USAGE

98       The  act  of closing all open file descriptors should be performed only
99       as the first action of a daemon process.  Closing file descriptors that
100       are  in  use  elsewhere in the current process normally leads to disas‐
101       trous results.
102

ATTRIBUTES

104       See attributes(5)  for descriptions of the following attributes:
105
106
107
108
109       ┌─────────────────────────────┬─────────────────────────────┐
110       │      ATTRIBUTE TYPE         │      ATTRIBUTE VALUE        │
111       ├─────────────────────────────┼─────────────────────────────┤
112       │MT-Level                     │Unsafe                       │
113       └─────────────────────────────┴─────────────────────────────┘
114

SEE ALSO

116       close(2), getrlimit(2), proc(4), attributes(5)
117
118
119
120SunOS 5.11                        27 Apr 2000                    closefrom(3C)
Impressum