use anyhow::{Result, ensure}; use fs2::FileExt; use prost::Message; use rmm_protocol::Envelope; use std::{ fs::{self, File, OpenOptions}, io::Write, os::unix::fs::{OpenOptionsExt, PermissionsExt}, path::Path, }; pub fn prepare_dir(dir: &Path) -> Result<()> { if !dir.exists() { fs::create_dir_all(dir)?; fs::set_permissions(dir, fs::Permissions::from_mode(0o700))?; } let meta = fs::symlink_metadata(dir)?; ensure!( meta.is_dir() && !meta.file_type().is_symlink() && meta.permissions().mode() & 0o077 == 0, "agent state must be a private directory (0700)" ); // SAFETY: geteuid has no preconditions. rmm_privileged_helper::private_path(dir, unsafe { libc::geteuid() }, true)?; Ok(()) } pub fn create_secret(path: &Path, bytes: &[u8]) -> Result<()> { let mut file = OpenOptions::new() .write(true) .create_new(true) .mode(0o600) .open(path)?; file.write_all(bytes)?; file.sync_all()?; File::open( path.parent() .ok_or_else(|| anyhow::anyhow!("missing parent"))?, )? .sync_all()?; Ok(()) } pub fn lock(dir: &Path) -> Result { let f = OpenOptions::new() .read(true) .write(true) .create(true) .truncate(false) .mode(0o600) .custom_flags(libc::O_NOFOLLOW) .open(dir.join("agent.lock"))?; // SAFETY: geteuid has no preconditions. rmm_privileged_helper::private_path( &dir.join("agent.lock"), unsafe { libc::geteuid() }, false, )?; f.try_lock_exclusive()?; Ok(f) } pub fn open(dir: &Path) -> Result { let path = dir.join("journal.sqlite"); if !path.exists() { create_secret(&path, &[])?; } // Refuse unsafe preexisting journals rather than opening a symlink. let uid = unsafe { libc::geteuid() }; rmm_privileged_helper::private_path(&path, uid, false)?; let db = rusqlite::Connection::open(path)?; rmm_privileged_helper::bounded_journal_capacity(&db)?; db.execute_batch("PRAGMA synchronous=FULL; PRAGMA journal_mode=DELETE; CREATE TABLE IF NOT EXISTS counter(id INTEGER PRIMARY KEY CHECK(id=1),seq INTEGER NOT NULL); INSERT OR IGNORE INTO counter VALUES(1,0); CREATE TABLE IF NOT EXISTS pending(id INTEGER PRIMARY KEY CHECK(id=1),payload BLOB NOT NULL CHECK(length(payload)<=1048576));")?; db.execute_batch(" CREATE TABLE IF NOT EXISTS job_attempts(job TEXT NOT NULL, attempt TEXT NOT NULL, offer BLOB NOT NULL, state TEXT NOT NULL, revision INTEGER NOT NULL DEFAULT 0, grant_id TEXT NOT NULL DEFAULT '', cancel_requested INTEGER NOT NULL DEFAULT 0, PRIMARY KEY(job,attempt)); CREATE TABLE IF NOT EXISTS job_events(id TEXT PRIMARY KEY, payload BLOB NOT NULL CHECK(length(payload)<=1048576)); CREATE TABLE IF NOT EXISTS safety(key TEXT PRIMARY KEY,value TEXT NOT NULL);")?; let check: String = db.query_row("PRAGMA integrity_check", [], |r| r.get(0))?; ensure!(check == "ok", "JOURNAL_INTEGRITY"); crate::reconcile::initialize(&db)?; File::open(dir)?.sync_all()?; Ok(db) } pub fn next_sequence(db: &mut rusqlite::Connection) -> Result { let tx = db.transaction()?; let seq: i64 = tx.query_row( "UPDATE counter SET seq=seq+1 WHERE id=1 RETURNING seq", [], |r| r.get(0), )?; tx.commit()?; Ok(u64::try_from(seq)?) } /// Do not let periodic metadata queue behind unacknowledged durable traffic. /// Results/control receipts retain priority; no old inventory is deleted. pub fn passive_collection_ready(db: &rusqlite::Connection) -> Result { Ok( !db.query_row("SELECT EXISTS(SELECT 1 FROM job_events)", [], |row| { row.get::<_, bool>(0) })?, ) } #[cfg(test)] pub fn store_pending(db: &rusqlite::Connection, e: &Envelope) -> Result<()> { db.execute("INSERT INTO pending VALUES(1,?1)", [e.encode_to_vec()])?; Ok(()) } #[cfg(test)] mod passive_pressure_tests { #[test] fn agent_journal_capacity_is_finite_and_preserves_sequence_on_reopen() { let dir = std::env::temp_dir().join(format!("rmm-capacity-{}", uuid::Uuid::new_v4())); super::prepare_dir(&dir).unwrap(); let mut db = super::open(&dir).unwrap(); assert_eq!(super::next_sequence(&mut db).unwrap(), 1); db.execute_batch("PRAGMA max_page_count=65536").unwrap(); drop(db); let mut db = super::open(&dir).unwrap(); assert_eq!( db.query_row("PRAGMA page_size", [], |r| r.get::<_, u32>(0)) .unwrap(), 4096 ); assert_eq!( db.query_row("PRAGMA max_page_count", [], |r| r.get::<_, u32>(0)) .unwrap(), 262144 ); assert_eq!(super::next_sequence(&mut db).unwrap(), 2); drop(db); std::fs::remove_dir_all(dir).unwrap(); } #[test] fn pending_durable_traffic_backpressures_collection_without_deletion() { let db = rusqlite::Connection::open_in_memory().unwrap(); db.execute_batch("CREATE TABLE job_events(id TEXT PRIMARY KEY,payload BLOB NOT NULL)") .unwrap(); assert!(super::passive_collection_ready(&db).unwrap()); db.execute("INSERT INTO job_events VALUES('unacked',x'01')", []) .unwrap(); assert!(!super::passive_collection_ready(&db).unwrap()); assert_eq!( db.query_row("SELECT count(*) FROM job_events", [], |row| row .get::<_, i64>(0)) .unwrap(), 1 ); db.execute("DELETE FROM job_events WHERE id='unacked'", []) .unwrap(); assert!(super::passive_collection_ready(&db).unwrap()); } } pub fn pending(db: &rusqlite::Connection) -> Result> { use rusqlite::OptionalExtension; let data: Option> = db .query_row("SELECT payload FROM pending WHERE id=1", [], |r| r.get(0)) .optional()?; data.map(|d| Ok(Envelope::decode(d.as_slice())?)) .transpose() } pub fn clear_pending(db: &rusqlite::Connection) -> Result<()> { db.execute("DELETE FROM pending WHERE id=1", [])?; Ok(()) } #[cfg(test)] mod tests { use super::*; #[test] fn durable_sequence_pending_and_exclusive_lock() { let path = std::env::temp_dir().join(format!("rmm-{}", uuid::Uuid::new_v4())); prepare_dir(&path).unwrap(); let lockfile = lock(&path).unwrap(); assert!(lock(&path).is_err()); let mut db = open(&path).unwrap(); assert_eq!(next_sequence(&mut db).unwrap(), 1); let e = Envelope { sequence: 2, ..Default::default() }; store_pending(&db, &e).unwrap(); drop(db); let mut db = open(&path).unwrap(); assert_eq!(next_sequence(&mut db).unwrap(), 2); assert_eq!(pending(&db).unwrap().unwrap(), e); clear_pending(&db).unwrap(); assert!(pending(&db).unwrap().is_none()); drop(db); drop(lockfile); fs::remove_dir_all(path).unwrap(); } }