forked from HULKs/hulk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_manager.rs
More file actions
128 lines (114 loc) · 3.47 KB
/
Copy pathservice_manager.rs
File metadata and controls
128 lines (114 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use std::fmt::Display;
use regex::Regex;
use serde::{Deserialize, Serialize};
use zbus::{
zvariant::{OwnedObjectPath, Value},
Connection, Error, Proxy,
};
#[derive(Debug)]
enum Service {
Hal,
Hulk,
Lola,
}
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub enum ServiceState {
Activating,
Active,
Deactivating,
Failed,
Inactive,
NotLoaded,
Reloading,
Unknown,
}
impl From<&str> for ServiceState {
fn from(string: &str) -> Self {
match string {
"activating" => ServiceState::Activating,
"active" => ServiceState::Active,
"deactivating" => ServiceState::Deactivating,
"failed" => ServiceState::Failed,
"inactive" => ServiceState::Inactive,
"reloading" => ServiceState::Reloading,
_ => ServiceState::Unknown,
}
}
}
impl Display for ServiceState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ServiceState::Activating => write!(f, "Activating"),
ServiceState::Active => write!(f, "Active"),
ServiceState::Deactivating => write!(f, "Deactivating"),
ServiceState::Failed => write!(f, "Failed"),
ServiceState::Inactive => write!(f, "Inactive"),
ServiceState::NotLoaded => write!(f, "NotLoaded"),
ServiceState::Reloading => write!(f, "Reloading"),
ServiceState::Unknown => write!(f, "Unknown"),
}
}
}
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct SystemServices {
pub hal: ServiceState,
pub hulk: ServiceState,
pub lola: ServiceState,
}
impl SystemServices {
pub async fn query(dbus_connection: &Connection) -> Result<Self, Error> {
Ok(Self {
hal: get_service_state(dbus_connection, Service::Hal).await?,
hulk: get_service_state(dbus_connection, Service::Hulk).await?,
lola: get_service_state(dbus_connection, Service::Lola).await?,
})
}
}
async fn get_unit_path(
dbus_connection: &Connection,
service: Service,
) -> Result<OwnedObjectPath, Error> {
let service_name = match service {
Service::Hal => "hal.service",
Service::Hulk => "hulk.service",
Service::Lola => "lola.service",
};
let proxy = Proxy::new(
dbus_connection,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
)
.await?;
proxy.call("GetUnit", &(service_name,)).await
}
async fn get_service_state(
dbus_connection: &Connection,
service: Service,
) -> Result<ServiceState, Error> {
let regex = Regex::new(r"Unit \w+\.service not loaded").unwrap();
let unit_path = match get_unit_path(dbus_connection, service).await {
Ok(unit_path) => unit_path,
Err(Error::MethodError(_, Some(message), _)) if regex.is_match(&message) => {
return Ok(ServiceState::NotLoaded);
}
Err(error) => return Err(error),
};
let proxy = Proxy::new(
dbus_connection,
"org.freedesktop.systemd1",
unit_path,
"org.freedesktop.DBus.Properties",
)
.await?;
if let Value::Str(state) = proxy
.call_method("Get", &("org.freedesktop.systemd1.Unit", "ActiveState"))
.await?
.body()
.deserialize()?
{
Ok(ServiceState::from(state.as_str()))
} else {
Err(Error::Failure("failed to get state".to_owned()))
}
}