Files
bananarchy/overlay/rootfs/usr/bin/bananarchy-time-sync.sh
T
Luxferre 8b466ef8ae Fix 2720 closed-flip call wakeups/preservation, live timezone sync, and Power menu
- Default CPU_RPM_SLEEP=0, CPU_L2_SLEEP=0, and MODEM_DRX_MODE=0 across bananarchy-power-policy.conf, bananarchy-power-policy.sh, bananarchy-display-power-sync, and kernel patch 0010 so closed-flip incoming calls wake APSS promptly via smd-edge without AT port contention.
- Serialize sxmo_modem.sh incoming/finished call checks, expire stale /tmp/sxmo_hangup_requested flags, re-verify live ModemManager call state before ringing/raising callscreen, guard idle/flip-open HANGUP_CLOSE paths in locker.so, callscreen.so, bananarchy-idle-lock.sh, and bananarchy-lid, preserve calls on flip-open (UNGRAB/RAISE), and add closed-flip cover call controls (bananarchy-cover-idle, bananarchy-cover-event.sh, bananarchy-volume-keys).
- Propagate live network/manual timezone updates across statusbar, locker.so, bananarchy-cover-idle, bananarchy-dsds, callscreen.so, and bananarchy-run-aligned without reboot (scripts/test-time-timezone-sync.py), remove non-functional Logout from the Power menu, ignore post-call sxmo_calls markers, add scripts/build-dwm-plugins.sh, and update a1114 (carbonara) hashes and docs.
2026-11-14 10:57:29 +02:00

262 lines
8.1 KiB
Bash
Executable File

#!/bin/sh
# Bananarchy network time synchronisation helper.
#
# chronyd resolves the pool name only when it starts and then waits up to
# minpoll 12 (4096 s) for the first measurement. If DNS was not ready at boot,
# its sources stay at "unknown address" and the clock is never set. This
# helper re-resolves the pool and requests an immediate burst when a network
# comes up, without shortening the long steady-state poll interval.
#
# Privileged chronyc commands work over chronyd's root-only Unix command
# socket even with cmdport 0, so no UDP command port is exposed.
# Runs as root; re-execs through doas -n when called from the X session.
set -u
SOCKET=/run/chrony/chronyd.sock
CHRONYC=/usr/bin/chronyc
RUNDIR=/run/bananarchy-time-sync
STAMP="$RUNDIR/last"
LOCK="$RUNDIR/lock"
# GPS-only mode: the user can select GNSS as the sole time source. In that
# mode chronyd is stopped by bananarchy-gps-time-sync.sh and no NTP burst,
# source refresh or clock step may be issued from this helper.
TIME_SOURCE_FILE="${BANANARCHY_TIME_SOURCE_FILE:-/home/user/.config/sxmo/time_source}"
GPS_TIME_HELPER="${BANANARCHY_GPS_TIME_HELPER:-/usr/bin/bananarchy-gps-time-sync.sh}"
# /proc/uptime is monotonic, so a multi-decade clock step cannot invalidate
# the stamp. Unsynchronised attempts are spaced 15 s apart; a synchronised
# clock only needs a source refresh every 5 min after a network change.
TRIGGER_MIN=15
REFRESH_MIN=300
WAIT_MAX=30
# NetworkManager emits several up/dns events per connection. DNS resolution
# runs before the lock is taken, so a slow boot retry cannot swallow the
# network-up event that would otherwise sync the clock immediately.
ATTEMPTS=8
BOOT_ATTEMPTS=30
SYNC_WAIT=15
# Host tests (and callers that already have privileges) can skip the doas hop.
if [ "${1:-}" != "refresh-ui" ] && [ "$(id -u)" != "0" ] && [ "${BANANARCHY_TIME_SYNC_NO_ESCALATION:-0}" != "1" ]; then
exec doas -n "$0" "$@"
fi
log() {
printf 'bananarchy-time-sync: %s\n' "$*" >&2
}
time_source() {
v="network"
if [ -r "$TIME_SOURCE_FILE" ]; then
read -r v < "$TIME_SOURCE_FILE" 2>/dev/null || v="network"
fi
case "$v" in gps) printf 'gps\n' ;; *) printf 'network\n' ;; esac
}
monotonic() {
awk '{ printf "%d\n", $1 }' /proc/uptime
}
chronyc_cmd() {
"$CHRONYC" -h "$SOCKET" "$@" >/dev/null 2>&1
}
wait_chronyd() {
i=0
while [ "$i" -lt "$WAIT_MAX" ]; do
if [ -S "$SOCKET" ] && chronyc_cmd activity; then
return 0
fi
sleep 1
i=$((i + 1))
done
return 1
}
# chronyc activity reports how many configured sources still have no address.
# A freshly started chronyd can also report zero sources of any kind while the
# pool name is unresolved, so require at least one known source as well.
resolved() {
"$CHRONYC" -h "$SOCKET" activity 2>/dev/null | awk '
/^[0-9]+ sources online$/ { on = $1 }
/^[0-9]+ sources offline$/ { off = $1 }
/^[0-9]+ sources doing burst/ { burst += $1 }
/^[0-9]+ sources with unknown address$/ { unknown = $1 }
END { exit !(unknown + 0 == 0 && on + off + burst + 0 > 0) }
'
}
synced() {
ref="$("$CHRONYC" -h "$SOCKET" tracking 2>/dev/null |
awk '/^Reference ID/ { print $4; exit }')"
[ -n "$ref" ] && [ "$ref" != "00000000" ]
}
burst_done() {
"$CHRONYC" -h "$SOCKET" activity 2>/dev/null | awk '
/^[0-9]+ sources doing burst/ { b = $1 }
END { exit !(b + 0 == 0) }
'
}
wait_sync_complete() {
wait_s="${BANANARCHY_SYNC_WAIT:-$SYNC_WAIT}"
sleep_s="${BANANARCHY_TIME_SYNC_SLEEP:-1}"
i=0
while [ "$i" -lt "$wait_s" ]; do
if synced && burst_done; then
return 0
fi
sleep "$sleep_s"
i=$((i + 1))
done
synced
}
refresh_statusbar() {
# chronyd can step the clock by decades (e.g. from a 1970 RTC to network
# time) or the user can switch timezones live. Republish desktop statusbar,
# xlocker, 2720 cover display, and realign bananarchy-run-aligned immediately.
su_cmd="${BANANARCHY_STATUS_REFRESH_CMD:-su}"
if [ "$(id -u)" = "0" ]; then
"$su_cmd" user -s /bin/sh -c '
HOME=/home/user DISPLAY=:0 XDG_RUNTIME_DIR=/run/user/10000 \
SXMO_WM=dwm SXMO_STATUS_NAME=default SXMO_STATE=/run/user/10000/sxmo.state \
PATH=/usr/share/sxmo/default_hooks:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin \
sxmo_hook_statusbar.sh refresh
' >/dev/null 2>&1 || true
else
HOME="${HOME:-/home/user}" DISPLAY="${DISPLAY:-:0}" \
XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/10000}" \
SXMO_WM=dwm SXMO_STATUS_NAME=default \
SXMO_STATE="${SXMO_STATE:-/run/user/10000/sxmo.state}" \
PATH="/usr/share/sxmo/default_hooks:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/usr/sbin:$PATH" \
sxmo_hook_statusbar.sh refresh >/dev/null 2>&1 || true
fi
locker_fifo="${BANANARCHY_LOCKER_FIFO:-/tmp/dwm_plugin_locker.fifo}"
if [ -p "$locker_fifo" ] || [ -f "$locker_fifo" ]; then
timeout 1 sh -c 'printf "REFRESH_TIME\n" > "$1"' _ "$locker_fifo" >/dev/null 2>&1 || true
fi
coverctl="${BANANARCHY_COVERCTL:-/usr/bin/bananarchy-coverctl}"
"$coverctl" REFRESH >/dev/null 2>&1 || true
killall -q -USR1 bananarchy-run-aligned >/dev/null 2>&1 || true
}
# refresh re-resolves pool names asynchronously. Keep retrying while DNS is
# still down; the boot caller allows a minute, later network events a few
# seconds. No stamp is written on failure, so the next event can retry.
wait_sources() {
attempts="$1"
i=0
while [ "$i" -lt "$attempts" ]; do
chronyc_cmd refresh
resolved && return 0
sleep 2
i=$((i + 1))
done
return 1
}
trigger() {
mode="${1:-network-up}"
if [ "$(time_source)" = "gps" ]; then
log "GPS is the sole time source; skipping NTP"
return 1
fi
attempts="$ATTEMPTS"
[ "$mode" = "boot" ] && attempts="$BOOT_ATTEMPTS"
wait_chronyd || {
log "chronyd command socket not available"
return 1
}
if ! wait_sources "$attempts"; then
log "NTP pool names did not resolve yet"
return 1
fi
mkdir -p "$RUNDIR" 2>/dev/null || return 1
# Only the stamp/burst section is serialised: NetworkManager emits several
# up/dns events per connection and a long DNS wait must not block them.
exec 9>"$LOCK" || return 1
flock -w 5 9 || return 1
now="$(monotonic)"
last=0
[ -r "$STAMP" ] && last="$(cat "$STAMP" 2>/dev/null || echo 0)"
case "$last" in ''|*[!0-9]*) last=0 ;; esac
if [ "$mode" != "force" ] && [ "$mode" != "sync" ]; then
if synced; then
[ "$((now - last))" -lt "$REFRESH_MIN" ] && return 0
else
[ "$((now - last))" -lt "$TRIGGER_MIN" ] && return 0
fi
fi
printf '%s\n' "$now" > "$STAMP" || return 1
chronyc_cmd online
if ! synced || [ "$mode" = "force" ] || [ "$mode" = "sync" ]; then
chronyc_cmd burst 2/4 || log "chronyc burst failed"
fi
return 0
}
# Config -> Sync net time: force a burst and report when the clock is set.
sync_now() {
if [ "$(time_source)" = "gps" ]; then
# Config -> Sync net time must still give the user a result without
# touching NTP when GNSS is the only configured source.
"$GPS_TIME_HELPER" sync
return $?
fi
if ! trigger sync; then
echo "Network time unavailable: chronyd or DNS is not ready"
return 1
fi
if wait_sync_complete; then
refresh_statusbar
printf 'Network time synced: %s\n' "$(date -u '+%Y-%m-%d %H:%M:%S UTC')"
return 0
fi
echo "Network time not confirmed yet; retry when the network is up"
return 1
}
case "${1:-status}" in
boot|network-up)
if [ "$(time_source)" = "gps" ]; then
[ "${1:-}" = "boot" ] && "$GPS_TIME_HELPER" boot >/dev/null 2>&1 || true
exit 0
fi
if trigger "$1"; then
wait_sync_complete || true
refresh_statusbar
fi
;;
force)
if [ "$(time_source)" = "gps" ]; then
"$GPS_TIME_HELPER" sync && refresh_statusbar
exit $?
fi
if trigger force; then
wait_sync_complete || true
refresh_statusbar
fi
;;
sync)
sync_now
exit $?
;;
refresh-ui)
refresh_statusbar
;;
status)
if [ ! -S "$SOCKET" ]; then
echo "chronyd command socket absent"
exit 1
fi
"$CHRONYC" -h "$SOCKET" tracking
"$CHRONYC" -h "$SOCKET" sources
;;
*)
echo "usage: $0 boot|network-up|force|sync|status" >&2
exit 2
;;
esac
exit 0