|
| 1 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | +// Copyright (C) 2026 Mohamed Hammad |
| 3 | + |
| 4 | +//! `systemctl list-unit-files` + `systemctl list-units` parsers and |
| 5 | +//! composition into a [`ServiceInventory`]. |
| 6 | +
|
| 7 | +use pearlite_schema::ServiceInventory; |
| 8 | +use std::collections::BTreeSet; |
| 9 | + |
| 10 | +/// Parse the stdout of |
| 11 | +/// `systemctl list-unit-files --no-pager --no-legend` into the three |
| 12 | +/// disjoint state buckets `(enabled, disabled, masked)`. |
| 13 | +/// |
| 14 | +/// `enabled-runtime` and `masked-runtime` are folded into the |
| 15 | +/// non-runtime variants — the difference matters at apply time but not |
| 16 | +/// for drift detection. |
| 17 | +/// |
| 18 | +/// `static`, `alias`, `indirect`, `generated`, and `transient` are |
| 19 | +/// silently skipped: those states are not user-configurable enable |
| 20 | +/// values and never appear in declared service config. |
| 21 | +#[must_use] |
| 22 | +pub fn parse_list_unit_files( |
| 23 | + stdout: &str, |
| 24 | +) -> (BTreeSet<String>, BTreeSet<String>, BTreeSet<String>) { |
| 25 | + let mut enabled = BTreeSet::new(); |
| 26 | + let mut disabled = BTreeSet::new(); |
| 27 | + let mut masked = BTreeSet::new(); |
| 28 | + for line in stdout.lines() { |
| 29 | + let mut iter = line.split_whitespace(); |
| 30 | + let Some(unit) = iter.next() else { continue }; |
| 31 | + let Some(state) = iter.next() else { continue }; |
| 32 | + match state { |
| 33 | + "enabled" | "enabled-runtime" => { |
| 34 | + enabled.insert(unit.to_owned()); |
| 35 | + } |
| 36 | + "disabled" => { |
| 37 | + disabled.insert(unit.to_owned()); |
| 38 | + } |
| 39 | + "masked" | "masked-runtime" => { |
| 40 | + masked.insert(unit.to_owned()); |
| 41 | + } |
| 42 | + _ => {} // static / alias / indirect / generated / transient |
| 43 | + } |
| 44 | + } |
| 45 | + (enabled, disabled, masked) |
| 46 | +} |
| 47 | + |
| 48 | +/// Parse the stdout of |
| 49 | +/// `systemctl list-units --no-pager --no-legend --all --plain` into the |
| 50 | +/// set of currently active, loaded units. |
| 51 | +/// |
| 52 | +/// Filter: `LOAD == loaded && ACTIVE == active`. Units that are |
| 53 | +/// not-found, error, masked, or inactive are skipped. |
| 54 | +#[must_use] |
| 55 | +pub fn parse_list_units(stdout: &str) -> BTreeSet<String> { |
| 56 | + let mut active = BTreeSet::new(); |
| 57 | + for line in stdout.lines() { |
| 58 | + let mut iter = line.split_whitespace(); |
| 59 | + let Some(unit) = iter.next() else { continue }; |
| 60 | + let Some(load) = iter.next() else { continue }; |
| 61 | + let Some(active_state) = iter.next() else { |
| 62 | + continue; |
| 63 | + }; |
| 64 | + if load == "loaded" && active_state == "active" { |
| 65 | + active.insert(unit.to_owned()); |
| 66 | + } |
| 67 | + } |
| 68 | + active |
| 69 | +} |
| 70 | + |
| 71 | +/// Combine `list-unit-files` and `list-units` outputs into one |
| 72 | +/// [`ServiceInventory`]. |
| 73 | +#[must_use] |
| 74 | +pub fn compose_inventory(unit_files: &str, units: &str) -> ServiceInventory { |
| 75 | + let (enabled, disabled, masked) = parse_list_unit_files(unit_files); |
| 76 | + let active = parse_list_units(units); |
| 77 | + ServiceInventory { |
| 78 | + enabled, |
| 79 | + disabled, |
| 80 | + masked, |
| 81 | + active, |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +#[cfg(test)] |
| 86 | +#[allow( |
| 87 | + clippy::expect_used, |
| 88 | + clippy::unwrap_used, |
| 89 | + clippy::panic, |
| 90 | + reason = "tests may use expect()/unwrap()/panic!() per Plan §4.2 + CLAUDE.md" |
| 91 | +)] |
| 92 | +mod tests { |
| 93 | + use super::*; |
| 94 | + |
| 95 | + const UNIT_FILES: &str = "\ |
| 96 | +nginx.service enabled enabled |
| 97 | +sshd.service enabled enabled |
| 98 | +NetworkManager.service enabled-runtime enabled |
| 99 | +bluetooth.service disabled enabled |
| 100 | +systemd-resolved.service masked enabled |
| 101 | +shutdown.target static - |
| 102 | +console-getty.service indirect - |
| 103 | +systemd-tmpfiles-setup.service alias - |
| 104 | +"; |
| 105 | + |
| 106 | + const UNITS: &str = "\ |
| 107 | +nginx.service loaded active running A high-performance web server |
| 108 | +sshd.service loaded active running OpenBSD Secure Shell server |
| 109 | +bluetooth.service loaded inactive dead Bluetooth service |
| 110 | +systemd-resolved.service masked inactive dead systemd-resolved.service |
| 111 | +not-found.service not-found inactive dead not-found.service |
| 112 | +"; |
| 113 | + |
| 114 | + #[test] |
| 115 | + fn list_unit_files_distinguishes_enabled_disabled_masked() { |
| 116 | + let (enabled, disabled, masked) = parse_list_unit_files(UNIT_FILES); |
| 117 | + |
| 118 | + assert_eq!(enabled.len(), 3); |
| 119 | + assert!(enabled.contains("nginx.service")); |
| 120 | + assert!(enabled.contains("sshd.service")); |
| 121 | + assert!(enabled.contains("NetworkManager.service")); // enabled-runtime |
| 122 | + |
| 123 | + assert_eq!(disabled.len(), 1); |
| 124 | + assert!(disabled.contains("bluetooth.service")); |
| 125 | + |
| 126 | + assert_eq!(masked.len(), 1); |
| 127 | + assert!(masked.contains("systemd-resolved.service")); |
| 128 | + } |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn list_unit_files_skips_static_alias_indirect() { |
| 132 | + let (enabled, disabled, masked) = parse_list_unit_files(UNIT_FILES); |
| 133 | + let all: BTreeSet<&str> = enabled |
| 134 | + .iter() |
| 135 | + .chain(disabled.iter()) |
| 136 | + .chain(masked.iter()) |
| 137 | + .map(String::as_str) |
| 138 | + .collect(); |
| 139 | + assert!(!all.contains("shutdown.target")); |
| 140 | + assert!(!all.contains("console-getty.service")); |
| 141 | + assert!(!all.contains("systemd-tmpfiles-setup.service")); |
| 142 | + } |
| 143 | + |
| 144 | + #[test] |
| 145 | + fn list_units_filters_to_loaded_active() { |
| 146 | + let active = parse_list_units(UNITS); |
| 147 | + assert_eq!(active.len(), 2); |
| 148 | + assert!(active.contains("nginx.service")); |
| 149 | + assert!(active.contains("sshd.service")); |
| 150 | + // Inactive: not active. |
| 151 | + assert!(!active.contains("bluetooth.service")); |
| 152 | + // Masked: not loaded. |
| 153 | + assert!(!active.contains("systemd-resolved.service")); |
| 154 | + // Not-found: not loaded. |
| 155 | + assert!(!active.contains("not-found.service")); |
| 156 | + } |
| 157 | + |
| 158 | + #[test] |
| 159 | + fn empty_outputs_yield_empty_inventory() { |
| 160 | + let inv = compose_inventory("", ""); |
| 161 | + assert!(inv.enabled.is_empty()); |
| 162 | + assert!(inv.disabled.is_empty()); |
| 163 | + assert!(inv.masked.is_empty()); |
| 164 | + assert!(inv.active.is_empty()); |
| 165 | + } |
| 166 | + |
| 167 | + #[test] |
| 168 | + fn compose_combines_both_streams() { |
| 169 | + let inv = compose_inventory(UNIT_FILES, UNITS); |
| 170 | + assert_eq!(inv.enabled.len(), 3); |
| 171 | + assert_eq!(inv.disabled.len(), 1); |
| 172 | + assert_eq!(inv.masked.len(), 1); |
| 173 | + assert_eq!(inv.active.len(), 2); |
| 174 | + } |
| 175 | + |
| 176 | + #[test] |
| 177 | + fn truncated_lines_are_skipped() { |
| 178 | + // Defensive: a corrupt or partial line must not panic. |
| 179 | + let truncated = "nginx.service\nsshd.service enabled\n"; |
| 180 | + let (enabled, _, _) = parse_list_unit_files(truncated); |
| 181 | + assert_eq!(enabled.len(), 1); |
| 182 | + assert!(enabled.contains("sshd.service")); |
| 183 | + } |
| 184 | +} |
0 commit comments