1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use super::abi::*;
use crate::{
    result_from_value,
    AsRawFd,
    Error,
};

pub const F_GETFD: u32 = 1;
pub const F_SETFD: u32 = 2;

#[inline]
#[allow(clippy::missing_safety_doc)]
pub unsafe fn fcntl(fd: &impl AsRawFd, cmd: u32, arg: usize) -> Result<isize, Error> {
    let ret = syscall_3(72, fd.as_raw_fd() as usize, cmd as usize, arg) as isize;
    result_from_value(ret)
}

#[inline]
pub fn fcntl_getfd(fd: &impl AsRawFd) -> Result<i32, Error> {
    // SAFETY: There isn't any pointer that `F_GETFD` could dereference, and the worst thing that
    // could happen is that the `fd` is invalid and the kernel returns an error.
    Ok(unsafe { fcntl(fd, F_GETFD, 0)? as i32 })
}

#[inline]
pub fn fcntl_setfd(fd: &impl AsRawFd, flags: i32) -> Result<i32, Error> {
    // SAFETY: There isn't any pointer that `F_GETFD` could dereference, and the worst thing that
    // could happen is that the `fd` or `flags` are invalid and the kernel returns an error.
    Ok(unsafe { fcntl(fd, F_SETFD, flags as usize)? as i32 })
}