steam-backlog-enforcer/pacman-hooks/50-steam-backlog-playtime-unblock.hook
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

30 lines
1.3 KiB
Plaintext

# steam-backlog-enforcer: release daily-gaming-budget bind mounts before pacman
# touches the packages they mask.
#
# While the daily gaming budget is exhausted, a refusal stub is bind-mounted
# over the launcher entry points. A bind-mounted file is a mountpoint, so
# unlink(2)/rename(2) over it returns EBUSY and package extraction fails. This
# hook clears the mounts for the duration of the transaction; the enforcer's
# 3-second tick re-applies them afterwards, so there is no PostTransaction half.
#
# The daemon side also refuses to (re-)mount while /var/lib/pacman/db.lck
# exists, which is what stops a tick from remounting mid-transaction.
#
# KEEP THE TARGET LIST IN SYNC with BLOCK_TARGETS in
# steam_backlog_enforcer/_playtime_block.py. The user-owned
# ~/.local/share/Steam/steam.sh is deliberately absent here: it is not owned by
# any package, so no pacman transaction can collide with it.
[Trigger]
Operation = Install
Operation = Upgrade
Operation = Remove
Type = Package
Target = steam
Target = lutris
[Action]
Description = steam-backlog-enforcer: releasing playtime bind mounts
When = PreTransaction
Exec = /bin/sh -c 'for t in /usr/bin/steam /usr/lib/steam/bin_steam.sh /usr/bin/lutris; do i=0; while mountpoint -q "$t" && [ "$i" -lt 20 ]; do umount -l "$t" || break; i=$((i+1)); done; done'