]> git.proxmox.com Git - proxmox.git/commitdiff
tools/daemon: add sd_notify wrapper
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Thu, 25 Apr 2019 08:35:01 +0000 (08:35 +0000)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Thu, 25 Apr 2019 11:01:28 +0000 (11:01 +0000)
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
src/tools/daemon.rs

index 432756d826b5e8a27834e399ae43d68acdd64282..870e561080266e2c6b3a361347b1f0fcd8c418f9 100644 (file)
@@ -1,6 +1,7 @@
 //! Helpers for daemons/services.
 
 use std::ffi::CString;
+use std::os::raw::{c_char, c_int};
 use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
 use std::os::unix::ffi::OsStrExt;
 use std::panic::UnwindSafe;
@@ -187,3 +188,34 @@ where
        .map_err(|_| ())
     )
 }
+
+#[link(name = "systemd")]
+extern "C" {
+    fn sd_notify(unset_environment: c_int, state: *const c_char) -> c_int;
+}
+
+pub enum SystemdNotify {
+    Ready,
+    Reloading,
+    Stopping,
+    Status(String),
+    MainPid(nix::unistd::Pid),
+}
+
+pub fn systemd_notify(state: SystemdNotify) -> Result<(), Error> {
+    let message = match state {
+        SystemdNotify::Ready => CString::new("READY=1"),
+        SystemdNotify::Reloading => CString::new("RELOADING=1"),
+        SystemdNotify::Stopping => CString::new("STOPPING=1"),
+        SystemdNotify::Status(msg) => CString::new(format!("STATUS={}", msg)),
+        SystemdNotify::MainPid(pid) => CString::new(format!("MAINPID={}", pid)),
+    }?;
+    let rc = unsafe { sd_notify(0, message.as_ptr()) };
+    if rc < 0 {
+        bail!(
+            "systemd_notify failed: {}",
+            std::io::Error::from_raw_os_error(-rc),
+        );
+    }
+    Ok(())
+}