From 78f24453432f04ba9ce31cb14e2c0c27900f82ac Mon Sep 17 00:00:00 2001 From: ShootingStarDragons Date: Thu, 7 May 2026 16:24:48 +0900 Subject: [PATCH 1/2] feat: add get_display function for the ffi::sd_uid_get_display, create a safe binding for it --- src/login.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/login.rs b/src/login.rs index 7e20dff..b97da4a 100644 --- a/src/login.rs +++ b/src/login.rs @@ -92,6 +92,17 @@ pub fn get_session(pid: Option) -> Result { Ok(ss) } +/// Determines the session identifier of a .user +/// +/// Specific user can be optionally targeted via their UID. +/// This method can be used to retrieve a session identifier. +pub fn get_display(uid: uid_t) -> Result { + let mut c_session: *mut c_char = ptr::null_mut(); + ffi_result(unsafe { libsystemd_sys::login::sd_uid_get_display(uid, &mut c_session) })?; + let ss = unsafe { free_cstring(c_session).unwrap() }; + Ok(ss) +} + /// Determines the seat identifier of the seat the session identified /// by the specified session identifier belongs to. /// From d0ab7e897854a278c86e4a08dff308fd4982946b Mon Sep 17 00:00:00 2001 From: ShootingStarDragons Date: Thu, 7 May 2026 16:47:00 +0900 Subject: [PATCH 2/2] chore: add unit test --- tests/login.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/login.rs b/tests/login.rs index 3e27114..cb9fb61 100644 --- a/tests/login.rs +++ b/tests/login.rs @@ -103,6 +103,36 @@ fn test_get_owner_uid() { } } +#[test] +fn test_get_display() { + let ou = login::get_owner_uid(None); + let has_systemd = booted(); + assert!(has_systemd.is_ok()); + if has_systemd.unwrap() { + // Running under systemd, inside a slice somewhere + // even in this case, we might get a "no data available" (github actions runners return + // this) + if let Err(e) = ou { + match e.raw_os_error() { + Some(libc::ENODATA) => { /* ok */ } + _ => panic!("{}", e), + } + return; + } + let ou = ou.unwrap(); + let display = login::get_display(ou); + if let Err(e) = display { + match e.raw_os_error() { + Some(libc::ENODATA) => { /* ok */ } + _ => panic!("{}", e), + } + return; + } + } else { + // Nothing meaningful to check here + } +} + #[test] fn test_get_sessions() { let sessions = login::get_sessions();