How to pass FD_CLOEXEC flag when opening file?

Hi,

I'm trying to open a file passing certain flags to control some aspects of how the file descriptor is managed, and in particular I wanted to pass the FD_CLOEXEC flag to ensure that the file descriptor will be closed when we execute exec or fork (although even having those flags, I'll take care of closing these file descriptors "manually" as soon as possible).

The problem is that std::fs::File::open does not allow to pass any flags, and when using std::fs::OpenOptions I don't have much more control than in the previous case, just being able to pass the "typical" flags controlling whether we can read or write.

Is there any way to use more advanced POSIX features when dealing with file descriptors?

Thank you for your insights.

Ok, according to this post ( https://fraserblog.codewise.org/rust-and-file-descriptors/ ), this flag is automatically enabled by the Rust library.

APIs like this are accessible through the nix crate.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.