linux_api/
close_range.rs

1use linux_syscall::{Result64, syscall};
2
3use crate::bindings;
4use crate::errno::Errno;
5use crate::posix_types::RawFd;
6
7bitflags::bitflags! {
8    /// Flags that can be used in the `flags` argument for the `close_range` syscall.
9    #[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
10    pub struct CloseRangeFlags: u32 {
11        const CLOSE_RANGE_CLOEXEC = bindings::LINUX_CLOSE_RANGE_CLOEXEC;
12        const CLOSE_RANGE_UNSHARE = bindings::LINUX_CLOSE_RANGE_UNSHARE;
13    }
14}
15
16pub fn close_range_raw(
17    first: core::ffi::c_uint,
18    last: core::ffi::c_uint,
19    flags: core::ffi::c_uint,
20) -> Result<core::ffi::c_int, Errno> {
21    unsafe { syscall!(linux_syscall::SYS_close_range, first, last, flags) }
22        .try_i64()
23        // the linux x86-64 syscall implementation returns an int so I don't think this should ever fail
24        .map(|x| x.try_into().expect("close_range() returned invalid int"))
25        .map_err(Errno::from)
26}
27
28pub fn close_range(
29    first: RawFd,
30    last: RawFd,
31    flags: CloseRangeFlags,
32) -> Result<core::ffi::c_int, Errno> {
33    close_range_raw(
34        first as core::ffi::c_uint,
35        last as core::ffi::c_uint,
36        flags.bits(),
37    )
38}