interplane.Get Omega
Read in your language

spark-supervisor

Process supervisor and health monitor for sovereign daemons

Brought by drakestapleton

branch main Branches Tags History

spark-supervisor / README.md

199 lines · 13272 bytes Raw Blame History Rendered
1# spark-supervisor: aien-supervise
2
3AIEN's process supervisor, written in seeded Omega. It runs AIEN services from line-format
4declaration files: services have no `.service`, `.timer` or `.path` files and no `systemd-run`. One
5systemd user unit, `tools/aien-supervise.service`, starts the daemon itself at boot (see below).
6
7This repository used to hold a Rust daemon. The Rust sources were removed when the Omega
8supervisor came to Radicle main; they stay in this repository's git history. Nothing in this
9repository's documents keeps them as an oracle.
10
11## What still depends on systemd on this machine
12
13The Spark runs DGX OS (Ubuntu). PID 1 is the distribution's systemd and stays so. aien-supervise
14calls no systemd program, but two things it relies on come from systemd today:
15
161. **cgroup delegation.** An unprivileged process may only create cgroups and move processes
17 inside a cgroup v2 subtree its user owns. On this machine that subtree is
18 `/sys/fs/cgroup/user.slice/user-UID.slice/user@UID.service`, created and chowned to the user by
19 systemd's user manager at login (controllers `cpu memory pids` delegated). A process started
20 from an ssh login sits in `session-N.scope`, outside that subtree, and cannot move itself in
21 (the common ancestor `user-UID.slice` is root-owned). So the supervisor must be started from a
22 process that already lives inside the delegated subtree; `run-limited` callers from anywhere
23 then reach it over its local socket.
242. **Boot start.** One systemd user unit, `tools/aien-supervise.service`, starts the daemon at boot and
25 hands it its delegated cgroup (`Delegate=cpu memory pids`). The route needs no cron line and no other unit:
26 every service is a file in `~/.config/aien-supervise/services`. cgroupfs is rebuilt at every boot, so
27 some boot-time root actor has to create or delegate the subtree; on this machine that actor is systemd.
28 Install: `cp tools/aien-supervise.service ~/.config/systemd/user/ && systemctl --user enable --now aien-supervise`.
29 Moving a service off its own systemd unit: stop the old unit, add `NAME.svc`, `aien-supervise reload`, and
30 once it has run healthy, `systemctl --user disable` the old unit (keep the file for the undo). Until that
31 disable, the old unit still starts at boot as well, and both copies compete for the same port.
32 Once the unit is installed it is the only start route: start and stop the daemon with
33 `systemctl --user start|stop aien-supervise`, not `ensure` or `shutdown` (a daemon started by `ensure`
34 from a login shell sits outside the delegated cgroup, and the unit then refuses to start a second one).
35 Right after a start, requests such as `aien-supervise start NAME` are refused with `daemon not running`
36 until the daemon has written its pid; wait until `aien-supervise status` says `daemon running`.
37
38What removes the dependency: a root-owned, one-time setup that creates
39`/sys/fs/cgroup/aien-supervise.slice` with `cpu memory pids` enabled in its parent and chowns it
40(and its `cgroup.procs`, `cgroup.subtree_control`) to the operator account; then
41`AIEN_SUPERVISE_CGROUP` points there and systemd's user manager is no longer involved. Replacing
42PID 1 itself is a separate, larger change (aienux).
43
44## Parts
45
46| Program | Source | Runs as | Role |
47| --- | --- | --- | --- |
48| `aien-supervise` | `om/contract.om`, `om/supervise.om`, `om/limits.om`, `om/limited.om`, `om/services.om` | the operator account | daemon (`run`), `ensure`, `status`, `start`/`stop`/`restart`, `check`, `reload`, `shutdown`, `run-limited` |
49| `aien-supervise-root` | `om/root.om` | root, via one sudoers rule | builds one service's sandbox and execs it; `stop`/`kill` its cgroup; `check` (dry run) |
50| `aien-watch` | `om/watch.om` | the service user, inside the sandbox | path trigger for a service's `watch` key |
51
52`om/lib.om` is shared. `om/syscalls.om` is generated by `tools/gen-syscalls.sh` from
53`systemd-analyze syscall-filter` and `<asm/unistd.h>`; systemd-analyze is used there only as a
54read-only oracle for group membership, and `tools/gen-syscalls.sh --check` fails on any difference.
55
56## Build
57
58```sh
59OMC=/path/to/omega-mv/seed/out/omc nice -n 19 sh tools/build.sh
60```
61
62The script refuses (exit 2) an omc other than the pin: omega-mv
63`15760b5009f00485ce51669c84d0126ed19fa479`, `seed/out/omc` sha256
64`a5613368f427aacd73ebeee16d46f8b3203405711d4de5b860d0cbee11ce4533`. Take that compiler from
65Radicle storage: `git -C <omega-mv storage> cat-file blob 15760b50:seed/out/omc`.
66
67## Service files
68
69Two directories, one line per directive (`key value...`), no quoting:
70
71* `~/.config/aien-supervise/services/NAME.svc`: user services, run as the operator account.
72* `/etc/aien-supervise/services/NAME.svc`: sandboxed services, root-owned, not group/other-writable.
73
74`NAME` is 1..32 bytes of `a-z 0-9 -`. The daemon reads the directories when it starts and on
75`aien-supervise reload` (adds new files, stops services whose file is gone).
76
77| Key | Meaning (systemd equivalent) |
78| --- | --- |
79| `exec PATH ARG...` | program and arguments; PATH absolute (ExecStart) |
80| `type simple\|oneshot` | default simple (Type) |
81| `restart always\|on-failure\|never` | default on-failure; oneshot never; `no` is accepted as `never` (Restart) |
82| `backoff MIN MAX` | restart delay MINĀ·2^k seconds, never above MAX, reset after 60 s of uptime (RestartSec) |
83| `after NAME...` | user services: start only after each named service is running and healthy; stop in reverse order (After, Requires) |
84| `health-http PORT PATH` | user services: healthy only when `GET PATH` on 127.0.0.1:PORT answers `HTTP/1.x 2..`; without it, healthy = the process is alive |
85| `every S` / `delay S` | oneshot period and first-run delay (OnCalendar, OnBootSec, Persistent) |
86| `timeout S` | oneshot run limit: SIGTERM, then SIGKILL after `stoptime` (TimeoutStartSec) |
87| `stoptime S` | grace between SIGTERM and SIGKILL of the whole process group, default 10 (TimeoutStopSec, KillMode=control-group) |
88| `autostart yes\|no` | start with the daemon, default yes |
89| `log-max BYTES` / `log-keep N` | stdout and stderr of the service go to `state/log/NAME.log`, rotated to `NAME.log.1..N` so that no file is ever larger than log-max; default 10 MiB, 3 |
90| `env K=V...` | user services: the environment is cleared and holds only these (Environment) |
91| `dir PATH`, `umask OCTAL`, `nice N` | WorkingDirectory, UMask, Nice (0..19, set in Omega) |
92| `memory-max BYTES`, `tasks-max N`, `cpu-weight 1..10000` | user services: cgroup v2 `memory.max` (with `memory.swap.max` 0 and `memory.oom.group` 1), `pids.max`, `cpu.weight` (MemoryMax, TasksMax, CPUWeight) |
93| `watch DIR SUFFIX PERIOD TIMEOUT` | run `exec` through `aien-watch` when DIR holds a matching entry |
94
95**Limits fail closed.** A user service that declares a limit runs in its own leaf
96`$AIEN_SUPERVISE_CGROUP/svc-NAME`. The daemon creates the leaf, writes each value and reads it
97back; the child moves itself into the leaf and reports back before it execs. If any step fails the
98service is not started, its state is `error` and its status line carries
99`reason=LIMIT_NOT_APPLIED errno=-N`. A service killed by its memory ceiling shows
100`LIMIT_OOM_KILLED`.
101
102**Change trigger: MISSING_IMPLEMENTATION.** Restarting a service when its declaration file
103changes needs inotify, and the pinned compiler has no inotify host words. Every status line says
104`change-trigger=MISSING_IMPLEMENTATION`. The daemon does not poll the files and runs no timer in
105its place; `tests/contract.py missing_trigger` checks that an edited file restarts nothing. The
106compiler change (host words for `inotify_init1`, `inotify_add_watch`, event reads) is a separate
107omega-mv landing, then a repin here.
108
109Sandboxed services (`/etc/aien-supervise/services`) keep the root helper's keys and mechanisms,
110documented with their per-directive table and probes in [docs/MIGRATION.md](docs/MIGRATION.md):
111`user`, `group`, `groups`, `ro`, `rw`, `hide`, `envfile`, `secret`, `net`, `allow`, `deny`,
112`net-default`, `listen`, `memory-max`, `memory-swap-max`, `tasks-max`, `cpu-quota`, `nofile`,
113`namespaces`, `syscalls`, `syscalls-deny`, `syscall-error`, `families`, `mdwe`. Their cgroup is
114`/sys/fs/cgroup/aien-supervise/NAME`, created by the root helper.
115
116## run-limited
117
118```sh
119aien-supervise run-limited --memory BYTES [--pids N] -- /absolute/CMD ARG...
120```
121
122Runs one command under a kernel-enforced memory ceiling (swap 0) and optional process ceiling.
123The caller connects to the running daemon's socket `state/limited.sock` (mode 0600, peer uid
124checked with `SO_PEERCRED`). The daemon makes a leaf `job-PID` under its cgroup root, applies and
125reads back the limits, and runs the command in it with the caller's working directory and the
126caller's standard input, output and error (opened from the caller's `/proc/PID/fd/0..2`). The
127command's environment is empty. The leaf is killed and removed afterwards. If the caller goes
128away, the command's process group is killed.
129
130| Exit | Meaning |
131| --- | --- |
132| the command's status | the command ran and exited (128+N when it died from signal N) |
133| 137 and `LIMIT_OOM_KILLED` on stderr | the memory ceiling killed it (`memory.events` `oom_kill` > 0) |
134| 125 and `LIMIT_NOT_APPLIED errno=-N` on stderr | no daemon, socket refused, or a limit could not be applied: the command did not run |
135| 2 | usage |
136
137## Privilege model
138
139The daemon runs as the operator account and holds no privilege. For a sandboxed service it runs
140`sudo -n /usr/local/libexec/aien-supervise/aien-supervise-root run NAME`. The helper refuses
141unless real and effective uid are 0 and the arguments are `run|stop|kill|check NAME`; it checks that
142every path it relies on is root-owned and not group/other-writable; it runs nothing as root except
143`nft -f` (the service's own table) and `prlimit --pid` (on itself).
144
145```
146Defaults!/usr/local/libexec/aien-supervise/aien-supervise-root !use_pty
147<operator> ALL=(root) NOPASSWD: /usr/local/libexec/aien-supervise/aien-supervise-root run *, /usr/local/libexec/aien-supervise/aien-supervise-root stop *, /usr/local/libexec/aien-supervise/aien-supervise-root kill *, /usr/local/libexec/aien-supervise/aien-supervise-root check *
148```
149
150## Install
151
152```sh
153sudo install -d -o root -g root -m 0755 /usr/local/libexec/aien-supervise /etc/aien-supervise /etc/aien-supervise/services
154sudo install -o root -g root -m 0755 out/aien-supervise-root out/aien-watch /usr/local/libexec/aien-supervise/
155install -m 0755 out/aien-supervise ~/.local/bin/aien-supervise
156```
157
158Start the daemon from a process inside the delegated subtree (see above), with
159`AIEN_SUPERVISE_CGROUP` naming a private directory under it, for example
160`.../user@UID.service/aien-supervise`. State lives in `~/.local/state/aien-supervise/`
161(`daemon.log`, `log/NAME.log*`, `st/NAME`, `ctl/`, `limited.sock`).
162`aien-supervise status` prints a daemon line, then one line per service.
163
164## Tests
165
166* `sh tests/user.sh`: daemon behaviour without root (restart/backoff, timers, timeout, requests,
167 TERM-to-KILL, environment, rotation, refusals, shutdown). 15 cases.
168* `python3 tests/contract.py`: cleared environment plus declared variables; always / on-failure /
169 never with bounded backoff; HTTP-health-gated start order, one status line per service and
170 reverse stop; bounded stdout+stderr rotation, grace then process-group kill with no surviving
171 child; the honest missing change trigger; a fake cgroup root that refuses writes (service and
172 `run-limited` both refused, command never run). 6 cases. The test waits on inotify events.
173* `OMC=... sh tests/limits.sh`: real cgroup v2 under the user's delegated subtree: a child at half
174 the ceiling succeeds, one past it is killed and reported, a fork bomb is held by `pids.max`,
175 pipes and exit status pass through, service limits read back, a service OOM is reported,
176 restart and rotation under a running daemon. Exits 3 (NOT_RUN) when no delegated subtree or
177 delegated tmux server is available.
178* `OMC=... python3 tests/mutants.py`: 20 source mutants (environment, each restart policy,
179 backoff bound, start order, HTTP health, reverse stop, status line, rotation bound, grace,
180 group kill, missing-trigger marker, explicit reload, fail-closed, memory, pids and cpu writes, OOM marker, exit
181 status); each must make its test fail.
182* `sudo -n sh tests/sandbox.sh` (root; qualifies the installed helper) and `sh tools/gen-syscalls.sh --check`.
183
184## Host helpers (recorded dependencies)
185
186The user-service path uses only Omega host words (`execenv`, `setprio`, cgroup files). The root
187helper still uses these host programs:
188
189| Helper | Used for | Omega successor |
190| --- | --- | --- |
191| `/usr/bin/setpriv` | uid/gid/groups, no_new_privs, capability drop | `setids`, `nonewpriv`, `capdrop` host words |
192| `/usr/bin/prlimit` | RLIMIT_NOFILE, RLIMIT_RTPRIO on the helper | `prlimit` host word (exists at the pin; not yet used by `om/root.om`) |
193| `/usr/bin/bwrap` | namespaces, binds, seccomp load | `unshare`, `mount`, `pivotroot`, `seccomp` host words |
194| `/usr/sbin/nft` | per-uid network table | nfnetlink in Omega |
195| `/usr/bin/sudo` | the single privilege crossing | none planned: the narrow rule is the design |
196
197## License
198
199Apache-2.0 WITH LLVM-exception. See [LICENSE](LICENSE) and [NOTICE](NOTICE).