diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d5c94d7..aafae61 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -81,6 +81,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false + - name: Each unit the README enables has an [Install] section + run: sh test/units/install-sections.sh - name: Check the example scripts and units run: | set -eu @@ -122,7 +124,8 @@ jobs: # A fresh install with contrib/systemd/ and no drop-in: pullgate run # under pullgate-run@.service, then pullgate listen under # pullgate-listen.service against a fake Telegram. A unit setting - # that breaks a run fails here (issue #19). + # that breaks a run fails here (issue #19). Then it enables the + # units as the README says, and each must be enabled (issue #20). run: sudo sh test/units/first-run.sh "${RUNNER_TEMP}/pullgate" lint: @@ -131,7 +134,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - name: shellcheck install.sh and test/units/first-run.sh + - name: shellcheck install.sh and test/units/*.sh # A fixed release of ShellCheck, checked by its SHA-256. Dependabot # does not update it. Update it by hand: the version and the SHA-256 # of shellcheck-VERSION.linux.x86_64.tar.xz together. @@ -147,7 +150,7 @@ jobs: echo "${SHELLCHECK_SHA256} $dir/shellcheck.tar.xz" | sha256sum --strict -c - tar -xJf "$dir/shellcheck.tar.xz" -C "$dir" "$dir/shellcheck-${SHELLCHECK_VERSION}/shellcheck" --version - "$dir/shellcheck-${SHELLCHECK_VERSION}/shellcheck" --shell=sh install.sh test/units/first-run.sh + "$dir/shellcheck-${SHELLCHECK_VERSION}/shellcheck" --shell=sh install.sh test/units/first-run.sh test/units/install-sections.sh - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: go-version-file: go.mod diff --git a/DESIGN.md b/DESIGN.md index 7ac300c..0afc955 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -696,8 +696,15 @@ says, with no drop-in. It runs the first `pullgate run` under the real `pullgate-run@.service`, checks the owners and modes of section 4, then runs `pullgate listen` under the real `pullgate-listen.service` against a fake Telegram on `127.0.0.1`, and checks that the listener sent the -question and removed it from the outbox. So a unit setting that breaks a -run or the listener fails CI. +question and removed it from the outbox. At the end it runs +`systemctl enable` on the four units of the README (a list in the +script), and checks that each is `enabled`. It also follows the README's +update from 0.1.0 for the listener: the old unit with `add-wants`, then the +new unit and `reenable`. So a unit setting that breaks a run or the listener, or a unit +that cannot be enabled, fails CI. Without root, CI also runs +`systemd-analyze verify` on every unit, and `test/units/install-sections.sh` +checks that each unit the README tells you to enable has an `[Install]` +section with `WantedBy=`. ## 15. Not in version 0.1 diff --git a/README.md b/README.md index 632f7c8..69a0d42 100644 --- a/README.md +++ b/README.md @@ -245,10 +245,16 @@ not work there. ### Update from 0.1.0 -In 0.1.0 the first `pullgate run` failed under `pullgate-run@.service` -with `chmod .../outbox: operation not permitted` (issue #19). A host may -have a drop-in with `RestrictSUIDSGID=no` for the run unit as a -workaround. Newer versions do not need it. After you update the binary: +To update a host from 0.1.0: update the binary, and copy the units of +`contrib/systemd/` of the new version to `/etc/systemd/system/` again. +The copy replaces the unit files, so keep your own changes in drop-ins +(`systemctl edit UNIT`); `systemctl cat UNIT` shows them. Then do the two +steps below. + +**The run unit (issue #19).** In 0.1.0 the first `pullgate run` failed +under `pullgate-run@.service` with +`chmod .../outbox: operation not permitted`. A host may have a drop-in +with `RestrictSUIDSGID=no` for the run unit. Newer versions do not need it. 1. Find the drop-in: `systemctl cat pullgate-run@NAME.service` shows each file and its path. It is in `/etc/systemd/system/pullgate-run@.service.d/` @@ -259,6 +265,25 @@ workaround. Newer versions do not need it. After you update the binary: The next run takes the setgid bit off the outbox by itself. +**The listener at boot (issue #20).** In 0.1.0, `pullgate-listen.service` +had no `[Install]` section, so `systemctl enable` did nothing and the +listener did not start after a reboot. Every host updated from 0.1.0 must +enable it again. A host may have the workaround +`systemctl add-wants multi-user.target pullgate-listen.service`. That made +the same link that `systemctl enable` makes now +(`/etc/systemd/system/multi-user.target.wants/pullgate-listen.service`), so +there is nothing to remove first (systemd has no `remove-wants`). With or +without the workaround, run: + +```sh +systemctl daemon-reload +systemctl reenable pullgate-listen.service +ls -l /etc/systemd/system/multi-user.target.wants/pullgate-listen.service +``` + +`reenable` removes the old link, if there is one, and makes it again from +the new `[Install]` section. The `ls` shows the link. + ## Development ```sh diff --git a/contrib/systemd/pullgate-listen.service b/contrib/systemd/pullgate-listen.service index 88ab118..2ac82a7 100644 --- a/contrib/systemd/pullgate-listen.service +++ b/contrib/systemd/pullgate-listen.service @@ -48,3 +48,8 @@ SystemCallFilter=~@privileged @resources SystemCallErrorNumber=EPERM DevicePolicy=closed PrivateIPC=yes + +[Install] +# systemctl enable starts the listener at boot. Without it, no question +# gets an answer after a reboot. +WantedBy=multi-user.target diff --git a/test/units/first-run.sh b/test/units/first-run.sh index 78ed1c6..a59d850 100644 --- a/test/units/first-run.sh +++ b/test/units/first-run.sh @@ -5,7 +5,8 @@ # `pullgate listen` under the real pullgate-listen.service, with no # drop-in. Then it checks the owners and modes of DESIGN.md section 4, and # that the listener read the outbox and sent the question. So a unit -# setting that breaks a run fails here (issue #19). +# setting that breaks a run fails here (issue #19). At the end it enables +# the units as the README says, and checks that each is enabled (#20). # # It changes the system: it installs /usr/local/bin/pullgate, adds users, # and writes to /etc and /var. Run it only as root, on a machine you throw @@ -176,6 +177,35 @@ if ! systemctl is-active --quiet pullgate-listen.service; then fi systemctl stop pullgate-listen.service +# A host updated from 0.1.0 with its workaround for issue #20, as the +# README says ("Update from 0.1.0"): the old unit (no [Install]) and +# add-wants, then the new unit, daemon-reload and reenable. +sed '/^\[Install\]/,$d' "$top/contrib/systemd/pullgate-listen.service" >/etc/systemd/system/pullgate-listen.service +systemctl daemon-reload +systemctl add-wants multi-user.target pullgate-listen.service || err "systemctl add-wants failed" +install -m 0644 -o root -g root "$top/contrib/systemd/pullgate-listen.service" /etc/systemd/system/ +systemctl daemon-reload +systemctl reenable pullgate-listen.service || err "systemctl reenable pullgate-listen.service failed" +if [ ! -L /etc/systemd/system/multi-user.target.wants/pullgate-listen.service ]; then + err "after reenable, multi-user.target does not want pullgate-listen.service" +fi +state="$(systemctl is-enabled pullgate-listen.service 2>/dev/null || true)" +if [ "$state" != enabled ]; then + err "after reenable, systemctl is-enabled says '$state', want 'enabled'" +fi +systemctl disable pullgate-listen.service || err "systemctl disable pullgate-listen.service failed" + +# Enable the units as the README says (no --now here). Each must then be +# "enabled", not "static": a unit with no [Install] section does not start +# after a reboot (issue #20). +for u in pullgate-listen.service "pullgate-apply@$name.path" "pullgate-run@$name.timer" "pullgate-check@$name.path"; do + systemctl enable "$u" || err "systemctl enable $u failed" + state="$(systemctl is-enabled "$u" 2>/dev/null || true)" + if [ "$state" != enabled ]; then + err "$u: systemctl is-enabled says '$state' after enable, want 'enabled'" + fi +done + if [ "$fail" -ne 0 ]; then exit 1 fi diff --git a/test/units/install-sections.sh b/test/units/install-sections.sh new file mode 100644 index 0000000..f4299a9 --- /dev/null +++ b/test/units/install-sections.sh @@ -0,0 +1,45 @@ +#!/bin/sh +# Each unit that the README tells you to enable must have an [Install] +# section with WantedBy=. Without it, `systemctl enable` does nothing, and +# the unit does not start after a reboot (issue #20). +# +# Usage: sh test/units/install-sections.sh (from the top of the repo) +set -eu + +# Lines joined at a trailing backslash; "sudo" and options before +# "enable" are allowed. +units="$(sed -e ':a' -e '/\\$/N; s/\\\n//; ta' README.md \ + | grep -E '^[[:space:]]*(sudo[[:space:]]+)?systemctl([[:space:]]+--?[A-Za-z-]+)*[[:space:]]+(re)?enable[[:space:]]' \ + | sed -e 's/#.*//' \ + | tr -s ' \t' '\n' \ + | grep -E '^pullgate-[A-Za-z0-9@._-]+\.(service|path|timer|socket)$' \ + | sort -u || true)" +# The README enables 4 units today. Fewer means the parsing broke. +n="$(echo "$units" | grep -c . || true)" +if [ "$n" -lt 4 ]; then + echo "FAIL: found $n 'systemctl enable' units in README.md, want at least 4. Fix: check the README, or this script" >&2 + exit 1 +fi + +fail=0 +for u in $units; do + # pullgate-run@NAME.timer is an instance of pullgate-run@.timer. + f="contrib/systemd/$(echo "$u" | sed -E 's/@[^.]+\./@./')" + if [ ! -f "$f" ]; then + echo "FAIL: README.md enables $u, but $f does not exist" >&2 + fail=1 + continue + fi + # The [Install] section and a WantedBy= line in it. + if ! awk ' + /^\[/ { sec = $0; next } + sec == "[Install]" && /^WantedBy=[^[:space:]]/ { ok = 1 } + END { exit ok ? 0 : 1 } + ' "$f"; then + echo "FAIL: $f has no [Install] section with WantedBy=. Fix: add one, so that systemctl enable starts $u at boot" >&2 + fail=1 + continue + fi + echo "ok: $u ($f) has [Install] with WantedBy=" +done +exit "$fail"