diff options
Diffstat (limited to 'src/dbus.rs')
-rw-r--r-- | src/dbus.rs | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/src/dbus.rs b/src/dbus.rs index 8fd2302..d23c7b6 100644 --- a/src/dbus.rs +++ b/src/dbus.rs @@ -1,7 +1,7 @@ use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; use zbus::{ - proxy, + proxy, Proxy, zvariant::{OwnedObjectPath, Type}, Connection, connection::Builder, address::{Address, transport::{Unix, Transport, UnixSocket}}, }; @@ -31,7 +31,7 @@ struct ServiceStatus { )] trait Manager { async fn list_units(&self) -> Result<Vec<ServiceStatus>>; - async fn get_unit(&self, unit_name: String) -> Result<OwnedObjectPath>; + async fn get_unit(&self, unit_name: &str) -> Result<OwnedObjectPath>; } async fn ensure_system_conn() -> Result<Arc<Connection>> { @@ -90,3 +90,26 @@ pub(crate) async fn check_systemd_all(user: Option<&str>) -> Result<String> { Err(anyhow!("Bad services: {}", bad.join(", "))) } } + +pub(crate) async fn check_systemd_unit(user: Option<&str>, unit: &str) -> Result<String> { + let conn = match user { + None => ensure_system_conn().await, + Some(username) => ensure_user_conn(username).await, + }?; + + let manager = ManagerProxy::new(&conn).await?; + let unit_object_path = if unit.contains('.') { + manager.get_unit(unit).await + } else { + manager.get_unit(&format!("{unit}.service")).await + }?; + + let unit_proxy = Proxy::new(&conn, "org.freedesktop.systemd1", unit_object_path, "org.freedesktop.systemd1.Unit").await?; + + let substate: String = unit_proxy.get_property("SubState").await?; + if matches!(substate.as_str(), "active" | "inactive" | "plugged" | "mounted" | "dead" | "listening" | "running" | "exited" | "waiting" | "abandoned" | "elapsed") { + Ok(format!("{unit}: {substate}")) + } else { + Err(anyhow!("{unit}: {substate}")) + } +} |