//! The one existing SQLite file, opened without CREATE only after the real //! OS guard confirms every reader stopped. This module performs no bootstrap, //! consumption, schema reset, outcome healing or readiness transition. use crate::{ clock::NativeClock, files::FileWitnesses, original::Original, participants::StableParticipant, retained::RetainedSet, returned::PreparedReservation, trust::TrustState, }; use anyhow::{Result, ensure}; use rmm_agent_core::{ auth::Binding, helper_reconcile::ExecutorLedgerGuard, reconcile::SyncClock, update_handoff::{ self, NativeEffect, NativeEffectClaim, NativeEffectStage, NativeFileRole, Participant, UpdateParticipantGuard, }, }; use rmm_domain::agent_update::UpdateDelegation; use rmm_domain::jobs::hash; use rmm_updater_handoff::{Event, Evidence, Handoff, Journal, Phase}; use rmm_updater_linux::services::StoppedServices; use rmm_updater_linux::{Budget, files::LedgerGuard}; use rusqlite::{Connection, OpenFlags}; use std::time::Duration; fn observation_budget() -> Result { let boot = std::fs::read_to_string("/proc/sys/kernel/random/boot_id")? .trim() .parse()?; Budget::new(boot, Duration::from_secs(5)) } const LEDGER: &str = "/var/lib/rmm-helper/consumption.sqlite"; fn file_role(role: NativeFileRole) -> rmm_updater_handoff::FileRole { match role { NativeFileRole::AgentBinary => rmm_updater_handoff::FileRole::AgentBinary, NativeFileRole::HelperBinary => rmm_updater_handoff::FileRole::HelperBinary, NativeFileRole::FullConfig => rmm_updater_handoff::FileRole::LinuxFullConfig, NativeFileRole::FullPin => rmm_updater_handoff::FileRole::LinuxFullPin, } } pub struct OwnedGuard<'a, 'stopped> { os: &'a LedgerGuard<'stopped>, original: &'a Original, binding: &'a Binding, clock: &'a NativeClock, budget: &'a Budget, updater: &'a StableParticipant, retained: &'a RetainedSet, files: &'a FileWitnesses, stopped: &'a StoppedServices, trust: &'a TrustState, sqlite_handle: usize, } pub struct OwnedInputs<'a, 'stopped> { pub os: &'a LedgerGuard<'stopped>, pub original: &'a Original, pub binding: &'a Binding, pub clock: &'a NativeClock, pub budget: &'a Budget, pub updater: &'a StableParticipant, pub retained: &'a RetainedSet, pub files: &'a FileWitnesses, pub stopped: &'a StoppedServices, pub trust: &'a TrustState, } pub struct OwnedLedger<'a, 'stopped> { db: Connection, guard: OwnedGuard<'a, 'stopped>, } impl<'a, 'stopped> OwnedLedger<'a, 'stopped> { pub fn open(input: OwnedInputs<'a, 'stopped>) -> Result { input.os.verify_for_effect(&observation_budget()?)?; ensure!( input.os.identity().sha256()? == input.original.handoff().ledger_identity_sha256 && input.retained.sha256() == input.original.handoff().retained_state_sha256, "UPDATE_OWNED_LEDGER_OR_STATE_BINDING" ); let db = Connection::open_with_flags( LEDGER, OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_NO_MUTEX, )?; db.busy_timeout(Duration::ZERO)?; // Per-connection durability/finite capacity, preserving DELETE journal // mode, all existing schema/rows, and the already held file identities. let mode: String = db.query_row("PRAGMA journal_mode", [], |r| r.get(0))?; let sync: u32 = db.query_row("PRAGMA synchronous", [], |r| r.get(0))?; ensure!( mode == "delete" && sync >= 2, "UPDATE_LEDGER_DURABILITY_REQUIRED" ); input.files.bind(input.original.handoff())?; let guard = OwnedGuard { os: input.os, original: input.original, binding: input.binding, clock: input.clock, budget: input.budget, updater: input.updater, retained: input.retained, files: input.files, stopped: input.stopped, trust: input.trust, sqlite_handle: unsafe { db.handle() } as usize, }; guard.check(&db)?; // Replay the complete original retained chain and its durable head. update_handoff::read(&db, &guard, input.original.permit().grant_id)?; db.execute_batch("PRAGMA foreign_keys=ON")?; ensure!( db.query_row("PRAGMA foreign_keys", [], |r| r.get::<_, bool>(0))?, "UPDATE_LEDGER_FOREIGN_KEYS_REQUIRED" ); rmm_agent_core::ledger::bounded_journal_capacity(&db)?; Ok(Self { db, guard }) } pub fn journal(&self) -> Result { update_handoff::read(&self.db, &self.guard, self.guard.original.permit().grant_id) } pub fn qualify_for_effects(&self) -> Result<()> { self.guard.check(&self.db)?; self.guard.original.check_current( self.guard.binding, self.guard.clock, self.guard.budget, )?; self.guard .trust .recheck(self.guard.original, self.guard.clock)?; self.guard .trust .qualify_history(&self.db, &self.guard, self.guard.binding) } pub fn mark_unknown( &mut self, observed_at: u64, reason: rmm_updater_handoff::UnknownReason, ) -> Result<()> { let journal = self.journal()?; update_handoff::mark_unknown( &mut self.db, &self.guard, self.guard.original.permit(), journal.head_sha256(), observed_at, reason, ) } pub fn append_observed(&mut self, evidence: Evidence) -> Result<()> { let journal = self.journal()?; let event = journal.prepare( u64::try_from(self.guard.clock.now()?.timestamp())?, evidence, )?; update_handoff::append( &mut self.db, &self.guard, self.guard.original.permit().grant_id, journal.head_sha256(), &event, ) } pub fn claim_native(&mut self, effect: &NativeEffect) -> Result { let journal = self.journal()?; update_handoff::claim_native_effect( &mut self.db, &self.guard, self.guard.original.permit().grant_id, journal.head_sha256(), effect, self.guard.clock.now()?, ) } pub fn observe_native(&mut self, claim: &NativeEffectClaim, observation: &str) -> Result<()> { let expected = match claim.effect() { NativeEffect::StopService { service } => { let role = match service { update_handoff::NativeService::Agent => { rmm_updater_linux::services::Service::Agent } update_handoff::NativeService::General => { rmm_updater_linux::services::Service::General } update_handoff::NativeService::Full => { rmm_updater_linux::services::Service::Full } }; let proof = self .guard .stopped .proofs() .into_iter() .find(|p| p.service == role) .ok_or_else(|| anyhow::anyhow!("UPDATE_STOP_WITNESS_MISSING"))?; hash(&serde_json::to_vec(&proof)?) } NativeEffect::SavePredecessor { role } => { self.native_observation_sha256(file_role(*role), false)? } NativeEffect::PublishReplacement { role } => { self.native_observation_sha256(file_role(*role), true)? } _ => anyhow::bail!("UPDATE_OWNED_OBSERVATION_NOT_ALLOWED"), }; ensure!(expected == observation, "UPDATE_NATIVE_OBSERVATION_CHANGED"); update_handoff::observe_native_effect( &mut self.db, &self.guard, claim, observation, self.guard.clock.now()?, ) } pub fn native_observation_sha256( &self, role: rmm_updater_handoff::FileRole, replaced: bool, ) -> Result { self.guard.files.native_observation_sha256(role, replaced) } pub fn target_sha256(&self, role: rmm_updater_handoff::FileRole) -> Result { self.guard .original .handoff() .files .iter() .find(|f| f.role == role) .map(|f| f.new_sha256.clone()) .ok_or_else(|| anyhow::anyhow!("UPDATE_TARGET_ROLE")) } pub fn verify_pair(&self) -> Result<()> { self.guard.files.verify_pair(self.guard.budget) } pub fn inflight_proof(&self) -> Result { let journal = self.journal()?; let mut statement = self.db.prepare( "SELECT event FROM agent_update_handoff_events WHERE grant_id=?1 ORDER BY sequence", )?; let events = statement .query_map([self.guard.original.permit().grant_id.to_string()], |r| { r.get(0) })? .collect::>>>()?; rmm_domain::agent_update::UpdateInflightProof::from_raw( &self.guard.original.handoff().bytes()?, self.guard.original.delegation().clone(), &events, journal.head_sha256().into(), ) .map_err(anyhow::Error::msg) } pub fn prepare_return_and_close(mut self) -> Result { let journal = self.journal()?; ensure!( journal.phase() == Phase::PairVerified, "UPDATE_RETURN_REQUIRES_PAIR_VERIFIED" ); let now = self.guard.clock.now()?; let event = journal.prepare( u64::try_from(now.timestamp())?, Evidence::LedgerReturnIntent, )?; let claims = update_handoff::prepare_return_and_starts( &mut self.db, &self.guard, self.guard.original.permit().grant_id, journal.head_sha256(), &event, now, )?; let proof = self.inflight_proof()?; let ledger = self.guard.os.identity().clone(); let old_services = self.guard.stopped.proofs(); self.close()?; Ok(PreparedReservation { claims, proof, ledger, old_services, }) } /// Close the actual SQLite connection BEFORE returning the OS flock. An /// open/failed-close connection never produces a released-ledger witness. pub fn close(self) -> Result<()> { self.guard.check(&self.db)?; self.db.close().map_err(|(_, e)| e)?; self.guard.os.verify_for_effect(&observation_budget()?) } } impl ExecutorLedgerGuard for OwnedGuard<'_, '_> { fn check(&self, db: &Connection) -> Result<()> { let observation = observation_budget()?; self.os.verify_for_effect(&observation)?; ensure!( unsafe { db.handle() } as usize == self.sqlite_handle, "UPDATE_SQLITE_CONNECTION_CHANGED" ); let path: String = db.query_row( "SELECT file FROM pragma_database_list WHERE name='main'", [], |r| r.get(0), )?; ensure!(path == LEDGER, "UPDATE_SQLITE_PATH_CHANGED"); Original::verify_retained( self.binding, self.original.start_bytes(), &self.original.handoff().bytes()?, self.original.delegation().clone(), )?; self.trust.recheck_protected()?; self.trust.recheck_original_binding(self.original)?; self.updater .verify_delegation(self.original.delegation(), &observation)?; self.retained.verify()?; let grant = self.original.permit().grant_id.to_string(); let start: Vec = db.query_row( "SELECT start FROM execution_evidence WHERE grant_id=?1", [&grant], |r| r.get(0), )?; ensure!( start == self.original.start_bytes(), "UPDATE_RETAINED_START_CHANGED" ); let unresolved: u64 = db.query_row( "SELECT count(*) FROM consumed WHERE result IS NULL AND grant_id<>?1", [grant], |r| r.get(0), )?; ensure!(unresolved == 0, "UPDATE_OTHER_UNRESOLVED_ATTEMPT"); self.os.verify_for_effect(&observation) } fn check_update_owner( &self, handoff: &Handoff, delegation: &UpdateDelegation, phase: Phase, _events: &[Event], ) -> Result<()> { ensure!( !matches!( phase, Phase::HandoffConsumed | Phase::ServicesStarted | Phase::HealthVerified | Phase::OutcomePersisted | Phase::Unknown ) && handoff.bytes()? == self.original.handoff().bytes()? && delegation == self.original.delegation(), "UPDATE_OWNED_WRITER_CONTEXT_CHANGED" ); self.original .check_current(self.binding, self.clock, self.budget)?; self.trust.recheck(self.original, self.clock)?; self.os.verify_for_effect(self.budget)?; self.updater.verify_delegation(delegation, self.budget) } } impl UpdateParticipantGuard for OwnedGuard<'_, '_> { fn participant(&self) -> Participant { Participant::StableUpdater } fn verify_update_event( &self, handoff: &Handoff, delegation: &UpdateDelegation, event: &Event, ) -> Result<()> { ensure!( handoff.bytes()? == self.original.handoff().bytes()? && delegation == self.original.delegation(), "UPDATE_EVENT_ORIGINAL_CHANGED" ); if matches!(event.evidence, Evidence::Unknown { .. }) { let observation = observation_budget()?; self.os.verify_for_effect(&observation)?; self.updater.verify_delegation(delegation, &observation)?; self.trust.recheck_protected()?; self.retained.verify()?; return Ok(()); } self.original .check_current(self.binding, self.clock, self.budget)?; self.trust.recheck(self.original, self.clock)?; self.updater.verify_delegation(delegation, self.budget)?; self.os.verify_for_effect(self.budget)?; self.retained.verify()?; match &event.evidence { Evidence::AllStopped { observation_sha256 } => { self.stopped.verify_still_stopped(self.budget)?; ensure!( *observation_sha256 == hash(&serde_json::to_vec(&self.stopped.proofs())?), "UPDATE_ALL_STOPPED_OBSERVATION" ); } Evidence::UpdaterOwnsLedger { process, ledger_identity_sha256, } => ensure!( process.pid == delegation.updater.pid && process.birth == delegation.updater.birth && process.image_sha256 == delegation.updater.image_sha256 && *ledger_identity_sha256 == self.os.identity().sha256()?, "UPDATE_OWNER_EVENT_OBSERVATION" ), Evidence::ReplaceIntent { role } => { self.files.verify_original_and_stage(*role, self.budget)? } Evidence::Replaced { role, observed_sha256, } => { self.files.verify_replaced(*role, self.budget)?; ensure!( handoff .files .iter() .any(|f| f.role == *role && f.new_sha256 == *observed_sha256), "UPDATE_REPLACED_OBSERVATION" ); } Evidence::PairVerified { pair, retained_state_sha256, } => { self.files.verify_pair(self.budget)?; ensure!( *pair == handoff.target && retained_state_sha256 == self.retained.sha256(), "UPDATE_PAIR_OBSERVATION" ); } Evidence::LedgerReturnIntent => self.files.verify_pair(self.budget)?, _ => anyhow::bail!("UPDATE_EVENT_WRITER_NOT_ALLOWED"), } Ok(()) } fn verify_native_effect( &self, handoff: &Handoff, delegation: &UpdateDelegation, effect: &NativeEffect, stage: NativeEffectStage, ) -> Result<()> { ensure!( handoff.bytes()? == self.original.handoff().bytes()? && delegation == self.original.delegation(), "UPDATE_NATIVE_ORIGINAL_CHANGED" ); self.original .check_current(self.binding, self.clock, self.budget)?; self.trust.recheck(self.original, self.clock)?; self.updater.verify_delegation(delegation, self.budget)?; self.os.verify_for_effect(self.budget)?; self.retained.verify()?; let role = |role: &NativeFileRole| match role { NativeFileRole::AgentBinary => rmm_updater_handoff::FileRole::AgentBinary, NativeFileRole::HelperBinary => rmm_updater_handoff::FileRole::HelperBinary, NativeFileRole::FullConfig => rmm_updater_handoff::FileRole::LinuxFullConfig, NativeFileRole::FullPin => rmm_updater_handoff::FileRole::LinuxFullPin, }; match (effect, stage) { (NativeEffect::StopService { .. }, NativeEffectStage::Observe) => { self.stopped.verify_still_stopped(self.budget)? } (NativeEffect::SavePredecessor { role: r }, NativeEffectStage::Claim) => { self.files.verify_original_and_stage(role(r), self.budget)? } (NativeEffect::SavePredecessor { role: r }, NativeEffectStage::Observe) | (NativeEffect::PublishReplacement { role: r }, NativeEffectStage::Claim) => { self.files.verify_saved(role(r), self.budget)? } (NativeEffect::PublishReplacement { role: r }, NativeEffectStage::Observe) => { self.files.verify_replaced(role(r), self.budget)? } (NativeEffect::ReturnLedger, NativeEffectStage::Claim) | (NativeEffect::StartService { .. }, NativeEffectStage::ReserveFutureStart) => { self.files.verify_pair(self.budget)? } _ => anyhow::bail!("UPDATE_NATIVE_OWNED_EFFECT_DENIED"), } Ok(()) } }