close_fds

crates.io Docs GitHub Actions Cirrus CI codecov

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)

Tier 2: "Guaranteed to build" (built, but not tested, in CI)

Tier 3: "Should work"

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:

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.