Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ pub fn get_session(pid: Option<pid_t>) -> Result<String> {
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<String> {
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.
///
Expand Down
30 changes: 30 additions & 0 deletions tests/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down