steam-backlog-enforcer/install.sh
Krzysztof kuhy Rudnicki 0c05d71008 Add a daily gaming-time budget with a binary-level cutoff
Accumulates time spent in Steam games and other launchers, and once the
daily budget (default 8h) is spent, shuts Steam down, kills the games, and
makes them unlaunchable until the next 06:00.

The gaming day starts at 06:00 local rather than midnight so a late session
counts against the day it began in; rollover is checked lazily against a
stored day key, so it survives suspend, reboot and daemon restarts with no
timer of its own. Elapsed time comes from wall-clock deltas clamped to two
tick intervals, which keeps suspends, missed ticks and NTP steps honest.

The block is a read-only bind mount of a refusal stub over all four launcher
entry points -- /usr/bin/steam is only a two-line exec wrapper, so masking it
alone leaves bin_steam.sh, Steam's own steam.sh and lutris live. A bind mount
mutates no bytes, umount restores exactly, and a reboot clears it
unconditionally; chmod/rename would leave Steam broken if the daemon died
mid-block. /proc/self/mountinfo is the authority on what is masked, never the
saved state, which is what makes a stale block release even if the state file
was deleted. The mount is deferred one tick past the cutoff so
`steam -shutdown` still works and games close cleanly.

Launchers are matched by cmdline, not comm: /usr/bin/lutris is a python3
script, so the kernel records its comm as "python3" and the existing scan
never sees it. The kill set is wider than the budget predicate, walking down
from each launcher, because a Lutris-launched Wine game carries no SteamAppId.

State is chattr +i'd -- ~/.config is user-writable, so root ownership alone
would not stop `rm` from resetting the counter -- and a missing state file
while mounts are live fails closed rather than lifting the block.

Also fixes a pre-existing bug: do_enforce returned when no game was assigned,
exiting the process. Since the budget is accounted for from inside that loop,
it silently stopped enforcing whenever a game was finished but not rescanned.

Verified on the desktop with a real game: AoE2 counted 18 processes, tripped
at the 60s demo budget, all processes killed, all four paths masked and
refusing, then gaming-unblock restored them byte-identical with
`pacman -Qkk steam lutris` reporting 0 altered files.

1322 tests pass at 100.00% branch coverage; pacman validated the ALPM hook.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QbEaXapjMSkxyzgvbdkUFb
2026-08-04 20:00:08 +02:00

77 lines
3.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Install script for Steam Backlog Enforcer.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
echo "=== Steam Backlog Enforcer Installer ==="
echo
# Install Python deps.
echo "Installing Python dependencies..."
pip3 install --break-system-packages requests howlongtobeatpy 2>/dev/null \
|| pip3 install requests howlongtobeatpy
# 'block-gaming' depends on guard-lib (guardctl) for tamper-resistant
# locking. Not fatal if missing - the rest of this tool works without it.
echo
echo "Checking for guard-lib (required by 'block-gaming')..."
if command -v guardctl >/dev/null 2>&1; then
echo "guardctl found on PATH."
elif [[ -x "$HOME/utils/guard-lib/install.sh" ]]; then
echo "guardctl not found - installing guard-lib from $HOME/utils/guard-lib..."
if [[ $EUID -eq 0 ]]; then
bash "$HOME/utils/guard-lib/install.sh"
else
echo "guard-lib install needs root: sudo bash \"$HOME/utils/guard-lib/install.sh\""
echo "('block-gaming' will not work until that is done; the rest of this tool is unaffected.)"
fi
else
echo "Warning: guardctl not found and ~/utils/guard-lib is not present."
echo "'block-gaming' requires guard-lib - set up ~/utils/guard-lib and run its install.sh, then re-run this installer."
echo "(The rest of this tool is unaffected.)"
fi
# Install systemd service (system-level, runs as root).
read -rp "Install systemd enforce service? [y/N] " ans
if [[ "${ans,,}" == "y" ]]; then
if [[ $EUID -ne 0 ]]; then
echo "Error: systemd service install needs root. Re-run with sudo."
exit 1
fi
SERVICE_SRC="$SCRIPT_DIR/steam-backlog-enforcer.service"
SERVICE_DST="/etc/systemd/system/steam-backlog-enforcer.service"
# Set the correct working directory, PYTHONPATH, and desktop user (the
# user whose Steam/X11 session the service should drive) in the service
# file. install.sh requires root, so it's invoked via `sudo` and
# SUDO_USER holds the real desktop user.
DESKTOP_USER="${SUDO_USER:-$USER}"
sed "s|WorkingDirectory=.*|WorkingDirectory=$SCRIPT_DIR|; \
s|PYTHONPATH=.*|PYTHONPATH=$SCRIPT_DIR|; \
s|__DESKTOP_USER__|$DESKTOP_USER|" \
"$SERVICE_SRC" > "$SERVICE_DST"
# Daily-gaming-budget pacman hook. While the budget is exhausted a refusal
# stub is bind-mounted over /usr/bin/steam and friends; a bind mount makes
# package extraction fail EBUSY, so the mounts must be dropped before any
# transaction touching those packages.
HOOK_SRC="$SCRIPT_DIR/pacman-hooks/50-steam-backlog-playtime-unblock.hook"
HOOK_DST="/etc/pacman.d/hooks/50-steam-backlog-playtime-unblock.hook"
mkdir -p /etc/pacman.d/hooks
install -m 644 "$HOOK_SRC" "$HOOK_DST"
echo "Installed pacman hook: $HOOK_DST"
systemctl daemon-reload
systemctl enable steam-backlog-enforcer
echo "Service installed and enabled."
echo " Start now: sudo systemctl start steam-backlog-enforcer"
echo " Check: sudo systemctl status steam-backlog-enforcer"
echo " Logs: sudo journalctl -u steam-backlog-enforcer -f"
fi
echo
echo "Done! Run manually with:"
echo " python3 -m steam_backlog_enforcer.main enforce"