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:
ariandClaude Sonnet 5.5 committed 2026-10-16 09:30:52 -04:00
1 parent 85af4659f1
commit 74802efd0e
7 files changed
+15 -12

No files matched your search

+1 -1
View File
@@ -63,7 +63,7 @@ Base: `http://127.0.0.1:9109`. Errors are `{"error":{"code","message"}}`.
|---|---|
| `GET /healthz` | public, `{"status":"ok","version":...}` |
| `GET /api/v1/systemd/units?state=failed` | `state` is an active state (`failed`, `active`, ...) |
| `GET /api/v1/systemd/units/{name}` | one unit with details |
| `GET /api/v1/systemd/units/{name}` | one unit with details; read-only, so only units systemd has loaded (unloaded or unknown is 404) |
| `GET /api/v1/systemd/timers` | |
| `GET /api/v1/journal?unit=&priority=err&lines=100` | priority by name or number |
| `GET /api/v1/journal/follow?unit=&priority=` | SSE, event `journal` |
+1 -1
View File
@@ -30,7 +30,7 @@ Design: [`plan.md`](plan.md) · Review fixes: [`plan/review.md`](plan/review.md)
- [x] **A1** Shared system-bus `zbus::Connection` + `ManagerProxy`
- [x] **A2** `list_units(state)` mapped to `UnitSummary`
- [x] **A3** `list_timers()` via `*.timer` units + `TimerProxy` props (no `ListTimers` method exists)
- [x] **A4** `unit_status(name)`: `GetUnit` first, `LoadUnit` as fallback. `Unit` props always, `Service` props for `.service` only.
- [x] **A4** `unit_status(name)`: `GetUnit` only (read-only; `LoadUnit` is never called, an unloaded unit is NotFound). `Unit` props always, `Service` props for `.service` only.
- [x] **A5** Unit-name validation. `NoSuchUnit` maps to `NotFound`.
- [x] **A6** `examples/systemd_dump.rs` prints JSON from the live bus
- [x] **A7** Unit tests for timestamp conversion and property mapping
+1 -1
View File
@@ -98,7 +98,7 @@ Precedence: built-in defaults < `--config /etc/skyg-linux-tracker.toml` < `SKYG_
### Sources (live reads only)
- **systemd** (`sources/systemd.rs`): one shared `zbus::Connection` to the system bus. `list_units()` → `ManagerProxy::list_units()`; `list_timers()` → `list_timers()` (next-elapse realtime converted to RFC3339); `unit_status(name)` → `load_unit` + properties (`ActiveState`, `SubState`, `ExecMainStatus`, `NRestarts`, `ActiveEnterTimestamp`). No caching — every call hits D-Bus.
- **systemd** (`sources/systemd.rs`): one shared `zbus::Connection` to the system bus. `list_units()` → `ManagerProxy::list_units()`; `list_timers()` → `list_timers()` (next-elapse realtime converted to RFC3339); `unit_status(name)` → `get_unit` (read-only, never `load_unit`) + properties (`ActiveState`, `SubState`, `ExecMainStatus`, `NRestarts`, `ActiveEnterTimestamp`). No caching — every call hits D-Bus.
- **journal** (`sources/journal.rs`): `Journal::open()`, `match_add("_SYSTEMD_UNIT", …)`, `seek_tail()`. Snapshot queries seek backward N entries. `follow()` wraps `Journal::wait()` in a `tokio::task::spawn_blocking` bridge (sd-journal is blocking) feeding a `tokio::sync::mpsc` — consumers (SSE endpoint, MCP tool, OTel log exporter) all read from channels.
- **docker** (`sources/docker.rs`): shared `bollard::Docker` over the unix socket. `ps()` → `list_containers` + `inspect_container` per container for `State.Health`, `StartedAt` (→ uptime), restart count. `logs(name, follow)` → `bollard` log stream (`stdout`, `stderr`, `timestamps`, `since`) as a `Stream<Item = LogOutput>`.
+1 -1
View File
@@ -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
View File
@@ -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
View File
@@ -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>;
}
+8 -6
View File
@@ -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)