-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix systemd stopping notif, vendor sd_notify
- Loading branch information
Showing
5 changed files
with
51 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use std::{env, ffi::OsString, io, os::unix::net::UnixDatagram}; | ||
|
||
// Sends state notifications to systemd. | ||
// | ||
// Based on https://github.com/lnicola/sd-notify (MIT/Apache-2.0) | ||
pub struct SdNotify { | ||
path: Option<OsString>, | ||
} | ||
|
||
impl SdNotify { | ||
pub fn new() -> Self { | ||
let path = env::var_os("NOTIFY_SOCKET"); | ||
env::remove_var("NOTIFY_SOCKET"); | ||
Self { path } | ||
} | ||
|
||
pub fn notify_ready(&self) { | ||
self.send(b"READY=1\n") | ||
.expect("Failed to signal ready to the service manager"); | ||
} | ||
|
||
pub fn notify_stopping(&self) { | ||
if let Err(err) = self.send(b"STOPPING=1\n") { | ||
log::error!("Failed to signal stopping to the service manager: {err}"); | ||
} | ||
} | ||
|
||
fn send(&self, msg: &[u8]) -> io::Result<()> { | ||
let Some(path) = self.path.as_ref() else { | ||
return Ok(()); | ||
}; | ||
|
||
let sock = UnixDatagram::unbound()?; | ||
let len = sock.send_to(msg, path)?; | ||
if len != msg.len() { | ||
return Err(io::Error::new(io::ErrorKind::WriteZero, "incomplete write")); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |