From 40dc7033e211c68138728d82f171fddf7d41e5ce Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 25 Sep 2022 20:13:57 +0100 Subject: [PATCH] pidfile option Signed-off-by: Ian Jackson --- server/server.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/server/server.rs b/server/server.rs index 05796d0..3fd58a4 100644 --- a/server/server.rs +++ b/server/server.rs @@ -25,6 +25,10 @@ pub struct Opts { #[structopt(long)] daemon: bool, + /// Write our pid to this file + #[structopt(long)] + pidfile: Option, + /// Print a config item, do not actually run #[structopt(long)] print_config: Option, @@ -127,6 +131,7 @@ pub async fn route_packet(global: &Global, fn main() { let opts = Opts::from_args(); + let daemon = if opts.daemon && opts.print_config.is_none() { Some(Daemoniser::phase1()) } else { @@ -158,6 +163,15 @@ async fn async_main(opts: Opts, daemon: Option) { } } + if let Some(pidfile_path) = opts.pidfile.as_ref() { + (||{ + let mut pidfile = fs::File::create(pidfile_path).context("create")?; + writeln!(pidfile, "{}", process::id()).context("write")?; + pidfile.flush().context("write (flush)")?; + Ok::<_,AE>(()) + })().with_context(|| format!("pidfile {:?}", pidfile_path))?; + } + let ipif = Ipif::start(&global_config.ipif, None)?; let ics = ics.into_iter().map(Arc::new).collect_vec(); -- 2.30.2