refactor daemon polling and event handling
This commit is contained in:
parent
d4b40e8cc1
commit
2ecd987be6
1 changed files with 113 additions and 92 deletions
|
|
@ -10,7 +10,7 @@ use std::{
|
||||||
|
|
||||||
use iddqd::{BiHashMap, IdOrdMap};
|
use iddqd::{BiHashMap, IdOrdMap};
|
||||||
|
|
||||||
use mio::{Events, Interest, Poll, Token, net::UnixListener, unix::SourceFd};
|
use mio::{Events, Interest, Poll, Token, event::Event, net::UnixListener, unix::SourceFd};
|
||||||
|
|
||||||
use rustix::{buffer::spare_capacity, net::SocketFlags, process::Uid};
|
use rustix::{buffer::spare_capacity, net::SocketFlags, process::Uid};
|
||||||
|
|
||||||
|
|
@ -53,7 +53,8 @@ fn next_token() -> Token {
|
||||||
|
|
||||||
// If the increment wrapped to 0, then we just increment it again.
|
// If the increment wrapped to 0, then we just increment it again.
|
||||||
if tok == 0 {
|
if tok == 0 {
|
||||||
return Token(NEXT_TOKEN_NUMBER.fetch_add(1, Ordering::SeqCst));
|
warn!("File descriptor token wrapped. That's... a lot.");
|
||||||
|
return next_token();
|
||||||
}
|
}
|
||||||
|
|
||||||
Token(tok)
|
Token(tok)
|
||||||
|
|
@ -274,6 +275,7 @@ impl Daemon {
|
||||||
self.fd.as_fd()
|
self.fd.as_fd()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const DAEMON: Token = Token(0);
|
||||||
|
|
||||||
/// Private helpers.
|
/// Private helpers.
|
||||||
impl Daemon {
|
impl Daemon {
|
||||||
|
|
@ -368,7 +370,6 @@ impl Daemon {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
let mut daemon_source = SourceFd(&raw_fd);
|
let mut daemon_source = SourceFd(&raw_fd);
|
||||||
const DAEMON: Token = Token(0);
|
|
||||||
self.tokfd
|
self.tokfd
|
||||||
.insert_unique(TokenFd {
|
.insert_unique(TokenFd {
|
||||||
token: DAEMON,
|
token: DAEMON,
|
||||||
|
|
@ -385,15 +386,24 @@ impl Daemon {
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if tracing::enabled!(tracing::Level::DEBUG) {
|
if tracing::enabled!(tracing::Level::DEBUG) {
|
||||||
//
|
|
||||||
trace!("Daemon loop iteration, with file descriptors: ");
|
trace!("Daemon loop iteration, with file descriptors: ");
|
||||||
for info in &self.fd_info {
|
for info in &self.fd_info {
|
||||||
trace!("- {}", info.display());
|
trace!("- {}", info.display());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match self.poller.poll(&mut events, TIMEOUT_NEVER) {
|
let poll_result = self.poller.poll(&mut events, TIMEOUT_NEVER);
|
||||||
Ok(_) => {
|
self.handle_poll(poll_result, &events)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_poll(
|
||||||
|
&mut self,
|
||||||
|
poll_result: Result<(), IoError>,
|
||||||
|
events: &Events,
|
||||||
|
) -> Result<(), IoError> {
|
||||||
|
match poll_result {
|
||||||
|
Ok(()) => {
|
||||||
trace!(
|
trace!(
|
||||||
"mio::Poller::poll() got events: {:?}",
|
"mio::Poller::poll() got events: {:?}",
|
||||||
events.iter().size_hint().0,
|
events.iter().size_hint().0,
|
||||||
|
|
@ -408,11 +418,12 @@ impl Daemon {
|
||||||
},
|
},
|
||||||
Err(e) if e.kind() == IoErrorKind::Interrupted => {
|
Err(e) if e.kind() == IoErrorKind::Interrupted => {
|
||||||
// EINTR is silly.
|
// EINTR is silly.
|
||||||
continue;
|
// Return early, and poll() again.
|
||||||
|
return Ok(());
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
if let Some(Errno::BADF) = e.raw_os_error().map(Errno::from_raw_os_error) {
|
if let Some(Errno::BADF) = e.raw_os_error().map(Errno::from_raw_os_error) {
|
||||||
unreachable!("EBADF on poller fd; IO safety violation?");
|
panic!("EBADF on poller fd; IO safety violation?");
|
||||||
}
|
}
|
||||||
warn!("mio Poll::poll() error: {e}");
|
warn!("mio Poll::poll() error: {e}");
|
||||||
self.fd_error_push(self.poller.as_raw_fd(), e)
|
self.fd_error_push(self.poller.as_raw_fd(), e)
|
||||||
|
|
@ -422,8 +433,16 @@ impl Daemon {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for event in &events {
|
for event in events {
|
||||||
trace!("event is {event:?}");
|
self.handle_event(event)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_event(&mut self, event: &Event) -> Result<(), IoError> {
|
||||||
|
trace!("Handling event {event:?}");
|
||||||
|
|
||||||
match event.token() {
|
match event.token() {
|
||||||
DAEMON => {
|
DAEMON => {
|
||||||
let is_sock = self.main_fd_info().kind == FdKind::Socket;
|
let is_sock = self.main_fd_info().kind == FdKind::Socket;
|
||||||
|
|
@ -431,7 +450,8 @@ impl Daemon {
|
||||||
// SAFETY: oh boy: disjoint borrows with extra steps.
|
// SAFETY: oh boy: disjoint borrows with extra steps.
|
||||||
let file_fd = unsafe { BorrowedFd::borrow_raw(self.fd.as_raw_fd()) };
|
let file_fd = unsafe { BorrowedFd::borrow_raw(self.fd.as_raw_fd()) };
|
||||||
self.read_cmd(&file_fd).unwrap();
|
self.read_cmd(&file_fd).unwrap();
|
||||||
continue;
|
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Accept, first.
|
// Accept, first.
|
||||||
|
|
@ -453,7 +473,8 @@ impl Daemon {
|
||||||
self.fd
|
self.fd
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
continue;
|
|
||||||
|
return Ok(());
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -467,7 +488,7 @@ impl Daemon {
|
||||||
other_token => {
|
other_token => {
|
||||||
// This must be a stream fd.
|
// This must be a stream fd.
|
||||||
let stream_fd = self.fd_for_token(other_token).unwrap_or_else(|| {
|
let stream_fd = self.fd_for_token(other_token).unwrap_or_else(|| {
|
||||||
unreachable!("tried to get fd for no-existent token? {other_token:?}")
|
unreachable!("tried to get fd for non-existent token? {other_token:?}")
|
||||||
});
|
});
|
||||||
|
|
||||||
if event.is_read_closed() {
|
if event.is_read_closed() {
|
||||||
|
|
@ -479,8 +500,8 @@ impl Daemon {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue