rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The user's GUI login on macOS: which login session this process runs in,
//! and whether a daemon a client starts goes through the home's LaunchAgent
//! ([`super::launch_agent`]) to run in a GUI login the client is outside
//! of. Elsewhere there is one kind of session, and a client starts the
//! daemon where it is.

use crate::daemon::login_session::LoginSession;

/// This process's login session, from its security session's attributes:
/// one with GUI access is the GUI login's. `None` where the OS has one kind
/// of session (not macOS), or does not say.
#[cfg(target_os = "macos")]
pub(crate) fn login_session() -> Option<LoginSession> {
    // `Security/AuthSession.h`.
    #[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);
    // SAFETY: both out-pointers are valid for writes for the call's
    // duration, and the call keeps neither.
    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
}

/// Where a daemon a client starts runs.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Placement {
    /// A detached child of the client, in the client's own login session.
    ClientSession,
    /// The home's LaunchAgent, in the user's GUI login.
    LaunchAgent,
}

/// Where a client in login session `client` starts the daemon: through the
/// LaunchAgent when the client is outside the GUI login and `gui_login`
/// says the user has one now. A client in the GUI login starts it there by
/// itself, and one with no GUI login to reach starts it where it is.
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,
    }
}

/// Where a daemon started from this process runs, with this process's own
/// login session: what `spawn_detached` does, and what
/// [`session_of_new_daemon`] predicts.
pub(super) fn new_daemon_placement() -> (Placement, Option<LoginSession>) {
    let client = login_session();
    (placement(client, exists), client)
}

/// The login session a daemon started from this process would run in.
pub(crate) fn session_of_new_daemon() -> Option<LoginSession> {
    match new_daemon_placement() {
        (Placement::LaunchAgent, _) => Some(LoginSession::Gui),
        (Placement::ClientSession, client) => client,
    }
}

/// Whether this user is logged in at the screen now: launchd has their GUI
/// login's domain only then.
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;