提交 e89669cc authored 作者: Serhij S's avatar Serhij S

system state

上级 ce45994c
...@@ -71,7 +71,6 @@ rflow = ["dep:rflow"] ...@@ -71,7 +71,6 @@ rflow = ["dep:rflow"]
modbus = ["dep:rmodbus"] modbus = ["dep:rmodbus"]
metrics = ["dep:metrics", "dep:metrics-exporter-prometheus", "dep:metrics-exporter-scope", "dep:tokio"] metrics = ["dep:metrics", "dep:metrics-exporter-prometheus", "dep:metrics-exporter-scope", "dep:tokio"]
async = ["dep:parking_lot_rt"] async = ["dep:parking_lot_rt"]
full = ["eapi", "modbus", "metrics", "pipe", "rvideo", "rflow", "async", "json", "msgpack", "hmi", "input-events"]
hmi = ["dep:egui", "dep:eframe", "dep:winit", "dep:once_cell"] hmi = ["dep:egui", "dep:eframe", "dep:winit", "dep:once_cell"]
input-events = ["dep:evdev"] input-events = ["dep:evdev"]
...@@ -84,6 +83,9 @@ msgpack = ["dep:rmp-serde"] ...@@ -84,6 +83,9 @@ msgpack = ["dep:rmp-serde"]
default = ["locking-default"] default = ["locking-default"]
full = ["eapi", "modbus", "metrics", "pipe", "rvideo", "rflow", "async", "json", "msgpack",
"hmi", "input-events"]
[dev-dependencies] [dev-dependencies]
insta = "1.36.1" insta = "1.36.1"
log = "0.4.21" log = "0.4.21"
......
...@@ -107,7 +107,7 @@ pub mod hub_async; ...@@ -107,7 +107,7 @@ pub mod hub_async;
pub mod io; pub mod io;
/// Task supervisor to manage real-time threads /// Task supervisor to manage real-time threads
pub mod supervisor; pub mod supervisor;
/// Linux systme tools /// Linux system tools
pub mod system; pub mod system;
/// Real-time thread functions to work with [`supervisor::Supervisor`] and standalone, Linux only /// Real-time thread functions to work with [`supervisor::Supervisor`] and standalone, Linux only
pub mod thread_rt; pub mod thread_rt;
......
use crate::{is_realtime, Result}; use crate::{is_realtime, Result};
use core::fmt; use core::fmt;
use std::convert::Infallible;
use std::str::FromStr;
/// Configure system parameters (global) while the process is running. Does nothing in simulated /// Configure system parameters (global) while the process is running. Does nothing in simulated
/// mode. A wrapper around [`rtsc::system::linux::SystemConfig`] which respects simulated/real-time /// mode. A wrapper around [`rtsc::system::linux::SystemConfig`] which respects simulated/real-time
...@@ -63,3 +65,92 @@ impl CpuGovernor { ...@@ -63,3 +65,92 @@ impl CpuGovernor {
} }
} }
} }
/// Standard systemd system state variants.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum StateVariant {
/// The system is initializing.
Initializing,
/// The system is starting.
Starting,
/// The system is running.
Running,
/// The system is degraded.
Degraded,
/// The system is in maintenance mode.
Maintenance,
/// The system is stopping.
Stopping,
/// The system is in some other state.
Other,
}
impl fmt::Display for StateVariant {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
StateVariant::Initializing => write!(f, "initializing"),
StateVariant::Starting => write!(f, "starting"),
StateVariant::Running => write!(f, "running"),
StateVariant::Degraded => write!(f, "degraded"),
StateVariant::Maintenance => write!(f, "maintenance"),
StateVariant::Stopping => write!(f, "stopping"),
StateVariant::Other => write!(f, "other"),
}
}
}
impl FromStr for StateVariant {
type Err = Infallible;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
Ok(match s {
"initializing" => StateVariant::Initializing,
"starting" => StateVariant::Starting,
"running" => StateVariant::Running,
"degraded" => StateVariant::Degraded,
"maintenance" => StateVariant::Maintenance,
"stopping" => StateVariant::Stopping,
_ => StateVariant::Other,
})
}
}
/// Get the current system state. Use CLI instead of direct D-Bus calls to avoid unnecessary
/// dependencies.
pub fn state() -> Result<StateVariant> {
std::process::Command::new("systemctl")
.arg("is-system-running")
.output()
.map_err(Into::into)
.and_then(|output| {
let state = std::str::from_utf8(&output.stdout).unwrap_or_default();
state.trim().parse().map_err(Into::into)
})
}
/// Wait until the system is in the running state.
pub fn wait_running_state() -> Result<()> {
loop {
if state()? == StateVariant::Running {
break;
}
std::thread::sleep(std::time::Duration::from_millis(500));
}
Ok(())
}
// A variant with D-Bus for future reference
/*
let connection = Connection::new_system()?;
let proxy = connection.with_proxy(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
Duration::from_millis(5000),
);
let (state_variant,): (Variant<String>,) = proxy.method_call(
"org.freedesktop.DBus.Properties",
"Get",
("org.freedesktop.systemd1.Manager", "SystemState"),
)?;
state_variant.0.parse::<SystemStateVariant>()?;
*/
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论