use std::{
io::{IsTerminal, stdin, stdout},
sync::OnceLock,
};
use crate::virt::{Evidence, Strength};
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Launch {
pub by: Launcher,
pub unit: Option<&'static str>,
pub notify_socket: bool,
pub evidence: Vec<Evidence>,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum Launcher {
Systemd,
Launchd,
WindowsService,
ContainerInit,
Init,
Terminal,
Ssh,
Cron,
Unknown,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct LaunchFacts {
pub(crate) service: Option<(Launcher, Option<String>, Strength)>,
pub(crate) is_pid1: bool,
pub(crate) notify_socket: bool,
pub(crate) ssh: bool,
pub(crate) cron: bool,
}
#[must_use]
pub fn launch() -> &'static Launch {
static FACTS: OnceLock<Launch> = OnceLock::new();
FACTS.get_or_init(|| {
decide(
crate::sys::launch(),
crate::virt::is_container(),
stdin().is_terminal() || stdout().is_terminal(),
)
})
}
#[must_use]
pub fn is_service() -> bool {
matches!(
launch().by,
Launcher::Systemd | Launcher::Launchd | Launcher::WindowsService | Launcher::ContainerInit
)
}
#[must_use]
pub fn is_interactive() -> bool {
stdin().is_terminal() && stdout().is_terminal()
}
pub(crate) fn decide(facts: LaunchFacts, in_container: bool, terminal: bool) -> Launch {
let mut evidence = Vec::new();
let mut note = |check, strength| evidence.push(Evidence { check, strength });
let mut unit = None;
let by = if let Some((by, name, strength)) = facts.service {
unit = name.map(crate::os::keep);
note("service manager", strength);
by
} else if facts.is_pid1 && in_container {
note("process id 1 in a container", Strength::Strong);
Launcher::ContainerInit
} else if facts.is_pid1 {
note("process id 1", Strength::Strong);
Launcher::Init
} else if facts.cron {
note("cron.service cgroup", Strength::Strong);
Launcher::Cron
} else if terminal && facts.ssh {
note("SSH_CONNECTION", Strength::Weak);
Launcher::Ssh
} else if terminal {
note("terminal", Strength::Strong);
Launcher::Terminal
} else if facts.ssh {
note("SSH_CONNECTION", Strength::Weak);
Launcher::Ssh
} else {
Launcher::Unknown
};
Launch {
by,
unit,
notify_socket: facts.notify_socket,
evidence,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_launch_order() {
let service = LaunchFacts {
service: Some((
Launcher::Systemd,
Some("site.service".into()),
Strength::Strong,
)),
notify_socket: true,
..LaunchFacts::default()
};
let l = decide(service, true, true);
assert_eq!(l.by, Launcher::Systemd);
assert_eq!(l.unit, Some("site.service"));
assert!(l.notify_socket);
let pid1 = LaunchFacts {
is_pid1: true,
..LaunchFacts::default()
};
assert_eq!(
decide(pid1.clone(), true, false).by,
Launcher::ContainerInit
);
assert_eq!(decide(pid1, false, false).by, Launcher::Init);
let cron = LaunchFacts {
cron: true,
..LaunchFacts::default()
};
assert_eq!(decide(cron, false, false).by, Launcher::Cron);
let ssh = LaunchFacts {
ssh: true,
..LaunchFacts::default()
};
let l = decide(ssh, false, true);
assert_eq!(l.by, Launcher::Ssh);
assert_eq!(l.evidence[0].strength, Strength::Weak);
assert_eq!(
decide(LaunchFacts::default(), false, true).by,
Launcher::Terminal
);
let l = decide(LaunchFacts::default(), false, false);
assert_eq!(l.by, Launcher::Unknown);
assert!(l.evidence.is_empty());
}
#[test]
fn test_launch_live_is_cached() {
assert!(std::ptr::eq(launch(), launch()));
let _ = (is_service(), is_interactive());
}
}