2020-08-14 10:55:20 +00:00
|
|
|
//! Bindings to epoll (Linux, Android).
|
2020-08-06 13:02:59 +00:00
|
|
|
|
|
|
|
use std::io;
|
2023-06-11 17:37:25 +00:00
|
|
|
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd};
|
2020-08-06 13:02:59 +00:00
|
|
|
use std::time::Duration;
|
|
|
|
|
2024-02-11 16:31:13 +00:00
|
|
|
#[cfg(not(target_os = "redox"))]
|
|
|
|
use rustix::event::{eventfd, EventfdFlags};
|
|
|
|
#[cfg(not(target_os = "redox"))]
|
2023-04-23 14:26:29 +00:00
|
|
|
use rustix::time::{
|
|
|
|
timerfd_create, timerfd_settime, Itimerspec, TimerfdClockId, TimerfdFlags, TimerfdTimerFlags,
|
|
|
|
Timespec,
|
|
|
|
};
|
|
|
|
|
2024-02-11 16:31:13 +00:00
|
|
|
use rustix::event::epoll;
|
|
|
|
use rustix::fd::OwnedFd;
|
|
|
|
use rustix::fs::{fcntl_getfl, fcntl_setfl, OFlags};
|
|
|
|
use rustix::io::{fcntl_getfd, fcntl_setfd, read, write, FdFlags};
|
|
|
|
use rustix::pipe::{pipe, pipe_with, PipeFlags};
|
|
|
|
|
2022-12-30 22:43:47 +00:00
|
|
|
use crate::{Event, PollMode};
|
2020-08-06 13:02:59 +00:00
|
|
|
|
|
|
|
/// Interface to epoll.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Poller {
|
|
|
|
/// File descriptor for the epoll instance.
|
2023-04-23 14:26:29 +00:00
|
|
|
epoll_fd: OwnedFd,
|
2023-08-16 15:33:58 +00:00
|
|
|
|
|
|
|
/// Notifier used to wake up epoll.
|
|
|
|
notifier: Notifier,
|
|
|
|
|
2020-08-14 14:08:59 +00:00
|
|
|
/// File descriptor for the timerfd that produces timeouts.
|
2024-02-11 16:31:13 +00:00
|
|
|
///
|
|
|
|
/// Redox does not support timerfd.
|
|
|
|
#[cfg(not(target_os = "redox"))]
|
2023-04-23 14:26:29 +00:00
|
|
|
timer_fd: Option<OwnedFd>,
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Poller {
|
|
|
|
/// Creates a new poller.
|
|
|
|
pub fn new() -> io::Result<Poller> {
|
|
|
|
// Create an epoll instance.
|
2020-08-27 18:01:38 +00:00
|
|
|
//
|
|
|
|
// Use `epoll_create1` with `EPOLL_CLOEXEC`.
|
2023-08-04 03:15:59 +00:00
|
|
|
let epoll_fd = epoll::create(epoll::CreateFlags::CLOEXEC)?;
|
2020-08-06 13:02:59 +00:00
|
|
|
|
2023-08-16 15:33:58 +00:00
|
|
|
// Set up notifier and timerfd.
|
|
|
|
let notifier = Notifier::new()?;
|
2024-02-11 16:31:13 +00:00
|
|
|
#[cfg(not(target_os = "redox"))]
|
2023-04-23 14:26:29 +00:00
|
|
|
let timer_fd = timerfd_create(
|
|
|
|
TimerfdClockId::Monotonic,
|
|
|
|
TimerfdFlags::CLOEXEC | TimerfdFlags::NONBLOCK,
|
|
|
|
)
|
2020-09-14 19:32:28 +00:00
|
|
|
.ok();
|
|
|
|
|
2020-08-14 11:46:58 +00:00
|
|
|
let poller = Poller {
|
|
|
|
epoll_fd,
|
2023-08-16 15:33:58 +00:00
|
|
|
notifier,
|
2024-02-11 16:31:13 +00:00
|
|
|
#[cfg(not(target_os = "redox"))]
|
2020-08-14 11:46:58 +00:00
|
|
|
timer_fd,
|
|
|
|
};
|
2020-09-14 19:32:28 +00:00
|
|
|
|
2023-08-04 03:15:59 +00:00
|
|
|
unsafe {
|
2024-02-11 16:31:13 +00:00
|
|
|
#[cfg(not(target_os = "redox"))]
|
2023-08-04 03:15:59 +00:00
|
|
|
if let Some(ref timer_fd) = poller.timer_fd {
|
|
|
|
poller.add(
|
|
|
|
timer_fd.as_raw_fd(),
|
|
|
|
Event::none(crate::NOTIFY_KEY),
|
|
|
|
PollMode::Oneshot,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
2023-04-23 14:26:29 +00:00
|
|
|
poller.add(
|
2023-08-16 15:33:58 +00:00
|
|
|
poller.notifier.as_fd().as_raw_fd(),
|
2023-08-14 17:03:20 +00:00
|
|
|
Event::readable(crate::NOTIFY_KEY),
|
2023-04-23 14:26:29 +00:00
|
|
|
PollMode::Oneshot,
|
|
|
|
)?;
|
2020-09-14 19:32:28 +00:00
|
|
|
}
|
Separate adding and modifying of file descriptors
This replaces Poller.insert() and Poller.interest() with Poller.add()
and Poller.modify(), and renames Poller.remove() to Poller.delete().
The method Poller.add() is used for adding a new file descriptor, while
Poller.modify() is used for updating an existing one. Poller.remove() is
renamed to Poller.delete() so the naming scheme of these methods follows
that of epoll, wepoll, etc.
This new setup means that adding a new socket only requires a single
call of Poller.add(), instead of a combination of Poller.insert() and
Poller.interest(). This reduces the amount of system calls necessary,
and leads to a more pleasant API.
On systems that use kqueue or ports, the behaviour of Poller.add() and
Poller.modify() is the same. This is because on these systems adding an
already existing file descriptor will just update its configuration.
This however is an implementation detail and should not be relied upon
by users.
Migrating to this new API is pretty simple, simply replace this:
poller.insert(&socket);
poller.interest(&socket, event);
With this:
poller.add(&socket, event);
And for cases where Poller.interest() was used for updating an existing
file descriptor, simply replace it will a call to Poller.modify().
See https://github.com/stjepang/polling/issues/16 and
https://github.com/stjepang/polling/pull/17 for more information.
2020-10-01 19:26:44 +00:00
|
|
|
|
2023-06-21 03:28:18 +00:00
|
|
|
tracing::trace!(
|
|
|
|
epoll_fd = ?poller.epoll_fd.as_raw_fd(),
|
2023-08-16 15:33:58 +00:00
|
|
|
notifier = ?poller.notifier,
|
2023-06-21 03:28:18 +00:00
|
|
|
"new",
|
2020-08-29 14:00:59 +00:00
|
|
|
);
|
2020-08-06 13:02:59 +00:00
|
|
|
Ok(poller)
|
|
|
|
}
|
|
|
|
|
2022-12-30 22:43:47 +00:00
|
|
|
/// Whether this poller supports level-triggered events.
|
|
|
|
pub fn supports_level(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Whether the poller supports edge-triggered events.
|
|
|
|
pub fn supports_edge(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
Separate adding and modifying of file descriptors
This replaces Poller.insert() and Poller.interest() with Poller.add()
and Poller.modify(), and renames Poller.remove() to Poller.delete().
The method Poller.add() is used for adding a new file descriptor, while
Poller.modify() is used for updating an existing one. Poller.remove() is
renamed to Poller.delete() so the naming scheme of these methods follows
that of epoll, wepoll, etc.
This new setup means that adding a new socket only requires a single
call of Poller.add(), instead of a combination of Poller.insert() and
Poller.interest(). This reduces the amount of system calls necessary,
and leads to a more pleasant API.
On systems that use kqueue or ports, the behaviour of Poller.add() and
Poller.modify() is the same. This is because on these systems adding an
already existing file descriptor will just update its configuration.
This however is an implementation detail and should not be relied upon
by users.
Migrating to this new API is pretty simple, simply replace this:
poller.insert(&socket);
poller.interest(&socket, event);
With this:
poller.add(&socket, event);
And for cases where Poller.interest() was used for updating an existing
file descriptor, simply replace it will a call to Poller.modify().
See https://github.com/stjepang/polling/issues/16 and
https://github.com/stjepang/polling/pull/17 for more information.
2020-10-01 19:26:44 +00:00
|
|
|
/// Adds a new file descriptor.
|
2023-08-04 03:15:59 +00:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The `fd` must be a valid file descriptor. The usual condition of remaining registered in
|
|
|
|
/// the `Poller` doesn't apply to `epoll`.
|
|
|
|
pub unsafe fn add(&self, fd: RawFd, ev: Event, mode: PollMode) -> io::Result<()> {
|
2023-06-21 03:28:18 +00:00
|
|
|
let span = tracing::trace_span!(
|
|
|
|
"add",
|
|
|
|
epoll_fd = ?self.epoll_fd.as_raw_fd(),
|
|
|
|
?fd,
|
|
|
|
?ev,
|
2023-04-23 14:26:29 +00:00
|
|
|
);
|
2023-06-21 03:28:18 +00:00
|
|
|
let _enter = span.enter();
|
2023-04-23 14:26:29 +00:00
|
|
|
|
2023-08-04 03:15:59 +00:00
|
|
|
epoll::add(
|
2023-04-23 14:26:29 +00:00
|
|
|
&self.epoll_fd,
|
|
|
|
unsafe { rustix::fd::BorrowedFd::borrow_raw(fd) },
|
2023-08-04 03:15:59 +00:00
|
|
|
epoll::EventData::new_u64(ev.key as u64),
|
2023-08-14 17:03:20 +00:00
|
|
|
epoll_flags(&ev, mode) | ev.extra.flags,
|
2023-04-23 14:26:29 +00:00
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
|
Separate adding and modifying of file descriptors
This replaces Poller.insert() and Poller.interest() with Poller.add()
and Poller.modify(), and renames Poller.remove() to Poller.delete().
The method Poller.add() is used for adding a new file descriptor, while
Poller.modify() is used for updating an existing one. Poller.remove() is
renamed to Poller.delete() so the naming scheme of these methods follows
that of epoll, wepoll, etc.
This new setup means that adding a new socket only requires a single
call of Poller.add(), instead of a combination of Poller.insert() and
Poller.interest(). This reduces the amount of system calls necessary,
and leads to a more pleasant API.
On systems that use kqueue or ports, the behaviour of Poller.add() and
Poller.modify() is the same. This is because on these systems adding an
already existing file descriptor will just update its configuration.
This however is an implementation detail and should not be relied upon
by users.
Migrating to this new API is pretty simple, simply replace this:
poller.insert(&socket);
poller.interest(&socket, event);
With this:
poller.add(&socket, event);
And for cases where Poller.interest() was used for updating an existing
file descriptor, simply replace it will a call to Poller.modify().
See https://github.com/stjepang/polling/issues/16 and
https://github.com/stjepang/polling/pull/17 for more information.
2020-10-01 19:26:44 +00:00
|
|
|
/// Modifies an existing file descriptor.
|
2023-08-04 03:15:59 +00:00
|
|
|
pub fn modify(&self, fd: BorrowedFd<'_>, ev: Event, mode: PollMode) -> io::Result<()> {
|
2023-06-21 03:28:18 +00:00
|
|
|
let span = tracing::trace_span!(
|
|
|
|
"modify",
|
|
|
|
epoll_fd = ?self.epoll_fd.as_raw_fd(),
|
|
|
|
?fd,
|
|
|
|
?ev,
|
2023-04-23 14:26:29 +00:00
|
|
|
);
|
2023-06-21 03:28:18 +00:00
|
|
|
let _enter = span.enter();
|
2023-04-23 14:26:29 +00:00
|
|
|
|
2023-08-04 03:15:59 +00:00
|
|
|
epoll::modify(
|
2023-04-23 14:26:29 +00:00
|
|
|
&self.epoll_fd,
|
2023-08-04 03:15:59 +00:00
|
|
|
fd,
|
|
|
|
epoll::EventData::new_u64(ev.key as u64),
|
2023-08-14 17:03:20 +00:00
|
|
|
epoll_flags(&ev, mode) | ev.extra.flags,
|
2023-04-23 14:26:29 +00:00
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 14:40:09 +00:00
|
|
|
/// Deletes a file descriptor.
|
2023-08-04 03:15:59 +00:00
|
|
|
pub fn delete(&self, fd: BorrowedFd<'_>) -> io::Result<()> {
|
2023-06-21 03:28:18 +00:00
|
|
|
let span = tracing::trace_span!(
|
|
|
|
"delete",
|
|
|
|
epoll_fd = ?self.epoll_fd.as_raw_fd(),
|
|
|
|
?fd,
|
|
|
|
);
|
|
|
|
let _enter = span.enter();
|
2023-04-23 14:26:29 +00:00
|
|
|
|
2023-08-04 03:15:59 +00:00
|
|
|
epoll::delete(&self.epoll_fd, fd)?;
|
2023-04-23 14:26:29 +00:00
|
|
|
|
|
|
|
Ok(())
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Waits for I/O events with an optional timeout.
|
2023-04-23 14:26:29 +00:00
|
|
|
#[allow(clippy::needless_update)]
|
2020-08-14 12:38:02 +00:00
|
|
|
pub fn wait(&self, events: &mut Events, timeout: Option<Duration>) -> io::Result<()> {
|
2023-06-21 03:28:18 +00:00
|
|
|
let span = tracing::trace_span!(
|
|
|
|
"wait",
|
|
|
|
epoll_fd = ?self.epoll_fd.as_raw_fd(),
|
|
|
|
?timeout,
|
2023-04-23 14:26:29 +00:00
|
|
|
);
|
2023-06-21 03:28:18 +00:00
|
|
|
let _enter = span.enter();
|
2020-08-29 14:00:59 +00:00
|
|
|
|
2024-02-11 16:31:13 +00:00
|
|
|
#[cfg(not(target_os = "redox"))]
|
2023-04-23 14:26:29 +00:00
|
|
|
if let Some(ref timer_fd) = self.timer_fd {
|
2020-09-14 19:32:28 +00:00
|
|
|
// Configure the timeout using timerfd.
|
2023-04-23 14:26:29 +00:00
|
|
|
let new_val = Itimerspec {
|
2020-09-14 19:32:28 +00:00
|
|
|
it_interval: TS_ZERO,
|
|
|
|
it_value: match timeout {
|
|
|
|
None => TS_ZERO,
|
2022-12-03 14:04:37 +00:00
|
|
|
Some(t) => {
|
|
|
|
let mut ts = TS_ZERO;
|
2023-08-05 15:17:53 +00:00
|
|
|
ts.tv_sec = t.as_secs() as _;
|
2023-08-14 01:49:43 +00:00
|
|
|
ts.tv_nsec = t.subsec_nanos() as _;
|
2022-12-03 14:04:37 +00:00
|
|
|
ts
|
|
|
|
}
|
2020-08-15 07:05:58 +00:00
|
|
|
},
|
2023-04-23 14:26:29 +00:00
|
|
|
..unsafe { std::mem::zeroed() }
|
2020-09-14 19:32:28 +00:00
|
|
|
};
|
2020-08-15 06:31:00 +00:00
|
|
|
|
2023-04-23 14:26:29 +00:00
|
|
|
timerfd_settime(timer_fd, TimerfdTimerFlags::empty(), &new_val)?;
|
2020-09-14 19:32:28 +00:00
|
|
|
|
|
|
|
// Set interest in timerfd.
|
Separate adding and modifying of file descriptors
This replaces Poller.insert() and Poller.interest() with Poller.add()
and Poller.modify(), and renames Poller.remove() to Poller.delete().
The method Poller.add() is used for adding a new file descriptor, while
Poller.modify() is used for updating an existing one. Poller.remove() is
renamed to Poller.delete() so the naming scheme of these methods follows
that of epoll, wepoll, etc.
This new setup means that adding a new socket only requires a single
call of Poller.add(), instead of a combination of Poller.insert() and
Poller.interest(). This reduces the amount of system calls necessary,
and leads to a more pleasant API.
On systems that use kqueue or ports, the behaviour of Poller.add() and
Poller.modify() is the same. This is because on these systems adding an
already existing file descriptor will just update its configuration.
This however is an implementation detail and should not be relied upon
by users.
Migrating to this new API is pretty simple, simply replace this:
poller.insert(&socket);
poller.interest(&socket, event);
With this:
poller.add(&socket, event);
And for cases where Poller.interest() was used for updating an existing
file descriptor, simply replace it will a call to Poller.modify().
See https://github.com/stjepang/polling/issues/16 and
https://github.com/stjepang/polling/pull/17 for more information.
2020-10-01 19:26:44 +00:00
|
|
|
self.modify(
|
2023-08-04 03:15:59 +00:00
|
|
|
timer_fd.as_fd(),
|
2023-08-14 17:03:20 +00:00
|
|
|
Event::readable(crate::NOTIFY_KEY),
|
2022-12-30 22:43:47 +00:00
|
|
|
PollMode::Oneshot,
|
2020-09-14 19:32:28 +00:00
|
|
|
)?;
|
|
|
|
}
|
2020-08-14 11:46:58 +00:00
|
|
|
|
2024-02-11 16:31:13 +00:00
|
|
|
#[cfg(not(target_os = "redox"))]
|
|
|
|
let timer_fd = &self.timer_fd;
|
|
|
|
#[cfg(target_os = "redox")]
|
|
|
|
let timer_fd: Option<core::convert::Infallible> = None;
|
|
|
|
|
2020-08-15 07:05:58 +00:00
|
|
|
// Timeout in milliseconds for epoll.
|
2024-02-11 16:31:13 +00:00
|
|
|
let timeout_ms = match (timer_fd, timeout) {
|
2020-09-14 19:32:28 +00:00
|
|
|
(_, Some(t)) if t == Duration::from_secs(0) => 0,
|
|
|
|
(None, Some(t)) => {
|
2020-09-14 19:06:16 +00:00
|
|
|
// Round up to a whole millisecond.
|
|
|
|
let mut ms = t.as_millis().try_into().unwrap_or(std::i32::MAX);
|
|
|
|
if Duration::from_millis(ms as u64) < t {
|
|
|
|
ms = ms.saturating_add(1);
|
|
|
|
}
|
|
|
|
ms
|
|
|
|
}
|
2020-09-14 19:32:28 +00:00
|
|
|
_ => -1,
|
2020-08-15 07:05:58 +00:00
|
|
|
};
|
|
|
|
|
2020-08-06 13:02:59 +00:00
|
|
|
// Wait for I/O events.
|
2023-08-04 03:15:59 +00:00
|
|
|
epoll::wait(&self.epoll_fd, &mut events.list, timeout_ms)?;
|
2023-06-21 03:28:18 +00:00
|
|
|
tracing::trace!(
|
|
|
|
epoll_fd = ?self.epoll_fd.as_raw_fd(),
|
|
|
|
res = ?events.list.len(),
|
|
|
|
"new events",
|
2023-04-23 14:26:29 +00:00
|
|
|
);
|
2020-08-06 13:02:59 +00:00
|
|
|
|
|
|
|
// Clear the notification (if received) and re-register interest in it.
|
2023-08-16 15:33:58 +00:00
|
|
|
self.notifier.clear();
|
Separate adding and modifying of file descriptors
This replaces Poller.insert() and Poller.interest() with Poller.add()
and Poller.modify(), and renames Poller.remove() to Poller.delete().
The method Poller.add() is used for adding a new file descriptor, while
Poller.modify() is used for updating an existing one. Poller.remove() is
renamed to Poller.delete() so the naming scheme of these methods follows
that of epoll, wepoll, etc.
This new setup means that adding a new socket only requires a single
call of Poller.add(), instead of a combination of Poller.insert() and
Poller.interest(). This reduces the amount of system calls necessary,
and leads to a more pleasant API.
On systems that use kqueue or ports, the behaviour of Poller.add() and
Poller.modify() is the same. This is because on these systems adding an
already existing file descriptor will just update its configuration.
This however is an implementation detail and should not be relied upon
by users.
Migrating to this new API is pretty simple, simply replace this:
poller.insert(&socket);
poller.interest(&socket, event);
With this:
poller.add(&socket, event);
And for cases where Poller.interest() was used for updating an existing
file descriptor, simply replace it will a call to Poller.modify().
See https://github.com/stjepang/polling/issues/16 and
https://github.com/stjepang/polling/pull/17 for more information.
2020-10-01 19:26:44 +00:00
|
|
|
self.modify(
|
2023-08-16 15:33:58 +00:00
|
|
|
self.notifier.as_fd(),
|
2023-08-14 17:03:20 +00:00
|
|
|
Event::readable(crate::NOTIFY_KEY),
|
2022-12-30 22:43:47 +00:00
|
|
|
PollMode::Oneshot,
|
2020-08-06 13:10:39 +00:00
|
|
|
)?;
|
2020-08-14 12:38:02 +00:00
|
|
|
Ok(())
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sends a notification to wake up the current or next `wait()` call.
|
|
|
|
pub fn notify(&self) -> io::Result<()> {
|
2023-06-21 03:28:18 +00:00
|
|
|
let span = tracing::trace_span!(
|
|
|
|
"notify",
|
|
|
|
epoll_fd = ?self.epoll_fd.as_raw_fd(),
|
2023-08-16 15:33:58 +00:00
|
|
|
notifier = ?self.notifier,
|
2020-08-29 14:00:59 +00:00
|
|
|
);
|
2023-06-21 03:28:18 +00:00
|
|
|
let _enter = span.enter();
|
2020-08-29 14:00:59 +00:00
|
|
|
|
2023-08-16 15:33:58 +00:00
|
|
|
self.notifier.notify();
|
Separate adding and modifying of file descriptors
This replaces Poller.insert() and Poller.interest() with Poller.add()
and Poller.modify(), and renames Poller.remove() to Poller.delete().
The method Poller.add() is used for adding a new file descriptor, while
Poller.modify() is used for updating an existing one. Poller.remove() is
renamed to Poller.delete() so the naming scheme of these methods follows
that of epoll, wepoll, etc.
This new setup means that adding a new socket only requires a single
call of Poller.add(), instead of a combination of Poller.insert() and
Poller.interest(). This reduces the amount of system calls necessary,
and leads to a more pleasant API.
On systems that use kqueue or ports, the behaviour of Poller.add() and
Poller.modify() is the same. This is because on these systems adding an
already existing file descriptor will just update its configuration.
This however is an implementation detail and should not be relied upon
by users.
Migrating to this new API is pretty simple, simply replace this:
poller.insert(&socket);
poller.interest(&socket, event);
With this:
poller.add(&socket, event);
And for cases where Poller.interest() was used for updating an existing
file descriptor, simply replace it will a call to Poller.modify().
See https://github.com/stjepang/polling/issues/16 and
https://github.com/stjepang/polling/pull/17 for more information.
2020-10-01 19:26:44 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 16:52:28 +00:00
|
|
|
impl AsRawFd for Poller {
|
|
|
|
fn as_raw_fd(&self) -> RawFd {
|
2023-04-23 14:26:29 +00:00
|
|
|
self.epoll_fd.as_raw_fd()
|
2022-08-18 16:52:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsFd for Poller {
|
|
|
|
fn as_fd(&self) -> BorrowedFd<'_> {
|
2023-04-23 14:26:29 +00:00
|
|
|
self.epoll_fd.as_fd()
|
2022-08-18 16:52:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 13:02:59 +00:00
|
|
|
impl Drop for Poller {
|
|
|
|
fn drop(&mut self) {
|
2023-06-21 03:28:18 +00:00
|
|
|
let span = tracing::trace_span!(
|
|
|
|
"drop",
|
|
|
|
epoll_fd = ?self.epoll_fd.as_raw_fd(),
|
2023-08-16 15:33:58 +00:00
|
|
|
notifier = ?self.notifier,
|
2020-08-29 14:00:59 +00:00
|
|
|
);
|
2023-06-21 03:28:18 +00:00
|
|
|
let _enter = span.enter();
|
2020-09-14 19:32:28 +00:00
|
|
|
|
2024-02-11 16:31:13 +00:00
|
|
|
#[cfg(not(target_os = "redox"))]
|
2023-04-23 14:26:29 +00:00
|
|
|
if let Some(timer_fd) = self.timer_fd.take() {
|
2023-08-04 03:15:59 +00:00
|
|
|
let _ = self.delete(timer_fd.as_fd());
|
2020-09-14 19:32:28 +00:00
|
|
|
}
|
2023-08-16 15:33:58 +00:00
|
|
|
let _ = self.delete(self.notifier.as_fd());
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-14 11:46:58 +00:00
|
|
|
/// `timespec` value that equals zero.
|
2024-02-11 16:31:13 +00:00
|
|
|
#[cfg(not(target_os = "redox"))]
|
2023-04-23 14:26:29 +00:00
|
|
|
const TS_ZERO: Timespec = unsafe { std::mem::transmute([0u8; std::mem::size_of::<Timespec>()]) };
|
|
|
|
|
|
|
|
/// Get the EPOLL flags for the interest.
|
|
|
|
fn epoll_flags(interest: &Event, mode: PollMode) -> epoll::EventFlags {
|
|
|
|
let mut flags = match mode {
|
|
|
|
PollMode::Oneshot => epoll::EventFlags::ONESHOT,
|
|
|
|
PollMode::Level => epoll::EventFlags::empty(),
|
|
|
|
PollMode::Edge => epoll::EventFlags::ET,
|
|
|
|
PollMode::EdgeOneshot => epoll::EventFlags::ET | epoll::EventFlags::ONESHOT,
|
|
|
|
};
|
|
|
|
if interest.readable {
|
|
|
|
flags |= read_flags();
|
|
|
|
}
|
|
|
|
if interest.writable {
|
|
|
|
flags |= write_flags();
|
|
|
|
}
|
|
|
|
flags
|
|
|
|
}
|
2020-08-14 11:46:58 +00:00
|
|
|
|
2020-08-06 13:02:59 +00:00
|
|
|
/// Epoll flags for all possible readability events.
|
2023-04-23 14:26:29 +00:00
|
|
|
fn read_flags() -> epoll::EventFlags {
|
|
|
|
use epoll::EventFlags as Epoll;
|
|
|
|
Epoll::IN | Epoll::HUP | Epoll::ERR | Epoll::PRI
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Epoll flags for all possible writability events.
|
2023-04-23 14:26:29 +00:00
|
|
|
fn write_flags() -> epoll::EventFlags {
|
|
|
|
use epoll::EventFlags as Epoll;
|
|
|
|
Epoll::OUT | Epoll::HUP | Epoll::ERR
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A list of reported I/O events.
|
|
|
|
pub struct Events {
|
2023-04-23 14:26:29 +00:00
|
|
|
list: epoll::EventVec,
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl Send for Events {}
|
|
|
|
|
|
|
|
impl Events {
|
|
|
|
/// Creates an empty list.
|
2023-08-14 17:03:20 +00:00
|
|
|
pub fn with_capacity(cap: usize) -> Events {
|
2023-04-23 14:26:29 +00:00
|
|
|
Events {
|
2023-08-14 17:03:20 +00:00
|
|
|
list: epoll::EventVec::with_capacity(cap),
|
2023-04-23 14:26:29 +00:00
|
|
|
}
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Iterates over I/O events.
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = Event> + '_ {
|
2023-08-04 03:15:59 +00:00
|
|
|
self.list.iter().map(|ev| {
|
|
|
|
let flags = ev.flags;
|
|
|
|
Event {
|
|
|
|
key: ev.data.u64() as usize,
|
|
|
|
readable: flags.intersects(read_flags()),
|
|
|
|
writable: flags.intersects(write_flags()),
|
2023-08-14 17:03:20 +00:00
|
|
|
extra: EventExtra { flags },
|
2023-08-04 03:15:59 +00:00
|
|
|
}
|
2020-08-06 13:02:59 +00:00
|
|
|
})
|
|
|
|
}
|
2023-08-14 17:03:20 +00:00
|
|
|
|
|
|
|
/// Clear the list.
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.list.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the capacity of the list.
|
|
|
|
pub fn capacity(&self) -> usize {
|
|
|
|
self.list.capacity()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extra information about this event.
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub struct EventExtra {
|
|
|
|
flags: epoll::EventFlags,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EventExtra {
|
|
|
|
/// Create an empty version of the data.
|
|
|
|
#[inline]
|
2023-08-22 01:32:21 +00:00
|
|
|
pub const fn empty() -> EventExtra {
|
2023-08-14 17:03:20 +00:00
|
|
|
EventExtra {
|
|
|
|
flags: epoll::EventFlags::empty(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add the interrupt flag to this event.
|
|
|
|
#[inline]
|
|
|
|
pub fn set_hup(&mut self, active: bool) {
|
|
|
|
self.flags.set(epoll::EventFlags::HUP, active);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add the priority flag to this event.
|
|
|
|
#[inline]
|
|
|
|
pub fn set_pri(&mut self, active: bool) {
|
|
|
|
self.flags.set(epoll::EventFlags::PRI, active);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tell if the interrupt flag is set.
|
|
|
|
#[inline]
|
|
|
|
pub fn is_hup(&self) -> bool {
|
|
|
|
self.flags.contains(epoll::EventFlags::HUP)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tell if the priority flag is set.
|
|
|
|
#[inline]
|
|
|
|
pub fn is_pri(&self) -> bool {
|
|
|
|
self.flags.contains(epoll::EventFlags::PRI)
|
|
|
|
}
|
2024-01-26 20:58:39 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn is_connect_failed(&self) -> Option<bool> {
|
|
|
|
Some(
|
|
|
|
self.flags.contains(epoll::EventFlags::ERR)
|
2024-03-21 05:04:46 +00:00
|
|
|
&& self.flags.contains(epoll::EventFlags::HUP),
|
2024-01-26 20:58:39 +00:00
|
|
|
)
|
|
|
|
}
|
2024-03-21 05:04:46 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn is_err(&self) -> Option<bool> {
|
|
|
|
Some(self.flags.contains(epoll::EventFlags::ERR))
|
|
|
|
}
|
2020-08-06 13:02:59 +00:00
|
|
|
}
|
2023-08-16 15:33:58 +00:00
|
|
|
|
|
|
|
/// The notifier for Linux.
|
|
|
|
///
|
|
|
|
/// Certain container runtimes do not expose eventfd to the client, as it relies on the host and
|
|
|
|
/// can be used to "escape" the container under certain conditions. Gramine is the prime example,
|
|
|
|
/// see [here](gramine). In this case, fall back to using a pipe.
|
|
|
|
///
|
|
|
|
/// [gramine]: https://gramine.readthedocs.io/en/stable/manifest-syntax.html#allowing-eventfd
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum Notifier {
|
|
|
|
/// The primary notifier, using eventfd.
|
2024-02-11 16:31:13 +00:00
|
|
|
#[cfg(not(target_os = "redox"))]
|
2023-08-16 15:33:58 +00:00
|
|
|
EventFd(OwnedFd),
|
|
|
|
|
|
|
|
/// The fallback notifier, using a pipe.
|
|
|
|
Pipe {
|
|
|
|
/// The read end of the pipe.
|
|
|
|
read_pipe: OwnedFd,
|
|
|
|
|
|
|
|
/// The write end of the pipe.
|
|
|
|
write_pipe: OwnedFd,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Notifier {
|
|
|
|
/// Create a new notifier.
|
|
|
|
fn new() -> io::Result<Self> {
|
|
|
|
// Skip eventfd for testing if necessary.
|
2024-02-11 16:31:13 +00:00
|
|
|
#[cfg(not(target_os = "redox"))]
|
|
|
|
{
|
|
|
|
if !cfg!(polling_test_epoll_pipe) {
|
|
|
|
// Try to create an eventfd.
|
|
|
|
match eventfd(0, EventfdFlags::CLOEXEC | EventfdFlags::NONBLOCK) {
|
|
|
|
Ok(fd) => {
|
|
|
|
tracing::trace!("created eventfd for notifier");
|
|
|
|
return Ok(Notifier::EventFd(fd));
|
|
|
|
}
|
2023-08-16 15:33:58 +00:00
|
|
|
|
2024-02-11 16:31:13 +00:00
|
|
|
Err(err) => {
|
|
|
|
tracing::warn!(
|
|
|
|
"eventfd() failed with error ({}), falling back to pipe",
|
|
|
|
err
|
|
|
|
);
|
|
|
|
}
|
2023-08-16 15:33:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let (read, write) = pipe_with(PipeFlags::CLOEXEC).or_else(|_| {
|
|
|
|
let (read, write) = pipe()?;
|
|
|
|
fcntl_setfd(&read, fcntl_getfd(&read)? | FdFlags::CLOEXEC)?;
|
|
|
|
fcntl_setfd(&write, fcntl_getfd(&write)? | FdFlags::CLOEXEC)?;
|
|
|
|
io::Result::Ok((read, write))
|
|
|
|
})?;
|
|
|
|
|
|
|
|
fcntl_setfl(&read, fcntl_getfl(&read)? | OFlags::NONBLOCK)?;
|
|
|
|
Ok(Notifier::Pipe {
|
|
|
|
read_pipe: read,
|
|
|
|
write_pipe: write,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The file descriptor to register in the poller.
|
|
|
|
fn as_fd(&self) -> BorrowedFd<'_> {
|
|
|
|
match self {
|
2024-02-11 16:31:13 +00:00
|
|
|
#[cfg(not(target_os = "redox"))]
|
2023-08-16 15:33:58 +00:00
|
|
|
Notifier::EventFd(fd) => fd.as_fd(),
|
|
|
|
Notifier::Pipe {
|
|
|
|
read_pipe: read, ..
|
|
|
|
} => read.as_fd(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Notify the poller.
|
|
|
|
fn notify(&self) {
|
|
|
|
match self {
|
2024-02-11 16:31:13 +00:00
|
|
|
#[cfg(not(target_os = "redox"))]
|
2023-08-16 15:33:58 +00:00
|
|
|
Self::EventFd(fd) => {
|
|
|
|
let buf: [u8; 8] = 1u64.to_ne_bytes();
|
|
|
|
let _ = write(fd, &buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
Self::Pipe { write_pipe, .. } => {
|
|
|
|
write(write_pipe, &[0; 1]).ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clear the notification.
|
|
|
|
fn clear(&self) {
|
|
|
|
match self {
|
2024-02-11 16:31:13 +00:00
|
|
|
#[cfg(not(target_os = "redox"))]
|
2023-08-16 15:33:58 +00:00
|
|
|
Self::EventFd(fd) => {
|
|
|
|
let mut buf = [0u8; 8];
|
|
|
|
let _ = read(fd, &mut buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
Self::Pipe { read_pipe, .. } => while read(read_pipe, &mut [0u8; 1024]).is_ok() {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|