//! A boot session the app mints and keeps, for a platform whose sandbox //! refuses the kernel's own (`kern.bootsessionuuid` on a real iPhone is //! `Operation not permitted`; the simulator allows it, so nothing caught it). //! //! The id is kept in the app's data folder beside the two readings taken with //! it: the continuous clock and the wall clock. The continuous clock starts //! again at boot, so on the next read: //! //! - a continuous clock LOWER than the kept reading is a reboot; //! - a continuous clock that moved on is the same boot only if the boot //! instant (wall − continuous) is where it was, because a phone that //! rebooted and has since been up longer than the kept reading looks the //! same on the continuous clock alone. //! //! Anything else mints a new id. A wall clock set between two launches //! therefore also mints one, and that is the safe side: a new id only says //! "no judge time yet this boot" (Unchecked until the relay is heard), while //! an id kept across a reboot would date a held receipt by a continuous clock //! that started again. Inside one process the id is read once and held //! ([`super::boot_session`]), so a wall-clock set never moves a running judge. use std::path::Path; /// The file in the data folder: the id, the continuous reading and the wall /// reading, one per line. pub const FILE: &str = "boot-session"; /// How far the boot instant may drift between two reads of one boot (the /// wall clock is slewed and stepped by the network's time). A reboot moves it /// by at least the kept uptime plus the time the phone was off. pub const SAME_BOOT_SLACK_MS: i64 = 10_000; /// What the file keeps. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Kept { pub id: String, pub cont_ms: i64, pub wall_ms: i64, } impl Kept { /// The continuous clock has not gone back and the boot instant has not /// moved: `cont_ms` and `wall_ms` were read in the boot this was kept in. pub fn is_this_boot(&self, cont_ms: i64, wall_ms: i64) -> bool { let boot_instant_moved = (wall_ms - cont_ms) - (self.wall_ms - self.cont_ms); cont_ms >= self.cont_ms && boot_instant_moved.abs() <= SAME_BOOT_SLACK_MS } fn parse(text: &str) -> Option { let mut lines = text.lines(); let id = lines.next()?.trim(); let cont_ms = lines.next()?.trim().parse().ok()?; let wall_ms = lines.next()?.trim().parse().ok()?; (!id.is_empty()).then(|| Kept { id: id.to_string(), cont_ms, wall_ms, }) } } /// A new id: 16 random bytes, hex. pub fn fresh_id() -> String { rand::random::<[u8; 16]>() .iter() .map(|byte| format!("{byte:02x}")) .collect() } /// The boot session at continuous time `cont_ms` and wall time `wall_ms`: /// the kept id when `file` was written in this boot, else one from `mint`. /// The readings are kept again either way. A file that cannot be read or /// written is an error, never an id nobody kept. pub fn session( file: &Path, cont_ms: i64, wall_ms: i64, mint: impl FnOnce() -> String, ) -> Result { let kept = match std::fs::read_to_string(file) { // A file that does not parse names no boot, so no boot is this one. Ok(text) => Kept::parse(&text), Err(error) if error.kind() == std::io::ErrorKind::NotFound => None, Err(error) => { return Err(format!( "the kept boot session {} is unreadable: {error}", file.display() )) } }; let id = match kept { Some(kept) if kept.is_this_boot(cont_ms, wall_ms) => kept.id, _ => mint(), }; // Written whole beside the file and renamed over it, so another process // of the app never reads half of one. let beside = file.with_extension(format!("{}.tmp", std::process::id())); // not a copy: the boot mark. std::fs::write(&beside, format!("{id}\n{cont_ms}\n{wall_ms}\n")) // not a copy: the boot mark. .and_then(|()| std::fs::rename(&beside, file)) .map_err(|error| { format!( "the boot session cannot be kept in {}: {error}", file.display() ) })?; Ok(id) } #[cfg(test)] #[path = "judge_minted_boot_tests.rs"] mod tests;