mirror of
https://codeberg.org/icewind/netnsd.git
synced 2026-06-03 09:04:07 +02:00
implement reload command
This commit is contained in:
parent
766f876230
commit
c93fb84773
3 changed files with 115 additions and 16 deletions
65
Cargo.lock
generated
65
Cargo.lock
generated
|
|
@ -570,6 +570,7 @@ dependencies = [
|
|||
"sd-notify",
|
||||
"serde",
|
||||
"serde_test",
|
||||
"sysinfo",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
"tokio-stream",
|
||||
|
|
@ -591,6 +592,15 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ntapi"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4"
|
||||
dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nu-ansi-term"
|
||||
version = "0.50.3"
|
||||
|
|
@ -600,6 +610,25 @@ dependencies = [
|
|||
"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]]
|
||||
name = "once_cell"
|
||||
version = "1.21.3"
|
||||
|
|
@ -883,6 +912,20 @@ dependencies = [
|
|||
"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]]
|
||||
name = "thiserror"
|
||||
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"
|
||||
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]]
|
||||
name = "windows"
|
||||
version = "0.61.3"
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
name = "netnsd"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
rust-version = "1.87.0"
|
||||
rust-version = "1.88.0"
|
||||
|
||||
[dependencies]
|
||||
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"
|
||||
either = "1.15.0"
|
||||
uzers = "0.12.1"
|
||||
sysinfo = "0.37.2"
|
||||
|
||||
[dev-dependencies]
|
||||
serde_test = "1.0.177"
|
||||
63
src/main.rs
63
src/main.rs
|
|
@ -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(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue