73 lines
2.4 KiB
Rust
73 lines
2.4 KiB
Rust
use anyhow::{Context, Result, ensure};
|
|
use rmm_domain::Inventory;
|
|
use std::{ffi::CString, fs, mem::MaybeUninit};
|
|
|
|
pub mod actions;
|
|
pub mod inventory;
|
|
mod process;
|
|
pub mod wire;
|
|
|
|
/// Fixed helper metadata program only. Bounded and killable because this mode
|
|
/// cannot launch APT or cause package effects; never use for transaction drivers.
|
|
pub fn apt_planning_metadata() -> Result<Vec<u8>> {
|
|
let output = process::run(
|
|
"/usr/bin/python3",
|
|
&[
|
|
"-I".into(),
|
|
"/usr/lib/rmm-apt-transaction/apt_transaction.py".into(),
|
|
"planning-status".into(),
|
|
],
|
|
std::time::Duration::from_secs(4),
|
|
32768,
|
|
)?;
|
|
ensure!(
|
|
output.code == Some(0) && !output.timed_out && !output.truncated,
|
|
"APT_PLANNING_UNAVAILABLE"
|
|
);
|
|
Ok(output.stdout)
|
|
}
|
|
|
|
/// Read-only local collection. No shell, package manager or hypervisor access.
|
|
pub fn collect() -> Result<Inventory> {
|
|
let os = fs::read_to_string("/etc/os-release").context("read OS release")?;
|
|
let hostname = fs::read_to_string("/proc/sys/kernel/hostname")?;
|
|
let path = CString::new("/")?;
|
|
let mut stat = MaybeUninit::<libc::statvfs>::uninit();
|
|
// SAFETY: path is NUL terminated; stat points to writable, aligned storage.
|
|
ensure!(
|
|
unsafe { libc::statvfs(path.as_ptr(), stat.as_mut_ptr()) } == 0,
|
|
"statvfs failed"
|
|
);
|
|
// SAFETY: successful statvfs initialized the structure.
|
|
let stat = unsafe { stat.assume_init() };
|
|
let inventory = Inventory {
|
|
os,
|
|
hostname: hostname.trim().to_owned(),
|
|
architecture: std::env::consts::ARCH.to_owned(),
|
|
root_total_bytes: stat
|
|
.f_blocks
|
|
.checked_mul(stat.f_frsize)
|
|
.context("disk size overflow")?,
|
|
root_available_bytes: stat
|
|
.f_bavail
|
|
.checked_mul(stat.f_frsize)
|
|
.context("disk free overflow")?,
|
|
root_available_inodes: stat.f_favail,
|
|
};
|
|
ensure!(inventory.validate(), "invalid local inventory");
|
|
Ok(inventory)
|
|
}
|
|
pub fn boot_id() -> Result<String> {
|
|
let id = fs::read_to_string("/proc/sys/kernel/random/boot_id")?;
|
|
Ok(uuid::Uuid::parse_str(id.trim())?.to_string())
|
|
}
|
|
#[cfg(test)]
|
|
mod tests {
|
|
#[test]
|
|
fn native_linux_inventory() {
|
|
let i = super::collect().unwrap();
|
|
assert!(i.validate());
|
|
assert!(!super::boot_id().unwrap().is_empty());
|
|
}
|
|
}
|