Files
rust-discovery/crates/updater-linux/src/lib.rs
T
2026-11-16 15:25:46 +01:00

92 lines
3.0 KiB
Rust

//! Linux OS primitives for the separately provisioned stable updater.
//!
//! These are not an approval, executable daemon or update capability. Every
//! mutating primitive requires the embedding worker's durable effect fence;
//! lost replies never resume an operation. No process-name/PID-only fallback.
#![cfg(target_os = "linux")]
pub mod files;
pub mod ipc;
pub mod process;
pub mod services;
use anyhow::{Result, ensure};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::time::{Duration, Instant};
use uuid::Uuid;
/// One original handoff's local deadline. Constructed after independent live
/// approval/clock/signature/boot/participant checks; possession is no authority.
pub struct Budget {
boot_id: Uuid,
until: Instant,
}
impl Budget {
pub fn new(boot_id: Uuid, remaining: Duration) -> Result<Self> {
ensure!(
!boot_id.is_nil() && !remaining.is_zero() && remaining <= Duration::from_secs(600),
"UPDATE_BUDGET"
);
let result = Self {
boot_id,
until: Instant::now()
.checked_add(remaining)
.ok_or_else(|| anyhow::anyhow!("UPDATE_BUDGET"))?,
};
result.check()?;
Ok(result)
}
pub fn check(&self) -> Result<()> {
ensure!(Instant::now() < self.until, "UPDATE_DEADLINE");
ensure!(
std::fs::read_to_string("/proc/sys/kernel/random/boot_id")?
.trim()
.parse::<Uuid>()?
== self.boot_id,
"UPDATE_BOOT_CHANGED"
);
Ok(())
}
pub(crate) fn remaining(&self) -> Result<Duration> {
self.check()?;
let remaining = self.until.saturating_duration_since(Instant::now());
ensure!(!remaining.is_zero(), "UPDATE_DEADLINE");
Ok(remaining)
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "effect", rename_all = "snake_case", deny_unknown_fields)]
pub enum Effect {
StopService { service: services::Service },
SavePredecessor { role: files::Role },
PublishReplacement { role: files::Role },
StartService { service: services::Service },
RestorePredecessor { role: files::Role },
SaveRejectedCandidate { role: files::Role },
ReturnLedger,
}
/// MUST perform fresh original-handoff, owner, clock, boot and retained-file
/// checks and COMMIT a create-once intent before returning. No permissive
/// implementation is provided. Effects are in addition to the core ledger's
/// typed event sequence; they never create/replace the original consumption.
pub trait DurableEffectFence {
fn before_effect(&mut self, effect: &Effect) -> Result<()>;
fn after_effect(&mut self, effect: &Effect, observation_sha256: &str) -> Result<()>;
}
pub(crate) fn digest(bytes: &[u8]) -> String {
hex::encode(Sha256::digest(bytes))
}
pub(crate) fn valid_digest(value: &str) -> bool {
value.len() == 64
&& value
.bytes()
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
}
#[cfg(test)]
mod tests;