systemd: make unit_status read-only (GetUnit only, never LoadUnit)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YShymaPrXmc3KYRUg2HMSs
This commit is contained in:
1 parent
85af4659f1
commit
74802efd0e
7 files changed
+15
-12
No files matched your search
+1
-1
@@ -205,7 +205,7 @@ impl TrackerService {
|
||||
}
|
||||
|
||||
#[tool(
|
||||
description = "Detailed status of one systemd unit: states, unit file path, enter/exit times and, for services, main_pid, exec_main_status, n_restarts (automatic restarts) and result (`success`, `exit-code`, `signal`, `timeout`). Unknown unit is an error."
|
||||
description = "Detailed status of one systemd unit: states, unit file path, enter/exit times and, for services, main_pid, exec_main_status, n_restarts (automatic restarts) and result (`success`, `exit-code`, `signal`, `timeout`). Read-only: only units systemd currently has loaded are visible; an unloaded or unknown unit is a not-found error."
|
||||
)]
|
||||
async fn get_systemd_unit(
|
||||
&self,
|
||||
|
||||
+1
-1
@@ -120,7 +120,7 @@ impl SystemdSource for FakeSystemd {
|
||||
.iter()
|
||||
.find(|u| u.name == name)
|
||||
.cloned()
|
||||
.ok_or_else(|| AppError::NotFound(format!("unit {name}")))
|
||||
.ok_or_else(|| AppError::NotFound(format!("unit not loaded or unknown: {name}")))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -83,7 +83,8 @@ pub trait SystemdSource: Send + Sync {
|
||||
async fn list_units(&self, state: Option<&str>) -> Result<Vec<UnitSummary>, AppError>;
|
||||
/// List `.timer` units with their schedule.
|
||||
async fn list_timers(&self) -> Result<Vec<TimerInfo>, AppError>;
|
||||
/// Status of one unit. Unknown unit is `AppError::NotFound`.
|
||||
/// Status of one unit. Read-only: never asks systemd to load a unit, so a
|
||||
/// unit that is not loaded (or unknown) is `AppError::NotFound`.
|
||||
async fn unit_status(&self, name: &str) -> Result<UnitStatus, AppError>;
|
||||
}
|
||||
|
||||
|
||||
@@ -286,18 +286,20 @@ impl SystemdSource for DbusSystemd {
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Note: for a name that is not loaded this calls `LoadUnit`, which asks
|
||||
/// systemd to load the unit (a side effect, though it does not start it).
|
||||
/// Read-only: uses `GetUnit`, so only units systemd already has in memory
|
||||
/// are visible. `LoadUnit` is never called (it would load arbitrary
|
||||
/// installed units); an unloaded unit is reported as not found.
|
||||
async fn unit_status(&self, name: &str) -> Result<UnitStatus, AppError> {
|
||||
validate_unit_name(name)?;
|
||||
let conn = self.conn().await?;
|
||||
let mgr = self.manager().await?;
|
||||
let path = match mgr.get_unit(name.to_string()).await {
|
||||
Ok(p) => p,
|
||||
Err(e) if is_no_such_unit(&e) => mgr
|
||||
.load_unit(name.to_string())
|
||||
.await
|
||||
.map_err(|e| self.err(e))?,
|
||||
Err(e) if is_no_such_unit(&e) => {
|
||||
return Err(AppError::NotFound(format!(
|
||||
"unit not loaded or unknown: {name}"
|
||||
)))
|
||||
}
|
||||
Err(e) => return Err(self.err(e)),
|
||||
};
|
||||
let unit = UnitProxy::builder(&conn)
|
||||
|
||||
Reference in new issue
Block a user