use std::ffi::OsString;
pub const VAR: &str = "TILO_MACHINE";
pub const DAEMON_SOCKETS: &str = "/run/tilo-runtime";
pub const DAEMON_NATIVE: &str = "native.sock";
pub const DAEMON_PROFILE_VAR: &str = "TILO_RUNTIME_PROFILE";
#[must_use]
pub fn account_daemon_profile() -> Option<std::path::PathBuf> {
#[cfg(target_os = "macos")]
let suffix = "Library/Application Support/Tilo/runtime";
#[cfg(target_os = "windows")]
let suffix = "AppData/Local/Tilo/runtime";
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
let suffix = ".local/state/tilo/runtime";
Some(super::home()?.join(suffix))
}
pub const DAEMON_SYSTEM_UNIT: &str = "/usr/lib/systemd/system/tilo-runtime.service";
pub const DAEMON_SYSTEM_PROFILE: &str = "/var/lib/tilo/state";
#[must_use]
pub fn serves_the_computer(system: bool, named: Option<&std::path::Path>) -> bool {
system && named.is_none_or(|profile| profile == std::path::Path::new(DAEMON_SYSTEM_PROFILE))
}
#[must_use]
pub fn system_daemon() -> bool {
cfg!(target_os = "linux") && std::path::Path::new(DAEMON_SYSTEM_UNIT).is_file()
}
pub const HOSTED_GUEST_INIT: &str = "/usr/libexec/tilo/hosted-init";
#[must_use]
pub fn hosted_guest() -> bool {
cfg!(target_os = "linux") && std::path::Path::new(HOSTED_GUEST_INIT).is_file()
}
#[cfg(unix)]
pub fn daemon_sockets(uid: u32) -> Result<std::path::PathBuf, String> {
use std::path::PathBuf;
if system_daemon() {
return Ok(PathBuf::from(DAEMON_SOCKETS));
}
account_sockets(uid)
}
#[cfg(unix)]
pub fn account_sockets(uid: u32) -> Result<std::path::PathBuf, String> {
use std::path::PathBuf;
#[cfg(target_os = "macos")]
{
let _ = uid;
Ok(darwin_temp_dir()?.join("tilo"))
}
#[cfg(not(target_os = "macos"))]
Ok(crate::var("XDG_RUNTIME_DIR")
.map(PathBuf::from)
.filter(|runtime| runtime.is_absolute() && runtime.is_dir())
.or_else(|| Some(PathBuf::from(format!("/run/user/{uid}"))).filter(|run| run.is_dir()))
.map_or_else(
|| std::env::temp_dir().join(format!("tilo-{uid}")),
|runtime| runtime.join("tilo"),
))
}
#[cfg(unix)]
pub fn daemon_native() -> Result<std::path::PathBuf, String> {
Ok(daemon_sockets(rustix::process::geteuid().as_raw())?.join(DAEMON_NATIVE))
}
#[cfg(windows)]
pub fn daemon_sockets() -> Result<std::path::PathBuf, String> {
account_daemon_profile()
.map(|profile| profile.join("sockets"))
.ok_or_else(|| "this account names no home folder (USERPROFILE)".to_owned())
}
#[cfg(windows)]
pub fn daemon_native() -> Result<std::path::PathBuf, String> {
Ok(daemon_sockets()?.join(DAEMON_NATIVE))
}
#[cfg(target_os = "macos")]
fn darwin_temp_dir() -> Result<std::path::PathBuf, String> {
use std::os::unix::ffi::OsStringExt as _;
let mut buf = vec![0_u8; 1024];
let len = unsafe {
libc::confstr(
libc::_CS_DARWIN_USER_TEMP_DIR,
buf.as_mut_ptr().cast(),
buf.len(),
)
};
if len == 0 || len > buf.len() {
return Err(format!(
"this account has no temporary directory: {}",
std::io::Error::last_os_error()
));
}
buf.truncate(len.saturating_sub(1));
Ok(std::path::PathBuf::from(std::ffi::OsString::from_vec(buf)))
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Reach {
Owned,
Hosted,
}
impl Reach {
#[must_use]
pub fn named(value: Option<&OsString>) -> Self {
match value.and_then(|value| value.to_str()).map(str::trim) {
Some("hosted") => Self::Hosted,
_ => Self::Owned,
}
}
#[must_use]
pub const fn is_owned(self) -> bool {
matches!(self, Self::Owned)
}
}
#[must_use]
pub fn reach() -> Reach {
Reach::named(crate::var(VAR).as_ref())
}
#[must_use]
pub fn owns_the_machine() -> bool {
reach().is_owned()
}
#[cfg(test)]
mod tests {
use super::*;
fn value(text: &str) -> OsString {
OsString::from(text)
}
#[test]
fn a_session_that_says_nothing_owns_its_machine() {
assert_eq!(Reach::named(None), Reach::Owned);
assert_eq!(Reach::named(Some(&value(""))), Reach::Owned);
assert_eq!(Reach::named(Some(&value("owned"))), Reach::Owned);
assert_eq!(Reach::named(Some(&value("something else"))), Reach::Owned);
}
#[test]
fn a_hosted_session_reaches_no_machine_of_its_own() {
assert_eq!(Reach::named(Some(&value("hosted"))), Reach::Hosted);
assert_eq!(Reach::named(Some(&value(" hosted "))), Reach::Hosted);
assert!(!Reach::Hosted.is_owned());
assert!(Reach::Owned.is_owned());
}
}