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

65
Cargo.lock generated
View file

@ -570,6 +570,7 @@ dependencies = [
"sd-notify", "sd-notify",
"serde", "serde",
"serde_test", "serde_test",
"sysinfo",
"thiserror", "thiserror",
"tokio", "tokio",
"tokio-stream", "tokio-stream",
@ -591,6 +592,15 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "ntapi"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "nu-ansi-term" name = "nu-ansi-term"
version = "0.50.3" version = "0.50.3"
@ -600,6 +610,25 @@ dependencies = [
"windows-sys 0.61.2", "windows-sys 0.61.2",
] ]
[[package]]
name = "objc2-core-foundation"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a180dd8642fa45cdb7dd721cd4c11b1cadd4929ce112ebd8b9f5803cc79d536"
dependencies = [
"bitflags",
]
[[package]]
name = "objc2-io-kit"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33fafba39597d6dc1fb709123dfa8289d39406734be322956a69f0931c73bb15"
dependencies = [
"libc",
"objc2-core-foundation",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.21.3" version = "1.21.3"
@ -883,6 +912,20 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "sysinfo"
version = "0.37.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16607d5caffd1c07ce073528f9ed972d88db15dd44023fa57142963be3feb11f"
dependencies = [
"libc",
"memchr",
"ntapi",
"objc2-core-foundation",
"objc2-io-kit",
"windows",
]
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "2.0.17" version = "2.0.17"
@ -1084,6 +1127,28 @@ version = "0.11.1+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]] [[package]]
name = "windows" name = "windows"
version = "0.61.3" version = "0.61.3"

View file

@ -2,7 +2,7 @@
name = "netnsd" name = "netnsd"
version = "0.1.0" version = "0.1.0"
edition = "2024" edition = "2024"
rust-version = "1.87.0" rust-version = "1.88.0"
[dependencies] [dependencies]
tokio = { version = "1.48.0", features = ["macros", "rt", "signal", "net", "io-util"] } tokio = { version = "1.48.0", features = ["macros", "rt", "signal", "net", "io-util"] }
@ -21,6 +21,7 @@ futures-concurrency = "7.6.3"
neli = "0.7.1" neli = "0.7.1"
either = "1.15.0" either = "1.15.0"
uzers = "0.12.1" uzers = "0.12.1"
sysinfo = "0.37.2"
[dev-dependencies] [dev-dependencies]
serde_test = "1.0.177" serde_test = "1.0.177"

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::config::{Config, ForwardSource, ForwardTarget, NamespaceName};
use crate::daemon::daemon; use crate::daemon::daemon;
use crate::down::down; use crate::down::down;
use crate::proxy::proxy; use crate::proxy::proxy;
use crate::up::up; 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 config;
mod daemon; mod daemon;
mod down;
mod link;
mod namespace; mod namespace;
mod proxy; mod proxy;
mod link;
mod down;
mod up; mod up;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@ -61,20 +66,48 @@ fn main() -> MainResult {
let config = Config::load(config)?; let config = Config::load(config)?;
daemon(config) daemon(config)
} }
Commands::Up {config} => { Commands::Up { config } => {
let config = Config::load(config)?; let config = Config::load(config)?;
up(config) up(config)
} }
Commands::Down => { Commands::Down => down(),
down()
}
Commands::Reload => reload(), Commands::Reload => reload(),
Commands::Proxy {source, target, source_namespace, target_namespace} => { Commands::Proxy {
proxy(source_namespace, target_namespace, source, target) source,
}, target,
source_namespace,
target_namespace,
} => proxy(source_namespace, target_namespace, source, target),
} }
} }
fn reload() -> MainResult { 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(())
} }