Skip to content

Commit 8da70cb

Browse files
authored
Miscellaneous documentation and clippy fixes for epoll. (#1135)
1 parent e2014c5 commit 8da70cb

3 files changed

Lines changed: 36 additions & 17 deletions

File tree

src/backend/libc/event/syscalls.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::backend::c;
44
#[cfg(any(linux_kernel, target_os = "redox"))]
55
use crate::backend::conv::ret_u32;
6-
use crate::backend::conv::{ret, ret_c_int, ret_owned_fd};
6+
use crate::backend::conv::{borrowed_fd, ret, ret_c_int, ret_owned_fd};
77
#[cfg(solarish)]
88
use crate::event::port::Event;
99
#[cfg(any(
@@ -14,11 +14,14 @@ use crate::event::port::Event;
1414
))]
1515
use crate::event::EventfdFlags;
1616
use crate::event::PollFd;
17-
use crate::fd::OwnedFd;
17+
use crate::fd::{BorrowedFd, OwnedFd};
1818
use crate::io;
19+
#[cfg(solarish)]
1920
use crate::utils::as_mut_ptr;
21+
#[cfg(any(linux_kernel, target_os = "redox"))]
22+
use crate::utils::as_ptr;
23+
use core::mem::MaybeUninit;
2024
use core::ptr::null_mut;
21-
use {crate::backend::conv::borrowed_fd, crate::fd::BorrowedFd, core::mem::MaybeUninit};
2225
#[cfg(all(feature = "alloc", bsd))]
2326
use {crate::event::kqueue::Event, crate::utils::as_ptr, core::ptr::null};
2427

@@ -181,10 +184,10 @@ pub(crate) fn port_send(
181184

182185
#[cfg(not(any(windows, target_os = "redox", target_os = "wasi")))]
183186
pub(crate) fn pause() {
184-
let r = unsafe { libc::pause() };
187+
let r = unsafe { c::pause() };
185188
let errno = libc_errno::errno().0;
186189
debug_assert_eq!(r, -1);
187-
debug_assert_eq!(errno, libc::EINTR);
190+
debug_assert_eq!(errno, c::EINTR);
188191
}
189192

190193
#[inline]
@@ -198,7 +201,7 @@ pub(crate) fn epoll_create(flags: super::epoll::CreateFlags) -> io::Result<Owned
198201
pub(crate) fn epoll_add(
199202
epoll: BorrowedFd<'_>,
200203
source: BorrowedFd,
201-
event: &mut crate::event::epoll::Event,
204+
event: &crate::event::epoll::Event,
202205
) -> io::Result<()> {
203206
// We use our own `Event` struct instead of libc's because
204207
// ours preserves pointer provenance instead of just using a `u64`,
@@ -208,7 +211,8 @@ pub(crate) fn epoll_add(
208211
borrowed_fd(epoll),
209212
c::EPOLL_CTL_ADD,
210213
borrowed_fd(source),
211-
as_mut_ptr(event).cast(),
214+
// The event is read-only even though libc has a non-const pointer.
215+
as_ptr(event) as *mut c::epoll_event,
212216
))
213217
}
214218
}
@@ -218,14 +222,15 @@ pub(crate) fn epoll_add(
218222
pub(crate) fn epoll_mod(
219223
epoll: BorrowedFd<'_>,
220224
source: BorrowedFd,
221-
event: &mut crate::event::epoll::Event,
225+
event: &crate::event::epoll::Event,
222226
) -> io::Result<()> {
223227
unsafe {
224228
ret(c::epoll_ctl(
225229
borrowed_fd(epoll),
226230
c::EPOLL_CTL_MOD,
227231
borrowed_fd(source),
228-
as_mut_ptr(event).cast(),
232+
// The event is read-only even though libc has a non-const pointer.
233+
as_ptr(event) as *mut c::epoll_event,
229234
))
230235
}
231236
}

src/backend/linux_raw/event/syscalls.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::backend::conv::{
1212
use crate::event::{epoll, EventfdFlags, PollFd};
1313
use crate::fd::{BorrowedFd, OwnedFd};
1414
use crate::io;
15+
#[cfg(feature = "alloc")]
1516
use core::mem::MaybeUninit;
1617
use linux_raw_sys::general::{EPOLL_CTL_ADD, EPOLL_CTL_DEL, EPOLL_CTL_MOD};
1718
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
@@ -51,6 +52,7 @@ pub(crate) fn poll(fds: &mut [PollFd<'_>], timeout: c::c_int) -> io::Result<usiz
5152

5253
#[inline]
5354
pub(crate) fn epoll_create(flags: epoll::CreateFlags) -> io::Result<OwnedFd> {
55+
// SAFETY: `__NR_epoll_create1` doesn't access any user memory.
5456
unsafe { ret_owned_fd(syscall_readonly!(__NR_epoll_create1, flags)) }
5557
}
5658

@@ -60,6 +62,8 @@ pub(crate) fn epoll_add(
6062
fd: BorrowedFd,
6163
event: &epoll::Event,
6264
) -> io::Result<()> {
65+
// SAFETY: `__NR_epoll_ctl` with `EPOLL_CTL_ADD` doesn't modify any user
66+
// memory, and it only reads from `event`.
6367
unsafe {
6468
ret(syscall_readonly!(
6569
__NR_epoll_ctl,
@@ -77,6 +81,8 @@ pub(crate) fn epoll_mod(
7781
fd: BorrowedFd,
7882
event: &epoll::Event,
7983
) -> io::Result<()> {
84+
// SAFETY: `__NR_epoll_ctl` with `EPOLL_CTL_MOD` doesn't modify any user
85+
// memory, and it only reads from `event`.
8086
unsafe {
8187
ret(syscall_readonly!(
8288
__NR_epoll_ctl,
@@ -90,6 +96,8 @@ pub(crate) fn epoll_mod(
9096

9197
#[inline]
9298
pub(crate) fn epoll_del(epfd: BorrowedFd<'_>, fd: BorrowedFd) -> io::Result<()> {
99+
// SAFETY: `__NR_epoll_ctl` with `EPOLL_CTL_DEL` doesn't access any user
100+
// memory.
93101
unsafe {
94102
ret(syscall_readonly!(
95103
__NR_epoll_ctl,
@@ -101,13 +109,16 @@ pub(crate) fn epoll_del(epfd: BorrowedFd<'_>, fd: BorrowedFd) -> io::Result<()>
101109
}
102110
}
103111

112+
#[cfg(feature = "alloc")]
104113
#[inline]
105114
pub(crate) fn epoll_wait(
106115
epfd: BorrowedFd<'_>,
107116
events: &mut [MaybeUninit<crate::event::epoll::Event>],
108117
timeout: c::c_int,
109118
) -> io::Result<usize> {
110119
let (buf_addr_mut, buf_len) = slice_mut(events);
120+
// SAFETY: `__NR_epoll_wait` doesn't access any user memory outside of
121+
// the `events` array.
111122
#[cfg(not(any(target_arch = "aarch64", target_arch = "riscv64")))]
112123
unsafe {
113124
ret_usize(syscall!(
@@ -118,6 +129,8 @@ pub(crate) fn epoll_wait(
118129
c_int(timeout)
119130
))
120131
}
132+
// SAFETY: `__NR_epoll_pwait` doesn't access any user memory outside of
133+
// the `events` array, as we don't pass it a `sigmask`.
121134
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
122135
unsafe {
123136
ret_usize(syscall!(

src/event/epoll.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#![allow(unused_qualifications)]
7474

7575
use super::epoll;
76+
#[cfg(feature = "alloc")]
7677
use crate::backend::c;
7778
pub use crate::backend::event::epoll::*;
7879
use crate::backend::event::syscalls;
@@ -102,14 +103,14 @@ pub fn create(flags: epoll::CreateFlags) -> io::Result<OwnedFd> {
102103
/// `epoll_ctl(self, EPOLL_CTL_ADD, data, event)`—Adds an element to an epoll
103104
/// object.
104105
///
105-
/// This registers interest in any of the events set in `event_flags` occurring on
106-
/// the file descriptor associated with `data`.
106+
/// This registers interest in any of the events set in `event_flags` occurring
107+
/// on the file descriptor associated with `data`.
107108
///
108109
/// Note that `close`ing a file descriptor does not necessarily unregister
109-
/// interest which can lead to spurious events being returned from [`epoll::wait`]. If
110-
/// a file descriptor is an `Arc<dyn SystemResource>`, then `epoll` can be
111-
/// thought to maintain a `Weak<dyn SystemResource>` to the file descriptor.
112-
/// Check the [faq] for details.
110+
/// interest which can lead to spurious events being returned from
111+
/// [`epoll::wait`]. If a file descriptor is an `Arc<dyn SystemResource>`, then
112+
/// `epoll` can be thought to maintain a `Weak<dyn SystemResource>` to the file
113+
/// descriptor. Check the [faq] for details.
113114
///
114115
/// # References
115116
/// - [Linux]
@@ -127,7 +128,7 @@ pub fn add(
127128
syscalls::epoll_add(
128129
epoll.as_fd(),
129130
source.as_fd(),
130-
&mut Event {
131+
&Event {
131132
flags: event_flags,
132133
data,
133134
#[cfg(all(libc, target_os = "redox"))]
@@ -156,7 +157,7 @@ pub fn modify(
156157
syscalls::epoll_mod(
157158
epoll.as_fd(),
158159
source.as_fd(),
159-
&mut Event {
160+
&Event {
160161
flags: event_flags,
161162
data,
162163
#[cfg(all(libc, target_os = "redox"))]

0 commit comments

Comments
 (0)