2026-03-11 14:26:59 +01:00
|
|
|
use std::{
|
2026-03-22 15:57:38 +01:00
|
|
|
env, io,
|
2026-03-19 20:44:57 +01:00
|
|
|
os::fd::{AsFd, BorrowedFd, IntoRawFd, OwnedFd, RawFd},
|
|
|
|
|
sync::{
|
2026-03-20 20:43:12 +01:00
|
|
|
Arc, LazyLock,
|
2026-03-19 20:44:57 +01:00
|
|
|
atomic::{AtomicUsize, Ordering},
|
|
|
|
|
},
|
2026-03-11 14:26:59 +01:00
|
|
|
time::Duration,
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-19 20:44:57 +01:00
|
|
|
use iddqd::{BiHashMap, IdOrdMap};
|
2026-03-11 14:26:59 +01:00
|
|
|
|
2026-03-19 12:36:46 +01:00
|
|
|
use mio::{Events, Interest, Poll, Token, net::UnixListener, unix::SourceFd};
|
2026-03-11 14:26:59 +01:00
|
|
|
|
2026-03-19 21:39:11 +01:00
|
|
|
use rustix::{buffer::spare_capacity, net::SocketFlags, process::Uid};
|
2026-03-11 14:26:59 +01:00
|
|
|
|
|
|
|
|
use serde_json::StreamDeserializer;
|
|
|
|
|
|
2026-03-22 17:15:04 +01:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
|
|
|
|
|
pub mod api;
|
|
|
|
|
use api::DaemonCmd;
|
|
|
|
|
|
2026-03-19 20:44:57 +01:00
|
|
|
use crate::{
|
2026-03-20 20:43:12 +01:00
|
|
|
SourceFile, SourceLine,
|
2026-03-19 20:44:57 +01:00
|
|
|
daemon_tokfd::{FdInfo, FdKind},
|
|
|
|
|
};
|
2026-03-11 14:26:59 +01:00
|
|
|
|
2026-03-19 19:45:09 +01:00
|
|
|
use crate::{OwnedFdWithFlags, TokenFd};
|
2026-03-11 14:26:59 +01:00
|
|
|
|
2026-03-22 15:57:38 +01:00
|
|
|
pub static UID: LazyLock<Uid> = LazyLock::new(rustix::process::getuid);
|
2026-03-19 12:36:46 +01:00
|
|
|
|
|
|
|
|
pub static USER_SOCKET_DIR: LazyLock<&'static Path> = LazyLock::new(|| {
|
|
|
|
|
let dir: Box<Path> = env::var_os("XDG_RUNTIME_DIR")
|
|
|
|
|
.map(PathBuf::from)
|
|
|
|
|
.unwrap_or_else(|| ["/", "run", "user", &UID.to_string()].into_iter().collect())
|
|
|
|
|
.into_boxed_path();
|
|
|
|
|
|
|
|
|
|
Box::leak(dir)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
pub static TMPDIR: LazyLock<&'static Path> = LazyLock::new(|| {
|
|
|
|
|
let dir: Box<Path> = env::temp_dir().into_boxed_path();
|
|
|
|
|
|
|
|
|
|
Box::leak(dir)
|
|
|
|
|
});
|
|
|
|
|
|
2026-03-11 14:26:59 +01:00
|
|
|
const TIMEOUT_NEVER: Option<Duration> = None;
|
|
|
|
|
|
2026-03-19 20:44:57 +01:00
|
|
|
static NEXT_TOKEN_NUMBER: AtomicUsize = AtomicUsize::new(1);
|
|
|
|
|
fn next_token() -> Token {
|
|
|
|
|
let tok = NEXT_TOKEN_NUMBER.fetch_add(1, Ordering::SeqCst);
|
|
|
|
|
|
|
|
|
|
// If the increment wrapped to 0, then we just increment it again.
|
|
|
|
|
if tok == 0 {
|
|
|
|
|
return Token(NEXT_TOKEN_NUMBER.fetch_add(1, Ordering::SeqCst));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Token(tok)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-11 14:26:59 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Daemon {
|
2026-03-20 20:43:12 +01:00
|
|
|
config_path: Arc<Path>,
|
2026-03-11 14:26:59 +01:00
|
|
|
fd: OwnedFdWithFlags,
|
2026-03-19 20:44:57 +01:00
|
|
|
path: Option<Box<Path>>,
|
|
|
|
|
|
|
|
|
|
poller: Poll,
|
|
|
|
|
|
|
|
|
|
fd_info: IdOrdMap<FdInfo>,
|
2026-03-11 14:26:59 +01:00
|
|
|
|
2026-03-19 19:45:09 +01:00
|
|
|
// Bijective mapping of [`mio::Token`]s to [`RawFd`]s.
|
|
|
|
|
tokfd: BiHashMap<TokenFd>,
|
2026-03-11 14:26:59 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-19 19:45:09 +01:00
|
|
|
/// `tokfd` handling.
|
|
|
|
|
impl Daemon {
|
2026-03-19 20:44:57 +01:00
|
|
|
fn fd_error_pop(&mut self, fd: RawFd) -> Option<IoError> {
|
2026-03-19 21:39:11 +01:00
|
|
|
let mut info = self.fd_info.get_mut(&fd).unwrap_or_else(|| {
|
|
|
|
|
if let Ok(name) = FdInfo::guess_name(fd) {
|
|
|
|
|
panic!(
|
|
|
|
|
"tried to pop error for unknown fd {fd} ({})",
|
|
|
|
|
name.to_string_lossy(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
panic!("tried to pop error for unknown fd {fd}")
|
|
|
|
|
});
|
|
|
|
|
info.error_buffer.pop_front().tap_some(|e| {
|
|
|
|
|
trace!("Popping error for {}: {e}", info.display());
|
|
|
|
|
})
|
2026-03-19 20:44:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn fd_error_push(&mut self, fd: RawFd, error: IoError) -> Result<(), IoError> {
|
2026-03-19 21:39:11 +01:00
|
|
|
let mut info = self
|
|
|
|
|
.fd_info
|
2026-03-19 20:44:57 +01:00
|
|
|
.get_mut(&fd)
|
2026-03-19 21:39:11 +01:00
|
|
|
.unwrap_or_else(|| panic!("tried to push error for unknown fd {fd}"));
|
|
|
|
|
trace!("Pushing error for {}: {}", info.display(), error);
|
|
|
|
|
info.error_buffer.try_push_back(error)
|
2026-03-19 20:44:57 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-19 21:39:11 +01:00
|
|
|
fn main_fd_info(&self) -> &FdInfo {
|
|
|
|
|
self.fd_info.get(&self.fd.as_raw_fd()).unwrap_or_else(|| {
|
|
|
|
|
unreachable!(
|
|
|
|
|
"Main daemon fd {:?} was not registered with fd_info",
|
|
|
|
|
self.fd,
|
|
|
|
|
)
|
|
|
|
|
})
|
2026-03-19 20:44:57 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-19 21:39:11 +01:00
|
|
|
fn register(&mut self, fd: RawFd, kind: FdKind) -> Token {
|
2026-03-19 20:44:57 +01:00
|
|
|
let token = next_token();
|
|
|
|
|
|
2026-03-19 21:39:11 +01:00
|
|
|
debug!(
|
|
|
|
|
"Registering new {} FdInfo for {fd} with token {token:?}",
|
|
|
|
|
kind.name_str(),
|
|
|
|
|
);
|
|
|
|
|
|
2026-03-19 20:44:57 +01:00
|
|
|
self.fd_info.insert_unique(FdInfo::new(fd, kind)).unwrap();
|
|
|
|
|
|
|
|
|
|
self.tokfd
|
|
|
|
|
.insert_unique(TokenFd { token, fd })
|
|
|
|
|
.unwrap_or_else(|e| todo!("{e}"));
|
|
|
|
|
|
|
|
|
|
let mut source = SourceFd(&fd);
|
|
|
|
|
self.poller
|
|
|
|
|
.registry()
|
|
|
|
|
.register(&mut source, token, Interest::READABLE)
|
|
|
|
|
.unwrap_or_else(|e| unreachable!("registering {fd:?} with poller failed: {e}"));
|
|
|
|
|
|
|
|
|
|
token
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-19 21:39:11 +01:00
|
|
|
fn deregister(&mut self, fd: RawFd) {
|
|
|
|
|
let info = self
|
|
|
|
|
.fd_info
|
|
|
|
|
.remove(&fd)
|
|
|
|
|
.unwrap_or_else(|| unreachable!("tried to unregister unknown fd {fd}"));
|
|
|
|
|
debug!("Unregistering fd {}; calling close()", info.display());
|
|
|
|
|
unsafe { rustix::io::close(fd) };
|
|
|
|
|
let res = unsafe { libc::fcntl(fd, libc::F_GETFD, 0) };
|
|
|
|
|
debug_assert_eq!(res, -1);
|
|
|
|
|
debug_assert_eq!(
|
|
|
|
|
IoError::last_os_error().raw_os_error(),
|
|
|
|
|
Some(Errno::BADF.raw_os_error()),
|
|
|
|
|
);
|
2026-03-20 20:43:12 +01:00
|
|
|
|
|
|
|
|
self.tokfd.remove2(&fd).unwrap_or_else(|| todo!());
|
2026-03-19 21:39:11 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-19 19:45:09 +01:00
|
|
|
fn fd_for_token(&self, token: Token) -> Option<RawFd> {
|
|
|
|
|
self.tokfd
|
|
|
|
|
.get1(&token)
|
|
|
|
|
.map(|TokenFd { fd, .. }| fd)
|
|
|
|
|
.copied()
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-22 15:57:38 +01:00
|
|
|
/// Not currently used, but here for completeness.
|
|
|
|
|
#[expect(dead_code)]
|
2026-03-19 19:45:09 +01:00
|
|
|
fn token_for_fd(&self, fd: RawFd) -> Option<Token> {
|
|
|
|
|
self.tokfd
|
|
|
|
|
.get2(&fd)
|
|
|
|
|
.map(|TokenFd { token, .. }| token)
|
|
|
|
|
.copied()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-11 14:26:59 +01:00
|
|
|
impl Daemon {
|
2026-03-20 20:43:12 +01:00
|
|
|
pub fn new(
|
|
|
|
|
config_path: Arc<Path>,
|
|
|
|
|
fd: OwnedFd,
|
|
|
|
|
kind: FdKind,
|
|
|
|
|
name: Option<Box<OsStr>>,
|
|
|
|
|
) -> Self {
|
2026-03-19 20:44:57 +01:00
|
|
|
let mut fd_info: IdOrdMap<FdInfo> = Default::default();
|
|
|
|
|
|
|
|
|
|
// Supposedly, the only possible ways this can fail are EMFILE, ENFILE, and ENOMEM.
|
|
|
|
|
// If any of those are the case, we're screwed anyway.
|
|
|
|
|
let poller = Poll::new().unwrap_or_else(|e| panic!("can't create new mio::Poll: {e}"));
|
|
|
|
|
// Make sure we register the poller in `fd_info`, so we can keep track of its errors.
|
|
|
|
|
fd_info
|
|
|
|
|
.insert_unique(FdInfo::new(poller.as_raw_fd(), FdKind::Poller))
|
|
|
|
|
.unwrap_or_else(|e| unreachable!("{e}"));
|
|
|
|
|
|
2026-03-11 14:26:59 +01:00
|
|
|
let fd = OwnedFdWithFlags::new_with_fallback(fd);
|
|
|
|
|
|
2026-03-19 20:44:57 +01:00
|
|
|
fd_info
|
|
|
|
|
.insert_unique(FdInfo::new(fd.as_raw_fd(), kind))
|
|
|
|
|
.unwrap_or_else(|e| unreachable!("{e}"));
|
|
|
|
|
|
|
|
|
|
debug!("opened daemon to {:?} file descriptor {fd:?}", name);
|
|
|
|
|
|
2026-03-22 15:57:38 +01:00
|
|
|
let path = name
|
|
|
|
|
.as_ref()
|
|
|
|
|
.map(PathBuf::from)
|
|
|
|
|
.map(PathBuf::into_boxed_path);
|
2026-03-11 14:26:59 +01:00
|
|
|
|
|
|
|
|
Self {
|
2026-03-20 20:43:12 +01:00
|
|
|
config_path,
|
2026-03-11 14:26:59 +01:00
|
|
|
fd,
|
2026-03-19 20:44:57 +01:00
|
|
|
path,
|
|
|
|
|
poller,
|
|
|
|
|
fd_info,
|
2026-03-19 19:45:09 +01:00
|
|
|
tokfd: Default::default(),
|
2026-03-11 14:26:59 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 20:43:12 +01:00
|
|
|
pub fn from_unix_socket_path(config_path: Arc<Path>, path: &Path) -> Result<Self, IoError> {
|
2026-03-19 12:36:46 +01:00
|
|
|
// We unconditionally unlink() `path` before binding, but completely ignore the result.
|
|
|
|
|
let _ = rustix::fs::unlink(path);
|
|
|
|
|
let listener = UnixListener::bind(path)
|
|
|
|
|
.tap_err(|e| error!("failed to bind AF_UNIX socket at {}: {e}", path.display()))?;
|
|
|
|
|
let listener_owned_fd = OwnedFd::from(listener);
|
2026-03-19 19:45:09 +01:00
|
|
|
// FIXME: should we KEEP_ALIVE?
|
2026-03-19 12:36:46 +01:00
|
|
|
rustix::net::sockopt::set_socket_keepalive(&listener_owned_fd, true).unwrap();
|
|
|
|
|
let path: Box<OsStr> = path.to_path_buf().into_boxed_path().into_boxed_os_str();
|
|
|
|
|
|
2026-03-20 20:43:12 +01:00
|
|
|
Ok(Self::new(
|
|
|
|
|
config_path,
|
|
|
|
|
listener_owned_fd,
|
|
|
|
|
FdKind::Socket,
|
|
|
|
|
Some(path),
|
|
|
|
|
))
|
2026-03-19 12:36:46 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-20 20:43:12 +01:00
|
|
|
pub fn open_default_socket(config_path: Arc<Path>) -> Result<Self, IoError> {
|
2026-03-19 12:36:46 +01:00
|
|
|
use IoErrorKind::*;
|
|
|
|
|
let preferred = USER_SOCKET_DIR.join("dynix.sock").into_boxed_path();
|
2026-03-20 20:43:12 +01:00
|
|
|
let constructed = match Self::from_unix_socket_path(config_path.clone(), &preferred) {
|
2026-03-19 12:36:46 +01:00
|
|
|
Ok(v) => v,
|
|
|
|
|
Err(e) if e.kind() == Unsupported => {
|
|
|
|
|
//
|
|
|
|
|
return Err(e);
|
|
|
|
|
},
|
|
|
|
|
Err(e) => {
|
|
|
|
|
warn!(
|
|
|
|
|
"failed binding AF_UNIX socket at {}: {e}; trying elsewhere",
|
2026-03-19 21:39:11 +01:00
|
|
|
preferred.display(),
|
2026-03-19 12:36:46 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let fallback = TMPDIR.join("dynix.sock").into_boxed_path();
|
2026-03-20 20:43:12 +01:00
|
|
|
Self::from_unix_socket_path(config_path, &fallback).tap_err(|e| {
|
2026-03-19 12:36:46 +01:00
|
|
|
error!(
|
|
|
|
|
"failed binding AF_UNIX socket at {}: {e}",
|
2026-03-19 21:39:11 +01:00
|
|
|
fallback.display(),
|
2026-03-19 12:36:46 +01:00
|
|
|
)
|
|
|
|
|
})?
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(constructed)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-11 14:26:59 +01:00
|
|
|
/// This panics if stdin cannot be opened.
|
|
|
|
|
///
|
|
|
|
|
/// If you want to handle that error, use [`Daemon::from_raw_parts()`].
|
2026-03-20 20:43:12 +01:00
|
|
|
pub fn from_stdin(config_path: Arc<Path>) -> Self {
|
2026-03-11 14:26:59 +01:00
|
|
|
let stdin = io::stdin();
|
|
|
|
|
let fd = stdin
|
|
|
|
|
.as_fd()
|
|
|
|
|
.try_clone_to_owned()
|
|
|
|
|
.expect("dynix daemon could not open stdin; try a Unix socket?");
|
|
|
|
|
|
2026-03-20 20:43:12 +01:00
|
|
|
Self::new(config_path, fd, FdKind::File, None)
|
2026-03-11 14:26:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//pub unsafe fn from_raw_parts(fd: OwnedFd) -> Self {
|
|
|
|
|
// Self {
|
|
|
|
|
// fd: OwnedFdWithFlags::new_with_fallback(fd),
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
pub fn fd(&self) -> BorrowedFd<'_> {
|
|
|
|
|
self.fd.as_fd()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Private helpers.
|
|
|
|
|
impl Daemon {
|
2026-03-19 20:44:57 +01:00
|
|
|
fn read_cmd(&mut self, fd: &BorrowedFd) -> Result<(), IoError> {
|
2026-03-22 15:57:38 +01:00
|
|
|
// FIXME: don't use a new allocation every time.
|
|
|
|
|
let mut cmd_buffer: Vec<u8> = Vec::with_capacity(1024);
|
2026-03-11 14:26:59 +01:00
|
|
|
|
2026-03-22 15:57:38 +01:00
|
|
|
let _count = rustix::io::read(fd, spare_capacity(&mut cmd_buffer))
|
2026-03-19 12:36:46 +01:00
|
|
|
.tap_err(|e| error!("read() on daemon fd {fd:?} failed: {e}"))?;
|
2026-03-11 14:26:59 +01:00
|
|
|
|
|
|
|
|
// The buffer might have existing data from the last read.
|
2026-03-20 20:43:12 +01:00
|
|
|
let deserializer = serde_json::Deserializer::from_slice(&cmd_buffer);
|
2026-03-11 14:26:59 +01:00
|
|
|
let stream: StreamDeserializer<_, DaemonCmd> = deserializer.into_iter();
|
|
|
|
|
for cmd in stream {
|
|
|
|
|
let cmd = match cmd {
|
|
|
|
|
Ok(cmd) => cmd,
|
|
|
|
|
Err(e) if e.is_eof() => {
|
2026-03-22 15:57:38 +01:00
|
|
|
warn!("Got EOF before a valid command");
|
|
|
|
|
debug!("command buffer was: {:?}", cmd_buffer.as_bstr());
|
2026-03-11 14:26:59 +01:00
|
|
|
return Ok(());
|
|
|
|
|
},
|
|
|
|
|
Err(e) => {
|
|
|
|
|
warn!("error deserializing command: {e}");
|
2026-03-20 20:43:12 +01:00
|
|
|
debug!("command buffer was: {:?}", cmd_buffer.as_bstr());
|
2026-03-11 14:26:59 +01:00
|
|
|
// Don't propagate the error unless we have too many.
|
2026-03-19 20:44:57 +01:00
|
|
|
self.fd_error_push(fd.as_raw_fd(), e.into()).tap_err(|e| {
|
2026-03-19 12:36:46 +01:00
|
|
|
error!("Accumulated too many errors for daemon fd {fd:?}: {e}")
|
2026-03-11 14:26:59 +01:00
|
|
|
})?;
|
|
|
|
|
return Ok(());
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
debug!("got cmd: {cmd:?}");
|
2026-03-19 21:39:11 +01:00
|
|
|
let _ = rustix::io::write(fd, b"");
|
|
|
|
|
info!("dispatching command");
|
2026-03-20 20:43:12 +01:00
|
|
|
self.dispatch_cmd(cmd).unwrap_or_else(|e| todo!("{e}"));
|
2026-03-11 14:26:59 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-20 20:43:12 +01:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn dispatch_cmd(&mut self, cmd: DaemonCmd) -> Result<(), IoError> {
|
|
|
|
|
let (name, value) = match cmd {
|
|
|
|
|
DaemonCmd::Append { name, value } => (name, value),
|
|
|
|
|
};
|
|
|
|
|
let mut opts = File::options();
|
|
|
|
|
opts.read(true)
|
|
|
|
|
.write(true)
|
|
|
|
|
.create(false)
|
|
|
|
|
.custom_flags(libc::O_CLOEXEC);
|
|
|
|
|
let source_file = SourceFile::open_from(self.config_path.clone(), opts)?;
|
|
|
|
|
let pri = crate::get_where(source_file.clone()).unwrap_or_else(|e| todo!("{e}"));
|
|
|
|
|
let new_pri = pri - 1;
|
|
|
|
|
//let new_pri_line =
|
|
|
|
|
// crate::get_next_prio_line(source_file.clone(), Arc::from(name), Arc::from(value));
|
|
|
|
|
// Get next priority line.
|
|
|
|
|
let source_lines = source_file.lines()?;
|
|
|
|
|
let penultimate = source_lines.get(source_lines.len() - 2);
|
|
|
|
|
// FIXME: don't rely on whitespace lol
|
|
|
|
|
debug_assert_eq!(penultimate.map(SourceLine::text).as_deref(), Some(" ];"));
|
|
|
|
|
let penultimate = penultimate.unwrap();
|
|
|
|
|
let new_generation = 0 - new_pri;
|
|
|
|
|
let new_line = SourceLine {
|
|
|
|
|
line: penultimate.line,
|
|
|
|
|
path: source_file.path(),
|
|
|
|
|
text: Arc::from(format!(
|
|
|
|
|
" {} = lib.mkOverride ({}) ({}); # DYNIX GENERATION {}",
|
|
|
|
|
name.to_nix_decl(),
|
|
|
|
|
new_pri,
|
|
|
|
|
value,
|
|
|
|
|
new_generation,
|
|
|
|
|
)),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
drop(source_lines);
|
|
|
|
|
|
|
|
|
|
crate::write_next_prio(source_file, new_line).unwrap_or_else(|e| todo!("{e}"));
|
2026-03-11 14:26:59 +01:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn enter_loop(&mut self) -> Result<Option<()>, IoError> {
|
|
|
|
|
let raw_fd = self.fd.as_raw_fd();
|
2026-03-19 20:44:57 +01:00
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
|
assert!(
|
|
|
|
|
self.fd_info.contains_key(&raw_fd),
|
|
|
|
|
"we should know about daemon fd {raw_fd}",
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
|
|
|
|
self.fd_info.contains_key(&self.poller.as_raw_fd()),
|
|
|
|
|
"we should know about poller fd {}",
|
|
|
|
|
self.poller.as_raw_fd(),
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-03-11 14:26:59 +01:00
|
|
|
let mut daemon_source = SourceFd(&raw_fd);
|
|
|
|
|
const DAEMON: Token = Token(0);
|
2026-03-19 19:45:09 +01:00
|
|
|
self.tokfd
|
|
|
|
|
.insert_unique(TokenFd {
|
|
|
|
|
token: DAEMON,
|
|
|
|
|
fd: raw_fd,
|
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
2026-03-19 20:44:57 +01:00
|
|
|
self.poller
|
|
|
|
|
.registry()
|
2026-03-11 14:26:59 +01:00
|
|
|
.register(&mut daemon_source, DAEMON, Interest::READABLE)
|
|
|
|
|
.unwrap_or_else(|e| unreachable!("registering mio Poll for daemon fd {raw_fd:?}: {e}"));
|
|
|
|
|
|
|
|
|
|
let mut events = Events::with_capacity(1024);
|
|
|
|
|
|
|
|
|
|
loop {
|
2026-03-19 21:39:11 +01:00
|
|
|
if tracing::enabled!(tracing::Level::DEBUG) {
|
|
|
|
|
//
|
|
|
|
|
trace!("Daemon loop iteration, with file descriptors: ");
|
|
|
|
|
for info in &self.fd_info {
|
|
|
|
|
trace!("- {}", info.display());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-22 15:57:38 +01:00
|
|
|
match self.poller.poll(&mut events, TIMEOUT_NEVER) {
|
2026-03-11 14:26:59 +01:00
|
|
|
Ok(_) => {
|
2026-03-19 21:39:11 +01:00
|
|
|
trace!(
|
|
|
|
|
"mio::Poller::poll() got events: {:?}",
|
|
|
|
|
events.iter().size_hint().0,
|
|
|
|
|
);
|
2026-03-11 14:26:59 +01:00
|
|
|
if events.is_empty() {
|
2026-03-22 15:57:38 +01:00
|
|
|
unreachable!(
|
|
|
|
|
"epoll_wait() with a \"forever\" timeout should never give empty events",
|
|
|
|
|
);
|
2026-03-11 14:26:59 +01:00
|
|
|
}
|
2026-03-22 15:57:38 +01:00
|
|
|
|
|
|
|
|
let _ = self.fd_error_pop(self.poller.as_raw_fd());
|
2026-03-11 14:26:59 +01:00
|
|
|
},
|
2026-03-19 21:39:11 +01:00
|
|
|
Err(e) if e.kind() == IoErrorKind::Interrupted => {
|
|
|
|
|
// EINTR is silly.
|
|
|
|
|
continue;
|
|
|
|
|
},
|
2026-03-11 14:26:59 +01:00
|
|
|
Err(e) => {
|
2026-03-19 21:39:11 +01:00
|
|
|
if let Some(Errno::BADF) = e.raw_os_error().map(Errno::from_raw_os_error) {
|
|
|
|
|
unreachable!("EBADF on poller fd; IO safety violation?");
|
|
|
|
|
}
|
2026-03-11 14:26:59 +01:00
|
|
|
warn!("mio Poll::poll() error: {e}");
|
2026-03-19 20:44:57 +01:00
|
|
|
self.fd_error_push(self.poller.as_raw_fd(), e)
|
|
|
|
|
.tap_err(|e| {
|
|
|
|
|
error!("accumulated too many errors for mio::Poll::poll(): {e}")
|
|
|
|
|
})?;
|
2026-03-11 14:26:59 +01:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for event in &events {
|
2026-03-19 21:39:11 +01:00
|
|
|
trace!("event is {event:?}");
|
2026-03-11 14:26:59 +01:00
|
|
|
match event.token() {
|
|
|
|
|
DAEMON => {
|
2026-03-19 21:39:11 +01:00
|
|
|
let is_sock = self.main_fd_info().kind == FdKind::Socket;
|
2026-03-19 12:36:46 +01:00
|
|
|
if !is_sock {
|
2026-03-19 20:44:57 +01:00
|
|
|
// SAFETY: oh boy: disjoint borrows with extra steps.
|
|
|
|
|
let file_fd = unsafe { BorrowedFd::borrow_raw(self.fd.as_raw_fd()) };
|
|
|
|
|
self.read_cmd(&file_fd).unwrap();
|
2026-03-19 12:36:46 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Accept, first.
|
|
|
|
|
let flags = SocketFlags::NONBLOCK | SocketFlags::CLOEXEC;
|
|
|
|
|
let stream_fd = match rustix::net::accept_with(&self.fd, flags) {
|
|
|
|
|
Ok(stream) => {
|
|
|
|
|
debug!(
|
|
|
|
|
"Accepted connection from socket {:?} as stream {:?}",
|
|
|
|
|
self.fd, stream,
|
|
|
|
|
);
|
|
|
|
|
stream
|
|
|
|
|
},
|
|
|
|
|
Err(e) => {
|
|
|
|
|
error!("accept4 on daemon socket failed: {e}");
|
2026-03-19 20:44:57 +01:00
|
|
|
self.fd_error_push(self.fd.as_raw_fd(), e.into())
|
|
|
|
|
.tap_err(|e| {
|
|
|
|
|
error!(
|
|
|
|
|
"Accumulated too many errors for daemon fd {:?}: {e}",
|
|
|
|
|
self.fd
|
|
|
|
|
)
|
|
|
|
|
})?;
|
2026-03-19 12:36:46 +01:00
|
|
|
continue;
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-19 19:45:09 +01:00
|
|
|
// Add this stream to our poll interest list.
|
2026-03-19 20:44:57 +01:00
|
|
|
// NOTE: `stream_fd` is now effectively `ManuallyDrop`.
|
|
|
|
|
let stream_fd = stream_fd.into_raw_fd();
|
2026-03-19 21:39:11 +01:00
|
|
|
let _token = self.register(stream_fd, FdKind::SockStream);
|
2026-03-19 12:36:46 +01:00
|
|
|
|
|
|
|
|
// Wait for the next poll to handle.
|
|
|
|
|
},
|
|
|
|
|
other_token => {
|
|
|
|
|
// This must be a stream fd.
|
2026-03-19 19:45:09 +01:00
|
|
|
let stream_fd = self.fd_for_token(other_token).unwrap_or_else(|| {
|
|
|
|
|
unreachable!("tried to get fd for no-existent token? {other_token:?}")
|
|
|
|
|
});
|
|
|
|
|
|
2026-03-19 21:39:11 +01:00
|
|
|
if event.is_read_closed() {
|
|
|
|
|
self.deregister(stream_fd);
|
|
|
|
|
} else {
|
|
|
|
|
// SAFETY: oh boy.
|
|
|
|
|
let stream_fd = unsafe { BorrowedFd::borrow_raw(stream_fd) };
|
|
|
|
|
self.read_cmd(&stream_fd).unwrap();
|
|
|
|
|
}
|
2026-03-11 14:26:59 +01:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-19 12:36:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for Daemon {
|
|
|
|
|
fn drop(&mut self) {
|
2026-03-19 20:44:57 +01:00
|
|
|
if let Some(path) = self.path.as_deref() {
|
|
|
|
|
let _ = rustix::fs::unlink(path);
|
2026-03-19 12:36:46 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-11 14:26:59 +01:00
|
|
|
}
|