"""Daily gaming-time budget: accounting, day rollover and cutoff policy. Tracks how long the user has spent gaming during the current *gaming day* and, once the budget is exhausted, drives the cutoff sequence implemented in :mod:`steam_backlog_enforcer._playtime_block`. Three design points that are load-bearing: **The gaming day starts at 06:00 local, not midnight.** A session that runs past midnight belongs to the day it started in; a midnight boundary would hand out a fresh 8-hour budget in the middle of a late session. Rollover is checked lazily on every tick against a stored day key rather than scheduled, so it survives suspend, reboot and daemon restarts with no timer of its own. **Elapsed time is measured from wall-clock deltas, clamped.** Counting "one tick = one interval" silently under-counts whenever the loop is slow and over-counts nothing; measuring the raw delta over-counts wildly across a suspend. Clamping each delta to a small multiple of the tick interval makes suspends, missed ticks, daemon restarts and NTP steps all behave. **Launchers are matched by cmdline, not by ``comm``.** ``/usr/bin/lutris`` is a ``#!/usr/bin/env python3`` script, so the kernel records its ``comm`` as ``python3`` and the ``comm``-based scan in :mod:`enforcer` never sees it. That is why :func:`get_pids_by_cmdline_names` exists alongside ``enforcer.get_pids_by_process_names`` rather than replacing it. """ from __future__ import annotations from dataclasses import dataclass, field, replace from datetime import datetime, timedelta, timezone import json import logging import os from pathlib import Path from typing import TYPE_CHECKING from steam_backlog_enforcer._playtime_block import ( kill_gaming_processes, mounted_targets, reconcile, request_steam_shutdown, steam_and_launcher_pids, ) from steam_backlog_enforcer._total_block import ( LAUNCHER_PROCESS_NAMES, is_total_block_active, ) from steam_backlog_enforcer._whitelist import _try_set_immutable, unlock_for_write from steam_backlog_enforcer.config import CONFIG_DIR, _atomic_write from steam_backlog_enforcer.enforcer import ( get_pids_by_process_names, get_running_steam_game_pids, ) from steam_backlog_enforcer.library_hider import _resolve_desktop_user, _run_as_user if TYPE_CHECKING: from steam_backlog_enforcer.config import Config logger = logging.getLogger(__name__) PLAYTIME_STATE_FILE = CONFIG_DIR / "playtime_state.json" PLAYTIME_DEMO_STATE_FILE = CONFIG_DIR / "playtime_demo_state.json" _PROC = Path("/proc") _SCHEMA_VERSION = 1 # The gaming day starts here, local time. 05:59 belongs to the previous day. _DAY_BOUNDARY_HOURS = 6 # Each tick's measured delta is capped at this many tick intervals. Two is the # smallest value that tolerates one skipped tick without inflating the count. _DELTA_CLAMP_FACTOR = 2.0 _DEFAULT_BUDGET_SECONDS = 8 * 3600.0 _DEMO_BUDGET_SECONDS = 60.0 # Seconds remaining at which to warn, descending. _WARN_AT: tuple[int, ...] = (3600, 1800, 600, 300) _DEMO_WARN_AT: tuple[int, ...] = (30, 10) # Grace between "budget exhausted" and masking the Steam binaries. Masking # makes `steam -shutdown` a no-op, so Steam must be given a moment to close # its games cleanly first — otherwise every single cutoff is a hard SIGTERM. _SHUTDOWN_GRACE_SECONDS = 3.0 _SIGKILL_AFTER_SECONDS = 30.0 _DEMO_SIGKILL_AFTER_SECONDS = 10.0 # argv[0] basenames that tell us nothing: the real program is argv[1]. _INTERPRETERS = frozenset({"bash", "env", "java", "perl", "python", "python3", "sh"}) # An interpreter invocation needs at least `