Skip to content

Commit b3cdb59

Browse files
committed
Add FIO(N)CLEX ioctl
1 parent d50036a commit b3cdb59

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-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: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ 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 `O_PATH` file descriptors.
21+
#[cfg(any(apple, linux_kernel))]
2022
#[inline]
2123
#[doc(alias = "FIOCLEX")]
2224
#[doc(alias = "FD_CLOEXEC")]
@@ -28,6 +30,23 @@ pub fn ioctl_fioclex<Fd: AsFd>(fd: Fd) -> io::Result<()> {
2830
}
2931
}
3032

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

0 commit comments

Comments
 (0)