1 .\" Copyright (c) 2020 Stephen Kitt <steve@sk2.org>
2 .\" and Copyright (c) 2021 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH CLOSE_RANGE 2 2021-08-27 "Linux" "Linux Programmer's Manual"
8 close_range \- close all file descriptors in a given range
11 .RI ( libc ", " \-lc )
14 .B #include <linux/close_range.h>
16 .BI "int close_range(unsigned int " first ", unsigned int " last ,
17 .BI " unsigned int " flags );
22 system call closes all open file descriptors from
28 Errors closing a given file descriptor are currently ignored.
31 is a bit mask containing 0 or more of the following:
33 .BR CLOSE_RANGE_CLOEXEC " (since Linux 5.11)"
34 Set the close-on-exec flag on the specified file descriptors,
35 rather than immediately closing them.
37 .B CLOSE_RANGE_UNSHARE
38 Unshare the specified file descriptors from any other processes
40 avoiding races with other threads sharing the file descriptor table.
45 On error, \-1 is returned and
47 is set to indicate the error.
57 The following can occur with
58 .B CLOSE_RANGE_UNSHARE
59 (when constructing the new descriptor table):
62 The number of open file descriptors exceeds the limit specified in
63 .I /proc/sys/fs/nr_open
66 This error can occur in situations where that limit was lowered before
70 .B CLOSE_RANGE_UNSHARE
74 Insufficient kernel memory was available.
77 first appeared in Linux 5.9.
78 Library support was added in glibc in version 2.34.
81 is a nonstandard function that is also present on FreeBSD.
83 .SS Closing all open file descriptors
84 .\" 278a5fbaed89dacd04e9d052f4594ffd0e0585de
85 To avoid blindly closing file descriptors
86 in the range of possible file descriptors,
87 this is sometimes implemented (on Linux)
88 by listing open file descriptors in
94 can take care of this without requiring
96 and within a single system call,
97 which provides significant performance benefits.
98 .SS Closing file descriptors before exec
99 .\" 60997c3d45d9a67daf01c56d805ae4fec37e0bd8
100 File descriptors can be closed safely using
104 /* we don't want anything past stderr here */
105 close_range(3, ~0U, CLOSE_RANGE_UNSHARE);
110 .B CLOSE_RANGE_UNSHARE
111 is conceptually equivalent to
115 unshare(CLONE_FILES);
116 close_range(first, last, 0);
120 but can be more efficient:
121 if the unshared range extends past
122 the current maximum number of file descriptors allocated
123 in the caller's file descriptor table
124 (the common case when
127 the kernel will unshare a new file descriptor table for the caller up to
129 copying as few file descriptors as possible.
130 This avoids subsequent
133 the whole operation is complete once the table is unshared.
134 .SS Closing files on \fBexec\fP
135 .\" 582f1fb6b721facf04848d2ca57f34468da1813e
136 This is particularly useful in cases where multiple
138 setup steps risk conflicting with each other.
139 For example, setting up a
141 profile can conflict with a
144 if the file descriptors are closed before the
147 the profile setup can't use them itself,
148 or control their closure;
149 if the file descriptors are closed afterwards,
150 the seccomp profile can't block the
152 call or any fallbacks.
154 .B CLOSE_RANGE_CLOEXEC
156 the descriptors can be marked before the
159 and the profile can control access to
161 without affecting the calling process.
163 The program shown below opens the files named in its command-line arguments,
164 displays the list of files that it has opened
165 (by iterating through the entries in
169 to close all file descriptors greater than or equal to 3,
170 and then once more displays the process's list of open files.
171 The following example demonstrates the use of the program:
175 $ \fBtouch /tmp/a /tmp/b /tmp/c\fP
176 $ \fB./a.out /tmp/a /tmp/b /tmp/c\fP
177 /tmp/a opened as FD 3
178 /tmp/b opened as FD 4
179 /tmp/c opened as FD 5
180 /proc/self/fd/0 ==> /dev/pts/1
181 /proc/self/fd/1 ==> /dev/pts/1
182 /proc/self/fd/2 ==> /dev/pts/1
183 /proc/self/fd/3 ==> /tmp/a
184 /proc/self/fd/4 ==> /tmp/b
185 /proc/self/fd/5 ==> /tmp/b
186 /proc/self/fd/6 ==> /proc/9005/fd
187 ========= About to call close_range() =======
188 /proc/self/fd/0 ==> /dev/pts/1
189 /proc/self/fd/1 ==> /dev/pts/1
190 /proc/self/fd/2 ==> /dev/pts/1
191 /proc/self/fd/3 ==> /proc/9005/fd
195 Note that the lines showing the pathname
197 result from the calls to
201 .\" SRC BEGIN (close_range.c)
209 #include <sys/syscall.h>
212 /* Show the contents of the symbolic links in /proc/self/fd */
218 char path[PATH_MAX], target[PATH_MAX];
222 dirp = opendir("/proc/self/fd");
233 if (dp\->d_type == DT_LNK) {
234 snprintf(path, sizeof(path), "/proc/self/fd/%s",
237 len = readlink(path, target, sizeof(target));
238 printf("%s ==> %.*s\en", path, (int) len, target);
246 main(int argc, char *argv[])
250 for (int j = 1; j < argc; j++) {
251 fd = open(argv[j], O_RDONLY);
256 printf("%s opened as FD %d\en", argv[j], fd);
261 printf("========= About to call close_range() =======\en");
263 if (syscall(SYS_close_range, 3, \(ti0U, 0) == \-1) {
264 perror("close_range");