Skip to content

Commit

Permalink
address some of the concerns
Browse files Browse the repository at this point in the history
  • Loading branch information
devnexen committed Nov 11, 2024
1 parent 7c83399 commit a3ca8a8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
19 changes: 9 additions & 10 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4001,12 +4001,13 @@ pub fn chflags<P: ?Sized + NixPath>(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;
}
}

Expand All @@ -4019,17 +4020,15 @@ feature! {
all(target_os = "linux", target_env = "gnu"),
target_os = "freebsd"
))]
pub fn close_range<F: std::os::fd::AsFd>(fdbegin: F, fdlast: F, flags: CloseRangeFlags) -> Result<Option<c_int>> {
use std::os::fd::AsRawFd;

pub unsafe fn close_range<F: std::os::fd::AsRawFd>(fdbegin: F, fdlast: F, flags: CloseRangeFlags) -> Result<Option<c_int>> {
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())
}
}
};
Expand Down
17 changes: 10 additions & 7 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit a3ca8a8

Please sign in to comment.