monitord/varlink/
machine.rs1use serde::{Deserialize, Serialize};
7use zlink::{proxy, ReplyError};
8
9use crate::varlink::unit::ProcessId;
10
11pub const MACHINE_SOCKET_PATH: &str = "/run/systemd/machine/io.systemd.Machine";
12
13#[proxy("io.systemd.Machine")]
15pub trait Machine {
16 #[zlink(more)]
19 async fn list(
20 &mut self,
21 ) -> zlink::Result<
22 impl futures_util::Stream<Item = zlink::Result<Result<ListOutput, MachineError>>>,
23 >;
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct ListOutput {
31 pub name: String,
33 pub class: String,
35 pub leader: Option<ProcessId>,
37}
38
39#[derive(Debug, Clone, PartialEq, ReplyError)]
41#[zlink(interface = "io.systemd.Machine")]
42pub enum MachineError {
43 NoSuchMachine,
45 NotSupported,
47}
48
49impl std::fmt::Display for MachineError {
50 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51 match self {
52 MachineError::NoSuchMachine => write!(f, "No such machine"),
53 MachineError::NotSupported => write!(f, "Not supported"),
54 }
55 }
56}
57
58impl std::error::Error for MachineError {}
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63
64 #[test]
65 fn test_list_output_deserialize() {
66 let json = r#"{"name":"testbox","id":"76c418060ef042fdb8b579e755988736","class":"container","service":"systemd-nspawn","unit":"systemd-nspawn@testbox.service","leader":{"pid":7430,"pidfdId":5367,"bootId":"6e68bb3ad68f43838ea71e746325e67b"},"timestamp":{"realtime":1789561088882217,"monotonic":86182609},"UID":0}"#;
68 let machine: ListOutput = serde_json::from_str(json).unwrap();
69 assert_eq!(machine.name, "testbox");
70 assert_eq!(machine.class, "container");
71 assert_eq!(machine.leader.and_then(|l| l.pid), Some(7430));
72 }
73}