Skip to content

Commit f0a0238

Browse files
authored
Merge pull request #20 from lnicola/watchdog-enabled-no-pid
Handle missing WATCHDOG_PID in watchdog_enabled
2 parents 95697fc + 1e93969 commit f0a0238

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
lines changed

CHANGELOG.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
- (breaking) the `unset_env` parameters were split off into separate unsafe functions
88
- (breaking) `watchdog_enabled` now returns `Option<Duration>`
9+
- the MSRV is now defined as 1.82
10+
11+
### Fixed
12+
- fixed `watchdog_enabled` to handle missing `WATCHDOG_PID`
913

1014
## [0.4.5] - 2025-01-18
1115

@@ -35,42 +39,42 @@
3539

3640
### Changed
3741

38-
- added `watchdog_enabled` (similar to [`sd_watchdog_enabled`](https://www.freedesktop.org/software/systemd/man/sd_watchdog_enabled.html))
42+
- added `watchdog_enabled` (similar to [`sd_watchdog_enabled`](https://www.freedesktop.org/software/systemd/man/sd_watchdog_enabled.html))
3943

4044
## [0.4.0] - 2022-01-12
4145

4246
### Changed
4347

44-
- (breaking) `NotifyState::Status`, `NotifyState::BusError` and `NotifyState::Custom` now contain a `&str` instead of a `String`
45-
- the crate is now using the 2021 edition
48+
- (breaking) `NotifyState::Status`, `NotifyState::BusError` and `NotifyState::Custom` now contain a `&str` instead of a `String`
49+
- the crate is now using the 2021 edition
4650

4751
## [0.3.0] - 2021-02-25
4852

4953
### Changed
5054

51-
- (breaking) `listen_fds` now returns an iterator over `RawFd` values
52-
- (breaking) `SD_LISTEN_FDS_START` is gone
55+
- (breaking) `listen_fds` now returns an iterator over `RawFd` values
56+
- (breaking) `SD_LISTEN_FDS_START` is gone
5357

5458
## [0.2.0] - 2021-02-18
5559

5660
### Changed
5761

58-
- (breaking) changed the `NotifyState::MainPid` and `NotifyState::Error` data from `i32` to `u32`
59-
- (breaking) changed `listen_fds` to return `Result<u32>` instead of `Result<i32>`
62+
- (breaking) changed the `NotifyState::MainPid` and `NotifyState::Error` data from `i32` to `u32`
63+
- (breaking) changed `listen_fds` to return `Result<u32>` instead of `Result<i32>`
6064

6165
### Fixed
6266

63-
- fixed `Display` implementation for `NotifyState::WatchdogUsec` and `NotifyState::ExtendTimeoutUsec`
64-
- removed a stray debug print
67+
- fixed `Display` implementation for `NotifyState::WatchdogUsec` and `NotifyState::ExtendTimeoutUsec`
68+
- removed a stray debug print
6569

6670
## [0.1.1] - 2019-10-20
6771

6872
### Added
6973

70-
- `listen_fds` function for file descriptor retrieval when using socket activation
74+
- `listen_fds` function for file descriptor retrieval when using socket activation
7175

7276
## [0.1.0] - 2019-09-22
7377

7478
### Added
7579

76-
- initial release
80+
- initial release

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
name = "sd-notify"
33
version = "0.5.0"
44
edition = "2021"
5+
rust-version = "1.82"
56
description = "Lightweight crate for systemd service state notifications"
67
readme = "README.md"
78
keywords = ["systemd", "sd_notify"]

src/lib.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -486,13 +486,8 @@ pub fn watchdog_enabled() -> Option<Duration> {
486486
let p = env::var(WATCHDOG_PID)
487487
.ok()
488488
.and_then(|s| u32::from_str(&s).ok());
489-
490-
match (s, p) {
491-
(Some(usec_val), Some(pid)) if pid == process::id() => {
492-
Some(Duration::from_micros(usec_val))
493-
}
494-
_ => None,
495-
}
489+
s.filter(|_| p.is_none_or(|pid| pid == process::id()))
490+
.map(Duration::from_micros)
496491
}
497492

498493
/// Asks the service manager for enabled watchdog.

tests/watchdog_enabled.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,28 @@ fn watchdog_enabled() {
3535
assert!(env::var_os("WATCHDOG_USEC").is_none());
3636
assert!(env::var_os("WATCHDOG_PID").is_none());
3737

38-
// no usec, no pip no unset env
38+
// valid usec, no pid
39+
unsafe {
40+
env::set_var("WATCHDOG_USEC", "10");
41+
}
42+
43+
unsafe {
44+
assert_eq!(
45+
sd_notify::watchdog_enabled_and_unset_env(),
46+
Some(Duration::from_micros(10))
47+
);
48+
}
49+
50+
assert!(env::var_os("WATCHDOG_USEC").is_none());
51+
assert!(env::var_os("WATCHDOG_PID").is_none());
52+
53+
// no usec, no pid, no unset env
3954
assert_eq!(sd_notify::watchdog_enabled(), None);
4055

4156
assert!(env::var_os("WATCHDOG_USEC").is_none());
4257
assert!(env::var_os("WATCHDOG_PID").is_none());
4358

44-
// valid pip
59+
// valid pid
4560
unsafe {
4661
env::set_var("WATCHDOG_USEC", "5");
4762
env::set_var("WATCHDOG_PID", process::id().to_string());

0 commit comments

Comments
 (0)