mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-26 01:27:24 +01:00
Compare commits
2 Commits
189cd73747
...
88eda8dc51
| Author | SHA1 | Date | |
|---|---|---|---|
| 88eda8dc51 | |||
| e393207e3a |
@ -0,0 +1,42 @@
|
||||
{
|
||||
"intent": "Make the morning wake-alarm floor (lockdown-entry + MIN_WAKE_AFTER_LOCKDOWN_HOURS) correct for ANY shutdown time, not just the shutdown hours the static 05:00/05:15/05:30/06:00/07:00 unlock triggers happen to cover.",
|
||||
"scope": [
|
||||
"linux_configuration/scripts/periodic_background/digital_wellbeing/setup_night_lockdown.sh (enter-script generator section only)",
|
||||
"Non-goal: did not remove or resize the existing staggered unlock triggers, which still handle console/GUI restore and remain a redundant fallback for the alarm in the common case"
|
||||
],
|
||||
"changes": [
|
||||
"Identified a gap in the earlier same-day fix: shutdown time is dynamically configurable (/etc/shutdown-schedule.conf, changed by the screen_locker sick-day feature) and can, in principle, be any hour. If a lockdown started late enough that entry+8h fell after the last static trigger (07:00), the alarm would silently never fire that morning -- confirmed via isolated arithmetic test (23:30 lockdown -> 07:30 floor, past every static trigger).",
|
||||
"Enter script now schedules a transient one-shot systemd-run --on-calendar timer, computed at lockdown-entry time, that fires at exactly lockdown_epoch + MIN_WAKE_AFTER_LOCKDOWN_HOURS and re-runs the (idempotent, self-dedup'd) unlock script -- guaranteeing the alarm gets a correctly-timed chance to fire regardless of what time lockdown actually started.",
|
||||
"Added MIN_WAKE_AFTER_LOCKDOWN_HOURS to the enter-script's own config default-fallback block (previously only the unlock script had it)."
|
||||
],
|
||||
"verification": [
|
||||
{
|
||||
"command": "bash -n setup_night_lockdown.sh; shellcheck -s bash setup_night_lockdown.sh (plus extracted enter-script heredoc body)",
|
||||
"result": "pass",
|
||||
"evidence": "Only pre-existing SC1091 info notice; no new warnings."
|
||||
},
|
||||
{
|
||||
"command": "sudo systemd-run --unit=night-lockdown-wake-floor-test --collect --on-calendar=\"<+20s>\" logger ...; wait; journalctl -t ...",
|
||||
"result": "pass",
|
||||
"evidence": "Transient one-shot timer fired at the exact scheduled second and self-removed via --collect (systemctl status afterward: 'could not be found')."
|
||||
},
|
||||
{
|
||||
"command": "sudo setup_night_lockdown.sh setup; sudo DRY_RUN=1 night-lockdown-enter.sh",
|
||||
"result": "pass",
|
||||
"evidence": "Redeployed cleanly; DRY_RUN log now includes 'would schedule a one-shot wake-floor trigger ~8h from now'; real STATE_FILE untouched (still UNLOCKED)."
|
||||
},
|
||||
{
|
||||
"command": "Isolated arithmetic check with a scratch STATE_DIR, simulating a 23:30 lockdown",
|
||||
"result": "pass",
|
||||
"evidence": "floor computed as 2026-07-19 07:30:00 -- correctly past the last static 07:00 trigger, demonstrating why the dynamic one-shot is necessary and that the math is correct."
|
||||
}
|
||||
],
|
||||
"risks": [
|
||||
"Real end-to-end behavior (does the scheduled one-shot actually re-run the unlock script and start the alarm at the exact floor moment during a genuine overnight cycle) is not yet confirmed live -- deferred to the next real lockdown cycle.",
|
||||
"systemd-run --unit name is static (night-lockdown-wake-floor); a stale leftover transient unit with the same name from a crashed prior run could in theory cause this scheduling call to fail. It runs through the existing run() wrapper, which logs a WARN and continues rather than aborting lockdown, and the static triggers remain a fallback for shutdown hours they do cover."
|
||||
],
|
||||
"rollback": [
|
||||
"git revert this commit and re-run 'sudo setup_night_lockdown.sh setup' to redeploy the prior script version.",
|
||||
"Manually remove any lingering night-lockdown-wake-floor.timer/.service with 'systemctl stop/reset-failed' if one is ever left active after a revert."
|
||||
]
|
||||
}
|
||||
@ -0,0 +1,49 @@
|
||||
{
|
||||
"intent": "Fix three live night-lockdown/wake bugs: (1) console briefly showed old kernel/systemd text instead of going black, (2) DP-0 did not reliably reconnect after wake, (3) the morning wake alarm could fire less than 8h after a late lockdown entry (23:00 lockdown -> 05:00 alarm).",
|
||||
"scope": [
|
||||
"linux_configuration/scripts/periodic_background/digital_wellbeing/setup_night_lockdown.sh",
|
||||
"Deployed artifacts: /usr/local/bin/night-lockdown-{enter,unlock}.sh, /etc/night-lockdown.conf",
|
||||
"One-time manual action: autorandr --save default (outside the script)",
|
||||
"Non-goal: did not redesign the VT-blank approach to an X-stays-alive black overlay (deferred fallback if this smaller fix doesn't hold)"
|
||||
],
|
||||
"changes": [
|
||||
"Enter script: blank the console in a background poller for the full duration of 'systemctl stop lightdm.service' (plus a final guaranteed blank after), instead of a single blank call only once stop returns, closing the visible-text gap.",
|
||||
"Enter script: stamp $STATE_DIR/locked_at_epoch when entering lockdown.",
|
||||
"Unlock script: add step 2a — retry 'autorandr --change --default default' for up to 15s as the desktop user after lightdm restarts, to recover a DisplayPort link that didn't re-enumerate cleanly.",
|
||||
"Unlock script: decouple maybe_start_morning_alarm() from the LOCKED/UNLOCKED idempotency gate (now also called from the already-UNLOCKED early-exit branch) so it can fire on a later staggered trigger (05:15/05:30/06:00/07:00) instead of only the first one.",
|
||||
"maybe_start_morning_alarm(): add MIN_WAKE_AFTER_LOCKDOWN_HOURS (default 8) floor check against locked_at_epoch, and its own alarm_started_date daily dedup marker.",
|
||||
"New config vars in /etc/night-lockdown.conf: MIN_WAKE_AFTER_LOCKDOWN_HOURS=8, AUTORANDR_PROFILE=default.",
|
||||
"Added explicit DRY_RUN NOTE banners to both scripts stating DRY_RUN cannot validate the console-blank timing race or the autorandr reconnect (pure log-only mode), so a clean dry run is never mistaken for proof those specific fixes work."
|
||||
],
|
||||
"verification": [
|
||||
{
|
||||
"command": "bash -n setup_night_lockdown.sh; shellcheck -s bash setup_night_lockdown.sh (plus extracted enter/unlock heredoc bodies)",
|
||||
"result": "pass",
|
||||
"evidence": "Only pre-existing SC1091 info notice (unrelated source of ../../lib/common.sh); no new warnings."
|
||||
},
|
||||
{
|
||||
"command": "sudo setup_night_lockdown.sh setup",
|
||||
"result": "pass",
|
||||
"evidence": "Redeployed /usr/local/bin/night-lockdown-{enter,unlock}.sh and /etc/night-lockdown.conf; verification block reported all files present and timer enabled."
|
||||
},
|
||||
{
|
||||
"command": "sudo DRY_RUN=1 night-lockdown-enter.sh / night-lockdown-unlock.sh, with locked_at_epoch temporarily set to 0h-ago and 9h-ago",
|
||||
"result": "pass",
|
||||
"evidence": "0h-ago logged 'too soon since lockdown entry (480m remaining) - skipping alarm this trigger'; 9h-ago proceeded to 'would wait for graphical session then start morning-routine.service'. Real STATE_FILE untouched under DRY_RUN and restored from backup after manual LOCKED-state tests."
|
||||
},
|
||||
{
|
||||
"command": "autorandr --save default; autorandr --change --default default",
|
||||
"result": "pass",
|
||||
"evidence": "Saved profile matches current live DP-0 (3840x2160 primary) + HDMI-0 (2560x1440) layout; reapplication reports 'default (detected) (current)'."
|
||||
}
|
||||
],
|
||||
"risks": [
|
||||
"Real-world effectiveness of the console-blank poller and the autorandr reconnect is NOT yet confirmed by a live (non-DRY_RUN) cycle -- user declined a live disruptive test during this session; verification deferred to tonight's real 23:00->05:00 cycle, to be checked via journalctl the next morning.",
|
||||
"autorandr recovery depends on the one-time saved profile staying valid; if the monitor arrangement changes, the profile must be re-saved or the unlock step will WARN and require manual xrandr.",
|
||||
"If the console-blank timing race turns out not to be fully fixed by reordering/hardening, the user has already agreed the fallback is a bigger redesign (X-stays-alive black overlay) rather than further patching this approach."
|
||||
],
|
||||
"rollback": [
|
||||
"git revert this commit and re-run 'sudo setup_night_lockdown.sh setup' to redeploy the prior script version.",
|
||||
"After rollback, confirm /etc/night-lockdown.conf no longer references MIN_WAKE_AFTER_LOCKDOWN_HOURS/AUTORANDR_PROFILE and that a lockdown/unlock cycle still completes without error."
|
||||
]
|
||||
}
|
||||
@ -129,6 +129,18 @@ CONSOLE_BLANK_MODE="1"
|
||||
ALARM_DAYS_DOW="1 5 6 7"
|
||||
# User systemd unit that runs the morning routine (wake alarm + workout lock).
|
||||
MORNING_ROUTINE_UNIT="morning-routine.service"
|
||||
|
||||
# Never start the morning alarm earlier than lockdown-entry + this many hours,
|
||||
# even though the unlock timer itself still fires on its normal staggered
|
||||
# schedule (05:00/05:15/05:30/06:00/07:00). A late lockdown entry (e.g. 23:00
|
||||
# instead of the usual 21:00) must not still wake you 6h later.
|
||||
MIN_WAKE_AFTER_LOCKDOWN_HOURS="8"
|
||||
|
||||
# autorandr profile to restore after the GUI comes back up. Create it once with
|
||||
# 'autorandr --save default' while all monitors are connected and arranged the
|
||||
# way you want; the unlock script re-applies it so a DisplayPort link that
|
||||
# didn't re-enumerate cleanly doesn't require a manual xrandr/replug.
|
||||
AUTORANDR_PROFILE="default"
|
||||
EOF
|
||||
chmod 0644 "$CONF_FILE"
|
||||
|
||||
@ -174,12 +186,21 @@ ALSA_CARDS="${ALSA_CARDS:-}"
|
||||
RGB_ENABLE="${RGB_ENABLE:-1}"
|
||||
MONITORED_USER_UNITS="${MONITORED_USER_UNITS:-}"
|
||||
MONITORED_PROCS="${MONITORED_PROCS:-}"
|
||||
MIN_WAKE_AFTER_LOCKDOWN_HOURS="${MIN_WAKE_AFTER_LOCKDOWN_HOURS:-8}"
|
||||
CONSOLE_TTY="${CONSOLE_TTY:-/dev/tty1}"
|
||||
CONSOLE_BLANK_MODE="${CONSOLE_BLANK_MODE:-1}"
|
||||
DRY_RUN="${DRY_RUN:-}"
|
||||
|
||||
logg() { logger -t night-lockdown "$*"; printf 'night-lockdown: %s\n' "$*"; }
|
||||
|
||||
if [[ "$DRY_RUN" == "1" ]]; then
|
||||
logg "DRY_RUN NOTE: log-only — lightdm is never actually stopped and the"
|
||||
logg "DRY_RUN NOTE: console framebuffer is never actually touched. This CANNOT"
|
||||
logg "DRY_RUN NOTE: exercise the blank-vs-VT-switch timing race (bug: visible"
|
||||
logg "DRY_RUN NOTE: console text). A clean DRY_RUN run is NOT proof that fix"
|
||||
logg "DRY_RUN NOTE: works — only a real run (DRY_RUN unset) validates it."
|
||||
fi
|
||||
|
||||
# Run a command best-effort: log-only under DRY_RUN, never abort the lockdown.
|
||||
run() {
|
||||
if [[ "$DRY_RUN" == "1" ]]; then
|
||||
@ -283,6 +304,27 @@ done
|
||||
# (05:00 timer, dead-man, or manual) then correctly restores.
|
||||
if [[ "$DRY_RUN" != "1" ]]; then
|
||||
echo LOCKED >"$STATE_FILE"
|
||||
lockdown_epoch="$(date +%s)"
|
||||
echo "$lockdown_epoch" >"$STATE_DIR/locked_at_epoch"
|
||||
|
||||
# 5a. Schedule a one-shot wake-floor trigger for exactly
|
||||
# lockdown_epoch + MIN_WAKE_AFTER_LOCKDOWN_HOURS. The staggered
|
||||
# 05:00/05:15/05:30/06:00/07:00 unlock triggers are a fixed list sized
|
||||
# for the usual shutdown hours (~21:00-23:00), but the shutdown hour
|
||||
# itself is configurable (/etc/shutdown-schedule.conf) and changes
|
||||
# dynamically (see the sick-day feature). If lockdown ever starts late
|
||||
# enough that the floor lands after every remaining fixed trigger, the
|
||||
# alarm would otherwise silently never fire that morning. This
|
||||
# transient timer fires at the exact floor moment for THIS lockdown
|
||||
# regardless of what time it actually started, re-running the
|
||||
# (idempotent, self-dedup'd) unlock script, which is where the alarm
|
||||
# is actually attempted.
|
||||
floor_epoch=$((lockdown_epoch + MIN_WAKE_AFTER_LOCKDOWN_HOURS * 3600))
|
||||
floor_cal="$(date -d "@$floor_epoch" '+%Y-%m-%d %H:%M:%S')"
|
||||
run systemd-run --unit=night-lockdown-wake-floor --collect \
|
||||
--on-calendar="$floor_cal" /usr/local/bin/night-lockdown-unlock.sh
|
||||
else
|
||||
logg "DRY_RUN: would schedule a one-shot wake-floor trigger ~${MIN_WAKE_AFTER_LOCKDOWN_HOURS}h from now"
|
||||
fi
|
||||
|
||||
# 6a. Silence the console BEFORE stopping the GUI, so the teardown itself
|
||||
@ -300,10 +342,30 @@ silence_console
|
||||
run systemctl mask getty@.service
|
||||
run systemctl stop 'getty@*.service'
|
||||
|
||||
# 7. Stop the GUI. This hands the VT back to fbcon, which unblanks it.
|
||||
# 7. Stop the GUI while continuously re-blanking the console in the
|
||||
# background. Activating a VT (which happens somewhere mid-way through the
|
||||
# GUI stop, not only once "systemctl stop" fully returns) makes fbcon
|
||||
# redraw/unblank as part of the switch, so a single blank-after-the-fact
|
||||
# call leaves a gap showing whatever was last on the console (old
|
||||
# kernel/systemd text) for however long the rest of the stop sequence
|
||||
# takes. Keep forcing it dark for the whole teardown instead of waiting.
|
||||
if [[ "$DRY_RUN" != "1" ]]; then
|
||||
(
|
||||
while :; do
|
||||
blank_console
|
||||
sleep 0.1
|
||||
done
|
||||
) &
|
||||
BLANK_POLLER_PID=$!
|
||||
fi
|
||||
run systemctl stop lightdm.service
|
||||
if [[ -n "${BLANK_POLLER_PID:-}" ]]; then
|
||||
kill "$BLANK_POLLER_PID" 2>/dev/null || true
|
||||
wait "$BLANK_POLLER_PID" 2>/dev/null || true
|
||||
unset BLANK_POLLER_PID
|
||||
fi
|
||||
|
||||
# 8. Now that fbcon owns the VT again, force it dark (DPMS off -> standby).
|
||||
# 8. Final guaranteed blank now that the GUI is fully down.
|
||||
blank_console
|
||||
|
||||
logg "night lockdown active — servers up, GUI down, login surface masked, console dark"
|
||||
@ -337,6 +399,8 @@ LOCK_USER="${LOCK_USER:-}"
|
||||
LOCK_UID="${LOCK_UID:-}"
|
||||
ALARM_DAYS_DOW="${ALARM_DAYS_DOW:-}"
|
||||
MORNING_ROUTINE_UNIT="${MORNING_ROUTINE_UNIT:-}"
|
||||
MIN_WAKE_AFTER_LOCKDOWN_HOURS="${MIN_WAKE_AFTER_LOCKDOWN_HOURS:-8}"
|
||||
AUTORANDR_PROFILE="${AUTORANDR_PROFILE:-default}"
|
||||
CONSOLE_TTY="${CONSOLE_TTY:-/dev/tty1}"
|
||||
RGB_ENABLE="${RGB_ENABLE:-1}"
|
||||
RGB_OFF_MODE="${RGB_OFF_MODE:-static}"
|
||||
@ -346,6 +410,16 @@ DRY_RUN="${DRY_RUN:-}"
|
||||
|
||||
logg() { logger -t night-lockdown "$*"; printf 'night-lockdown: %s\n' "$*"; }
|
||||
|
||||
if [[ "$DRY_RUN" == "1" ]]; then
|
||||
logg "DRY_RUN NOTE: log-only — lightdm is never actually started and"
|
||||
logg "DRY_RUN NOTE: autorandr is never actually invoked. This CANNOT exercise"
|
||||
logg "DRY_RUN NOTE: the real DisplayPort reconnect or graphical-session wait."
|
||||
logg "DRY_RUN NOTE: A clean DRY_RUN run is NOT proof those work — only a real"
|
||||
logg "DRY_RUN NOTE: run (DRY_RUN unset) validates them. The alarm floor/dedup"
|
||||
logg "DRY_RUN NOTE: logic (locked_at_epoch, alarm_started_date) IS exercised"
|
||||
logg "DRY_RUN NOTE: faithfully here, since that part is pure calculation."
|
||||
fi
|
||||
|
||||
run() {
|
||||
if [[ "$DRY_RUN" == "1" ]]; then
|
||||
logg "DRY_RUN: would run: $*"
|
||||
@ -358,11 +432,14 @@ run() {
|
||||
|
||||
# Fire the morning wake alarm on alarm days. The always-on lockdown removed the
|
||||
# hibernate/resume event that used to start morning-routine.service, so we start
|
||||
# it here once the desktop session is back. Only runs when we actually unlocked
|
||||
# from a real lockdown (this function is reached only past the idempotency gate).
|
||||
# it here once the desktop session is back. Called on every staggered trigger
|
||||
# (not just the one that performs the console/GUI restore), because
|
||||
# MIN_WAKE_AFTER_LOCKDOWN_HOURS below may push the actual alarm start past the
|
||||
# trigger that unlocked the screen — dedup is its own alarm_started_date marker,
|
||||
# independent of the LOCKED/UNLOCKED state file.
|
||||
maybe_start_morning_alarm() {
|
||||
[[ -n "$LOCK_USER" && -n "$LOCK_UID" && -n "$MORNING_ROUTINE_UNIT" ]] || return 0
|
||||
local dow today_is_alarm_day=false d
|
||||
local dow today_is_alarm_day=false d today
|
||||
dow="$(date +%u)" # 1=Mon .. 7=Sun
|
||||
for d in $ALARM_DAYS_DOW; do
|
||||
[[ "$d" == "$dow" ]] && today_is_alarm_day=true
|
||||
@ -371,6 +448,23 @@ maybe_start_morning_alarm() {
|
||||
logg "not an alarm day (dow=$dow) — skipping morning alarm"
|
||||
return 0
|
||||
fi
|
||||
today="$(date +%F)"
|
||||
if [[ "$(cat "$STATE_DIR/alarm_started_date" 2>/dev/null || echo '')" == "$today" ]]; then
|
||||
logg "morning alarm already started today — skipping"
|
||||
return 0
|
||||
fi
|
||||
# Never wake earlier than lockdown-entry + MIN_WAKE_AFTER_LOCKDOWN_HOURS. A
|
||||
# late lockdown entry (e.g. 23:00 instead of the usual 21:00) must not still
|
||||
# fire the alarm at the normal 05:00 trigger; wait for a later staggered
|
||||
# trigger (05:15/05:30/06:00/07:00) that clears the floor instead.
|
||||
local locked_at now min_wake
|
||||
locked_at="$(cat "$STATE_DIR/locked_at_epoch" 2>/dev/null || echo 0)"
|
||||
now="$(date +%s)"
|
||||
min_wake=$((locked_at + MIN_WAKE_AFTER_LOCKDOWN_HOURS * 3600))
|
||||
if ((locked_at > 0 && now < min_wake)); then
|
||||
logg "too soon since lockdown entry ($(( (min_wake - now) / 60 ))m remaining) — skipping alarm this trigger"
|
||||
return 0
|
||||
fi
|
||||
if [[ "$DRY_RUN" == "1" ]]; then
|
||||
logg "DRY_RUN: would wait for graphical session then start $MORNING_ROUTINE_UNIT"
|
||||
return 0
|
||||
@ -387,13 +481,17 @@ maybe_start_morning_alarm() {
|
||||
logg "alarm day (dow=$dow) — starting $MORNING_ROUTINE_UNIT"
|
||||
run sudo -u "$LOCK_USER" env "XDG_RUNTIME_DIR=/run/user/$LOCK_UID" \
|
||||
systemctl --user start --no-block "$MORNING_ROUTINE_UNIT"
|
||||
echo "$today" >"$STATE_DIR/alarm_started_date"
|
||||
}
|
||||
|
||||
mkdir -p "$STATE_DIR"
|
||||
|
||||
# Idempotency — lifting when already unlocked is a safe no-op.
|
||||
# Idempotency — lifting when already unlocked is a safe no-op for the
|
||||
# console/GUI restore, but a still-pending morning alarm (delayed past this
|
||||
# trigger by MIN_WAKE_AFTER_LOCKDOWN_HOURS) gets one more chance to fire.
|
||||
if [[ "$(cat "$STATE_FILE" 2>/dev/null || echo UNLOCKED)" == "UNLOCKED" ]]; then
|
||||
logg "already UNLOCKED — nothing to do"
|
||||
maybe_start_morning_alarm
|
||||
logg "already UNLOCKED — nothing else to do"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
@ -433,6 +531,32 @@ run systemctl unmask getty@.service
|
||||
# 2. Start the GUI — lightdm autologins back into i3/dwm.
|
||||
run systemctl start lightdm.service
|
||||
|
||||
# 2a. Reconnect/reconfigure displays. A DisplayPort link does not always
|
||||
# re-enumerate cleanly on its own after the console teardown, leaving a
|
||||
# monitor disconnected until someone runs xrandr by hand. Retry autorandr
|
||||
# for a bit while the session comes up instead. Requires a saved profile
|
||||
# ('autorandr --save default' run once while all monitors are connected).
|
||||
if [[ -n "$LOCK_USER" && -n "$LOCK_UID" ]] && command -v autorandr >/dev/null 2>&1; then
|
||||
if [[ "$DRY_RUN" == "1" ]]; then
|
||||
logg "DRY_RUN: would run autorandr --change as $LOCK_USER"
|
||||
else
|
||||
autorandr_ok=false
|
||||
for ((i = 0; i < 15; i++)); do
|
||||
if sudo -u "$LOCK_USER" env "XDG_RUNTIME_DIR=/run/user/$LOCK_UID" DISPLAY=:0 \
|
||||
autorandr --change --default "$AUTORANDR_PROFILE" >/dev/null 2>&1; then
|
||||
autorandr_ok=true
|
||||
break
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
if [[ "$autorandr_ok" == true ]]; then
|
||||
logg "autorandr: displays restored (profile '$AUTORANDR_PROFILE')"
|
||||
else
|
||||
logg "WARN: autorandr did not settle after retries — displays may need manual xrandr"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
# 3. Restore audio (unmute the cards muted at lockdown).
|
||||
for card in $ALSA_CARDS; do
|
||||
command -v amixer >/dev/null 2>&1 && run amixer -c "$card" -q set Master unmute
|
||||
|
||||
Loading…
Reference in New Issue
Block a user