Simplify is_systemd_used

This commit is contained in:
Daniel Huang 2024-08-21 13:14:36 -04:00 committed by GitHub
parent c3de3d24dd
commit 4df7261a6c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only
use std::path::Path;
use std::process::{Command, Stdio};
use std::sync::OnceLock;
@ -33,21 +34,7 @@ pub fn stop_systemd_target() {
///Determine if systemd is used as the init system. This should work on all linux distributions.
pub fn is_systemd_used() -> &'static bool {
static IS_SYSTEMD_USED: OnceLock<bool> = OnceLock::new();
IS_SYSTEMD_USED.get_or_init(|| {
match Command::new("ls").args(["/run/systemd/system"]).output() {
Ok(output) => {
if output.status.success() {
true
} else {
false
}
}
Err(error) => {
warn!("unable to check if systemd is used: {}", error);
false
}
}
})
IS_SYSTEMD_USED.get_or_init(|| Path::new("/run/systemd/system").exists())
}
#[cfg(feature = "systemd")]