//! Closed systemd service set; status is corroborated with held kernel process //! identity. No shell, enable/reboot, global kill or repeated stop/start. use crate::{ Budget, DurableEffectFence, Effect, digest, process::{HeldProcess, ProcessProof}, }; use anyhow::{Result, ensure}; use serde::{Deserialize, Serialize}; use std::{ collections::BTreeMap, fs::File, io::Read, os::fd::{AsRawFd, FromRawFd}, os::unix::net::UnixStream, process::{Child, Command, Stdio}, thread, time::{Duration, Instant}, }; #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum Service { Agent, General, Full, Apt, Updater, } impl Service { fn handoff_service(self) -> Result { Ok(match self { Self::Agent => rmm_updater_handoff::Service::LinuxAgent, Self::General => rmm_updater_handoff::Service::LinuxGeneralHelper, Self::Full => rmm_updater_handoff::Service::LinuxFullHelper, Self::Apt => rmm_updater_handoff::Service::LinuxAptHelper, Self::Updater => anyhow::bail!("UPDATE_UPDATER_NOT_IN_TARGET_SET"), }) } pub fn unit(self) -> &'static str { match self { Self::Agent => "rmm-agent-remote.service", Self::General => "rmm-helper-cloud.service", Self::Full => "rmm-full-maintenance-helper.service", Self::Apt => "rmm-apt-helper.service", Self::Updater => "rmm-agent-updater.service", } } fn uid(self) -> u32 { if self == Self::Agent { 993 } else { 0 } } } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct ServiceProof { pub service: Service, pub invocation_id: String, pub enabled: String, pub process: ProcessProof, pub unit_sha256: String, } pub struct HeldService { proof: ServiceProof, process: HeldProcess, } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct ServicePolicy { pub service: Service, pub image_sha256: String, pub unit_sha256: String, } pub struct Systemd { policies: Vec, } impl Systemd { pub fn observe_updater(pin: ServicePolicy, budget: &Budget) -> Result { ensure!( pin.service == Service::Updater && crate::valid_digest(&pin.image_sha256) && crate::valid_digest(&pin.unit_sha256), "UPDATE_UPDATER_PINS" ); Self { policies: vec![pin], } .observe(Service::Updater, budget) } /// Full is an ordinary required participant in the current Linux topology; /// APT may additionally be installed but is never in the restart set. pub fn new(policies: Vec) -> Result { let services: Vec<_> = policies.iter().map(|p| p.service).collect(); ensure!( services == [Service::Agent, Service::General, Service::Full] || services == [ Service::Agent, Service::General, Service::Full, Service::Apt ], "UPDATE_CLOSED_SERVICE_SET" ); ensure!( policies.iter().all( |p| crate::valid_digest(&p.image_sha256) && crate::valid_digest(&p.unit_sha256) ), "UPDATE_SERVICE_PINS" ); ensure!( policies[1].image_sha256 == policies[2].image_sha256, "UPDATE_HELPER_PAIR_MISMATCH" ); Ok(Self { policies }) } pub fn authenticate_helper_peer( &self, socket: &UnixStream, budget: &Budget, ) -> Result { for service in [Service::General, Service::Full] { let held = self.observe(service, budget)?; if let Ok(peer) = held.authenticate_peer(socket, budget) { return Ok(peer); } } anyhow::bail!("UPDATE_HELPER_PEER_NOT_BOUND") } fn status(service: Service, budget: &Budget) -> Result> { budget.check()?; let output = command( &[ "show", service.unit(), "-p", "LoadState", "-p", "ActiveState", "-p", "MainPID", "-p", "InvocationID", "-p", "UnitFileState", "-p", "FragmentPath", "-p", "DropInPaths", ], budget, )?; parse_service_status(&output) } fn failure_status(service: Service, budget: &Budget) -> Result> { let bytes = command( &[ "show", service.unit(), "-p", "SubState", "-p", "ControlPID", "-p", "Result", "-p", "ExecMainCode", "-p", "ExecMainStatus", "-p", "Job", "-p", "ControlGroup", ], budget, )?; let mut status = BTreeMap::new(); for line in std::str::from_utf8(&bytes)?.lines() { let (k, v) = line .split_once('=') .ok_or_else(|| anyhow::anyhow!("UPDATE_FAILED_STATUS"))?; ensure!( status.insert(k.to_owned(), v.to_owned()).is_none(), "UPDATE_FAILED_STATUS_DUPLICATE" ); } ensure!(status.len() == 7, "UPDATE_FAILED_STATUS_FIELDS"); Ok(status) } fn failed_start(&self, service: Service, budget: &Budget) -> Result { let pin = self .policies .iter() .find(|p| p.service == service) .ok_or_else(|| anyhow::anyhow!("UPDATE_SERVICE_NOT_PINNED"))?; let first = Self::status(service, budget)?; let extra = Self::failure_status(service, budget)?; ensure!( first["ActiveState"] == "failed" && first["MainPID"] == "0" && extra["SubState"] == "failed" && extra["ControlPID"] == "0" && extra["Job"].is_empty() && extra["ControlGroup"].is_empty() && matches!( extra["Result"].as_str(), "exit-code" | "signal" | "core-dump" | "timeout" | "protocol" | "resources" | "start-limit-hit" ) && first["InvocationID"].len() == 32 && first["InvocationID"].bytes().any(|b| b != b'0') && first["InvocationID"].bytes().all(|b| b.is_ascii_hexdigit()) && unit_composition(&first)? == pin.unit_sha256, "UPDATE_START_FAILURE_NOT_CLOSED" ); ensure!( Self::status(service, budget)? == first && Self::failure_status(service, budget)? == extra, "UPDATE_START_FAILURE_CHANGED" ); let mut status = first; status.extend(extra); Ok(FailedStartProof { service, unit_sha256: pin.unit_sha256.clone(), status, }) } pub fn verify_failed_start(&self, expected: &FailedStartProof, budget: &Budget) -> Result<()> { ensure!( self.failed_start(expected.service, budget)? == *expected, "UPDATE_FAILED_START_CHANGED" ); Ok(()) } pub fn verify_inactive(&self, service: Service, budget: &Budget) -> Result<()> { let pin = self .policies .iter() .find(|p| p.service == service) .ok_or_else(|| anyhow::anyhow!("UPDATE_SERVICE_NOT_PINNED"))?; let status = Self::status(service, budget)?; let extra = Self::failure_status(service, budget)?; ensure!( status["ActiveState"] == "inactive" && status["MainPID"] == "0" && extra["ControlPID"] == "0" && extra["Job"].is_empty() && extra["ControlGroup"].is_empty() && unit_composition(&status)? == pin.unit_sha256, "UPDATE_SERVICE_NOT_CLOSED_INACTIVE" ); ensure!( Self::status(service, budget)? == status && Self::failure_status(service, budget)? == extra, "UPDATE_INACTIVE_SERVICE_CHANGED" ); Ok(()) } pub fn observe(&self, service: Service, budget: &Budget) -> Result { let pin = self .policies .iter() .find(|p| p.service == service) .ok_or_else(|| anyhow::anyhow!("UPDATE_SERVICE_NOT_PINNED"))?; let first = Self::status(service, budget)?; ensure!( first.get("ActiveState").is_some_and(|s| s == "active"), "UPDATE_SERVICE_INACTIVE" ); let pid = first .get("MainPID") .ok_or_else(|| anyhow::anyhow!("UPDATE_SERVICE_PID"))? .parse()?; let invocation_id = first .get("InvocationID") .ok_or_else(|| anyhow::anyhow!("UPDATE_SERVICE_INSTANCE"))? .to_owned(); ensure!( invocation_id.len() == 32 && invocation_id.bytes().all(|b| b.is_ascii_hexdigit()), "UPDATE_SERVICE_INSTANCE" ); // Unit composition must be independently provisioned. No unrecognized // drop-in is silently ignored or inherited as an update permission. let unit_sha = unit_composition(&first)?; ensure!(unit_sha == pin.unit_sha256, "UPDATE_UNIT_CHANGED"); let process = HeldProcess::open( pid, service.uid(), if service == Service::Updater { 0 } else { 984 }, &pin.image_sha256, )?; ensure!( Self::status(service, budget)? == first, "UPDATE_SERVICE_CHANGED_DURING_OBSERVATION" ); process.verify()?; Ok(HeldService { proof: ServiceProof { service, invocation_id, enabled: first["UnitFileState"].clone(), process: process.proof().clone(), unit_sha256: unit_sha, }, process, }) } pub fn stop_all( self, budget: &Budget, fence: &mut impl DurableEffectFence, ) -> Result { let mut original = Vec::new(); for pin in &self.policies { if pin.service == Service::Apt { let s = Self::status(Service::Apt, budget)?; ensure!( s["ActiveState"] == "inactive" && s["MainPID"] == "0" && s["UnitFileState"] == "disabled" && unit_composition(&s)? == pin.unit_sha256, "UPDATE_APT_MUST_REMAIN_CLOSED" ); } else { original.push(self.observe(pin.service, budget)?); } } for held in &original { held.process.verify()?; let effect = Effect::StopService { service: held.proof.service, }; fence.before_effect(&effect)?; budget.check()?; command(&["--no-block", "stop", held.proof.service.unit()], budget)?; loop { budget.check()?; let state = Self::status(held.proof.service, budget)?; if state["ActiveState"] == "inactive" && state["MainPID"] == "0" && held.process.exited()? { break; } ensure!( state["MainPID"] == "0" || state["MainPID"] == held.proof.process.pid.to_string(), "UPDATE_STOP_PID_CHANGED" ); thread::sleep(Duration::from_millis(20)); } fence.after_effect(&effect, &digest(&serde_json::to_vec(&held.proof)?))?; } let result = StoppedServices { systemd: self, original, failed: None, subsequently_exited: Vec::new(), early_full_failure: None, }; result.verify_still_stopped(budget)?; Ok(result) } } impl HeldService { pub fn started_proof(&self, budget: &Budget) -> Result { self.verify(budget)?; ensure!( self.proof.service != Service::Apt, "UPDATE_APT_NOT_IN_START_SET" ); Ok(rmm_updater_handoff::StartedService { service: self.proof.service.handoff_service()?, process: rmm_updater_handoff::Process { pid: self.proof.process.pid, birth: self.proof.process.birth, image_sha256: self.proof.process.image_sha256.clone(), }, }) } pub fn proof(&self) -> &ServiceProof { &self.proof } pub fn verify(&self, budget: &Budget) -> Result<()> { self.process.verify()?; let current = Systemd::status(self.proof.service, budget)?; ensure!( current["ActiveState"] == "active" && current["MainPID"] == self.proof.process.pid.to_string() && current["InvocationID"] == self.proof.invocation_id && current["UnitFileState"] == self.proof.enabled && unit_composition(¤t)? == self.proof.unit_sha256, "UPDATE_SERVICE_BINDING_CHANGED" ); self.process.verify() } /// Authenticate the actual local connection independently of request JSON. /// Root alone is insufficient; the fixed service's held pidfd must match. pub fn authenticate_peer( &self, socket: &UnixStream, budget: &Budget, ) -> Result { use rmm_domain::agent_update::{ParticipantIdentity, ParticipantRole}; self.verify(budget)?; let mut peer = std::mem::MaybeUninit::::uninit(); let mut len = std::mem::size_of::() as libc::socklen_t; // SAFETY: live Unix socket, correctly sized writable ucred and length. let result = unsafe { libc::getsockopt( socket.as_raw_fd(), libc::SOL_SOCKET, libc::SO_PEERCRED, peer.as_mut_ptr().cast(), &mut len, ) }; ensure!( result == 0 && len as usize == std::mem::size_of::(), "UPDATE_PEER_CREDENTIALS" ); let peer = unsafe { peer.assume_init() }; ensure!( peer.pid > 0 && peer.pid as u32 == self.proof.process.pid && peer.uid == 0 && peer.gid == self.proof.process.gid, "UPDATE_PEER_SERVICE_BINDING" ); let role = match self.proof.service { Service::Updater => ParticipantRole::LinuxUpdater, Service::General => ParticipantRole::LinuxGeneralHelper, Service::Full => ParticipantRole::LinuxFullHelper, _ => anyhow::bail!("UPDATE_PARTICIPANT_ROLE"), }; self.verify(budget)?; Ok(ParticipantIdentity { role, pid: self.proof.process.pid, birth: self.proof.process.birth, image_sha256: self.proof.process.image_sha256.clone(), security_principal: "uid:0".into(), service_instance_sha256: digest(&serde_json::to_vec(&self.proof)?), }) } } pub struct StoppedServices { systemd: Systemd, original: Vec, failed: Option, subsequently_exited: Vec, early_full_failure: Option, } /// Concrete systemd failure of our acknowledged once-only start. A timeout, /// lost command reply, activating/auto-restart or residual control process is /// never represented by this value. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct FailedStartProof { pub service: Service, pub unit_sha256: String, pub status: BTreeMap, } pub enum Startup { Complete { started: Vec, stopped: StoppedServices, }, KnownFailed(KnownStartupFailure), } /// Retained only by the live worker that actually started and stopped the new /// General under the original conditional reservation. Not deserializable. pub struct EarlyFullFailure { general_start: ServiceProof, general_stop_sha256: String, failure: FailedStartProof, } impl EarlyFullFailure { pub fn general_start(&self) -> &ServiceProof { &self.general_start } pub fn general_stop_sha256(&self) -> &str { &self.general_stop_sha256 } pub fn failure(&self) -> &FailedStartProof { &self.failure } pub fn failure_sha256(&self) -> Result { Ok(digest(&serde_json::to_vec(&self.failure)?)) } } pub struct KnownStartupFailure { stopped: StoppedServices, target: Systemd, started: Vec, failure: FailedStartProof, } impl KnownStartupFailure { pub fn failure(&self) -> &FailedStartProof { &self.failure } pub fn started(&self) -> &[HeldService] { &self.started } pub fn verify(&self, budget: &Budget) -> Result<()> { budget.check()?; self.target.verify_failed_start(&self.failure, budget)?; for held in &self.started { held.verify(budget)?; } for service in [Service::General, Service::Full, Service::Agent] { if service == self.failure.service || self.started.iter().any(|s| s.proof.service == service) { continue; } self.target.verify_inactive(service, budget)?; } ensure!( self.stopped .original .iter() .all(|s| s.process.exited().unwrap_or(false)), "UPDATE_OLD_PROCESS_STILL_LIVE" ); Ok(()) } /// An early Full failure leaves only the newly started General reader. /// The embedding fence must hold its original first-return conditional /// reservation. No SQLite may be opened until this exact held PID exits. pub fn stop_reserved_general( mut self, budget: &Budget, fence: &mut impl DurableEffectFence, ) -> Result<(StoppedServices, ServiceProof, String)> { self.verify(budget)?; ensure!( self.failure.service == Service::Full && self.started.len() == 1 && self.started[0].proof.service == Service::General, "UPDATE_CONDITIONAL_STOP_ONLY_EARLY_FULL_FAILURE" ); let held = &self.started[0]; let proof = held.proof.clone(); let effect = Effect::StopService { service: Service::General, }; fence.before_effect(&effect)?; command(&["--no-block", "stop", Service::General.unit()], budget)?; loop { budget.check()?; self.target.verify_failed_start(&self.failure, budget)?; self.target.verify_inactive(Service::Agent, budget)?; let state = Systemd::status(Service::General, budget)?; ensure!( state["MainPID"] == "0" || state["MainPID"] == proof.process.pid.to_string(), "UPDATE_CONDITIONAL_STOP_PID_CHANGED" ); if state["ActiveState"] == "inactive" && state["MainPID"] == "0" && held.process.exited()? { break; } thread::sleep(Duration::from_millis(20)); } let observation = digest(&serde_json::to_vec(&( "reserved_general_stop_v1", &proof, &self.failure, ))?); fence.after_effect(&effect, &observation)?; self.stopped.subsequently_exited.append(&mut self.started); self.stopped.failed = Some(self.failure.clone()); self.stopped.early_full_failure = Some(EarlyFullFailure { general_start: proof.clone(), general_stop_sha256: observation.clone(), failure: self.failure, }); self.stopped.verify_still_stopped(budget)?; Ok((self.stopped, proof, observation)) } } impl StoppedServices { pub fn early_full_failure(&self, budget: &Budget) -> Result<&EarlyFullFailure> { self.verify_still_stopped(budget)?; let proof = self .early_full_failure .as_ref() .ok_or_else(|| anyhow::anyhow!("UPDATE_NO_RETAINED_EARLY_FAILURE"))?; ensure!( self.subsequently_exited.len() == 1 && self.subsequently_exited[0].proof == proof.general_start && self.subsequently_exited[0].process.exited()?, "UPDATE_RETAINED_GENERAL_STOP_CHANGED" ); Ok(proof) } pub fn verify_still_stopped(&self, budget: &Budget) -> Result<()> { for pin in &self.systemd.policies { let s = Systemd::status(pin.service, budget)?; if let Some(failed) = self.failed.as_ref().filter(|f| f.service == pin.service) { // Unit composition is still the provisioned original unit; // only code/config files were part of the approved transition. self.systemd.verify_failed_start(failed, budget)?; } else { ensure!( s["ActiveState"] == "inactive" && s["MainPID"] == "0" && unit_composition(&s)? == pin.unit_sha256, "UPDATE_READER_NOT_STOPPED" ); } if pin.service == Service::Apt { ensure!( s["UnitFileState"] == "disabled", "UPDATE_APT_MUST_REMAIN_CLOSED" ); } } ensure!( self.original .iter() .chain(self.subsequently_exited.iter()) .all(|s| s.process.exited().unwrap_or(false)), "UPDATE_OLD_PROCESS_STILL_LIVE" ); Ok(()) } pub fn proofs(&self) -> Vec { self.original.iter().map(|s| s.proof.clone()).collect() } /// The embedding fence MUST verify the exact PairVerified and durable /// LedgerReturnIntent, released same guard and new target hashes first. pub fn start_once( self, new: Systemd, budget: &Budget, fence: &mut impl DurableEffectFence, ) -> Result> { match self.start_once_observed(new, budget, fence)? { Startup::Complete { started, .. } => Ok(started), Startup::KnownFailed(_) => anyhow::bail!("UPDATE_START_KNOWN_FAILED"), } } pub fn start_once_observed( self, new: Systemd, budget: &Budget, fence: &mut impl DurableEffectFence, ) -> Result { self.verify_still_stopped(budget)?; ensure!( self.systemd .policies .iter() .map(|p| p.service) .eq(new.policies.iter().map(|p| p.service)), "UPDATE_START_SET_CHANGED" ); let mut started = Vec::new(); for service in [Service::General, Service::Full, Service::Agent] { let effect = Effect::StartService { service }; fence.before_effect(&effect)?; budget.check()?; command(&["--no-block", "start", service.unit()], budget)?; loop { budget.check()?; let state = Systemd::status(service, budget)?; if state["ActiveState"] == "active" && state["MainPID"] != "0" { let held = new.observe(service, budget)?; ensure!( !self .original .iter() .any(|old| old.proof.process.pid == held.proof.process.pid), "UPDATE_PROCESS_REUSE" ); fence.after_effect(&effect, &digest(&serde_json::to_vec(&held.proof)?))?; started.push(held); break; } if state["ActiveState"] == "failed" { let failure = new.failed_start(service, budget)?; let result = KnownStartupFailure { stopped: self, target: new, started, failure, }; result.verify(budget)?; return Ok(Startup::KnownFailed(result)); } ensure!( state["ActiveState"] == "activating" || state["ActiveState"] == "active", "UPDATE_START_UNPROVEN" ); thread::sleep(Duration::from_millis(20)); } } // Return in the canonical Handoff stop-set order, not actual start order. started.sort_by_key(|s| s.proof.service); Ok(Startup::Complete { started, stopped: self, }) } } fn parse_service_status(raw: &[u8]) -> Result> { const FIELDS: [&str; 7] = [ "LoadState", "ActiveState", "MainPID", "InvocationID", "UnitFileState", "FragmentPath", "DropInPaths", ]; let mut fields = BTreeMap::new(); for row in std::str::from_utf8(raw)?.lines() { let (k, v) = row .split_once('=') .ok_or_else(|| anyhow::anyhow!("UPDATE_SYSTEMD_RESPONSE"))?; ensure!( FIELDS.contains(&k) && fields.insert(k.into(), v.into()).is_none(), "UPDATE_SYSTEMD_PROPERTIES" ); } ensure!( fields.len() == FIELDS.len() && fields.get("LoadState").is_some_and(|s| s == "loaded"), "UPDATE_SYSTEMD_LOADED" ); Ok(fields) } fn unit_composition(fields: &BTreeMap) -> Result { use crate::files::Directory; use std::{ffi::OsStr, path::Path}; let fragment = fields .get("FragmentPath") .ok_or_else(|| anyhow::anyhow!("UPDATE_UNIT_FRAGMENT"))?; let mut paths = vec![fragment.as_str()]; paths.extend( fields .get("DropInPaths") .ok_or_else(|| anyhow::anyhow!("UPDATE_UNIT_DROPINS"))? .split_ascii_whitespace(), ); let mut proof = Vec::new(); for path in paths { let path = Path::new(path); ensure!(path.is_absolute(), "UPDATE_UNIT_PATH"); let directory = Directory::open( path.parent() .ok_or_else(|| anyhow::anyhow!("UPDATE_UNIT_PARENT"))?, 0, false, )?; let file = directory.inspect(path.file_name().unwrap_or(OsStr::new("")), 0, 65536)?; proof.push((path.to_string_lossy().into_owned(), file.proof().clone())); } Ok(digest(&serde_json::to_vec(&proof)?)) } struct OwnedCommand { child: Child, pidfd: File, } impl Drop for OwnedCommand { fn drop(&mut self) { // Signal this exact retained kernel process, including on every early // error. An exited child cannot turn the descriptor into another PID. unsafe { libc::syscall( libc::SYS_pidfd_send_signal, self.pidfd.as_raw_fd(), libc::SIGKILL, 0usize, 0, ) }; let _ = self.child.wait(); } } fn drain(stream: &mut dyn Read, buffer: &mut Vec) -> Result<()> { let mut bytes = [0; 1024]; loop { match stream.read(&mut bytes) { Ok(0) => break, Ok(n) => { ensure!(buffer.len() + n <= 16384, "UPDATE_COMMAND_OUTPUT_LIMIT"); buffer.extend_from_slice(&bytes[..n]); } Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => break, Err(e) => return Err(e.into()), } } Ok(()) } fn command(args: &[&str], budget: &Budget) -> Result> { budget.check()?; let mut child = Command::new("/usr/bin/systemctl") .args(["--system", "--no-ask-password", "--no-pager"]) .args(args) .env_clear() .env("PATH", "/usr/bin:/bin") .env("LC_ALL", "C") .stdin(Stdio::null()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .spawn()?; // A live Child has not been reaped, so this pidfd cannot target a reused PID. let raw = unsafe { libc::syscall(libc::SYS_pidfd_open, child.id(), 0) }; if raw < 0 { let _ = child.kill(); let _ = child.wait(); anyhow::bail!("UPDATE_COMMAND_PIDFD_UNAVAILABLE"); } let pidfd = unsafe { File::from_raw_fd(raw as i32) }; let mut owned = OwnedCommand { child, pidfd }; let deadline = Instant::now() + Duration::from_secs(10); let mut stdout = owned.child.stdout.take().unwrap(); let mut stderr = owned.child.stderr.take().unwrap(); for fd in [stdout.as_raw_fd(), stderr.as_raw_fd()] { ensure!( unsafe { libc::fcntl(fd, libc::F_SETFL, libc::O_NONBLOCK) } >= 0, "UPDATE_COMMAND_NONBLOCK" ); } let mut out = Vec::new(); let mut error = Vec::new(); loop { drain(&mut stdout, &mut out)?; drain(&mut stderr, &mut error)?; if let Some(status) = owned.child.try_wait()? { drain(&mut stdout, &mut out)?; drain(&mut stderr, &mut error)?; ensure!(status.success(), "UPDATE_SYSTEMD_COMMAND_FAILED"); return Ok(out); } if budget.check().is_err() || Instant::now() >= deadline { // Kill only our held systemctl client. A request may already have // reached systemd: the embedding worker must record Unknown. anyhow::bail!("UPDATE_SYSTEMD_REPLY_UNKNOWN"); } thread::sleep(Duration::from_millis(5)); } } #[cfg(test)] mod status_tests { use super::*; #[test] fn accepts_actual_seven_property_shape_and_denies_missing_or_extra_properties() { let status=b"LoadState=loaded\nActiveState=active\nMainPID=123\nInvocationID=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\nUnitFileState=enabled\nFragmentPath=/etc/systemd/system/rmm-agent-remote.service\nDropInPaths=\n"; assert_eq!(parse_service_status(status).unwrap().len(), 7); let mut extra = status.to_vec(); extra.extend_from_slice(b"Unexpected=1\n"); assert!(parse_service_status(&extra).is_err()); assert!(parse_service_status(b"LoadState=loaded\nActiveState=active\n").is_err()); let mut duplicate = status.to_vec(); duplicate.extend_from_slice(b"MainPID=321\n"); assert!(parse_service_status(&duplicate).is_err()); } }