Add support for watchdog

This commit is contained in:
Mathieu Trossevin 2024-01-04 13:47:14 +01:00
parent a14254bb83
commit 8fdd802e82
Signed by: mtrossevin
GPG key ID: D1DBB7EA828374E9
2 changed files with 35 additions and 1 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "storefd"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
license = "MIT"
authors = ["Mathieu Trossevin <mtrossevin@evolix.fr>"]

View file

@ -162,6 +162,40 @@ impl Notifier {
}
}
/// Check for watchdog support at runtime
///
/// If `unset_env` is true, the environment variables related to watchdog support will be cleared.
///
/// # Return
///
/// * [`None`] if watchdog support is not enabled.
/// * The timeout before which the watchdog expects a response from the process otherwise.
pub fn is_watchdog_enabled(unset_env: bool) -> Option<std::time::Duration> {
let timeout = std::env::var("WATCHDOG_USEC").ok();
let watchdog_pid = std::env::var("WATCHDOG_PID").ok();
if unset_env {
std::env::remove_var("WATCHDOG_USEC");
std::env::remove_var("WATCHDOG_PID");
}
let timeout = timeout
.and_then(|timeout| timeout.parse::<u64>().ok())
.map(std::time::Duration::from_micros)?;
let watchdog_pid = if let Some(pid) = watchdog_pid {
pid.parse::<u32>().ok()?
} else {
return Some(timeout);
};
if watchdog_pid == std::process::id() {
Some(timeout)
} else {
None
}
}
/// Status changes, see `sd_notify(3)`.
#[allow(clippy::module_name_repetitions)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]