Skip to content

Commit 4c4feae

Browse files
authored
Merge pull request #21 from lnicola/booted
Fix `booted` to work as expected when systemd is not running
2 parents f0a0238 + 21df56d commit 4c4feae

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
- the MSRV is now defined as 1.82
1010

1111
### Fixed
12+
13+
- fixed `booted` to return `Ok(false)` when not running under systemd
1214
- fixed `watchdog_enabled` to handle missing `WATCHDOG_PID`
1315

1416
## [0.4.5] - 2025-01-18

src/lib.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,18 @@ impl NotifyState<'_> {
131131
/// # Example
132132
///
133133
/// ```no_run
134-
/// let _ = sd_notify::booted();
134+
/// match sd_notify::booted() {
135+
/// Ok(true) => { println!("System is booted under systemd")},
136+
/// Ok(false) => { println!("System is not booted under systemd")},
137+
/// Err(e) => { println!("Error: {}", e)},
138+
/// }
135139
/// ```
136140
pub fn booted() -> io::Result<bool> {
137-
let m = fs::symlink_metadata("/run/systemd/system")?;
138-
Ok(m.is_dir())
141+
match fs::symlink_metadata("/run/systemd/system") {
142+
Ok(m) => Ok(m.is_dir()),
143+
Err(e) if matches!(e.kind(), ErrorKind::NotFound) => Ok(false),
144+
Err(e) => Err(e),
145+
}
139146
}
140147

141148
// Constants for env variable names so that we don't typo them.

0 commit comments

Comments
 (0)