use std::{
fs,
path::{Path, PathBuf},
process::{Command, Stdio},
thread,
time::Duration,
};
use allot_shared::{
autostart::{LAUNCHD_LABEL, launch_agent, launch_agent_file, launch_agent_program},
paths::Paths,
};
use anyhow::{Context, bail};
use super::{autostart::Found, read_text, write_atomic};
const BOOTSTRAP_TRIES: u32 = 5;
const BOOTSTRAP_RETRY: Duration = Duration::from_millis(200);
fn plist_file(paths: &Paths) -> PathBuf {
launch_agent_file(paths.home())
}
fn domain() -> String {
let uid = unsafe { libc::getuid() };
format!("gui/{uid}")
}
fn service() -> String {
format!("{}/{LAUNCHD_LABEL}", domain())
}
pub(super) fn title(paths: &Paths) -> String {
format!(
"allotd at login, LaunchAgent {}",
plist_file(paths).display()
)
}
pub(super) fn set_up(paths: &Paths, allotd: &Path) -> anyhow::Result<Option<String>> {
let file = plist_file(paths);
let plist = launch_agent(allotd);
let old = read_text(&file)?;
let rewritten = old.as_deref() != Some(plist.as_str());
let mut loaded = is_loaded();
if rewritten {
write_atomic(&file, &plist)?;
if loaded {
launchctl(&["bootout", &service()])?;
loaded = false;
}
}
let was_loaded = loaded;
if loaded {
launchctl(&["kickstart", &service()])?;
} else {
bootstrap(&file, rewritten && old.is_some())?;
}
Ok(if old.is_none() {
Some("registered and started".to_owned())
} else if rewritten {
Some(found(old.as_deref()).replaced_note(allotd, "updated and started"))
} else if !was_loaded {
Some("loaded and started".to_owned())
} else {
None
})
}
pub(super) fn manual_set_up(paths: &Paths, allotd: &Path) -> Vec<String> {
let file = plist_file(paths);
let plist = launch_agent(allotd);
let mut manual = Vec::new();
if read_text(&file).ok().flatten().as_deref() != Some(plist.as_str()) {
manual.push(format!("Write this to {}:", file.display()));
manual.extend(plist.lines().map(|line| format!(" {line}")));
}
manual.push(format!(
"launchctl bootstrap {} \"{}\"",
domain(),
file.display()
));
manual.push(format!("launchctl kickstart {}", service()));
manual
}
pub(super) fn remove(paths: &Paths, allotd: &Path) -> anyhow::Result<Option<String>> {
let file = plist_file(paths);
if !found(read_text(&file)?.as_deref()).starts(allotd) {
return Ok(None);
}
if is_loaded() {
launchctl(&["bootout", &service()])?;
}
fs::remove_file(&file).with_context(|| format!("deleting {}", file.display()))?;
Ok(Some(format!("removed the LaunchAgent {}", file.display())))
}
pub(super) fn manual_remove(paths: &Paths) -> String {
format!(
"launchctl bootout {}, then delete {}",
service(),
plist_file(paths).display()
)
}
pub(super) fn current(paths: &Paths) -> Found {
read_text(&plist_file(paths)).map_or(Found::Unknown, |plist| found(plist.as_deref()))
}
fn found(plist: Option<&str>) -> Found {
match plist {
None => Found::Missing,
Some(plist) => launch_agent_program(plist).map_or(Found::Unknown, Found::Runs),
}
}
fn bootstrap(file: &Path, retry: bool) -> anyhow::Result<()> {
let file = file.to_string_lossy();
let tries = if retry { BOOTSTRAP_TRIES } else { 1 };
let mut attempt = 1;
loop {
match launchctl(&["bootstrap", &domain(), &file]) {
Err(_) if attempt < tries => {
attempt += 1;
thread::sleep(BOOTSTRAP_RETRY);
}
result => return result,
}
}
}
fn is_loaded() -> bool {
Command::new("launchctl")
.args(["print", &service()])
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.is_ok_and(|status| status.success())
}
fn launchctl(args: &[&str]) -> anyhow::Result<()> {
let output = Command::new("launchctl")
.args(args)
.stdin(Stdio::null())
.output()
.context("running launchctl")?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
let stdout = String::from_utf8_lossy(&output.stdout);
let reason = if stderr.trim().is_empty() {
stdout
} else {
stderr
};
bail!("launchctl {} failed: {}", args.join(" "), reason.trim());
}
Ok(())
}