diff --git a/Cargo.toml b/Cargo.toml index a3d70c9..ff07d26 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "mapped-file" description = "Construct a memory mapping over any file object" keywords = ["unix", "mmap", "generic", "file", "fd"] -version = "0.0.8" +version = "0.0.8-r1" edition = "2021" repository="https://github.com/notflan/mapped-file" license="MIT" @@ -10,10 +10,18 @@ license="MIT" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] +default = ["default-cloexec"] + # Add support for file-descriptor operations other than simply mapping # TODO: XXX: Incomplete, will be enabled by default when complete file=[] +# Default to opening any internal file-descriptors as `O_CLOEXEC` (close-on-execute.) +# For `file` feature: `MFD_CLOEXEC`/`FD_CLOEXEC` will be used in creation of all memfds by default. +# +# NOTE: This is currently not configureable within the interface. To prevent API-break, this feature exists to allow some configuration of this option. (TODO: In the future, this should be in the interface for `MemoryFile`/`NamedMemoryFile` et al.) +default-cloexec = [] + [dependencies] lazy_static = "1.4.0" libc = "0.2.132" diff --git a/src/file/memory.rs b/src/file/memory.rs index 9dfbba2..1c14231 100644 --- a/src/file/memory.rs +++ b/src/file/memory.rs @@ -28,7 +28,7 @@ static UNNAMED: &'static CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"\0") }; -const DEFAULT_FLAGS: c_uint = MFD_CLOEXEC; +const DEFAULT_FLAGS: c_uint = if cfg!(feature="default-cloexec") { MFD_CLOEXEC } else { 0 }; #[inline(always)] //XXX: Is the static bound required here? @@ -235,3 +235,14 @@ impl From for std::fs::File } raw::impl_io_for_fd!(MemoryFile => .0.as_raw_fd()); + +#[cfg(test)] +mod test { + #[test] + fn default_flag_cloexec_visible() + { + println!("Default flags: {} (default-cloexec: {})", super::DEFAULT_FLAGS, cfg!(feature="default-cloexec")); + + assert_eq!(super::DEFAULT_FLAGS, cfg!(feature="default-cloexec").then(|| super::MFD_CLOEXEC).unwrap_or_default(), "Compile-time default creation flags are not in accordance with provided global crate configuration"); + } +}