diff options
Diffstat (limited to 'src/platform')
| -rw-r--r-- | src/platform/linux.rs | 9 | ||||
| -rw-r--r-- | src/platform/windows.rs | 10 |
2 files changed, 12 insertions, 7 deletions
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() |
