1use std::{
2 mem,
3 os::fd::{AsRawFd as _, BorrowedFd, IntoRawFd as _, OwnedFd},
4};
5
6use crate::{
7 Errno,
8 io::fd,
9 kernel::{self, errno},
10};
11
12pub mod raw;
13
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub enum Failure {
16 Get(Errno),
17 Set(Errno),
18}
19
20pub trait Flags: From<Failure> + Sized {
21 const READ: libc::c_int;
22 const WRITE: libc::c_int;
23 const BIT: libc::c_int;
24}
25
26pub trait Fd: errno::Source {
28 fn try_limit() -> Result<(libc::rlim_t, libc::rlim_t), Errno> {
29 let mut limit = mem::MaybeUninit::uninit();
30 let result = unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, limit.as_mut_ptr()) };
32 unsafe { kernel::raw::Status::new(result).try_zero::<Self, Errno>() }?;
34 let limit = unsafe { limit.assume_init() };
36 Ok((limit.rlim_cur, limit.rlim_max))
37 }
38
39 fn update<E: Flags>(file: BorrowedFd<'_>) -> Result<(), E> {
41 update::<Self, E, true, true>(file)
42 }
43
44 fn remove<E: Flags>(file: BorrowedFd<'_>) -> Result<(), E> {
46 update::<Self, E, true, false>(file)
47 }
48
49 fn try_update<E: Flags>(file: BorrowedFd<'_>) -> Result<(), E> {
51 update::<Self, E, false, true>(file)
52 }
53
54 fn close<E: From<Errno>>(file: OwnedFd) -> Result<(), E> {
57 let raw = unsafe { libc::close(file.into_raw_fd()) };
59 unsafe { kernel::raw::Status::new(raw).try_zero::<Self, E>() }
61 }
62
63 fn duplicate<E: From<Errno>>(file: BorrowedFd<'_>) -> Result<OwnedFd, E> {
64 let raw = unsafe { libc::fcntl(file.as_raw_fd(), libc::F_DUPFD_CLOEXEC, 0) };
66 unsafe { <OwnedFd as fd::raw::Adopt>::try_adopt::<Self, E>(raw) }
68 }
69
70 fn set<const COMMAND: libc::c_int, E: From<Errno>>(
71 file: BorrowedFd<'_>,
72 flags: libc::c_int,
73 ) -> Result<(), E> {
74 set::<Self, E, COMMAND, false>(file, flags, E::from)
75 }
76
77 fn set_retry<const COMMAND: libc::c_int, E: From<Errno>>(
79 file: BorrowedFd<'_>,
80 flags: libc::c_int,
81 ) -> Result<(), E> {
82 set::<Self, E, COMMAND, true>(file, flags, E::from)
83 }
84 fn read<'a, E: From<Errno>>(
85 file: BorrowedFd<'_>,
86 output: &'a mut [u8],
87 ) -> Result<&'a mut [u8], E> {
88 let raw = unsafe { libc::read(file.as_raw_fd(), output.as_mut_ptr().cast(), output.len()) };
90 unsafe { crate::io::raw::Buffer::try_native::<Self, E>(output, raw) }
92 }
93
94 fn write<'a, E: From<Errno>>(file: BorrowedFd<'_>, input: &'a [u8]) -> Result<&'a [u8], E> {
95 let raw = raw::write(file, input);
96 unsafe { crate::io::raw::Buffer::try_native::<Self, E>(input, raw) }
98 }
99}
100
101impl<T: errno::Source> Fd for T {}
102
103fn update<S: Fd + ?Sized, E: Flags, const RETRY: bool, const ENABLED: bool>(
104 file: BorrowedFd<'_>,
105) -> Result<(), E> {
106 const {
107 assert!(
108 (E::READ == libc::F_GETFD && E::WRITE == libc::F_SETFD)
109 || (E::READ == libc::F_GETFL && E::WRITE == libc::F_SETFL)
110 );
111 }
112 let flags = unsafe {
115 kernel::raw::try_retry::<_, E, RETRY>(
116 || {
117 kernel::raw::Status::new(libc::fcntl(file.as_raw_fd(), E::READ))
118 .try_value::<S, Errno>()
119 },
120 |error| E::from(Failure::Get(error)),
121 )
122 }?
123 .get();
124 let flags = if ENABLED {
125 flags | E::BIT
126 } else {
127 flags & !E::BIT
128 };
129 if E::WRITE == libc::F_SETFD {
130 set::<S, E, { libc::F_SETFD }, RETRY>(file, flags, |error| E::from(Failure::Set(error)))
131 } else {
132 set::<S, E, { libc::F_SETFL }, RETRY>(file, flags, |error| E::from(Failure::Set(error)))
133 }
134}
135
136fn set<S: Fd + ?Sized, E, const COMMAND: libc::c_int, const RETRY: bool>(
137 file: BorrowedFd<'_>,
138 flags: libc::c_int,
139 classify: impl FnOnce(Errno) -> E,
140) -> Result<(), E> {
141 const { assert!(COMMAND == libc::F_SETFD || COMMAND == libc::F_SETFL) };
142 unsafe {
145 kernel::raw::try_retry::<_, E, RETRY>(
146 || {
147 kernel::raw::Status::new(libc::fcntl(file.as_raw_fd(), COMMAND, flags))
148 .try_zero::<S, Errno>()
149 },
150 classify,
151 )
152 }
153}