standard-ai / sendfd

Send file descriptors over unix sockets in Rust
Apache License 2.0
22 stars 10 forks source link

Consider using MSG_CMSG_CLOEXEC flag #16

Open FuuuOverclocking opened 1 month ago

FuuuOverclocking commented 1 month ago
MSG_CMSG_CLOEXEC (recvmsg() only; since Linux 2.6.23)
              Set the close-on-exec flag for the file descriptor
              received via a UNIX domain file descriptor using the
              SCM_RIGHTS operation (described in [unix(7)](https://man7.org/linux/man-pages/man7/unix.7.html)).  This flag is
              useful for the same reasons as the O_CLOEXEC flag of
              [open(2)](https://man7.org/linux/man-pages/man2/open.2.html).

Consider using MSG_CMSG_CLOEXEC flag, close-on-exec should be default behavior.

Links:

FuuuOverclocking commented 1 month ago

17

cbranch commented 3 weeks ago

Rust-created files and sockets have O_CLOEXEC set, e.g. https://github.com/rust-lang/rust/blob/4d215e2426d52ca8d1af166d5f6b5e172afbff67/library/std/src/sys/pal/unix/fs.rs#L1177 https://github.com/rust-lang/rust/blob/4d215e2426d52ca8d1af166d5f6b5e172afbff67/library/std/src/sys/pal/unix/net.rs#L85

While it may not be possible to set this on non-Linux systems as part of the recvmsg call, this crate should either set it immediately on reception of the file descriptors, or else document that the returned file descriptors do not have this flag set.

nagisa commented 3 weeks ago

I'm happy to take a PR that implements it in a portable way (even if the implementation ends up slightly racy.)

MSG_CMSG_CLOEXEC is supported on Linux, FreeBSD and various other BSDs but notably not on Darwin. So a fallback will be necessary.