//! Passive, bounded Linux observations. No refresh/update or configuration writes. //! `received_at` is intentionally absent: the authenticated server assigns it. use crate::{ actions::{ServiceUnit, reboot_required}, process, }; use std::{ collections::BTreeMap, ffi::{CStr, CString}, fs, io::Read, mem::MaybeUninit, net::{Ipv4Addr, Ipv6Addr}, path::Path, time::{Duration, SystemTime}, }; fn serialize_time( time: &SystemTime, serializer: S, ) -> Result { serde::Serialize::serialize(&chrono::DateTime::::from(*time), serializer) } fn serialize_optional_time( time: &Option, serializer: S, ) -> Result { serde::Serialize::serialize(&time.map(chrono::DateTime::::from), serializer) } #[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)] #[serde(rename_all = "snake_case")] pub enum Quality { Valid, Unknown, Unsupported, PermissionDenied, Error, } #[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)] #[serde(rename_all = "snake_case")] pub enum Completeness { Complete, Partial, Unknown, } #[derive(Debug, Clone, serde::Serialize)] pub struct Observation { pub source_id: String, #[serde(serialize_with = "serialize_time")] pub observed_at: SystemTime, pub quality: Quality, pub completeness: Completeness, pub truncated: bool, pub error_code: Option, pub data: Option, } impl Observation { fn valid(source: &str, data: T) -> Self { Self { source_id: source.into(), observed_at: SystemTime::now(), quality: Quality::Valid, completeness: Completeness::Complete, truncated: false, error_code: None, data: Some(data), } } fn unavailable(source: &str, quality: Quality, code: &str) -> Self { let quality = if quality == Quality::Valid { Quality::Unknown } else { quality }; Self { source_id: source.into(), observed_at: SystemTime::now(), quality, completeness: Completeness::Unknown, truncated: false, error_code: Some(code.into()), data: None, } } fn partial(mut self, code: &str) -> Self { self.completeness = Completeness::Partial; self.error_code = Some(code.into()); self } } fn read(path: &str, cap: usize) -> Observation { match fs::File::open(path).and_then(|f| { let mut bytes = vec![]; f.take(cap as u64 + 1).read_to_end(&mut bytes)?; Ok(bytes) }) { Ok(mut bytes) => { let truncated = bytes.len() > cap; bytes.truncate(cap); match String::from_utf8(bytes) { Ok(text) => { let mut observation = Observation::valid(path, text); if truncated { observation = observation.partial("SOURCE_LIMIT"); observation.truncated = true; } observation } Err(_) => Observation::unavailable(path, Quality::Error, "NON_UTF8_SOURCE"), } } Err(e) => Observation::unavailable( path, match e.kind() { std::io::ErrorKind::PermissionDenied => Quality::PermissionDenied, std::io::ErrorKind::NotFound => Quality::Unsupported, _ => Quality::Error, }, "SOURCE_UNREADABLE", ), } } fn map(source: Observation, parse: impl FnOnce(T) -> Option) -> Observation { let data = source.data.and_then(parse); let failed = data.is_none() && source.quality == Quality::Valid; Observation { source_id: source.source_id, observed_at: source.observed_at, quality: if failed { Quality::Error } else { source.quality }, completeness: if failed { Completeness::Unknown } else { source.completeness }, truncated: source.truncated, error_code: if failed { Some("SOURCE_PARSE_ERROR".into()) } else { source.error_code }, data, } } #[derive(Debug, Clone, serde::Serialize)] pub struct Cpu { pub logical_processors: usize, pub model: Option, /// Cumulative jiffies, not a fabricated instant utilization percentage. pub aggregate_jiffies: Vec, } #[derive(Debug, Clone, serde::Serialize)] pub struct Memory { pub total_bytes: u64, pub available_bytes: Option, pub swap_total_bytes: u64, pub swap_free_bytes: u64, } #[derive(Debug, Clone, serde::Serialize)] pub struct OperatingSystem { pub release: BTreeMap, pub kernel: String, pub architecture: String, pub hostname: String, pub boot_id: String, pub uptime_seconds: f64, } #[derive(Debug, Clone, serde::Serialize)] pub struct Interface { pub name: String, pub addresses: Vec, pub mac: Option, pub operstate: Option, pub rx_bytes: Option, pub tx_bytes: Option, pub mtu: Option, } #[derive(Debug, Clone, serde::Serialize)] pub struct Route { pub interface: String, pub destination: String, pub gateway: String, pub mask_or_prefix: String, pub flags: u32, pub metric: u32, } #[derive(Debug, Clone, serde::Serialize)] pub struct Dns { pub nameservers: Vec, pub search: Vec, pub resolver_symlink: Option, } #[derive(Debug, Clone, serde::Serialize)] pub struct Volume { pub mount_point: String, pub filesystem: String, pub device: String, pub total_bytes: u64, pub free_bytes: u64, pub available_bytes: u64, pub available_percent: Option, pub total_inodes: u64, pub free_inodes: u64, pub available_inodes: u64, pub available_inode_percent: Option, } #[derive(Debug, Clone, serde::Serialize)] pub struct SoftwarePackage { pub name: String, pub version: String, pub architecture: String, pub status: String, } #[derive(Debug, Clone)] pub struct ServiceStatus { pub unit: ServiceUnit, pub id: String, pub load_state: String, pub active_state: String, pub sub_state: String, pub unit_file_state: String, } #[derive(Debug, Clone)] pub struct ServiceExpectation { pub unit: ServiceUnit, pub expected_active: bool, } #[derive(Debug, Clone)] pub struct ServiceCheck { pub expectation: ServiceExpectation, pub status: Observation, pub matches: Option, } #[derive(Debug, Clone, serde::Serialize)] pub struct AvailableUpdate { pub package: String, pub architecture: String, pub candidate_version: String, pub installed_version: Option, } #[derive(Debug, Clone, serde::Serialize)] pub struct UpdateStatus { pub available: Vec, /// Local metadata file age is NOT an exact last scan or last install time. #[serde(serialize_with = "serialize_optional_time")] pub newest_local_metadata_at: Option, #[serde(serialize_with = "serialize_optional_time")] pub last_successful_scan_at: Option, pub metadata_fresh: Option, pub reboot_required: Option, pub history_quality: Quality, } #[derive(Debug, Clone, serde::Serialize)] pub struct Hardware { pub readable_dmi: BTreeMap, } #[derive(Debug, Clone, serde::Serialize)] pub struct LinuxInventory { pub os: Observation, pub cpu: Observation, pub memory: Observation, pub interfaces: Observation>, pub routes: Observation>, pub dns: Observation, pub volumes: Observation>>, pub software: Observation>, pub updates: Observation, pub hardware: Observation, pub smart: Observation<()>, } pub fn collect_native() -> LinuxInventory { LinuxInventory { os: operating_system(), cpu: cpu(), memory: memory(), interfaces: interfaces(), routes: routes(), dns: dns(), volumes: volumes(), software: software(), updates: updates(), hardware: hardware(), smart: Observation::unavailable( "linux.smart", Quality::Unsupported, "NO_VERIFIED_READ_ONLY_SMART_PROVIDER", ), } } /// Local availability only, not a production support/VM acceptance claim and /// never evidence of authority to execute an operation. pub fn capabilities() -> BTreeMap> { use std::os::unix::fs::PermissionsExt; let executable = |path: &str| { fs::metadata(path).is_ok_and(|m| m.is_file() && m.permissions().mode() & 0o111 != 0) }; let mut result = BTreeMap::new(); for (name, available, source) in [ ( "inventory.proc", Path::new("/proc/meminfo").is_file(), "/proc", ), ( "software.dpkg", executable("/usr/bin/dpkg-query"), "/usr/bin/dpkg-query", ), ( "updates.local_apt", executable("/usr/bin/apt-cache") && executable("/usr/bin/apt"), "local APT metadata", ), ( "services.systemd", Path::new("/run/systemd/system").is_dir() && executable("/usr/bin/systemctl"), "systemd runtime", ), ( "logs.journald", executable("/usr/bin/journalctl") && (Path::new("/run/systemd/journal").is_dir() || Path::new("/var/log/journal").is_dir()), "journald runtime", ), ( "scripts.posix_sh", crate::actions::Interpreter::PosixSh.available(), "/bin/sh", ), ( "scripts.bash", crate::actions::Interpreter::Bash.available(), "/bin/bash", ), ] { let observation = if available { Observation::valid(source, true) } else { Observation { data: Some(false), ..Observation::unavailable( source, Quality::Unsupported, "LOCAL_PROVIDER_UNAVAILABLE", ) } }; result.insert(name.into(), observation); } result.insert( "packages.mutation".into(), Observation { data: Some(false), ..Observation::unavailable( "linux.apt-helper", Quality::Unsupported, "ISOLATED_NO_REBOOT_HELPER_NOT_INTEGRATED", ) }, ); result.insert( "hardware.smart".into(), Observation { data: Some(false), ..Observation::unavailable( "linux.smart", Quality::Unsupported, "NO_VERIFIED_READ_ONLY_SMART_PROVIDER", ) }, ); result } pub fn operating_system() -> Observation { let release = read("/etc/os-release", 32 * 1024); let kernel = read("/proc/sys/kernel/osrelease", 4096); let hostname = read("/proc/sys/kernel/hostname", 4096); let uptime = read("/proc/uptime", 4096); for (quality, completeness, truncated) in [ (release.quality, release.completeness, release.truncated), (kernel.quality, kernel.completeness, kernel.truncated), (hostname.quality, hostname.completeness, hostname.truncated), (uptime.quality, uptime.completeness, uptime.truncated), ] { if quality != Quality::Valid || completeness != Completeness::Complete { let mut failed = Observation::unavailable("linux.os", quality, "OS_SOURCE_INCOMPLETE"); failed.truncated = truncated; return failed; } } let boot = crate::boot_id(); if let (Some(release), Some(kernel), Some(hostname), Some(uptime), Ok(boot_id)) = (release.data, kernel.data, hostname.data, uptime.data, boot) { let Some(uptime_seconds) = uptime .split_whitespace() .next() .and_then(|x| x.parse::().ok()) .filter(|x| x.is_finite() && *x >= 0.0) else { return Observation::unavailable("linux.os", Quality::Error, "INVALID_UPTIME"); }; let release = release .lines() .filter(|l| !l.starts_with('#')) .filter_map(|line| { line.split_once('=') .map(|(k, v)| (k.into(), v.trim_matches('"').into())) }) .collect(); Observation::valid( "linux.os:/etc/os-release,/proc", OperatingSystem { release, kernel: kernel.trim().into(), architecture: std::env::consts::ARCH.into(), hostname: hostname.trim().into(), boot_id, uptime_seconds, }, ) } else { Observation::unavailable("linux.os", Quality::Error, "OS_SOURCE_UNAVAILABLE") } } pub fn cpu() -> Observation { let info = read("/proc/cpuinfo", 1024 * 1024); let stat = read("/proc/stat", 256 * 1024); for (quality, completeness, truncated) in [ (info.quality, info.completeness, info.truncated), (stat.quality, stat.completeness, stat.truncated), ] { if quality != Quality::Valid || completeness != Completeness::Complete { let mut failed = Observation::unavailable("linux.cpu", quality, "CPU_SOURCE_INCOMPLETE"); failed.truncated = truncated; return failed; } } match (info.data, stat.data) { (Some(info), Some(stat)) => { let logical_processors = info .lines() .filter(|l| { l.split_once(':') .is_some_and(|(k, _)| k.trim() == "processor") }) .count(); let model = info.lines().find_map(|l| { l.split_once(':') .filter(|(k, _)| k.trim() == "model name") .map(|(_, v)| v.trim().to_owned()) }); let aggregate_jiffies: Option> = stat.lines().find(|l| l.starts_with("cpu ")).and_then(|l| { l.split_whitespace() .skip(1) .map(str::parse) .collect::, _>>() .ok() }); match aggregate_jiffies { Some(aggregate_jiffies) if logical_processors > 0 => Observation::valid( "linux.cpu:/proc/cpuinfo,/proc/stat", Cpu { logical_processors, model, aggregate_jiffies, }, ), _ => Observation::unavailable("linux.cpu", Quality::Error, "INVALID_CPU_DATA"), } } _ => Observation::unavailable("linux.cpu", Quality::Error, "CPU_SOURCE_UNAVAILABLE"), } } pub fn parse_memory(text: &str) -> Option { let values: BTreeMap<_, _> = text .lines() .filter_map(|l| { let (k, v) = l.split_once(':')?; let mut fields = v.split_whitespace(); let value = fields.next()?.parse::().ok()?; (fields.next()? == "kB").then_some((k, value.checked_mul(1024)?)) }) .collect(); let memory = Memory { total_bytes: *values.get("MemTotal")?, available_bytes: values.get("MemAvailable").copied(), swap_total_bytes: *values.get("SwapTotal")?, swap_free_bytes: *values.get("SwapFree")?, }; (memory.total_bytes > 0 && memory .available_bytes .is_none_or(|v| v <= memory.total_bytes) && memory.swap_free_bytes <= memory.swap_total_bytes) .then_some(memory) } pub fn memory() -> Observation { let mut observation = map(read("/proc/meminfo", 64 * 1024), |s| parse_memory(&s)); if observation .data .as_ref() .is_some_and(|m| m.available_bytes.is_none()) { observation = observation.partial("MEM_AVAILABLE_UNKNOWN"); } observation } fn sys_text(path: &str) -> Option { read(path, 4096).data.map(|s| s.trim().into()) } fn sys_number(path: &str) -> Option { sys_text(path)?.parse().ok() } pub fn interfaces() -> Observation> { let mut head = std::ptr::null_mut(); // SAFETY: head is writable storage for the libc-owned linked list. if unsafe { libc::getifaddrs(&mut head) } != 0 { return interface_observation(Err(std::io::Error::last_os_error())); } struct Addresses(*mut libc::ifaddrs); impl Drop for Addresses { fn drop(&mut self) { unsafe { libc::freeifaddrs(self.0); } } } let _guard = Addresses(head); let mut entries: BTreeMap> = BTreeMap::new(); let mut current = head; while !current.is_null() { // SAFETY: pointers come from the live getifaddrs list; family determines // sockaddr layout, addresses are copied before freeifaddrs. unsafe { let item = &*current; let name = CStr::from_ptr(item.ifa_name).to_string_lossy().into_owned(); let addresses = entries.entry(name).or_default(); if !item.ifa_addr.is_null() { match i32::from((*item.ifa_addr).sa_family) { libc::AF_INET => { let addr = &*item.ifa_addr.cast::(); addresses .push(Ipv4Addr::from(addr.sin_addr.s_addr.to_ne_bytes()).to_string()); } libc::AF_INET6 => { let addr = &*item.ifa_addr.cast::(); addresses.push(format!( "{}%{}", Ipv6Addr::from(addr.sin6_addr.s6_addr), addr.sin6_scope_id )); } _ => {} } } current = item.ifa_next; } } let data = entries .into_iter() .map(|(name, mut addresses)| { addresses.sort(); addresses.dedup(); let base = format!("/sys/class/net/{name}"); Interface { mac: sys_text(&format!("{base}/address")), operstate: sys_text(&format!("{base}/operstate")), rx_bytes: sys_number(&format!("{base}/statistics/rx_bytes")), tx_bytes: sys_number(&format!("{base}/statistics/tx_bytes")), mtu: sys_number(&format!("{base}/mtu")), name, addresses, } }) .collect::>(); interface_observation(Ok(data)) } fn interface_observation(data: std::io::Result>) -> Observation> { let data = match data { Ok(data) => data, Err(error) => { return Observation::unavailable( "linux.network:getifaddrs", if error.kind() == std::io::ErrorKind::PermissionDenied { Quality::PermissionDenied } else { Quality::Error }, "GETIFADDRS_FAILED", ); } }; // An interface may legitimately have no IP address. Completeness depends // on provider success and readable fields, not on address assignment. let incomplete = data.iter().any(|i| { i.mac.is_none() || i.operstate.is_none() || i.rx_bytes.is_none() || i.tx_bytes.is_none() || i.mtu.is_none() }); let result = Observation::valid("linux.network:getifaddrs,/sys/class/net", data); if incomplete { result.partial("INTERFACE_FIELDS_UNAVAILABLE") } else { result } } #[cfg(test)] mod interface_regressions { use super::*; #[test] fn addressless_interface_is_complete_but_provider_failure_is_not() { let interface = Interface { name: "unconfigured0".into(), addresses: vec![], mac: Some("02:00:00:00:00:01".into()), operstate: Some("down".into()), rx_bytes: Some(0), tx_bytes: Some(0), mtu: Some(1500), }; let observation = interface_observation(Ok(vec![interface.clone()])); assert_eq!(observation.quality, Quality::Valid); assert_eq!(observation.completeness, Completeness::Complete); let failed = interface_observation(Err(std::io::Error::from_raw_os_error(libc::EIO))); assert_eq!(failed.quality, Quality::Error); assert_eq!(failed.completeness, Completeness::Unknown); assert!(failed.data.is_none()); let denied = interface_observation(Err(std::io::Error::from_raw_os_error(libc::EACCES))); assert_eq!(denied.quality, Quality::PermissionDenied); let missing_field = Interface { mtu: None, ..interface }; assert_eq!( interface_observation(Ok(vec![missing_field])).completeness, Completeness::Partial ); } } pub fn routes() -> Observation> { let v4 = read("/proc/net/route", 256 * 1024); let v6 = read("/proc/net/ipv6_route", 256 * 1024); let mut failed = v4.data.is_none() || v6.data.is_none() || v4.truncated || v6.truncated; let mut routes = vec![]; if let Some(text) = v4.data { for line in text.lines().skip(1) { let fields: Vec<_> = line.split_whitespace().collect(); let parsed = (|| { if fields.len() < 8 { return None; } let ip = |x: &str| { u32::from_str_radix(x, 16) .ok() .map(|n| Ipv4Addr::from(n.to_le_bytes()).to_string()) }; Some(Route { interface: fields[0].into(), destination: ip(fields[1])?, gateway: ip(fields[2])?, mask_or_prefix: ip(fields[7])?, flags: u32::from_str_radix(fields[3], 16).ok()?, metric: fields[6].parse().ok()?, }) })(); match parsed { Some(route) => routes.push(route), None => failed = true, } } } if let Some(text) = v6.data { for line in text.lines() { let f: Vec<_> = line.split_whitespace().collect(); let parsed = (|| { if f.len() < 10 { return None; } let ip = |x: &str| { u128::from_str_radix(x, 16) .ok() .map(|n| Ipv6Addr::from(n).to_string()) }; Some(Route { interface: f[9].into(), destination: ip(f[0])?, gateway: ip(f[4])?, mask_or_prefix: u8::from_str_radix(f[1], 16).ok()?.to_string(), flags: u32::from_str_radix(f[8], 16).ok()?, metric: u32::from_str_radix(f[5], 16).ok()?, }) })(); match parsed { Some(route) => routes.push(route), None => failed = true, } } } let result = Observation::valid("linux.routes:/proc/net/route,/proc/net/ipv6_route", routes); if failed { result.partial("ROUTES_INCOMPLETE") } else { result } } pub fn dns() -> Observation { map(read("/etc/resolv.conf", 64 * 1024), |text| { let mut nameservers = vec![]; let mut search = vec![]; for line in text.lines() { let fields: Vec<_> = line .split(['#', ';']) .next() .unwrap_or("") .split_whitespace() .collect(); match fields.first().copied() { Some("nameserver") => { nameservers.extend(fields.iter().skip(1).map(|x| (*x).to_owned())) } Some("search" | "domain") => { search.extend(fields.iter().skip(1).map(|x| (*x).to_owned())) } _ => {} } } Some(Dns { nameservers, search, resolver_symlink: fs::read_link("/etc/resolv.conf") .ok() .map(|p| p.to_string_lossy().into_owned()), }) }) } fn mount_unescape(value: &str) -> Option { let mut result = vec![]; let bytes = value.as_bytes(); let mut i = 0; while i < bytes.len() { if bytes[i] == b'\\' { let octal = bytes.get(i + 1..i + 4)?; if !octal.iter().all(|b| (b'0'..=b'7').contains(b)) { return None; } let value = (octal[0] - b'0') as u16 * 64 + (octal[1] - b'0') as u16 * 8 + (octal[2] - b'0') as u16; result.push(u8::try_from(value).ok()?); i += 4; } else { result.push(bytes[i]); i += 1; } } String::from_utf8(result).ok() } pub fn volumes() -> Observation>> { let mut raw = read("/proc/self/mountinfo", 512 * 1024); if raw .data .as_ref() .is_some_and(|text| text.lines().count() > 4096) { raw = raw.partial("MOUNT_COUNT_LIMIT"); raw.truncated = true; } map(raw, |text| { let mut observations = vec![]; for line in text.lines().take(4096) { let Some((left, right)) = line.split_once(" - ") else { observations.push(Observation::unavailable( "linux.mountinfo", Quality::Error, "INVALID_MOUNT", )); continue; }; let left: Vec<_> = left.split_whitespace().collect(); let right: Vec<_> = right.split_whitespace().collect(); if left.len() < 5 || right.len() < 2 { observations.push(Observation::unavailable( "linux.mountinfo", Quality::Error, "INVALID_MOUNT", )); continue; } let Some(mount) = mount_unescape(left[4]) else { observations.push(Observation::unavailable( "linux.mountinfo", Quality::Error, "INVALID_MOUNT_ENCODING", )); continue; }; observations.push(volume(&mount, right[0], right[1])); } Some(observations) }) .with_volume_completeness() } impl Observation>> { fn with_volume_completeness(mut self) -> Self { if self .data .as_ref() .is_some_and(|v| v.iter().any(|x| x.quality != Quality::Valid)) { self = self.partial("VOLUME_ERRORS"); } self } } fn volume(mount: &str, filesystem: &str, device: &str) -> Observation { // Remote filesystems can hang statvfs indefinitely; explicitly do not probe. const LOCAL: &[&str] = &[ "ext2", "ext3", "ext4", "xfs", "btrfs", "tmpfs", "overlay", "vfat", "exfat", "f2fs", "zfs", "squashfs", "ramfs", "devtmpfs", ]; let source = format!("linux.statvfs:{mount}"); if !LOCAL.contains(&filesystem) { return Observation::unavailable( &source, Quality::Unsupported, "FILESYSTEM_NOT_SAFE_FOR_INLINE_STATVFS", ); } let Ok(path) = CString::new(mount) else { return Observation::unavailable(&source, Quality::Error, "INVALID_MOUNT_PATH"); }; let mut stat = MaybeUninit::::uninit(); // SAFETY: path is terminated and stat is aligned writable storage. if unsafe { libc::statvfs(path.as_ptr(), stat.as_mut_ptr()) } != 0 { let e = std::io::Error::last_os_error(); return Observation::unavailable( &source, if e.kind() == std::io::ErrorKind::PermissionDenied { Quality::PermissionDenied } else { Quality::Error }, "STATVFS_FAILED", ); } let stat = unsafe { stat.assume_init() }; let Some(total_bytes) = stat.f_blocks.checked_mul(stat.f_frsize) else { return Observation::unavailable(&source, Quality::Error, "VOLUME_OVERFLOW"); }; let Some(free_bytes) = stat.f_bfree.checked_mul(stat.f_frsize) else { return Observation::unavailable(&source, Quality::Error, "VOLUME_OVERFLOW"); }; let Some(available_bytes) = stat.f_bavail.checked_mul(stat.f_frsize) else { return Observation::unavailable(&source, Quality::Error, "VOLUME_OVERFLOW"); }; Observation::valid( &source, Volume { mount_point: mount.into(), filesystem: filesystem.into(), device: mount_unescape(device).unwrap_or_else(|| device.into()), total_bytes, free_bytes, available_bytes, available_percent: (total_bytes > 0) .then(|| available_bytes as f64 * 100.0 / total_bytes as f64), total_inodes: stat.f_files, free_inodes: stat.f_ffree, available_inodes: stat.f_favail, available_inode_percent: (stat.f_files > 0) .then(|| stat.f_favail as f64 * 100.0 / stat.f_files as f64), }, ) } fn journal_permission_failure(stderr: &[u8]) -> bool { let s = String::from_utf8_lossy(stderr).to_ascii_lowercase(); s.contains("permission denied") || s.contains("access denied") || s.contains("insufficient permissions") || s.contains("not seeing messages from other users") } fn command(program: &str, args: &[&str], cap: usize) -> Observation { let started = SystemTime::now(); match process::run( program, &args.iter().map(|s| (*s).into()).collect::>(), Duration::from_secs(15), cap, ) { Ok(output) => { if output.timed_out { return Observation::unavailable(program, Quality::Unknown, "PROVIDER_TIMEOUT"); } if program == "/usr/bin/journalctl" && journal_permission_failure(&output.stderr) { return Observation::unavailable( "linux.journald", Quality::PermissionDenied, "JOURNAL_ACCESS_DENIED_OR_FILTERED", ); } if output.code != Some(0) { let stderr = String::from_utf8_lossy(&output.stderr); let quality = if stderr.contains("Permission denied") || stderr.contains("Access denied") { Quality::PermissionDenied } else { Quality::Error }; return Observation::unavailable(program, quality, "PROVIDER_EXIT_FAILURE"); } match String::from_utf8(output.stdout) { Ok(text) => { let mut observation = Observation::valid(program, text); observation.observed_at = started; if output.truncated { observation = observation.partial("OUTPUT_LIMIT"); observation.truncated = true; } observation } Err(_) => Observation::unavailable(program, Quality::Error, "NON_UTF8_OUTPUT"), } } Err(e) => { let quality = e .downcast_ref::() .map(|e| match e.kind() { std::io::ErrorKind::NotFound => Quality::Unsupported, std::io::ErrorKind::PermissionDenied => Quality::PermissionDenied, _ => Quality::Error, }) .unwrap_or(Quality::Error); Observation::unavailable(program, quality, "PROVIDER_UNAVAILABLE") } } } pub fn software() -> Observation> { let raw = command( "/usr/bin/dpkg-query", &[ "--show", "--showformat=${Package}\t${Version}\t${Architecture}\t${db:Status-Status}\\n", ], 512 * 1024, ); let mut parse_failure = false; let mut result = map(raw, |text| { let packages = text .lines() .filter_map(|line| { let f: Vec<_> = line.split('\t').collect(); if f.len() != 4 { parse_failure = true; return None; } (f[3] == "installed").then(|| SoftwarePackage { name: f[0].into(), version: f[1].into(), architecture: f[2].into(), status: f[3].into(), }) }) .collect(); Some(packages) }); if parse_failure { result = result.partial("DPKG_PARSE_PARTIAL"); } result } pub fn service_status(unit: &ServiceUnit) -> Observation { if !Path::new("/run/systemd/system").is_dir() { return Observation::unavailable( "linux.systemd", Quality::Unsupported, "SYSTEMD_NOT_RUNNING", ); } let raw = command( "/usr/bin/systemctl", &[ "show", "--no-pager", "--property=Id,LoadState,ActiveState,SubState,UnitFileState", "--", unit.as_str(), ], 32 * 1024, ); map(raw, |text| { let f: BTreeMap<_, _> = text.lines().filter_map(|l| l.split_once('=')).collect(); Some(ServiceStatus { unit: unit.clone(), id: f.get("Id")?.to_string(), load_state: f.get("LoadState")?.to_string(), active_state: f.get("ActiveState")?.to_string(), sub_state: f.get("SubState")?.to_string(), unit_file_state: f.get("UnitFileState")?.to_string(), }) }) } pub fn check_services(expectations: &[ServiceExpectation]) -> anyhow::Result> { anyhow::ensure!( expectations.len() <= 128, "too many service expectations; maximum 128" ); Ok(check_services_inner(expectations)) } fn check_services_inner(expectations: &[ServiceExpectation]) -> Vec { expectations .iter() .take(128) .map(|expectation| { let status = service_status(&expectation.unit); let matches = status .data .as_ref() .filter(|s| s.id == expectation.unit.as_str() && s.load_state == "loaded") .map(|s| { if expectation.expected_active { s.active_state == "active" } else { s.active_state == "inactive" } }); ServiceCheck { expectation: expectation.clone(), status, matches, } }) .collect() } pub fn updates() -> Observation { // Both commands read existing metadata only; never `apt update`. APT's list // output is not a stable API, so parsing is checked and marked partial. let policy = command("/usr/bin/apt-cache", &["policy"], 64 * 1024); if policy.data.is_none() { return Observation::unavailable( "linux.apt-local", policy.quality, "APT_CACHE_UNAVAILABLE", ); } let raw = command("/usr/bin/apt", &["list", "--upgradable"], 256 * 1024); let mut parse_failure = false; let mut result = map(raw, |text| { let mut available = vec![]; for line in text .lines() .filter(|l| !l.is_empty() && !l.starts_with("Listing")) { let fields: Vec<_> = line.split_whitespace().collect(); if fields.len() < 3 || !fields[0].contains('/') { parse_failure = true; continue; } available.push(AvailableUpdate { package: fields[0].split('/').next().unwrap_or("").into(), candidate_version: fields[1].into(), architecture: fields[2].into(), installed_version: line .split_once("upgradable from: ") .and_then(|(_, v)| v.strip_suffix(']')) .map(str::to_owned), }); } let newest_local_metadata_at = fs::read_dir("/var/lib/apt/lists") .ok() .into_iter() .flatten() .filter_map(Result::ok) .filter_map(|entry| { let name = entry.file_name(); let name = name.to_string_lossy(); if name.ends_with("_InRelease") || name.ends_with("_Release") || name.contains("_Packages") { entry.metadata().ok()?.modified().ok() } else { None } }) .max(); Some(UpdateStatus { available, newest_local_metadata_at, last_successful_scan_at: None, metadata_fresh: None, reboot_required: reboot_required(), history_quality: Quality::Unknown, }) }); result.source_id = "linux.apt-local:apt-cache-policy,apt-list,/var/lib/apt/lists".into(); if result.data.is_some() { result = result.partial(if parse_failure { "APT_PARSE_PARTIAL" } else { "METADATA_FRESHNESS_AND_HISTORY_UNKNOWN" }); } result } pub fn hardware() -> Observation { let mut readable_dmi = BTreeMap::new(); for field in [ "sys_vendor", "product_name", "product_version", "board_vendor", "board_name", "bios_vendor", "bios_version", "bios_date", ] { if let Some(value) = sys_text(&format!("/sys/class/dmi/id/{field}")) { readable_dmi.insert(field.into(), value); } } if readable_dmi.is_empty() { Observation::unavailable( "linux.dmi:/sys/class/dmi/id", Quality::Unsupported, "DMI_UNAVAILABLE", ) } else { Observation::valid("linux.dmi:/sys/class/dmi/id", Hardware { readable_dmi }) .partial("READABLE_DMI_ONLY_NO_SMART_OR_ENCRYPTION_PROOF") } } #[derive(Debug, Clone)] pub struct JournalRequest { unit: ServiceUnit, since_epoch: u64, until_epoch: u64, max_lines: usize, max_bytes: usize, since_filter: String, until_filter: String, } impl JournalRequest { pub fn new( unit: ServiceUnit, allowed_units: &[ServiceUnit], since_epoch: u64, until_epoch: u64, max_lines: usize, max_bytes: usize, ) -> anyhow::Result { anyhow::ensure!(allowed_units.contains(&unit), "log unit not allowlisted"); anyhow::ensure!( until_epoch >= since_epoch && until_epoch - since_epoch <= 86400 && until_epoch <= i64::MAX as u64, "invalid log window (maximum 24h)" ); anyhow::ensure!( (1..=1000).contains(&max_lines) && (1..=256 * 1024).contains(&max_bytes), "invalid log limits" ); Ok(Self { unit, since_epoch, until_epoch, max_lines, max_bytes, since_filter: format!("@{since_epoch}"), until_filter: format!("@{until_epoch}"), }) } /// Journald timestamps are microseconds. Round inward, never widen the /// signed RFC3339 interval by flooring a fractional start to whole seconds. pub fn precise_window( mut self, since: chrono::DateTime, until: chrono::DateTime, ) -> anyhow::Result { anyhow::ensure!( u64::try_from(since.timestamp())? == self.since_epoch && u64::try_from(until.timestamp())? == self.until_epoch, "LOG_WINDOW_BINDING" ); let start = since .timestamp_micros() .checked_add(i64::from( !since.timestamp_subsec_nanos().is_multiple_of(1000), )) .ok_or_else(|| anyhow::anyhow!("LOG_TIMESTAMP_OVERFLOW"))?; let end = until.timestamp_micros(); anyhow::ensure!(start <= end, "LOG_WINDOW_BELOW_NATIVE_PRECISION"); self.since_filter = format!("@{}.{:06}", start / 1_000_000, start % 1_000_000); self.until_filter = format!("@{}.{:06}", end / 1_000_000, end % 1_000_000); Ok(self) } } #[derive(Debug, Clone, serde::Serialize)] pub struct JournalExcerpt { pub unit: ServiceUnit, pub since_epoch: u64, pub until_epoch: u64, pub since_filter: String, pub until_filter: String, pub json_lines: Vec, pub next_cursor: Option, } /// This is live diagnosis, NOT called by collect_native(). The helper must /// require the same current plan-bound authorization as all other live jobs. pub fn query_journal(request: &JournalRequest) -> Observation { if !Path::new("/run/systemd/journal").is_dir() && !Path::new("/var/log/journal").is_dir() { return Observation::unavailable( "linux.journald", Quality::Unsupported, "JOURNAL_UNAVAILABLE", ); } let since = &request.since_filter; let until = &request.until_filter; // Ask for one extra line to detect the line cap; bounded subprocess output // also enforces a combined stdout+stderr byte cap. let count = (request.max_lines + 1).to_string(); let raw = command( "/usr/bin/journalctl", &[ "--no-pager", "--quiet", "--output=json", "--unit", request.unit.as_str(), "--since", since.as_str(), "--until", until.as_str(), "--lines", &count, ], request.max_bytes, ); let mut line_truncated = false; let mut parse_failure = false; let mut result = map(raw, |text| { let mut lines: Vec<_> = text .lines() .filter_map( |line| match serde_json::from_str::(line) { Ok(value) if value.is_object() => Some(line.to_owned()), _ => { parse_failure = true; None } }, ) .collect(); // journalctl -n returns the newest lines. Do not invent a durable cursor // without a JSON parser and server acknowledgement integration. line_truncated = lines.len() > request.max_lines; if line_truncated { lines.remove(0); } Some(JournalExcerpt { unit: request.unit.clone(), since_epoch: request.since_epoch, until_epoch: request.until_epoch, since_filter: request.since_filter.clone(), until_filter: request.until_filter.clone(), json_lines: lines, next_cursor: None, }) }); if line_truncated { result = result.partial("LOG_LINE_LIMIT"); result.truncated = true; } if parse_failure { result = result.partial("JOURNAL_JSON_INVALID_OR_TRUNCATED"); result.truncated = true; if result .data .as_ref() .is_none_or(|excerpt| excerpt.json_lines.is_empty()) { result.quality = Quality::Error; result.completeness = Completeness::Unknown; result.data = None; } } // A readable empty journal is not proof of complete coverage/permissions. if result.data.is_some() && !parse_failure && !line_truncated { result = result.partial("JOURNAL_ACCESS_COVERAGE_AND_CURSOR_UNVERIFIED"); } result } #[cfg(test)] mod tests { use super::*; #[test] fn memory_units_and_mount_escapes() { let memory = parse_memory( "MemTotal: 100 kB\nMemAvailable: 25 kB\nSwapTotal: 10 kB\nSwapFree: 3 kB\n", ) .unwrap(); assert_eq!(memory.total_bytes, 102400); assert_eq!(memory.available_bytes, Some(25600)); assert_eq!(memory.swap_free_bytes, 3072); assert!(parse_memory("MemTotal: 18446744073709551615 kB").is_none()); assert_eq!(mount_unescape("/a\\040b\\134c").as_deref(), Some("/a b\\c")); assert!(mount_unescape("/a\\999").is_none()); } #[test] fn journal_limits_and_allowlist() { assert!(journal_permission_failure( b"Hint: You are currently not seeing messages from other users and the system." )); assert!(journal_permission_failure( b"No journal files were opened due to insufficient permissions." )); assert!(!journal_permission_failure(b"-- No entries --")); let unit = ServiceUnit::new("demo.service").unwrap(); assert!(JournalRequest::new(unit.clone(), &[], 0, 60, 10, 100).is_err()); assert!( JournalRequest::new(unit.clone(), std::slice::from_ref(&unit), 60, 0, 10, 100).is_err() ); assert!( JournalRequest::new(unit.clone(), std::slice::from_ref(&unit), 0, 60, 1001, 100) .is_err() ); assert!(JournalRequest::new(unit.clone(), &[unit], 0, 60, 10, 100).is_ok()); } #[test] fn journal_fractional_window_is_rounded_inward_not_widened() { let unit = ServiceUnit::new("demo.service").unwrap(); let request = JournalRequest::new( unit.clone(), std::slice::from_ref(&unit), 100, 101, 10, 1024, ) .unwrap() .precise_window( chrono::DateTime::from_timestamp(100, 123_456_789).unwrap(), chrono::DateTime::from_timestamp(101, 987_654_999).unwrap(), ) .unwrap(); assert_eq!(request.since_filter, "@100.123457"); assert_eq!(request.until_filter, "@101.987654"); } #[test] fn harmless_native_read_providers() { assert_eq!(memory().quality, Quality::Valid); assert_eq!(cpu().quality, Quality::Valid); assert_eq!(operating_system().quality, Quality::Valid); let interfaces = interfaces(); assert!(interfaces.data.is_some()); let volumes = volumes(); assert!(volumes.data.as_ref().is_some_and(|v| { v.iter() .any(|x| x.data.as_ref().is_some_and(|v| v.mount_point == "/")) })); let software = software(); assert!(matches!( software.quality, Quality::Valid | Quality::Unsupported )); let status = service_status(&ServiceUnit::new("nonexistent-rmm-test.service").unwrap()); if !Path::new("/run/systemd/system").is_dir() { assert_eq!(status.quality, Quality::Unsupported); } assert_eq!(hardware().completeness, Completeness::Partial); let update_status = updates(); assert!(matches!( update_status.quality, Quality::Valid | Quality::Unsupported )); if let Some(data) = update_status.data { assert_eq!(data.metadata_fresh, None); assert_eq!(data.last_successful_scan_at, None); } } }