diff options
author | Jesse Luehrs <doy@tozt.net> | 2020-04-12 00:08:03 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2020-04-12 00:08:03 -0400 |
commit | 236f06736e45c2a70f43589c9d447a0a3ef240b5 (patch) | |
tree | c390d4cbfec5223ac1aefe3947f8e1bb885757d2 /src/bin/rbw-agent/daemon.rs | |
parent | 91d1d1890bdc3ee75b69e00d5368c5b29a4f461c (diff) | |
download | rbw-236f06736e45c2a70f43589c9d447a0a3ef240b5.tar.gz rbw-236f06736e45c2a70f43589c9d447a0a3ef240b5.zip |
improve error handling and reporting
Diffstat (limited to 'src/bin/rbw-agent/daemon.rs')
-rw-r--r-- | src/bin/rbw-agent/daemon.rs | 27 |
1 files changed, 17 insertions, 10 deletions
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<StartupAck> { 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 }) } |