Skip to content

Commit df61816

Browse files
authored
Add FIO(N)CLEX ioctl (#1400)
1 parent 5930dc0 commit df61816

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/backend/linux_raw/c.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub(crate) type size_t = usize;
1010
pub(crate) use linux_raw_sys::ctypes::*;
1111
pub(crate) use linux_raw_sys::errno::{EBADF, EINVAL};
1212
pub(crate) use linux_raw_sys::general::{__kernel_fd_set as fd_set, __FD_SETSIZE as FD_SETSIZE};
13-
pub(crate) use linux_raw_sys::ioctl::{FIONBIO, FIONREAD};
13+
pub(crate) use linux_raw_sys::ioctl::{FIOCLEX, FIONBIO, FIONCLEX, FIONREAD};
1414
// Import the kernel's `uid_t` and `gid_t` if they're 32-bit.
1515
#[cfg(feature = "thread")]
1616
pub(crate) use linux_raw_sys::general::futex_waitv;

src/io/ioctl.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ use backend::fd::AsFd;
1616
///
1717
/// This is similar to `fcntl(fd, F_SETFD, FD_CLOEXEC)`, except that it avoids
1818
/// clearing any other flags that might be set.
19-
#[cfg(apple)]
19+
///
20+
/// Linux: Note that `ioctl` can not be used on `OFlags::PATH` file
21+
/// descriptors.
22+
#[cfg(any(apple, linux_kernel))]
2023
#[inline]
2124
#[doc(alias = "FIOCLEX")]
2225
#[doc(alias = "FD_CLOEXEC")]
@@ -28,6 +31,24 @@ pub fn ioctl_fioclex<Fd: AsFd>(fd: Fd) -> io::Result<()> {
2831
}
2932
}
3033

34+
/// `ioctl(fd, FIONCLEX, NULL)`—Remove the close-on-exec flag.
35+
///
36+
/// This is similar to `fcntl_setfd(fd, FdFlags::empty())`, except that it avoids
37+
/// clearing any other flags that might be set.
38+
///
39+
/// Linux: Note that `ioctl` can not be used on `OFlags::PATH` file
40+
/// descriptors.
41+
#[cfg(any(apple, linux_kernel))]
42+
#[inline]
43+
#[doc(alias = "FIONCLEX")]
44+
pub fn ioctl_fionclex<Fd: AsFd>(fd: Fd) -> io::Result<()> {
45+
// SAFETY: `FIONCLEX` is a no-argument setter opcode.
46+
unsafe {
47+
let ctl = ioctl::NoArg::<{ c::FIONCLEX }>::new();
48+
ioctl::ioctl(fd, ctl)
49+
}
50+
}
51+
3152
/// `ioctl(fd, FIONBIO, &value)`—Enables or disables non-blocking mode.
3253
///
3354
/// # References

0 commit comments

Comments
 (0)