implement reload command

This commit is contained in:
Robin Appelman 2025-11-14 19:21:10 +01:00
commit c93fb84773
3 changed files with 115 additions and 16 deletions

View file

@ -1,18 +1,23 @@
use std::path::{PathBuf};
use clap::{Parser, Subcommand};
use main_error::MainResult;
use crate::config::{Config, ForwardSource, ForwardTarget, NamespaceName};
use crate::daemon::daemon;
use crate::down::down;
use crate::proxy::proxy;
use crate::up::up;
use clap::{Parser, Subcommand};
use main_error::MainResult;
use std::path::PathBuf;
use nix::errno::Errno;
use nix::sys::signal::{kill, Signal};
use nix::unistd::Pid;
use sysinfo::{ProcessRefreshKind, RefreshKind, System, UpdateKind};
use tracing::{error, info, warn};
mod config;
mod daemon;
mod down;
mod link;
mod namespace;
mod proxy;
mod link;
mod down;
mod up;
#[derive(Parser, Debug)]
@ -55,26 +60,54 @@ enum Commands {
fn main() -> MainResult {
let args: Args = Args::parse();
tracing_subscriber::fmt::init();
match args.command {
Commands::Daemon { config } => {
let config = Config::load(config)?;
daemon(config)
}
Commands::Up {config} => {
Commands::Up { config } => {
let config = Config::load(config)?;
up(config)
}
Commands::Down => {
down()
}
Commands::Down => down(),
Commands::Reload => reload(),
Commands::Proxy {source, target, source_namespace, target_namespace} => {
proxy(source_namespace, target_namespace, source, target)
},
Commands::Proxy {
source,
target,
source_namespace,
target_namespace,
} => proxy(source_namespace, target_namespace, source, target),
}
}
fn reload() -> MainResult {
todo!()
}
let s = System::new_with_specifics(
RefreshKind::default()
.with_processes(ProcessRefreshKind::default().with_cmd(UpdateKind::OnlyIfNotSet)),
);
let mut found = false;
for proc in s.processes_by_exact_name("netnsd".as_ref()) {
if proc.cmd().get(1).and_then(|s| s.to_str()) == Some("daemon") {
found = true;
match kill(Pid::from_raw(proc.pid().as_u32() as i32), Signal::SIGHUP) {
Ok(_) => {
info!("Sent reload command to daemon")
},
Err(Errno::EPERM) => {
error!("Sending signal not permitted, try are you running the command as root?");
},
Err(error) => {
error!(%error, "Unexpected error");
}
}
}
}
if !found {
warn!("No daemon process found")
}
Ok(())
}