From 21df56d546c43c600e339e6ad46a1cfbcfaa7f64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Mon, 9 Mar 2026 12:50:50 +0200 Subject: [PATCH] Fix booted to work as expected when systemd is not running --- CHANGELOG.md | 2 ++ src/lib.rs | 13 ++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1bbf1b..4d1c9a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ - the MSRV is now defined as 1.82 ### Fixed + + - fixed `booted` to return `Ok(false)` when not running under systemd - fixed `watchdog_enabled` to handle missing `WATCHDOG_PID` ## [0.4.5] - 2025-01-18 diff --git a/src/lib.rs b/src/lib.rs index ddc9fdc..03e0bd3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -131,11 +131,18 @@ impl NotifyState<'_> { /// # Example /// /// ```no_run -/// let _ = sd_notify::booted(); +/// match sd_notify::booted() { +/// Ok(true) => { println!("System is booted under systemd")}, +/// Ok(false) => { println!("System is not booted under systemd")}, +/// Err(e) => { println!("Error: {}", e)}, +/// } /// ``` pub fn booted() -> io::Result { - let m = fs::symlink_metadata("/run/systemd/system")?; - Ok(m.is_dir()) + match fs::symlink_metadata("/run/systemd/system") { + Ok(m) => Ok(m.is_dir()), + Err(e) if matches!(e.kind(), ErrorKind::NotFound) => Ok(false), + Err(e) => Err(e), + } } // Constants for env variable names so that we don't typo them.