From 236f06736e45c2a70f43589c9d447a0a3ef240b5 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 12 Apr 2020 00:08:03 -0400 Subject: improve error handling and reporting --- src/bin/rbw-agent/daemon.rs | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'src/bin/rbw-agent/daemon.rs') diff --git a/src/bin/rbw-agent/daemon.rs b/src/bin/rbw-agent/daemon.rs index 64cf298..80c94ad 100644 --- a/src/bin/rbw-agent/daemon.rs +++ b/src/bin/rbw-agent/daemon.rs @@ -1,35 +1,42 @@ +use anyhow::Context as _; + pub struct StartupAck { writer: std::os::unix::io::RawFd, } impl StartupAck { - pub fn ack(&self) { - nix::unistd::write(self.writer, &[0]).unwrap(); - nix::unistd::close(self.writer).unwrap(); + pub fn ack(&self) -> anyhow::Result<()> { + nix::unistd::write(self.writer, &[0])?; + nix::unistd::close(self.writer)?; + Ok(()) } } impl Drop for StartupAck { fn drop(&mut self) { - nix::unistd::close(self.writer).unwrap(); + // best effort close here, can't do better in a destructor + let _ = nix::unistd::close(self.writer); } } -pub fn daemonize() -> StartupAck { +pub fn daemonize() -> anyhow::Result { let runtime_dir = rbw::dirs::runtime_dir(); - std::fs::create_dir_all(&runtime_dir).unwrap(); + std::fs::create_dir_all(&runtime_dir) + .context("failed to create runtime directory")?; - let (r, w) = nix::unistd::pipe().unwrap(); + let (r, w) = nix::unistd::pipe()?; let res = daemonize::Daemonize::new() .pid_file(runtime_dir.join("pidfile")) .exit_action(move || { - nix::unistd::close(w).unwrap(); + // unwraps are necessary because not really a good way to handle + // errors here otherwise + let _ = nix::unistd::close(w); let mut buf = [0; 1]; nix::unistd::read(r, &mut buf).unwrap(); nix::unistd::close(r).unwrap(); }) .start(); - nix::unistd::close(r).unwrap(); + let _ = nix::unistd::close(r); match res { Ok(_) => (), @@ -46,5 +53,5 @@ pub fn daemonize() -> StartupAck { } } - StartupAck { writer: w } + Ok(StartupAck { writer: w }) } -- cgit v1.2.3-54-g00ecf