sd_switch/systemd/
mod.rs

1pub mod dbus;
2mod dbus_manager;
3mod dbus_unit;
4pub mod ini;
5pub mod systemctl;
6
7use crate::error::Error;
8use std::{fmt::Display, result::Result, str::FromStr, time::Duration};
9
10pub enum SystemStatus {
11    Initializing,
12    Starting,
13    Running,
14    Degraded,
15    Maintenance,
16    Stopping,
17}
18
19impl FromStr for SystemStatus {
20    type Err = Error;
21
22    fn from_str(s: &str) -> Result<Self, Self::Err> {
23        match s {
24            "initializing" => Ok(SystemStatus::Initializing),
25            "starting" => Ok(SystemStatus::Starting),
26            "running" => Ok(SystemStatus::Running),
27            "degraded" => Ok(SystemStatus::Degraded),
28            "maintenance" => Ok(SystemStatus::Maintenance),
29            "stopping" => Ok(SystemStatus::Stopping),
30            _ => Err(Error::SdSwitch(format!("invalid system status {s}"))),
31        }
32    }
33}
34
35impl Display for SystemStatus {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        match self {
38            SystemStatus::Initializing => write!(f, "initializing"),
39            SystemStatus::Starting => write!(f, "starting"),
40            SystemStatus::Running => write!(f, "running"),
41            SystemStatus::Degraded => write!(f, "degraded"),
42            SystemStatus::Maintenance => write!(f, "maintenance"),
43            SystemStatus::Stopping => write!(f, "stopping"),
44        }
45    }
46}
47
48pub trait UnitStatus {
49    fn name(&self) -> &str;
50    fn description(&self) -> &str;
51    fn active_state(&self) -> &str;
52}
53
54pub trait JobSet {
55    fn reload_unit(&mut self, unit_name: &str) -> Result<(), Error>;
56
57    fn restart_unit(&mut self, unit_name: &str) -> Result<(), Error>;
58
59    fn start_unit(&mut self, unit_name: &str) -> Result<(), Error>;
60
61    fn stop_unit(&mut self, unit_name: &str) -> Result<(), Error>;
62
63    fn wait_for_all<F>(&mut self, job_handler: F, timeout: Duration) -> Result<(), Error>
64    where
65        F: Fn(&str, &str) + Send + 'static;
66}
67
68pub trait ServiceManager {
69    type JobSet: JobSet;
70    type UnitManager: UnitManager;
71    type UnitStatus: UnitStatus;
72
73    /// Checks whether this service manager has a working connection to systemd
74    /// and that systemd is running.
75    fn system_status(&self) -> Result<SystemStatus, Error>;
76
77    /// Performs a systemd daemon reload, blocking until complete.
78    fn daemon_reload(&self) -> Result<(), Error>;
79
80    fn reset_failed(&self) -> Result<(), Error>;
81
82    /// Builds a unit manager for the unit with the given address.
83    fn unit_manager(&self, address: &Self::UnitStatus) -> Result<Self::UnitManager, Error>;
84
85    fn new_job_set(&self) -> Result<Self::JobSet, Error>;
86
87    fn list_units_by_states(&self, states: &[&str]) -> Result<Vec<Self::UnitStatus>, Error>;
88}
89
90pub trait UnitManager {
91    fn refuse_manual_start(&self) -> Result<bool, Error>;
92
93    fn refuse_manual_stop(&self) -> Result<bool, Error>;
94}