Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions install/user/first-run/enable-user-units.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,40 @@
# first session has bluetooth pairing, sleep lock, etc. live immediately
# instead of waiting for the next login. ConditionPath* in the unit files
# keep the enabled units inert on hardware they don't apply to.
#
# Enable one unit at a time. systemctl validates an entire multi-unit list
# before applying any of it, so one missing unit (for example a package that
# forgot to install a unit into /usr/lib/systemd/user/) would leave every
# later unit disabled under set -e (issue #10484).

set -euo pipefail

systemctl --user daemon-reload
systemctl --user enable --now \
bt-agent.service \
owed.service \
omarchy-recover-internal-monitor.service \
omarchy-sleep-lock.service \
omarchy-migrate-notify.service \
omarchy-fcitx5.service \
units=(
bt-agent.service
owed.service
omarchy-recover-internal-monitor.service
omarchy-sleep-lock.service
omarchy-migrate-notify.service
omarchy-fcitx5.service
omarchy-crash-watch.service
)

systemctl --user daemon-reload

enabled=0
for unit in "${units[@]}"; do
if systemctl --user enable --now "$unit"; then
enabled=1
else
echo "warning: could not enable $unit" >&2
fi
done

omarchy-hook-install theme-set /usr/share/owe/10-owe-sync

# A unit that fails must not fail the step while others enabled, or first-run
# never completes; fail it only when none did.
if (( ! enabled )); then
echo "error: no omarchy user units could be enabled" >&2
exit 1
Comment on lines +42 to +46

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 security Failed sleep lock is not retried If omarchy-sleep-lock.service fails to enable while another unit succeeds, this script reports success. First-run is then marked complete, so later logins do not retry the failed unit. The service starts the monitor that locks the session before general suspend; without it, the session can remain unlocked after waking, even if the missing unit is later restored. Preserve the partial enablement, but keep failed units eligible for retry.

How this was verified: The sleep-lock unit starts the suspend-event monitor, while the first-run completion marker prevents another enable attempt.

fi
116 changes: 116 additions & 0 deletions test/shell.d/enable-user-units-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

# first-run must not abort the whole unit list when one unit is missing
# (issue #10484). systemctl enable with multiple names validates the full list
# first; a single missing name enables nothing.

script="$ROOT/install/user/first-run/enable-user-units.sh"
[[ -f $script ]] || fail "enable-user-units.sh exists"

grep -F 'for unit in' "$script" >/dev/null ||
fail "enable-user-units enables units one at a time"
grep -F 'could not enable' "$script" >/dev/null ||
fail "enable-user-units warns when a unit fails"
# Must not keep the multi-arg enable that aborts the whole list under set -e.
if grep -E 'enable --now[[:space:]]+\\$' "$script" >/dev/null; then
fail "enable-user-units must not multi-arg enable under one set -e line"
fi
pass "enable-user-units enables units one at a time"

test_tmp=$(mktemp -d)
trap 'rm -rf "$test_tmp"' EXIT
mock_bin="$test_tmp/bin"
log="$test_tmp/systemctl.log"
mkdir -p "$mock_bin"

cat >"$mock_bin/systemctl" <<'SH'
#!/bin/bash
printf '%s\n' "$*" >>"$OMARCHY_TEST_SYSTEMCTL_LOG"
# daemon-reload always succeeds.
[[ $1 == "--user" && $2 == "daemon-reload" ]] && exit 0
# enable --now: fail the injected missing unit, or every unit.
if [[ $1 == "--user" && $2 == "enable" && $3 == "--now" ]]; then
unit=$4
if [[ $unit == "${OMARCHY_TEST_MISSING_UNIT:-}" || -n ${OMARCHY_TEST_ALL_MISSING:-} ]]; then
Comment on lines +35 to +38

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Mock misses multi-unit calls The mock checks only $4 for the missing unit. If the script passed more than one unit per enable call, a missing unit in a later argument could go unnoticed by the mock, while real systemctl would reject the call. The log assertions do not require one unit per call, so the test could pass while missing the regression it is meant to catch. Check every unit argument and assert that each enable call contains just one unit.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

echo "Failed to enable unit: Unit $unit does not exist" >&2
exit 1
fi
exit 0
fi
exit 0
SH
chmod +x "$mock_bin/systemctl"

# The real one would write into $HOME and fail where OWE is not installed.
cat >"$mock_bin/omarchy-hook-install" <<'SH'
#!/bin/bash
printf 'omarchy-hook-install %s\n' "$*" >>"$OMARCHY_TEST_SYSTEMCTL_LOG"
SH
chmod +x "$mock_bin/omarchy-hook-install"

# Missing unit in the middle must not stop later units.
: >"$log"
export OMARCHY_TEST_SYSTEMCTL_LOG="$log"
export OMARCHY_TEST_MISSING_UNIT="omarchy-sleep-lock.service"
PATH="$mock_bin:/usr/bin:/bin" bash "$script" >"$test_tmp/out" 2>"$test_tmp/err" || status=$?
status=${status:-0}
(( status == 0 )) || fail "enable-user-units exits 0 when some units still enable" "status=$status err=$(cat "$test_tmp/err")"

grep -F 'enable --now bt-agent.service' "$log" >/dev/null ||
fail "enable-user-units still tries bt-agent" "$(cat "$log")"
grep -F 'enable --now omarchy-sleep-lock.service' "$log" >/dev/null ||
fail "enable-user-units still tries the missing unit" "$(cat "$log")"
grep -F 'enable --now omarchy-crash-watch.service' "$log" >/dev/null ||
fail "enable-user-units continues after a missing unit" "$(cat "$log")"
grep -F 'could not enable omarchy-sleep-lock.service' "$test_tmp/err" >/dev/null ||
fail "enable-user-units warns about the missing unit" "$(cat "$test_tmp/err")"
grep -Fx 'omarchy-hook-install theme-set /usr/share/owe/10-owe-sync' "$log" >/dev/null ||
fail "enable-user-units installs the OWE hook after a missing unit" "$(cat "$log")"
pass "enable-user-units continues after a missing unit"

# Prove the old multi-arg failure mode for contrast (documentation of the bug).
: >"$log"
cat >"$mock_bin/systemctl-atomic" <<'SH'
#!/bin/bash
printf '%s\n' "$*" >>"$OMARCHY_TEST_SYSTEMCTL_LOG"
if [[ $* == *enable* && $* == *does-not-exist* ]]; then
echo "Failed to enable unit: Unit does-not-exist.service does not exist" >&2
exit 1
fi
exit 0
SH
chmod +x "$mock_bin/systemctl-atomic"
if PATH="$mock_bin:$PATH" systemctl-atomic --user enable --now bt-agent.service does-not-exist.service 2>/dev/null; then
fail "multi-arg enable should fail when one unit is missing"
fi
# No partial enables recorded as separate lines for the good unit alone — the
# atomic call is one line that failed entirely.
lines=$(grep -c 'enable --now' "$log" || true)
(( lines == 1 )) || fail "atomic multi-arg enable is a single failed call" "$(cat "$log")"
pass "multi-arg enable fails the whole list when one unit is missing"

# All units present → clean success, no warnings.
: >"$log"
: >"$test_tmp/err"
unset OMARCHY_TEST_MISSING_UNIT
PATH="$mock_bin:/usr/bin:/bin" bash "$script" >"$test_tmp/out" 2>"$test_tmp/err"
[[ ! -s $test_tmp/err ]] || fail "enable-user-units is quiet when every unit enables" "$(cat "$test_tmp/err")"
for unit in bt-agent.service owed.service omarchy-recover-internal-monitor.service omarchy-sleep-lock.service \
omarchy-migrate-notify.service omarchy-fcitx5.service omarchy-crash-watch.service; do
grep -F "enable --now $unit" "$log" >/dev/null ||
fail "enable-user-units enables $unit" "$(cat "$log")"
done
pass "enable-user-units enables every shipped unit when all exist"

# No unit enables → first-run must see the step fail.
: >"$log"
status=0
OMARCHY_TEST_ALL_MISSING=1 PATH="$mock_bin:/usr/bin:/bin" bash "$script" >"$test_tmp/out" 2>"$test_tmp/err" || status=$?
(( status != 0 )) || fail "enable-user-units fails when no unit could be enabled" "$(cat "$test_tmp/err")"
grep -F 'no omarchy user units could be enabled' "$test_tmp/err" >/dev/null ||
fail "enable-user-units says why it failed" "$(cat "$test_tmp/err")"
pass "enable-user-units fails when no unit could be enabled"