From 7c36f821b1a476954d8d10e0f6a107421bbc4dc5 Mon Sep 17 00:00:00 2001 From: Lef Date: Tue, 10 Nov 2026 23:33:58 +0200 Subject: [PATCH 1/2] omamobile-sleepd: notice a resume when logind says so, not at the next poll The watcher is frozen inside its sleep while the phone sleeps, so after a resume it only noticed it, and lit the PIN pad for a power-key wake, when the rest of that sleep ran out: 4.9 s after the kernel finished resuming on the Pixel 8 Pro with the toggle off (the 5 s poll), up to 2 s with it on. Listen for logind's PrepareForSleep(false) with busctl wait (a user service can't use busctl monitor) and end the wait with USR2 so the loop reads suspend_stats at once. PrepareForSleep(true) is ignored. The poll stays as the fallback: a raw write to /sys/power/state never sends the signal. The listener and busctl under it are killed with the watcher. Tests: a FIFO stands in for logind, a one-shot real hang for the frozen sleep; the new case fails on the old script. --- packages/omamobile-shell/bin/omamobile-sleepd | 45 ++++++++++++++++- packages/omamobile-shell/test/sleepd.sh | 48 ++++++++++++++++++- 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/packages/omamobile-shell/bin/omamobile-sleepd b/packages/omamobile-shell/bin/omamobile-sleepd index 491f476c9..04b66808d 100755 --- a/packages/omamobile-shell/bin/omamobile-sleepd +++ b/packages/omamobile-shell/bin/omamobile-sleepd @@ -127,21 +127,64 @@ request() { systemctl suspend 2>&1 | sed 's/^/omamobile-sleepd: systemctl: /' } -# Waits, but a USR1 (omamobile-sleepd now) ends the wait early. +# Waits, but a USR1 (omamobile-sleepd now) ends the wait early, and so does a +# USR2 (logind says the phone just resumed, see listen). +resumed_evt=0 mon= pause() { + # The event came while the loop was busy: don't sleep on it. + if ((resumed_evt)); then resumed_evt=0; return 0; fi sleep "$1" & local pid=$! wait "$pid" 2>/dev/null kill "$pid" 2>/dev/null + resumed_evt=0 return 0 } +# Kills a process and everything under it: the listener runs busctl inside a +# command substitution, and killing only its own pid would leave busctl waiting. +kill_tree() { + local c + for c in $(pgrep -P "$1" 2>/dev/null); do kill_tree "$c"; done + kill "$1" 2>/dev/null +} +# Only a listener that is still our job: its pid may have been reused since it ended. +stop_listener() { [[ -n $mon && " $(jobs -p | tr '\n' ' ') " == *" $mon "* ]] && kill_tree "$mon"; return 0; } + +# The watcher is frozen inside its sleep while the phone sleeps, so after a +# resume it only noticed it (and lit the PIN pad for a power-key wake) when +# the rest of that sleep ran out: 4.9 s with the toggle off (the 5 s poll), +# up to 2 s with it on. logind announces the end of a sleep with +# PrepareForSleep(false); that ends the wait at once and the loop reads +# suspend_stats right away. The poll stays: a raw `echo freeze > +# /sys/power/state` never sends the signal, and anything unexpected here just +# stops the listener. PrepareForSleep(true) is ignored: it comes before a +# sleep this watcher did not ask for. busctl wait needs no monitor rights, +# which a user service doesn't have; it returns after each message, so the +# listener subscribes again for the next one. +listen() { + local out + while out=$(busctl --system --json=short wait /org/freedesktop/login1 \ + org.freedesktop.login1.Manager PrepareForSleep 2>/dev/null); do + case $out in + *'[false]'*) kill -USR2 $$ 2>/dev/null || return ;; + *'[true]'*) ;; + *) break ;; + esac + done + # No trace otherwise: the watcher would quietly be back on the poll. + log "no more resume events from logind: back to polling every ${poll}-${idle_poll} s" +} + want_now=0 watch() { local t ok fail woke last_ok last_fail off_since= deadline=0 requested=-1 cause= said= reason now_at= last_ok=$(count success) last_fail=$(count fail) trap 'want_now=1' USR1 + trap 'resumed_evt=1' USR2 + listen & mon=$! + trap 'stop_listener' EXIT log "watching: sleep ${after_off} s after the screen goes off behind a secure lock, ${grace} s after a dark resume (toggle omamobile-sleep)" while :; do t=$(clock) diff --git a/packages/omamobile-shell/test/sleepd.sh b/packages/omamobile-shell/test/sleepd.sh index e79ac494f..f878bca88 100755 --- a/packages/omamobile-shell/test/sleepd.sh +++ b/packages/omamobile-shell/test/sleepd.sh @@ -15,6 +15,13 @@ fake() { printf '#!/bin/bash\nT=%q\n%s\n' "$t" "$2" >"$F/$1"; chmod +x "$F/$1"; fake sleep 'echo $(( $(cat "$T/clock") + ${1%%.*} )) >"$T/clock" # "the user presses power" at a given time: the watcher gets its signal. if [[ -f $T/now_at ]] && (( $(cat "$T/clock") >= $(cat "$T/now_at") )); then rm -f "$T/now_at"; kill -USR1 $PPID; fi +# A one-shot real hang that moves no clock: the frozen sleep of a phone that +# is asleep. The "logind" signal arrives 0.3 s later (events in $T/evts). +if [[ -f $T/hang ]]; then + rm -f "$T/hang" + ( /usr/bin/sleep 0.3; for e in $(cat "$T/evts"); do printf "{\"type\":\"b\",\"data\":[%s]}\n" "$e" >"$T/evt"; /usr/bin/sleep 0.1; done ) >/dev/null 2>&1 & + exec /usr/bin/sleep 20 +fi # "the panel finishes powering off" at a given time. if [[ -f $T/off_at ]] && (( $(cat "$T/clock") >= $(cat "$T/off_at") )); then rm -f "$T/off_at"; echo 4 >"$T/sys/class/backlight/panel/bl_power"; fi' fake omarchy-toggle-enabled '[[ $1 == omamobile-sleep && -f $T/toggle ]]' @@ -24,7 +31,9 @@ fake omarchy-shell '[[ "$*" == "lock status" ]] && printf "{\"locked\":true,\"se [[ "$*" == "lock dark" ]] && echo "omarchy-shell lock dark at $(cat "$T/clock")" >>"$T/log" true' fake ss 'cat "$T/ssh"' -fake busctl 'printf "{\"type\":\"a(ssssuu)\",\"data\":[[%s]]}\n" "$(cat "$T/inhibitors")"' +fake busctl '# the resume listener (busctl wait): a FIFO stands in for the logind signals; none: it fails and the poll carries on +if [[ " $* " == *" wait "* ]]; then [[ -p $T/evt ]] || exit 1; exec cat "$T/evt"; fi +printf "{\"type\":\"a(ssssuu)\",\"data\":[[%s]]}\n" "$(cat "$T/inhibitors")"' fake pactl 'cat "$T/sinks"' fake omamobile-keys 'echo "omamobile-keys $* at $(cat "$T/clock")" >>"$T/log"' fake systemctl 'echo "systemctl $* at $(cat "$T/clock")" >>"$T/log" @@ -168,6 +177,43 @@ reset; echo false >"$t/secure"; sleepd now >"$t/out" [[ $(suspends) == 0 ]] || fail "now with no watcher still checks the lock" echo "ok - sleepd: now (the power key) sleeps within 2 s through the watcher, and does nothing with the toggle off" +fake systemctl 'echo "systemctl $* at $(cat "$T/clock")" >>"$T/log" +case "$*" in + suspend) + echo $(( $(cat "$T/sys/power/suspend_stats/success") + 1 )) >"$T/sys/power/suspend_stats/success" + cat "$T/wake_irq" 2>/dev/null >"$T/sys/power/pm_wakeup_irq" || echo 145 >"$T/sys/power/pm_wakeup_irq" + echo $(( $(cat "$T/clock") + 600 )) >"$T/clock" + [[ -f $T/on_resume ]] && bash "$T/on_resume" + exit 0 ;; + "--user --quiet is-active omamobile-sleepd.service") [[ -f $T/active ]] ;; +esac' +# The resume event: a power-key wake while the watcher sits in a long sleep +# (the real one is frozen inside it, so the rest of it runs after the resume). +# Without the event the 20 s real hang below would outlast the 10 s limit. +watchto() { + timeout 10 env HOME="$t/home" PATH="$F:$PATH" OMAMOBILE_SLEEP_SYSFS="$t/sys" OMAMOBILE_SLEEP_PROCFS="$t/proc" \ + OMAMOBILE_SLEEP_CLOCK="$t/clock" OMAMOBILE_SLEEPD_STOP_AT=$((1000 + $1)) bash "$here/bin/omamobile-sleepd" >"$t/out" 2>&1 +} +lit='echo 0 >"%s"; echo true >"%s"; touch "%s"\n' +pkey() { reset; mkfifo "$t/evt"; echo 111 >"$t/wake_irq"; printf "$lit" "$t/sys/class/backlight/panel/bl_power" "$t/dpms" "$t/hang" >"$t/on_resume"; echo "$1" >"$t/evts"; } +# PrepareForSleep(false) ends the wait at once. +pkey false; watchto 700 || fail "event: PrepareForSleep(false) ends the sleeping watcher's wait (timed out)" +grep -c '^omamobile-keys wake' "$t/log" >/dev/null || fail "event: the power-key wake lights the PIN pad" +rm -f "$t/evt" +# A true ahead of the false (the signal before a sleep) is read and ignored: the false after it still arrives. +pkey "true false"; watchto 700 || fail "event: only the false signal ends the wait (timed out)" +[[ $(suspends) == 1 ]] || fail "event: PrepareForSleep(true) does not start a sleep" +grep -c "no more resume events from logind" "$t/out" >/dev/null && fail "event: a true then a false keeps the listener going" +rm -f "$t/evt" +# No signal source at all (busctl wait fails): the poll carries on as before. +reset; echo 111 >"$t/wake_irq"; printf 'echo 0 >"%s"; echo true >"%s"\n' "$t/sys/class/backlight/panel/bl_power" "$t/dpms" >"$t/on_resume"; watch 700 +grep -c '^omamobile-keys wake' "$t/log" >/dev/null || fail "event: with no listener the poll still lights the PIN pad" +grep -c "no more resume events from logind: back to polling" "$t/out" >/dev/null || fail "event: a listener that gives up says so" +# Nothing of the listener is left behind. +sleep 0.5; pgrep -f "cat $t/evt" >/dev/null && fail "event: the listener's busctl is killed with the watcher" +rm -f "$t/evt" "$t/evts" +echo "ok - sleepd: logind's PrepareForSleep(false) ends the wait at once, true is ignored, the poll is the fallback, nothing is left running" + reset; out=$(sleepd check) || fail "check: exit 0 when nothing stops sleep" [[ $out == "nothing stops sleep" ]] || fail "check: says so ($out)" echo true >"$t/dpms"; echo 0 >"$t/sys/class/backlight/panel/bl_power" From 45d384e75db426a777db0ecc42df83abe313dd2b Mon Sep 17 00:00:00 2001 From: Lef Date: Wed, 11 Nov 2026 07:21:01 +0200 Subject: [PATCH 2/2] omamobile-sleepd: name logind as the sender, stop the listener before busctl busctl wait now names org.freedesktop.login1, so a PrepareForSleep from another process on the system bus is not taken for logind's. kill_tree takes a process's children's pids and kills the process first, so on a normal stop the listener can't get to its "no more resume events" line because busctl died under it. The test checks the sender in the arguments. --- packages/omamobile-shell/bin/omamobile-sleepd | 9 ++++++--- packages/omamobile-shell/test/sleepd.sh | 3 ++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/omamobile-shell/bin/omamobile-sleepd b/packages/omamobile-shell/bin/omamobile-sleepd index 04b66808d..c93da4adc 100755 --- a/packages/omamobile-shell/bin/omamobile-sleepd +++ b/packages/omamobile-shell/bin/omamobile-sleepd @@ -143,10 +143,13 @@ pause() { # Kills a process and everything under it: the listener runs busctl inside a # command substitution, and killing only its own pid would leave busctl waiting. +# The process goes first, its children's pids are taken before that, so the +# listener can't log its "no more events" line because busctl died under it. kill_tree() { - local c - for c in $(pgrep -P "$1" 2>/dev/null); do kill_tree "$c"; done + local c kids + kids=$(pgrep -P "$1" 2>/dev/null) kill "$1" 2>/dev/null + for c in $kids; do kill_tree "$c"; done } # Only a listener that is still our job: its pid may have been reused since it ended. stop_listener() { [[ -n $mon && " $(jobs -p | tr '\n' ' ') " == *" $mon "* ]] && kill_tree "$mon"; return 0; } @@ -164,7 +167,7 @@ stop_listener() { [[ -n $mon && " $(jobs -p | tr '\n' ' ') " == *" $mon "* ]] && # listener subscribes again for the next one. listen() { local out - while out=$(busctl --system --json=short wait /org/freedesktop/login1 \ + while out=$(busctl --system --json=short wait org.freedesktop.login1 /org/freedesktop/login1 \ org.freedesktop.login1.Manager PrepareForSleep 2>/dev/null); do case $out in *'[false]'*) kill -USR2 $$ 2>/dev/null || return ;; diff --git a/packages/omamobile-shell/test/sleepd.sh b/packages/omamobile-shell/test/sleepd.sh index f878bca88..56301d753 100755 --- a/packages/omamobile-shell/test/sleepd.sh +++ b/packages/omamobile-shell/test/sleepd.sh @@ -32,7 +32,7 @@ fake omarchy-shell '[[ "$*" == "lock status" ]] && printf "{\"locked\":true,\"se true' fake ss 'cat "$T/ssh"' fake busctl '# the resume listener (busctl wait): a FIFO stands in for the logind signals; none: it fails and the poll carries on -if [[ " $* " == *" wait "* ]]; then [[ -p $T/evt ]] || exit 1; exec cat "$T/evt"; fi +if [[ " $* " == *" wait "* ]]; then echo "$*" >"$T/wait-args"; [[ -p $T/evt ]] || exit 1; exec cat "$T/evt"; fi printf "{\"type\":\"a(ssssuu)\",\"data\":[[%s]]}\n" "$(cat "$T/inhibitors")"' fake pactl 'cat "$T/sinks"' fake omamobile-keys 'echo "omamobile-keys $* at $(cat "$T/clock")" >>"$T/log"' @@ -199,6 +199,7 @@ pkey() { reset; mkfifo "$t/evt"; echo 111 >"$t/wake_irq"; printf "$lit" "$t/sys/ # PrepareForSleep(false) ends the wait at once. pkey false; watchto 700 || fail "event: PrepareForSleep(false) ends the sleeping watcher's wait (timed out)" grep -c '^omamobile-keys wake' "$t/log" >/dev/null || fail "event: the power-key wake lights the PIN pad" +[[ $(<"$t/wait-args") == *"wait org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager PrepareForSleep"* ]] || fail "event: the listener names logind as the sender ($(<"$t/wait-args"))" rm -f "$t/evt" # A true ahead of the false (the signal before a sleep) is read and ignored: the false after it still arrives. pkey "true false"; watchto 700 || fail "event: only the false signal ends the wait (timed out)"