diff --git a/arm.sh b/arm.sh index 47cc0e7..ef714d2 100755 --- a/arm.sh +++ b/arm.sh @@ -23,6 +23,9 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" readonly SCRIPT_DIR readonly USER_UNIT_DIR="$HOME/.config/systemd/user" +# shellcheck source=scripts/disarm_guard.sh +source "$SCRIPT_DIR/scripts/disarm_guard.sh" + # Units that must be on disk for enforcement to be schedulable. readonly UNITS=( "workout-locker.service" @@ -41,11 +44,13 @@ readonly TIMERS=( SKIP_TODAY=0 STATUS_ONLY=0 +REARM=0 usage() { - echo "Usage: $(basename "$0") [--skip-today] [--status]" + echo "Usage: $(basename "$0") [--skip-today] [--status] [--rearm]" echo " --skip-today Arm the locker but exempt today (no lock until tomorrow)" echo " --status Report whether enforcement is armed; change nothing" + echo " --rearm Clear the disarm marker + unmask units, then arm" exit 0 } @@ -62,10 +67,21 @@ add_skip_for_today() { main() { if ((STATUS_ONLY)); then + # --status changes nothing, so it stays usable while disarmed - but it + # must say so loudly, or "armed: no" reads like a bug instead of intent. + if report_disarmed; then + return + fi verify_armed return fi + if ((REARM)); then + clear_disarm_marker + else + refuse_if_disarmed + fi + echo "Installing units into $USER_UNIT_DIR" mkdir -p "$USER_UNIT_DIR" for unit in "${UNITS[@]}"; do @@ -103,6 +119,10 @@ while [[ $# -gt 0 ]]; do STATUS_ONLY=1 shift ;; + --rearm) + REARM=1 + shift + ;; -h | --help) usage ;; diff --git a/install.sh b/install.sh index b2305b6..0796c80 100755 --- a/install.sh +++ b/install.sh @@ -87,6 +87,12 @@ else fi # 3. systemd units ------------------------------------------------------------ +# This step installs *and enables* every unit in the repo, which would clobber +# the systemd mask symlinks. Respect the disarm marker; ./arm.sh --rearm undoes. +# shellcheck source=scripts/disarm_guard.sh +source "$SCRIPT_DIR/scripts/disarm_guard.sh" +refuse_if_disarmed + echo "[3/4] Installing systemd user units..." mkdir -p "$SYSTEMD_USER_DIR" shopt -s nullglob diff --git a/install_systemd.sh b/install_systemd.sh index 0b1deba..0d76728 100755 --- a/install_systemd.sh +++ b/install_systemd.sh @@ -14,6 +14,12 @@ LOCKER_TIMER_NAME="workout-locker.timer" SYNC_SERVICE_NAME="workout-sync.service" SYNC_TIMER_NAME="workout-sync.timer" +# shellcheck source=scripts/disarm_guard.sh +source "$SCRIPT_DIR/scripts/disarm_guard.sh" +# Installing units here would overwrite the systemd mask symlinks, so this +# path has to respect the disarm marker too. Re-arm via: ./arm.sh --rearm +refuse_if_disarmed + # Check if service is already installed if [ -f "$USER_SERVICE_DIR/$SERVICE_NAME" ]; then echo "Screen locker systemd service is already installed." diff --git a/scripts/disarm_guard.sh b/scripts/disarm_guard.sh new file mode 100755 index 0000000..afbde6c --- /dev/null +++ b/scripts/disarm_guard.sh @@ -0,0 +1,83 @@ +#!/bin/bash + +# ============================================================================ +# Disarm guard — shared by arm.sh, install_systemd.sh and install.sh. +# +# Masking the systemd units is not enough on its own. Every install path here +# *copies* unit files into ~/.config/systemd/user, which silently overwrites +# systemd's /dev/null mask symlinks and re-arms enforcement. So a mask alone +# loses to anyone — including a future agent session — running the obvious +# install command by reflex. +# +# The marker file is the deliberate second step: while it exists, no script +# may install units or enable timers. Removing it is an explicit `--rearm`. +# +# Source this file; it defines DISARM_MARKER, report_disarmed, +# refuse_if_disarmed and clear_disarm_marker. +# ============================================================================ + +# Machine-scoped, deliberately OUTSIDE the repo: a marker inside screen_locker/ +# would be erased by `git clean` and is gitignored state anyway. This guards the +# machine, not the checkout. +DISARM_MARKER="${DISARM_MARKER:-$HOME/.local/share/screen_locker/DISARMED}" +readonly DISARM_MARKER + +# Print the disarmed banner. Returns 0 when disarmed, 1 when armed. +report_disarmed() { + [[ -f $DISARM_MARKER ]] || return 1 + echo "================================================================" + echo " ENFORCEMENT DISARMED - the screen locker will not trigger." + echo "================================================================" + echo "Marker: $DISARM_MARKER" + if [[ -s $DISARM_MARKER ]]; then + sed 's/^/ | /' "$DISARM_MARKER" + fi + return 0 +} + +# Hard stop for any path that installs units or enables timers. +# Read-only paths (--status) should call report_disarmed instead. +refuse_if_disarmed() { + report_disarmed || return 0 + echo "" >&2 + echo "Refusing to arm while disarmed. To re-arm deliberately:" >&2 + echo " ./arm.sh --rearm" >&2 + exit 1 +} + +# Remove the marker and unmask the units, so a subsequent install can proceed. +# Masking is what `systemctl --user mask` left behind; `cp` would clobber those +# symlinks silently, so unmask explicitly and let systemd report failures. +clear_disarm_marker() { + local unit + local -a masked_units=( + "workout-locker.service" + "workout-locker.timer" + "early-bird-workout-check.timer" + "workout-sync.service" + "workout-sync.timer" + ) + + if [[ -f $DISARM_MARKER ]]; then + rm -f "$DISARM_MARKER" + echo "Removed disarm marker: $DISARM_MARKER" + fi + + for unit in "${masked_units[@]}"; do + # is-enabled prints "masked" for masked units; unmask only those, so a + # normal re-arm does not spew errors about units that were never masked. + if [[ $(systemctl --user is-enabled "$unit" 2>/dev/null) == masked* ]]; then + systemctl --user unmask "$unit" + fi + done + + # Restore any unit files that were moved aside when disarming, so `cp` in + # the caller is not the only thing standing between here and a working unit. + local stash + for stash in "$HOME/.config/systemd/user"/disabled-*/; do + [[ -d $stash ]] || continue + echo "Note: preserved unit files remain in $stash" + done + + systemctl --user daemon-reload +}