close_fds
A small Rust library that makes it easy to close all open file descriptors.
OS support
close_fds
has three OS support tiers, similar to Rust's support tiers:
Tier 1: "Guaranteed to work" (tested in CI)
- Linux (glibc and musl)
- macOS
- FreeBSD
- Windows (MSVC and GNU toolchains)
Note:
close_fds
does NOT have support for closing open file handles; just file descriptors.
Tier 2: "Guaranteed to build" (built, but not tested, in CI)
- NetBSD
Tier 3: "Should work"
- OpenBSD
- DragonflyBSD
Note: As stated in the license, close_fds
comes with no warranty.
OS-specific notes
Here is a list of the methods that iter_open_fds()
, iter_possible_fds()
, and close_open_fds()
will try on various platforms to improve performance when listing the open file descriptors:
- Linux
/proc/self/fd
if/proc
is mounted (very efficient)
- macOS
/dev/fd
(very efficient)
- FreeBSD
/dev/fd
if anfdescfs
appears to be mounted there (very efficient)- The
kern.proc.nfds
sysctl to get the number of open file descriptors (moderately efficient, unless large numbers of file descriptors are open)
- NetBSD
fcntl(0, F_MAXFD)
to get the maximum open file descriptor (moderately efficient)
On the BSDs, close_open_fds()
may also call closefrom()
, which is very efficient.
If none of the methods listed above are available, it will fall back on a simple loop through every possible file descriptor number -- from minfd
to sysconf(_SC_OPEN_MAX)
(getmaxstdio()
on Windows).
Note: The most common use case, close_open_fds(3, &[])
, is very efficient on Linux (with /proc
mounted), macOS, and all of the BSDs.