A small Rust library that makes it easy to close all open file descriptors.
close_fds has three OS support tiers, similar to Rust's support tiers:
close_fds does NOT have support for closing open file handles; just file descriptors.Note: As stated in the license, close_fds comes with no warranty.
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:
/proc/self/fd if /proc is mounted (very efficient)/dev/fd (very efficient)/dev/fd if an fdescfs appears to be mounted there (very efficient)kern.proc.nfds sysctl to get the number of open file descriptors (moderately efficient, unless large numbers of file descriptors are open)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.