diff --git a/Cargo.lock b/Cargo.lock index d6ad74ed2..aa1f7d42f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -115,12 +115,6 @@ dependencies = [ "tracing-subscriber", ] -[[package]] -name = "boxfnonce" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5988cb1d626264ac94100be357308f29ff7cbdd3b36bda27f450a4ee3f713426" - [[package]] name = "bstr" version = "0.2.17" @@ -413,11 +407,10 @@ dependencies = [ [[package]] name = "daemonize" -version = "0.4.1" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70c24513e34f53b640819f0ac9f705b673fcf4006d7aab8778bee72ebfc89815" +checksum = "ab8bfdaacb3c887a54d41bdf48d3af8873b3f5566469f8ba21b92057509f116e" dependencies = [ - "boxfnonce", "libc", ] diff --git a/boringtun-cli/Cargo.toml b/boringtun-cli/Cargo.toml index fec916fc7..108e5e046 100644 --- a/boringtun-cli/Cargo.toml +++ b/boringtun-cli/Cargo.toml @@ -9,7 +9,7 @@ documentation = "https://docs.rs/boringtun/0.5.2/boringtun/" edition = "2021" [dependencies] -daemonize = "0.4.1" +daemonize = "0.5.0" clap = { version = "3.1.6", features = ["env"] } tracing = "0.1.40" tracing-subscriber = "0.3.9" diff --git a/boringtun-cli/src/main.rs b/boringtun-cli/src/main.rs index 922ce1071..700a5e055 100644 --- a/boringtun-cli/src/main.rs +++ b/boringtun-cli/src/main.rs @@ -4,7 +4,7 @@ use boringtun::device::drop_privileges::drop_privileges; use boringtun::device::{DeviceConfig, DeviceHandle}; use clap::{Arg, Command}; -use daemonize::Daemonize; +use daemonize::{Daemonize, Outcome}; use std::fs::File; use std::os::unix::net::UnixDatagram; use std::process::exit; @@ -118,24 +118,28 @@ fn main() { .with_ansi(false) .init(); - let daemonize = Daemonize::new() - .working_directory("/tmp") - .exit_action(move || { + let daemonize = Daemonize::new().working_directory("/tmp"); + + match daemonize.execute() { + Outcome::Parent(Ok(_)) => { + // In parent process, child forked ok let mut b = [0u8; 1]; if sock2.recv(&mut b).is_ok() && b[0] == 1 { println!("BoringTun started successfully"); + exit(0); } else { eprintln!("BoringTun failed to start"); exit(1); - }; - }); - - match daemonize.start() { - Ok(_) => tracing::info!("BoringTun started successfully"), - Err(e) => { - tracing::error!(error = ?e); + } + } + Outcome::Parent(Err(e)) => { + eprintln!("BoringTun failed to start - Fork error: {}", e); exit(1); } + Outcome::Child(_) => { + // In child process, we'll continue below with code that is common with foreground exec + tracing::info!("BoringTun started"); + } } } else { tracing_subscriber::fmt()