basic reloading

This commit is contained in:
Robin Appelman 2025-10-30 18:56:02 +01:00
commit 78e716f949
9 changed files with 562 additions and 32 deletions

View file

@ -1,20 +1,27 @@
mod namespace;
use crate::config::{Config, NamespaceName};
use crate::config::{Config, NamespaceConfig, NamespaceName};
use crate::daemon::namespace::{NamespaceError, NetNs};
use main_error::MainResult;
use sd_notify::{notify, NotifyState};
use sd_notify::{NotifyState, notify};
use std::io::Error as IoError;
use std::pin::pin;
use futures::FutureExt;
use thiserror::Error;
use tokio::runtime::Runtime;
use tokio::signal::ctrl_c;
use tokio::signal::unix::{SignalKind, signal};
use futures::StreamExt;
use futures_concurrency::stream::Merge;
use tokio_stream::wrappers::SignalStream;
use tracing::{debug, error, info};
pub fn daemon(config: Config) -> MainResult {
let rt = Runtime::new()?;
Ok(rt.block_on(daemon_async(config))?)
}
async fn daemon_async(config: Config) -> Result<(), DaemonError> {
async fn daemon_async(mut config: Config) -> Result<(), DaemonError> {
for namespace in &config.namespaces {
println!("{}:", namespace.name);
for forward in &namespace.forward {
@ -22,28 +29,104 @@ async fn daemon_async(config: Config) -> Result<(), DaemonError> {
}
}
let namespaces = config
.namespaces
.iter()
.map(|ns| ActiveNamespace::new(&ns.name))
.collect::<Result<Vec<_>, _>>()?;
let mut state = State::default();
state.update(&config)?;
// now the namespaces are setup, we can tell systemd to start any service depending on them
notify(true, &[NotifyState::Ready]).map_err(DaemonError::Notify)?;
let _ = ctrl_c().await;
let reload_signal = signal(SignalKind::hangup()).map_err(DaemonError::ReloadSignal)?;
let reload_signal = SignalStream::new(reload_signal).map(|_| Event::Reload);
let quit_signal = ctrl_c().into_stream().map(|_| Event::Quit);
let events = (reload_signal, quit_signal).merge();
let mut events = pin!(events);
while let Some(event) = events.next().await {
debug!(?event, "handling event");
match event {
Event::Quit => {
break;
}
Event::Reload => {
info!("reloading config");
match NotifyState::monotonic_usec_now() {
Ok(notify_time) => {
notify(true, &[NotifyState::Reloading, notify_time]).map_err(DaemonError::Notify)?;
}
Err(error) => {
error!(%error, "failed to get current time, not sending reload signal");
}
}
match config.reload() {
Ok(new_config) => {
state.update(&new_config)?;
config = new_config;
},
Err(error) => {
error!(%error, "Failed to load new config");
}
}
notify(true, &[NotifyState::Ready]).map_err(DaemonError::Notify)?;
}
}
}
Ok(())
}
#[derive(Debug)]
enum Event {
Reload,
Quit,
}
#[derive(Default)]
struct State {
namespaces: Vec<ActiveNamespace>,
}
impl State {
pub fn update(&mut self, config: &Config) -> Result<(), DaemonError> {
self.namespaces.retain(|existing| {
config
.namespaces
.iter()
.any(|new| &new.name == existing.name())
});
for new in &config.namespaces {
if !self.has_namespace(&new.name) {
self.namespaces.push(ActiveNamespace::new(new)?);
}
}
Ok(())
}
fn has_namespace(&self, name: &NamespaceName) -> bool {
self
.namespaces
.iter()
.any(|existing| existing.name() == name)
}
}
struct ActiveNamespace {
ns: NetNs,
}
impl ActiveNamespace {
pub fn new(name: &NamespaceName) -> Result<Self, DaemonError> {
let ns = NetNs::new(name)?;
pub fn new(config: &NamespaceConfig) -> Result<Self, DaemonError> {
let ns = NetNs::new(&config.name)?;
Ok(ActiveNamespace { ns })
}
pub fn name(&self) -> &NamespaceName {
self.ns.name()
}
}
#[derive(Debug, Error)]
@ -51,5 +134,7 @@ pub enum DaemonError {
#[error(transparent)]
Namespace(#[from] NamespaceError),
#[error("Error sending notification to systemd: {0:#}")]
Notify(IoError)
Notify(IoError),
#[error("Error setting up reload signal listener: {0:#}")]
ReloadSignal(IoError),
}