//! Linux OS ports for the common independently verified helper reconciliation. use anyhow::{Result, ensure}; use chrono::{DateTime, Utc}; pub use rmm_agent_core::helper_reconcile::*; use rusqlite::Connection; use std::{ fs::File, path::{Path, PathBuf}, }; use uuid::Uuid; #[derive(Default)] pub struct NativeClock(rmm_privileged_helper::TrustedClock); impl Clock for NativeClock { fn now(&self) -> Result> { ensure!(self.0.trusted(), "CLOCK_UNTRUSTED"); Ok(Utc::now()) } } pub struct NativeBoot; impl BootSource for NativeBoot { fn current_boot(&self) -> Result { Ok(Uuid::parse_str(&rmm_agent_linux::boot_id()?)?) } } /// Acquire instead of, not on top of, the existing listener's lock_state call. /// Holding this guard is mandatory through snapshot/apply/can_start/consume and /// actual effect. Production uses owner_uid=0; tests use their own private UID. pub struct ExecutorGuard { _file: File, directory: PathBuf, _not_sync: std::marker::PhantomData>, } impl ExecutorGuard { pub fn acquire(directory: &Path, owner_uid: u32) -> Result { ensure!(directory.is_absolute(), "ABSOLUTE_STATE_DIRECTORY_REQUIRED"); let file = rmm_privileged_helper::lock_state(directory, owner_uid)?; Ok(Self { _file: file, directory: directory.canonicalize()?, _not_sync: std::marker::PhantomData, }) } pub(crate) fn check(&self, db: &Connection) -> Result<()> { let path: String = db.query_row( "SELECT file FROM pragma_database_list WHERE name='main'", [], |r| r.get(0), )?; ensure!( Path::new(&path).canonicalize()? == self.directory.join("consumption.sqlite").canonicalize()?, "NOT_THE_SHARED_EXECUTOR_LEDGER" ); Ok(()) } } impl ExecutorLedgerGuard for ExecutorGuard { fn check(&self, db: &Connection) -> Result<()> { ExecutorGuard::check(self, db) } }