diff --git a/CHANGELOG.md b/CHANGELOG.md index c1c31ba..e1bbf1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ - (breaking) the `unset_env` parameters were split off into separate unsafe functions - (breaking) `watchdog_enabled` now returns `Option` + - the MSRV is now defined as 1.82 + + ### Fixed + - fixed `watchdog_enabled` to handle missing `WATCHDOG_PID` ## [0.4.5] - 2025-01-18 @@ -35,42 +39,42 @@ ### Changed -- added `watchdog_enabled` (similar to [`sd_watchdog_enabled`](https://www.freedesktop.org/software/systemd/man/sd_watchdog_enabled.html)) + - added `watchdog_enabled` (similar to [`sd_watchdog_enabled`](https://www.freedesktop.org/software/systemd/man/sd_watchdog_enabled.html)) ## [0.4.0] - 2022-01-12 ### Changed -- (breaking) `NotifyState::Status`, `NotifyState::BusError` and `NotifyState::Custom` now contain a `&str` instead of a `String` -- the crate is now using the 2021 edition + - (breaking) `NotifyState::Status`, `NotifyState::BusError` and `NotifyState::Custom` now contain a `&str` instead of a `String` + - the crate is now using the 2021 edition ## [0.3.0] - 2021-02-25 ### Changed -- (breaking) `listen_fds` now returns an iterator over `RawFd` values -- (breaking) `SD_LISTEN_FDS_START` is gone + - (breaking) `listen_fds` now returns an iterator over `RawFd` values + - (breaking) `SD_LISTEN_FDS_START` is gone ## [0.2.0] - 2021-02-18 ### Changed -- (breaking) changed the `NotifyState::MainPid` and `NotifyState::Error` data from `i32` to `u32` -- (breaking) changed `listen_fds` to return `Result` instead of `Result` + - (breaking) changed the `NotifyState::MainPid` and `NotifyState::Error` data from `i32` to `u32` + - (breaking) changed `listen_fds` to return `Result` instead of `Result` ### Fixed -- fixed `Display` implementation for `NotifyState::WatchdogUsec` and `NotifyState::ExtendTimeoutUsec` -- removed a stray debug print + - fixed `Display` implementation for `NotifyState::WatchdogUsec` and `NotifyState::ExtendTimeoutUsec` + - removed a stray debug print ## [0.1.1] - 2019-10-20 ### Added -- `listen_fds` function for file descriptor retrieval when using socket activation + - `listen_fds` function for file descriptor retrieval when using socket activation ## [0.1.0] - 2019-09-22 ### Added -- initial release + - initial release diff --git a/Cargo.toml b/Cargo.toml index 7bbbe78..80a10f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ name = "sd-notify" version = "0.5.0" edition = "2021" +rust-version = "1.82" description = "Lightweight crate for systemd service state notifications" readme = "README.md" keywords = ["systemd", "sd_notify"] diff --git a/src/lib.rs b/src/lib.rs index 5c62894..ddc9fdc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -486,13 +486,8 @@ pub fn watchdog_enabled() -> Option { let p = env::var(WATCHDOG_PID) .ok() .and_then(|s| u32::from_str(&s).ok()); - - match (s, p) { - (Some(usec_val), Some(pid)) if pid == process::id() => { - Some(Duration::from_micros(usec_val)) - } - _ => None, - } + s.filter(|_| p.is_none_or(|pid| pid == process::id())) + .map(Duration::from_micros) } /// Asks the service manager for enabled watchdog. diff --git a/tests/watchdog_enabled.rs b/tests/watchdog_enabled.rs index f516957..ab4f8de 100644 --- a/tests/watchdog_enabled.rs +++ b/tests/watchdog_enabled.rs @@ -35,13 +35,28 @@ fn watchdog_enabled() { assert!(env::var_os("WATCHDOG_USEC").is_none()); assert!(env::var_os("WATCHDOG_PID").is_none()); - // no usec, no pip no unset env + // valid usec, no pid + unsafe { + env::set_var("WATCHDOG_USEC", "10"); + } + + unsafe { + assert_eq!( + sd_notify::watchdog_enabled_and_unset_env(), + Some(Duration::from_micros(10)) + ); + } + + assert!(env::var_os("WATCHDOG_USEC").is_none()); + assert!(env::var_os("WATCHDOG_PID").is_none()); + + // no usec, no pid, no unset env assert_eq!(sd_notify::watchdog_enabled(), None); assert!(env::var_os("WATCHDOG_USEC").is_none()); assert!(env::var_os("WATCHDOG_PID").is_none()); - // valid pip + // valid pid unsafe { env::set_var("WATCHDOG_USEC", "5"); env::set_var("WATCHDOG_PID", process::id().to_string());