summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs6
-rw-r--r--src/opt.rs29
-rw-r--r--src/platform/linux.rs9
-rw-r--r--src/platform/windows.rs10
4 files changed, 36 insertions, 18 deletions
diff --git a/src/main.rs b/src/main.rs
index 23d8e09..7d0e7bd 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -65,12 +65,10 @@ fn splash_banner() {
fn main_0() -> Result<()> {
trap_exit()?;
let opt = opt::Opt::from_args()?;
- if opt.daemon {
- platform::daemonize_1();
- }
- opt.set_opt()?;
+ let initialized = opt.set_opt()?;
splash_banner();
platform::bootstrap()?;
+ initialized.log();
let _guard = EnsureCleanup;
diff --git a/src/opt.rs b/src/opt.rs
index 87bc910..814b48f 100644
--- a/src/opt.rs
+++ b/src/opt.rs
@@ -40,7 +40,7 @@ const DEFAULT_DELAY_MS: u64 = 0;
#[cfg(target_os = "linux")] const DEFAULT_NFT_COMMAND: &str = "nft";
pub struct Opt {
- pub daemon: bool,
+ daemon: bool,
log_level: LogLevel,
no_splash: bool,
fake: bool,
@@ -125,7 +125,7 @@ impl Opt {
})
}
- pub fn set_opt(self) -> Result<()> {
+ pub fn set_opt(self) -> Result<InitializedOpts> {
set_opt("OPT_DAEMON", &OPT_DAEMON, self.daemon)?;
set_opt("OPT_LOG_LEVEL", &OPT_LOG_LEVEL, self.log_level)?;
set_opt("OPT_NO_SPLASH", &OPT_NO_SPLASH, self.no_splash)?;
@@ -139,7 +139,26 @@ impl Opt {
#[cfg(target_os = "linux")] set_opt("OPT_QUEUE_NUM", &OPT_QUEUE_NUM, self.queue_num)?;
#[cfg(target_os = "linux")] set_opt("OPT_NFT_COMMAND", &OPT_NFT_COMMAND, self.nft_command)?;
- Ok(())
+ Ok(InitializedOpts)
+ }
+}
+
+pub struct InitializedOpts;
+
+impl InitializedOpts {
+ pub fn log(&self) {
+ log_println!(LogLevel::Info, "OPT_DAEMON: {}", daemon());
+ log_println!(LogLevel::Info, "OPT_NO_SPLASH: {}", no_splash());
+ log_println!(LogLevel::Info, "OPT_LOG_LEVEL: {}", log_level());
+ log_println!(LogLevel::Info, "OPT_DELAY_MS: {}", delay_ms());
+ log_println!(LogLevel::Info, "OPT_FAKE: {}", fake());
+ log_println!(LogLevel::Info, "OPT_FAKE_TTL: {}", fake_ttl());
+ log_println!(LogLevel::Info, "OPT_FAKE_AUTOTTL: {}", fake_autottl());
+ log_println!(LogLevel::Info, "OPT_FAKE_BADSUM: {}", fake_badsum());
+ #[cfg(target_os = "linux")]
+ log_println!(LogLevel::Info, "OPT_QUEUE_NUM: {}", queue_num());
+ #[cfg(target_os = "linux")]
+ log_println!(LogLevel::Info, "OPT_NFT_COMMAND: {}", nft_command());
}
}
@@ -226,9 +245,5 @@ fn set_opt<T: std::fmt::Display>(
value: T,
) -> Result<()> {
cell.set(value).map_err(|_| anyhow!("{name} already initialized"))?;
-
- let v = cell.get().expect("just set; qed");
- log_println!(LogLevel::Info, "{name}: {v}");
-
Ok(())
}
diff --git a/src/platform/linux.rs b/src/platform/linux.rs
index bf81ceb..bb829fc 100644
--- a/src/platform/linux.rs
+++ b/src/platform/linux.rs
@@ -134,6 +134,8 @@ pub fn bootstrap() -> Result<()> {
exit_if_not_root();
if !opt::daemon() {
lock_pid_file()?;
+ } else {
+ daemonize_1();
}
Ok(())
@@ -281,19 +283,18 @@ fn daemonize() -> Result<()> {
daemonize.start()?;
log_file.set_len(0)?;
- // TODO: detach damonize and opt.rs and use log_println here
- println!("start as daemon: pid {}", std::process::id());
+ log_println!(LogLevel::Info, "start as daemon: pid {}", std::process::id());
Ok(())
}
-pub fn daemonize_1() {
+fn daemonize_1() {
const EXIT_DAEMON_FAIL: i32 = 2;
match daemonize() {
Ok(_) => {},
Err(e) => {
- println!("{PKG_NAME}: fail to start as daemon: {e}");
+ log_println!(LogLevel::Error, "fail to start as daemon: {e}");
std::process::exit(EXIT_DAEMON_FAIL);
}
}
diff --git a/src/platform/windows.rs b/src/platform/windows.rs
index 9ad2474..2bda39e 100644
--- a/src/platform/windows.rs
+++ b/src/platform/windows.rs
@@ -62,6 +62,10 @@ fn lock_handle() -> MutexGuard<'static, WinDivert<NetworkLayer>> {
}
pub fn bootstrap() -> Result<()> {
+ if opt::daemonize() {
+ service_main();
+ }
+
Ok(())
}
@@ -119,7 +123,7 @@ pub fn run() -> Result<()> {
Ok(())
}
-fn service_main() -> Result<()> {
+fn service_run() -> Result<()> {
let opt = opt::Opt::from_args()?;
opt.set_opt()?;
bootstrap()?;
@@ -129,14 +133,14 @@ fn service_main() -> Result<()> {
Ok(())
}
-fn service_main_1() {
+fn service_run_1() {
if service_main().is_err() {
std::process::exit(1);
}
std::process::exit(0);
}
-pub fn daemonize_1() {
+fn service_main() {
use windows_services::Command;
match windows_services::Service::new()