1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright 2025 Lablup Inc. and Jeongkyu Shin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! systemd unit rendering for `all-smi service install` (issue #309).
//!
//! There is exactly one unit definition in this repository:
//! `packaging/systemd/all-smi.service`. The Debian package installs a
//! byte-identical copy through `debian/all-smi.service` (a CI job
//! enforces that), and this module embeds the same file with
//! `include_str!` and rewrites the handful of directives that depend on
//! how the operator is installing it.
//!
//! Rewrites applied:
//!
//! | Directive | System scope | User scope |
//! |---|---|---|
//! | `ExecStart=` | canonicalized `current_exe()` | same |
//! | `User=` / `Group=` | `--service-user` value, or dropped so the service runs as root | dropped |
//! | `WantedBy=` | `multi-user.target` | `default.target` |
//! | everything in [`USER_SCOPE_DROPPED_PREFIXES`] | kept | dropped |
//!
//! Everything else, hardening included, is passed through verbatim so
//! the shipped unit stays the single source of truth: editing the
//! sandboxing there changes the packaged service and every subcommand
//! install at once.
//!
//! One consequence worth knowing when reading logs: `ProtectHome=true`
//! survives into a system-scope unit, so a service running as root
//! cannot see `/root/.config/all-smi/config.toml`. That is deliberate.
//! The machine-wide `/etc/all-smi/config.toml` is the configuration path
//! for a system service, and it is a discovery candidate exactly so this
//! works (see [`crate::common::paths::candidate_config_paths`]).
use Path;
use Scope;
/// Marker comment stamped as the first line of every unit this tool
/// writes. `install` and `uninstall` both refuse to touch a unit file
/// that lacks it unless `--force` is passed, which keeps a hand-written
/// or distro-shipped unit from being silently clobbered.
pub const MANAGED_MARKER: &str = "# Managed by 'all-smi service'";
/// The canonical unit, embedded verbatim at compile time.
pub const UNIT_TEMPLATE: &str = include_str!;
/// Directives stripped when rendering a user-scope unit.
///
/// # The rule
///
/// A per-user `systemd --user` manager runs unprivileged. Any directive
/// whose setup needs a privilege the manager does not hold makes the
/// unit fail *before* `ExecStart` is reached, so the service never
/// starts at all. The failure surfaces as an opaque "control process
/// exited with error code" from `systemctl --user start`, with the real
/// cause only in the journal. Every such directive must be dropped here.
///
/// Directives that are implemented purely with `prctl` or a seccomp
/// filter need no privilege and are kept in both scopes:
/// `NoNewPrivileges=` and `RestrictSUIDSGID=`.
///
/// # Verified failure modes
///
/// Reproduced against systemd 255 on Ubuntu 24.04 with a real per-user
/// manager, using `/bin/sleep` as `ExecStart` so the application itself
/// was out of the picture:
///
/// | Directive | Exit status | Journal |
/// |---|---|---|
/// | `SupplementaryGroups=` | `216/GROUP` | `Failed to determine supplementary groups: No such process` |
/// | `ProtectKernelModules=` | `218/CAPABILITIES` | `Failed to set up user namespacing for unprivileged user` then `Failed to drop capabilities: Operation not permitted` |
///
/// `ProtectKernelModules=` is the non-obvious one: it reads as pure
/// seccomp, but it also strips `CAP_SYS_MODULE` from the capability
/// bounding set and hides `/usr/lib/modules`, and an unprivileged
/// manager can only do that from inside a user namespace. Where the host
/// denies unprivileged user namespaces, the unit dies at 218 before
/// exec.
pub const USER_SCOPE_DROPPED_PREFIXES: & = &;
// The complementary "kept in user scope" list lives in the test module
// (`USER_SCOPE_KEPT_HARDENING` in `template_tests.rs`). It is a test
// contract, not runtime data: the renderer only ever consults the drop
// list above. Keeping it here would be dead code in the binary target,
// where dead-code analysis is per target and a `pub` item is not
// automatically live the way it is in the library target.
/// Inputs for [`render_unit`].
/// Reasons an executable path cannot be expressed in a systemd
/// `ExecStart=` line.
/// Render the unit for one install.
/// Whether `contents` was written by this tool.
/// Turn an executable path into a systemd `ExecStart` token.
///
/// systemd applies its own unescaping to the value, so a path can only
/// be embedded once it is known to survive that round trip:
///
/// * `%` starts a specifier and is escaped as `%%`.
/// * Whitespace, `;`, and `'` split or terminate the command line, so
/// such a path is wrapped in double quotes.
/// * A double quote, backslash, or newline cannot be expressed safely
/// and is rejected outright rather than silently mangled.
// Test module lives in `template_tests.rs` to keep this file under the
// 500-line soft limit.