From b9587deadb088db5c9afb84003af364268b41da0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nara=20D=C3=ADaz=20Vi=C3=B1olas?= Date: Fri, 22 May 2026 10:11:44 +0200 Subject: [PATCH] chore: put systemd-homed extra support behind 'systemd' feature --- daemon/Cargo.toml | 4 ++-- daemon/src/lib.rs | 30 +++++++++++++++++++++++------- daemon/src/main.rs | 2 +- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/daemon/Cargo.toml b/daemon/Cargo.toml index 44daa5f..000ad41 100644 --- a/daemon/Cargo.toml +++ b/daemon/Cargo.toml @@ -28,8 +28,8 @@ whitespace-conf = "1" #TODO: reduce features tokio = { workspace = true, features = ["full"] } xdg = "3.0" -zbus_systemd = { version = "0.26000.0", features = ["home1"] } +zbus_systemd = { version = "0.26000.0", features = ["home1"], optional = true } [features] default = ["systemd"] -systemd = ["tracing-journald"] +systemd = ["tracing-journald", "zbus_systemd"] diff --git a/daemon/src/lib.rs b/daemon/src/lib.rs index 23e36e5..c414474 100644 --- a/daemon/src/lib.rs +++ b/daemon/src/lib.rs @@ -7,7 +7,6 @@ use std::io::Read; use std::os::unix::fs::OpenOptionsExt; use std::path::{Path, PathBuf}; use zbus::Connection; -use zbus_systemd::home1::ManagerProxy; pub use cosmic_applets_config::time::TimeAppletConfig; pub use cosmic_bg_config::state::State as BgState; @@ -22,12 +21,12 @@ pub struct UserFilter { } impl UserFilter { - pub async fn new() -> Result { - let login_defs_data = fs::read_to_string("/etc/login.defs").unwrap_or_default(); - let login_defs = whitespace_conf::parse(&login_defs_data); + #[cfg(feature = "systemd")] + async fn get_homed_uids() -> Result, zbus::Error> { + use zbus_systemd::home1; let connection = Connection::system().await?; - let homed = ManagerProxy::new(&connection).await?; + let homed = home1::ManagerProxy::new(&connection).await?; let homed_uids = homed .list_homes() @@ -36,7 +35,24 @@ impl UserFilter { .map(|(_, uid, ..)| *uid) .collect(); - Ok(Self { + Ok(homed_uids) + } + + pub async fn new() -> Self { + let login_defs_data = fs::read_to_string("/etc/login.defs").unwrap_or_default(); + let login_defs = whitespace_conf::parse(&login_defs_data); + + #[cfg(feature = "systemd")] + let homed_uids = Self::get_homed_uids() + .await + .inspect_err(|e| { + tracing::warn!("failed to list dynamic UIDs from systemd-homed: {e:?}") + }) + .unwrap_or_default(); + #[cfg(not(feature = "systemd"))] + let homed_uids = BTreeSet::new(); + + Self { uid_min: login_defs .get("UID_MIN") .and_then(|x| x.parse::().ok()) @@ -46,7 +62,7 @@ impl UserFilter { .and_then(|x| x.parse::().ok()) .unwrap_or(65000), homed_uids, - }) + } } pub fn filter(&self, user: &pwd::Passwd) -> bool { diff --git a/daemon/src/main.rs b/daemon/src/main.rs index a9d35b9..419a20d 100644 --- a/daemon/src/main.rs +++ b/daemon/src/main.rs @@ -71,7 +71,7 @@ struct GreeterProxy; #[zbus::interface(name = "com.system76.CosmicGreeter")] impl GreeterProxy { async fn get_user_data(&mut self) -> Result { - let user_filter = UserFilter::new().await?; + let user_filter = UserFilter::new().await; // The pwd::Passwd method is unsafe (but not labelled as such) due to using global state (libc pwent functions). // To prevent issues, this should only be called once in the entire process space at a time