use crate::daemon::login_session::LoginSession;
#[cfg(target_os = "macos")]
pub(crate) fn login_session() -> Option<LoginSession> {
#[link(name = "Security", kind = "framework")]
extern "C" {
fn SessionGetInfo(session: u32, session_id: *mut u32, attributes: *mut u32) -> i32;
}
const CALLER_SECURITY_SESSION: u32 = u32::MAX;
const SESSION_HAS_GRAPHIC_ACCESS: u32 = 0x0010;
let (mut session_id, mut attributes) = (0, 0);
let status =
unsafe { SessionGetInfo(CALLER_SECURITY_SESSION, &mut session_id, &mut attributes) };
if status != 0 {
tracing::debug!(status, "SessionGetInfo failed");
return None;
}
Some(if attributes & SESSION_HAS_GRAPHIC_ACCESS != 0 {
LoginSession::Gui
} else {
LoginSession::Background
})
}
#[cfg(not(target_os = "macos"))]
pub(crate) fn login_session() -> Option<LoginSession> {
None
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Placement {
ClientSession,
LaunchAgent,
}
pub(super) fn placement(
client: Option<LoginSession>,
gui_login: impl FnOnce() -> bool,
) -> Placement {
match client {
Some(LoginSession::Background) if gui_login() => Placement::LaunchAgent,
Some(LoginSession::Background | LoginSession::Gui) | None => Placement::ClientSession,
}
}
pub(super) fn new_daemon_placement() -> (Placement, Option<LoginSession>) {
let client = login_session();
(placement(client, exists), client)
}
pub(crate) fn session_of_new_daemon() -> Option<LoginSession> {
match new_daemon_placement() {
(Placement::LaunchAgent, _) => Some(LoginSession::Gui),
(Placement::ClientSession, client) => client,
}
}
fn exists() -> bool {
#[cfg(target_os = "macos")]
{
let domain = super::service::launchd_domain();
super::service::run_manager_command("launchctl", &["print", &domain]).is_ok()
}
#[cfg(not(target_os = "macos"))]
{
false
}
}
#[cfg(test)]
#[path = "gui_login_tests.rs"]
mod tests;