chiark / gitweb /
pidfile option
[hippotat.git] / server / server.rs
index 05796d06bf5bde08a481e7a8e1ec74b649b82ee6..3fd58a491bdcfa6d092ab310813e3f0d4fc9ad46 100644 (file)
@@ -25,6 +25,10 @@ pub struct Opts {
   #[structopt(long)]
   daemon: bool,
 
+  /// Write our pid to this file
+  #[structopt(long)]
+  pidfile: Option<String>,
+
   /// Print a config item, do not actually run
   #[structopt(long)]
   print_config: Option<String>,
@@ -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<Daemoniser>) {
       }
     }
 
+    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();