implement RealmFromCitadelPid in realmsd

This commit is contained in:
Bruce Leidl 2022-09-19 10:05:33 -04:00
parent 1136cee2f7
commit c79ff22a98
6 changed files with 74 additions and 13 deletions

36
Cargo.lock generated
View File

@ -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",
]

View File

@ -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"

View File

@ -12,7 +12,7 @@ pub struct RealmItem {
name: String,
description: String,
realmfs: String,
namespace: String,
namespace: u64,
status: u8,
}

View File

@ -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"

View File

@ -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<()> {

View File

@ -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<PidLookupResult> 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<RealmManager>,
@ -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 }
}
}