diff options
Diffstat (limited to 'src/bin/rbw-agent/daemon.rs')
-rw-r--r-- | src/bin/rbw-agent/daemon.rs | 26 |
1 files changed, 10 insertions, 16 deletions
diff --git a/src/bin/rbw-agent/daemon.rs b/src/bin/rbw-agent/daemon.rs index 4957c28..640d5e2 100644 --- a/src/bin/rbw-agent/daemon.rs +++ b/src/bin/rbw-agent/daemon.rs @@ -1,22 +1,16 @@ +use std::os::fd::AsRawFd as _; + pub struct StartupAck { - writer: std::os::unix::io::RawFd, + writer: std::os::unix::io::OwnedFd, } impl StartupAck { - pub fn ack(&self) -> anyhow::Result<()> { - nix::unistd::write(self.writer, &[0])?; - nix::unistd::close(self.writer)?; + pub fn ack(self) -> anyhow::Result<()> { + nix::unistd::write(&self.writer, &[0])?; Ok(()) } } -impl Drop for StartupAck { - fn drop(&mut self) { - // best effort close here, can't do better in a destructor - let _ = nix::unistd::close(self.writer); - } -} - pub fn daemonize() -> anyhow::Result<StartupAck> { let stdout = std::fs::OpenOptions::new() .append(true) @@ -34,18 +28,18 @@ pub fn daemonize() -> anyhow::Result<StartupAck> { .stderr(stderr); let res = match daemonize.execute() { daemonize::Outcome::Parent(_) => { + drop(w); + let mut buf = [0; 1]; // 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(); + nix::unistd::read(r.as_raw_fd(), &mut buf).unwrap(); + drop(r); std::process::exit(0); } daemonize::Outcome::Child(res) => res, }; - let _ = nix::unistd::close(r); + drop(r); match res { Ok(_) => (), |