1 .\" Copyright (c) 2020 Stephen Kitt <steve@sk2.org>
2 .\" and Copyright (c) 2021 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH CLOSE_RANGE 2 2021-03-22 "Linux" "Linux Programmer's Manual"
28 close_range \- close all file descriptors in a given range
31 .B #include <linux/close_range.h>
33 .BI "int close_range(unsigned int " first ", unsigned int " last ,
34 .BI " unsigned int " flags );
38 There is no glibc wrapper for this system call; see NOTES.
42 system call closes all open file descriptors from
48 Errors closing a given file descriptor are currently ignored.
51 is a bit mask containing 0 or more of the following:
53 .BR CLOSE_RANGE_CLOEXEC " (since Linux 5.11)"
54 Set the close-on-exec flag on the specified file descriptors,
55 rather than immediately closing them.
57 .B CLOSE_RANGE_UNSHARE
58 Unshare the specified file descriptors from any other processes
60 avoiding races with other threads sharing the file descriptor table.
65 On error, \-1 is returned and
67 is set to indicate the error.
77 The following can occur with
78 .B CLOSE_RANGE_UNSHARE
79 (when constructing the new descriptor table):
82 The number of open file descriptors exceeds the limit specified in
83 .IR /proc/sys/fs/nr_open
86 This error can occur in situations where that limit was lowered before
90 .B CLOSE_RANGE_UNSHARE
94 Insufficient kernel memory was available.
97 first appeared in Linux 5.9.
98 Library support was added in glibc in version 2.34.
101 is a nonstandard function that is also present on FreeBSD.
103 Glibc does not provide a wrapper for this system call; call it using
105 .SS Closing all open file descriptors
106 .\" 278a5fbaed89dacd04e9d052f4594ffd0e0585de
107 To avoid blindly closing file descriptors
108 in the range of possible file descriptors,
109 this is sometimes implemented (on Linux)
110 by listing open file descriptors in
116 can take care of this without requiring
118 and within a single system call,
119 which provides significant performance benefits.
120 .SS Closing file descriptors before exec
121 .\" 60997c3d45d9a67daf01c56d805ae4fec37e0bd8
122 File descriptors can be closed safely using
126 /* we don't want anything past stderr here */
127 close_range(3, ~0U, CLOSE_RANGE_UNSHARE);
132 .B CLOSE_RANGE_UNSHARE
133 is conceptually equivalent to
137 unshare(CLONE_FILES);
138 close_range(first, last, 0);
142 but can be more efficient:
143 if the unshared range extends past
144 the current maximum number of file descriptors allocated
145 in the caller's file descriptor table
146 (the common case when
149 the kernel will unshare a new file descriptor table for the caller up to
151 copying as few file descriptors as possible.
152 This avoids subsequent
155 the whole operation is complete once the table is unshared.
156 .SS Closing files on \fBexec\fP
157 .\" 582f1fb6b721facf04848d2ca57f34468da1813e
158 This is particularly useful in cases where multiple
160 setup steps risk conflicting with each other.
161 For example, setting up a
163 profile can conflict with a
166 if the file descriptors are closed before the
169 the profile setup can't use them itself,
170 or control their closure;
171 if the file descriptors are closed afterwards,
172 the seccomp profile can't block the
174 call or any fallbacks.
176 .B CLOSE_RANGE_CLOEXEC
178 the descriptors can be marked before the
181 and the profile can control access to
183 without affecting the calling process.
185 The program shown below opens the files named in its command-line arguments,
186 displays the list of files that it has opened
187 (by iterating through the entries in
191 to close all file descriptors greater than or equal to 3,
192 and then once more displays the process's list of open files.
193 The following example demonstrates the use of the program:
197 $ \fBtouch /tmp/a /tmp/b /tmp/c\fP
198 $ \fB./a.out /tmp/a /tmp/b /tmp/c\fP
199 /tmp/a opened as FD 3
200 /tmp/b opened as FD 4
201 /tmp/c opened as FD 5
202 /proc/self/fd/0 ==> /dev/pts/1
203 /proc/self/fd/1 ==> /dev/pts/1
204 /proc/self/fd/2 ==> /dev/pts/1
205 /proc/self/fd/3 ==> /tmp/a
206 /proc/self/fd/4 ==> /tmp/b
207 /proc/self/fd/5 ==> /tmp/b
208 /proc/self/fd/6 ==> /proc/9005/fd
209 ========= About to call close_range() =======
210 /proc/self/fd/0 ==> /dev/pts/1
211 /proc/self/fd/1 ==> /dev/pts/1
212 /proc/self/fd/2 ==> /dev/pts/1
213 /proc/self/fd/3 ==> /proc/9005/fd
217 Note that the lines showing the pathname
219 result from the calls to
226 #include <linux/close_range.h>
229 #include <sys/syscall.h>
234 /* Show the contents of the symbolic links in /proc/self/fd */
239 DIR *dirp = opendir("/proc/self/fd");
246 struct dirent *dp = readdir(dirp);
250 if (dp\->d_type == DT_LNK) {
251 char path[PATH_MAX], target[PATH_MAX];
252 snprintf(path, sizeof(path), "/proc/self/fd/%s",
255 ssize_t len = readlink(path, target, sizeof(target));
256 printf("%s ==> %.*s\en", path, (int) len, target);
264 main(int argc, char *argv[])
266 for (int j = 1; j < argc; j++) {
267 int fd = open(argv[j], O_RDONLY);
272 printf("%s opened as FD %d\en", argv[j], fd);
277 printf("========= About to call close_range() =======\en");
279 if (syscall(__NR_close_range, 3, \(ti0U, 0) == \-1) {
280 perror("close_range");