chore: put systemd-homed extra support behind 'systemd' feature

This commit is contained in:
Nara Díaz Viñolas 2026-05-22 10:11:44 +02:00 • committed by Michael Murphy
parent 8ff96e94f7
commit b9587deadb
3 changed files with 26 additions and 10 deletions

View file

@ -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"]

View file

@ -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<Self, zbus::Error> {
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<BTreeSet<u32>, 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::<u32>().ok())
@ -46,7 +62,7 @@ impl UserFilter {
.and_then(|x| x.parse::<u32>().ok())
.unwrap_or(65000),
homed_uids,
})
}
}
pub fn filter(&self, user: &pwd::Passwd) -> bool {

View file

@ -71,7 +71,7 @@ struct GreeterProxy;
#[zbus::interface(name = "com.system76.CosmicGreeter")]
impl GreeterProxy {
async fn get_user_data(&mut self) -> Result<String, GreeterError> {
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