diff --git a/src/unistd.rs b/src/unistd.rs index b414866cdd..27ec5cb497 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -4001,12 +4001,13 @@ pub fn chflags(path: &P, flags: FileFlag) -> Result<()> { libc_bitflags! { /// Options for close_range() #[cfg_attr(docsrs, doc(cfg(feature = "fs")))] - pub struct CloseRangeFlags : c_uint { + pub struct CloseRangeFlags : c_int { #[cfg(all(target_os = "linux", target_env = "gnu"))] - /// Unshare the file descriptors range before closing them - CLOSE_RANGE_UNSHARE; - /// Set the close-on-exec flag on the file descriptors range - CLOSE_RANGE_CLOEXEC; + /// Unshare the file descriptors table, then close the file descriptors specified in the + /// range + CLOSE_RANGE_UNSHARE as c_int; + /// Set the close-on-exec flag on the file descriptors range instead of closing them + CLOSE_RANGE_CLOEXEC as c_int; } } @@ -4019,17 +4020,15 @@ feature! { all(target_os = "linux", target_env = "gnu"), target_os = "freebsd" ))] -pub fn close_range(fdbegin: F, fdlast: F, flags: CloseRangeFlags) -> Result> { - use std::os::fd::AsRawFd; - +pub unsafe fn close_range(fdbegin: F, fdlast: F, flags: CloseRangeFlags) -> Result> { let raw = unsafe { Errno::clear(); cfg_if! { if #[cfg(all(target_os = "linux", target_env = "gnu"))] { - libc::syscall(libc::SYS_close_range, fdbegin.as_fd().as_raw_fd() as u32, fdlast.as_fd().as_raw_fd() as u32, flags.bits() as i32) + libc::syscall(libc::SYS_close_range, fdbegin.as_raw_fd() as u32, fdlast.as_raw_fd() as u32, flags.bits()) } else { - libc::close_range(fdbegin.as_fd().as_raw_fd() as u32, fdlast.as_fd().as_raw_fd() as u32, flags.bits() as i32) + libc::close_range(fdbegin.as_raw_fd() as u32, fdlast.as_raw_fd() as u32, flags.bits()) } } }; diff --git a/test/test_unistd.rs b/test/test_unistd.rs index 61dbc812e0..bd7993edd4 100644 --- a/test/test_unistd.rs +++ b/test/test_unistd.rs @@ -1401,14 +1401,17 @@ fn test_group_from() { fn test_close_range() { use tempfile::NamedTempFile; const CONTENTS: &[u8] = b"abcdef123456"; - let mut tempfile1 = NamedTempFile::new().unwrap(); - let mut tempfile2 = NamedTempFile::new().unwrap(); - let mut tempfile3 = NamedTempFile::new().unwrap(); - tempfile3.write_all(CONTENTS).unwrap(); - tempfile2.write_all(CONTENTS).unwrap(); - tempfile1.write_all(CONTENTS).unwrap(); + let mut tempfile: [NamedTempFile; 3] = [ + NamedTempFile::new().unwrap(), + NamedTempFile::new().unwrap(), + NamedTempFile::new().unwrap(), + ]; + + for tf in &mut tempfile { + let _ = tf.write_all(CONTENTS); + } let areclosed = - close_range(tempfile1, tempfile3, CloseRangeFlags::CLOSE_RANGE_CLOEXEC); + unsafe { close_range(tempfile[0].as_file().as_fd(), tempfile[2].as_file().as_fd(), CloseRangeFlags::CLOSE_RANGE_CLOEXEC) }; assert_eq!( areclosed .expect("close_range failed")