From c79ff22a987ea5c9621db88df5ec638439011b3e Mon Sep 17 00:00:00 2001 From: Bruce Leidl Date: Mon, 19 Sep 2022 10:05:33 -0400 Subject: [PATCH] implement RealmFromCitadelPid in realmsd --- Cargo.lock | 36 ++++++++++++++++++++++++++++ libcitadel/Cargo.toml | 1 + realm-config-ui/src/realmsd.rs | 2 +- realmsd/Cargo.toml | 1 + realmsd/src/events.rs | 4 ++-- realmsd/src/realms_manager.rs | 43 ++++++++++++++++++++++++++-------- 6 files changed, 74 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c504b94..fc30027 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -382,6 +382,15 @@ dependencies = [ "libc", ] +[[package]] +name = "crc32fast" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" +dependencies = [ + "cfg-if 1.0.0", +] + [[package]] name = "crossbeam-channel" version = "0.3.9" @@ -614,6 +623,16 @@ dependencies = [ "rustc_version", ] +[[package]] +name = "flate2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f82b0f4c27ad9f8bfd1f3208d882da2b09c301bc1c828fd3a00d0216d2fbbff6" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + [[package]] name = "futures-channel" version = "0.3.21" @@ -1098,6 +1117,7 @@ dependencies = [ "libc", "nix 0.17.0", "posix-acl", + "procfs", "serde", "serde_derive", "sodiumoxide", @@ -1477,6 +1497,21 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "procfs" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0941606b9934e2d98a3677759a971756eb821f75764d0e0d26946d08e74d9104" +dependencies = [ + "bitflags", + "byteorder", + "chrono", + "flate2", + "hex", + "lazy_static", + "libc", +] + [[package]] name = "pwhash" version = "0.3.1" @@ -1599,6 +1634,7 @@ version = "0.1.0" dependencies = [ "libcitadel", "serde", + "serde_repr", "zbus", "zvariant", ] diff --git a/libcitadel/Cargo.toml b/libcitadel/Cargo.toml index 8770628..5eb3829 100644 --- a/libcitadel/Cargo.toml +++ b/libcitadel/Cargo.toml @@ -18,6 +18,7 @@ bincode = "1.2" walkdir = "2" dbus = "0.6" posix-acl = "1.0.0" +procfs = "0.12.0" [dependencies.inotify] version = "0.8" diff --git a/realm-config-ui/src/realmsd.rs b/realm-config-ui/src/realmsd.rs index 1f14b4e..710678e 100644 --- a/realm-config-ui/src/realmsd.rs +++ b/realm-config-ui/src/realmsd.rs @@ -12,7 +12,7 @@ pub struct RealmItem { name: String, description: String, realmfs: String, - namespace: String, + namespace: u64, status: u8, } diff --git a/realmsd/Cargo.toml b/realmsd/Cargo.toml index 3054e37..a43574e 100644 --- a/realmsd/Cargo.toml +++ b/realmsd/Cargo.toml @@ -9,4 +9,5 @@ libcitadel = { path = "../libcitadel" } zbus = "=2.0.0-beta.5" zvariant = "2.7.0" serde = { version = "1.0", features = ["derive"] } +serde_repr = "0.1.8" diff --git a/realmsd/src/events.rs b/realmsd/src/events.rs index 7083fb1..20ff44d 100644 --- a/realmsd/src/events.rs +++ b/realmsd/src/events.rs @@ -38,9 +38,9 @@ impl EventHandler { } fn on_started(&self, realm: &Realm) -> zbus::Result<()> { - let namespace = realm.pid_namespace().unwrap_or(String::new()); + let pid_ns = realm.pid_ns().unwrap_or(0); let status = realm_status(realm); - self.with_server(|server| server.realm_started(realm.name(), namespace.as_str(), status)) + self.with_server(|server| server.realm_started(realm.name(), pid_ns, status)) } fn on_stopped(&self, realm: &Realm) -> zbus::Result<()> { diff --git a/realmsd/src/realms_manager.rs b/realmsd/src/realms_manager.rs index e4ddda6..be2ce02 100644 --- a/realmsd/src/realms_manager.rs +++ b/realmsd/src/realms_manager.rs @@ -1,15 +1,41 @@ -use libcitadel::{RealmManager, Realm, OverlayType, Result}; +use libcitadel::{RealmManager, Realm, OverlayType, Result, PidLookupResult}; use std::sync::Arc; use zbus::{dbus_interface, ObjectServer,Connection}; use zvariant::derive::Type; use std::thread; use std::collections::HashMap; use serde::{Serialize,Deserialize}; +use serde_repr::Serialize_repr; use crate::events::EventHandler; use libcitadel::terminal::Base16Scheme; pub const REALMS_SERVER_OBJECT_PATH: &str = "/com/subgraph/realms"; +#[derive(Serialize_repr, Type, Debug, PartialEq)] +#[repr(u32)] +pub enum PidLookupResultCode { + Unknown = 1, + Realm = 2, + Citadel = 3, +} + +#[derive(Debug, Type, Serialize)] +pub struct RealmFromCitadelPid { + code: PidLookupResultCode, + realm: String, +} + +impl From for RealmFromCitadelPid { + fn from(result: PidLookupResult) -> Self { + match result { + PidLookupResult::Unknown => RealmFromCitadelPid { code: PidLookupResultCode::Unknown, realm: String::new() }, + PidLookupResult::Realm(realm) => RealmFromCitadelPid { code: PidLookupResultCode::Realm, realm: realm.name().to_string() }, + PidLookupResult::Citadel => RealmFromCitadelPid { code: PidLookupResultCode::Citadel, realm: String::new() }, + } + } +} + + #[derive(Clone)] pub struct RealmsManagerServer { manager: Arc, @@ -223,11 +249,8 @@ impl RealmsManagerServer { }); } - fn realm_from_citadel_pid(&self, pid: u32) -> String { - match self.manager.realm_by_pid(pid) { - Some(r) => r.name().to_string(), - None => String::new(), - } + fn realm_from_citadel_pid(&self, pid: u32) -> RealmFromCitadelPid { + self.manager.realm_by_pid(pid).into() } fn realm_config(&self, name: &str) -> RealmConfig { @@ -277,7 +300,7 @@ impl RealmsManagerServer { } #[dbus_interface(signal)] - pub fn realm_started(&self, realm: &str, namespace: &str, status: u8) -> zbus::Result<()> { Ok(()) } + pub fn realm_started(&self, realm: &str, pid_ns: u64, status: u8) -> zbus::Result<()> { Ok(()) } #[dbus_interface(signal)] pub fn realm_stopped(&self, realm: &str, status: u8) -> zbus::Result<()> { Ok(()) } @@ -319,7 +342,7 @@ struct RealmItem { name: String, description: String, realmfs: String, - namespace: String, + pid_ns: u64, status: u8, } @@ -328,9 +351,9 @@ impl RealmItem { let name = realm.name().to_string(); let description = realm.notes().unwrap_or(String::new()); let realmfs = realm.config().realmfs().to_string(); - let namespace = realm.pid_namespace().unwrap_or(String::new()); + let pid_ns = realm.pid_ns().unwrap_or_default(); let status = realm_status(realm); - RealmItem { name, description, realmfs, namespace, status } + RealmItem { name, description, realmfs, pid_ns, status } } }