Sync network time when a connection comes up

This commit is contained in:
Luxferre committed 2026-11-07 21:53:07 +02:00
1 parent 38d54f10ff
commit 9ac39ce6bf
13 files changed
+365 -8

No files matched your search

@@ -0,0 +1,9 @@
#!/bin/sh
# NetworkManager dispatcher: refresh NTP sources when a connection comes up.
# The helper throttles and waits for chronyd, so run it without blocking NM.
case "$2" in
up|dhcp4-change|dhcp6-change|dns-change|connectivity-change|vpn-up) ;;
*) exit 0 ;;
esac
/usr/bin/bananarchy-time-sync.sh network-up >/dev/null 2>&1 &
exit 0
+5 -4
View File
@@ -1,7 +1,8 @@
# Bananarchy: long NTP polls so chronyd does not keep waking Wi-Fi (U6).
# After a long uptime chronyd ramps to maxpoll anyway; this makes it explicit
# and removes the initial iburst burst of packets.
pool pool.ntp.org minpoll 12 maxpoll 16
# Bananarchy: long NTP polls keep Wi-Fi asleep between measurements. iburst
# syncs the clock as soon as the pool resolves (at boot or after a network-up
# refresh). cmdport 0 keeps the UDP command port closed; the network-up hook
# uses chronyd's root-only Unix command socket instead.
pool pool.ntp.org iburst minpoll 12 maxpoll 16
driftfile /var/lib/chrony/chrony.drift
rtcsync
cmdport 0
+2
View File
@@ -54,6 +54,8 @@ permit nopass :wheel as root cmd bananarchy-privacy-ctl
permit nopass :wheel as root cmd /usr/bin/bananarchy-display-power-sync
permit nopass :wheel as root cmd /usr/bin/bananarchy-power-policy.sh
permit nopass :wheel as root cmd bananarchy-power-policy.sh
permit nopass :wheel as root cmd /usr/bin/bananarchy-time-sync.sh
permit nopass :wheel as root cmd bananarchy-time-sync.sh
permit nopass :wheel as root cmd bananarchy-display-power-sync
permit nopass :wheel as root cmd /usr/bin/plymouth args quit
permit nopass :wheel as root cmd plymouth args quit
+15
View File
@@ -0,0 +1,15 @@
#!/sbin/openrc-run
name="Bananarchy network time sync"
description="Trigger NTP synchronisation once the network is up"
depend() {
need chronyd net
after networkmanager
}
start() {
ebegin "Triggering Bananarchy network time synchronisation"
/usr/bin/bananarchy-time-sync.sh boot >/dev/null 2>&1 &
eend 0
}
+138
View File
@@ -0,0 +1,138 @@
#!/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"
# /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
if [ "$(id -u)" != "0" ]; then
exec doas -n "$0" "$@"
fi
log() {
printf 'bananarchy-time-sync: %s\n' "$*" >&2
}
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.
resolved() {
"$CHRONYC" -h "$SOCKET" activity 2>/dev/null |
grep -q '^0 sources with unknown address$'
}
synced() {
ref="$("$CHRONYC" -h "$SOCKET" tracking 2>/dev/null |
awk '/^Reference ID/ { print $4; exit }')"
[ -n "$ref" ] && [ "$ref" != "00000000" ]
}
# 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}"
attempts=3
[ "$mode" = "boot" ] && attempts=30
mkdir -p "$RUNDIR" 2>/dev/null || return 1
# NetworkManager emits several up/dns events per connection; serialise them
# so only one helper refreshes and bursts at a time.
exec 9>"$LOCK" || return 1
flock -n 9 || return 0
wait_chronyd || {
log "chronyd command socket not available"
return 1
}
if ! wait_sources "$attempts"; then
log "NTP pool names did not resolve yet"
return 0
fi
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" ]; 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" ]; then
chronyc_cmd burst 2/4 || log "chronyc burst failed"
fi
return 0
}
case "${1:-status}" in
boot|network-up)
trigger "$1"
;;
force)
trigger force
;;
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|status" >&2
exit 2
;;
esac
exit 0