diff --git a/AGENTS.md b/AGENTS.md index 547411b..79228c8 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -67,7 +67,12 @@ All cross-compilation scripts run inside rootless Docker (`alpine:3.24` `armv7` drive `WIFI_LISTEN_INTERVAL` (wcn36xx module option), `MODEM_DRX_MODE` (`AT$QCDRX`) and `DSDS_SECONDARY_MODE=eco` (QMI secondary GW provisioning session park on `sxmo_wm.sh display off/on`). Never park a SIM with -`--uim-sim-power-off`: it clears PIN verification. Deep standby is `bananarchy-suspend.sh` / `bananarchy-deep-standby.sh` / `sxmo_autosuspend.sh` / `sxmo_suspend.sh`, gated by `/etc/bananarchy-no-deep-standby` (shipped present = disabled) and inert because `CONFIG_SUSPEND` is off. A full custom module tree rebuild is deliberately **not** planned: this firmware has no PSCI `SYSTEM_SUSPEND`, so `CONFIG_SUSPEND` would only give s2idle (`freeze`), not `mem` / DDR self-refresh, for <1 mA of saving (see `BATTERY-OPTIMIZATION-ANALYSIS.md` §14). Never force `sxmo.noidle`/`sxmo.nosuspend` from the session scripts and never reintroduce the infinite `sxmo_not_suspendable` wakelock. `/etc/bananarchy/` is root-only. +`--uim-sim-power-off`: it clears PIN verification. Network time sync is +`etc/chrony/chrony.conf` (`iburst`, `minpoll 12 maxpoll 16`, `cmdport 0`) plus +`usr/bin/bananarchy-time-sync.sh`, the `bananarchy-time-sync` OpenRC service +and the NetworkManager dispatcher hook, which call `chronyc refresh` / `burst` +over chronyd's root-only Unix socket when a connection comes up; keep +`var/lib/chrony` owned by the `chrony` user. Deep standby is `bananarchy-suspend.sh` / `bananarchy-deep-standby.sh` / `sxmo_autosuspend.sh` / `sxmo_suspend.sh`, gated by `/etc/bananarchy-no-deep-standby` (shipped present = disabled) and inert because `CONFIG_SUSPEND` is off. A full custom module tree rebuild is deliberately **not** planned: this firmware has no PSCI `SYSTEM_SUSPEND`, so `CONFIG_SUSPEND` would only give s2idle (`freeze`), not `mem` / DDR self-refresh, for <1 mA of saving (see `BATTERY-OPTIMIZATION-ANALYSIS.md` §14). Never force `sxmo.noidle`/`sxmo.nosuspend` from the session scripts and never reintroduce the infinite `sxmo_not_suspendable` wakelock. `/etc/bananarchy/` is root-only. - **Image residue**: `rebuild-image.sh` strips `/var/cache/apk`, all `apk.log*` files, and unused gettext locale trees before `mke2fs` and aborts the build if any apk cache/log remains; never add an `apk` call after that guard. - **No on-device compiler**: `scripts/rootfs-packages.txt` ships no `gcc`/`g++`/`musl-dev`/`make` and no Janet runtime (`janet`, `janet-dev`, `jpm`); cross-compile native code with `scripts/build-*.sh`. On-device development uses the Yabasic FFI SDK (`neoui.yab` / `neonet.yab`), which requires `yabasic`, `libffi`, `libneoui.so`, and `libcurl.so.4` — never remove those. `scripts/check-rebuild.sh` guards both sides. - **Core tools**: `curl`, `bc`, `ed`, `rlwrap`, `mandoc`, `nano`, `less`, and `frotz` are pinned core tools asserted by `scripts/check-rebuild.sh`. Perl, Lua, optional Tcl extensions, and the `sqlite` CLI are excluded, while `tcl`, `python3`, `bash`, `luajit`, `gawk`, `jq`, `gojq`, and `quickjs` remain as hard runtime dependencies. diff --git a/BATTERY-OPTIMIZATION-ANALYSIS.md b/BATTERY-OPTIMIZATION-ANALYSIS.md index 1d8a4c2..50f27ce 100644 --- a/BATTERY-OPTIMIZATION-ANALYSIS.md +++ b/BATTERY-OPTIMIZATION-ANALYSIS.md @@ -144,7 +144,9 @@ msm-modem-uim-selection msm8909-modem-wwan-port networkmanager postmarketos-zram rfkill rmtfs sshd tinydm udev-postmount wpa_supplicant` (no `bluetooth`, no `zram-init`). - `chronyd` running (2 procs) with `pool pool.ntp.org iburst`, `cmdport 0`, `rtcsync`, - `makestep 1 -1`. `chronyc` unusable (`cmdport 0`); after 43 h uptime it is almost certainly at + `makestep 1 -1`. `cmdport 0` closes the UDP command port but leaves chronyd's + root-only Unix socket (`/run/chrony/chronyd.sock`) available for privileged + `chronyc` commands (verified in section 16); after 43 h uptime it is almost certainly at `maxpoll` ≈ 1024 s, not the initial 64 s. - `superd` started **without `-l /dev/null`** → the shipped user hook (`~/.config/sxmo/hooks/sxmo_hook_start.sh`) wins over the default hook and the zero-log @@ -249,7 +251,7 @@ Ordered by expected impact, with measured evidence. Regression risks are stated. | **U3** | **`statusbar_periodics`**: `sxmo_run_aligned.sh 60 sxmo_hook_statusbar.sh periodics` is running and its guard passes. Replace `mmcli -m any -J`, `nmcli -t con show --active`, `nmcli ... device wifi list`, `rfkill list wifi`, `xsetroot -name` with D-Bus `PropertiesChanged` signals, or raise to ≥300 s and only `xsetroot` on change. | Removes a 60 s QMI/D-Bus/process burst and a full dwm bar redraw (SPI frame). | Statusbar icons lag up to the new interval. | | **U4** | **Battery udev path**: filter `85-bananarchy-battery.rules` to `KERNEL=="pm8916-bms-vm"`/`ENV{POWER_SUPPLY_NAME}`, drop the `(sleep 2; ... recheck)` fork, avoid `su` per event. | Measured 2 uevents/min; removes the `su`/`sleep` forks. Lower priority than previously stated. | Charging icon must still update instantly on plug/unplug (keep the charger event). | | **U5** | **Bluetooth (latent)**: set `AutoEnable=false`, finite `DiscoverableTimeout`/`PairableTimeout`, `ControllerMode=bredr`, and keep BT out of the default runlevel. | BT is already off (soft-blocked, daemon not running) — **no current saving today**; this prevents a future drain when a user enables BT. | BT must still auto-reconnect when explicitly enabled. | -| **U6** | **chronyd**: remove from the default runlevel or set `minpoll 12 maxpoll 16`; rely on `rtcsync`/RTC (`osclock`, `swclock-offset-boot` are already in the boot runlevel). | Measured running with `pool pool.ntp.org iburst`, but after 43 h uptime it is at ~1024 s polls → **low priority**. Removes a daemon and occasional DNS/NTP Wi-Fi wakeups. | Clock drift without NTP. | +| **U6** | **chronyd**: remove from the default runlevel or set `minpoll 12 maxpoll 16`; rely on `rtcsync`/RTC (`osclock`, `swclock-offset-boot` are already in the boot runlevel). | Measured running with `pool pool.ntp.org iburst`, but after 43 h uptime it is at ~1024 s polls → **low priority**. Removes a daemon and occasional DNS/NTP Wi-Fi wakeups. | Clock drift without NTP. **Refined in section 16**: removing `iburst` broke the initial sync entirely, so `iburst` was restored and a network-up hook was added; the long steady-state polls are unchanged. | | **U7** | ~~Duplicate `wpa_supplicant` / respawn storm~~ | **Removed — see C1.** One stable `-u` instance; the `yesno` typo is harmless. Optionally fix the cosmetic typo. | None. | | **U8** | **NetworkManager**: add `[device] wifi.scan-interval=600` (no override exists today), `ipv6.method=ignore` for Wi-Fi profiles, keep `wifi.powersave=3` and `connectivity.enabled=false`. | Fewer roaming scans and IPv6 ND/MLD wakeups (measured IPv6 active on wlan0). | Roaming responsiveness; IPv6-only networks. | | **U9** | **DSDS/modem userspace**: make `dsds-daemon` `poll_stored_sms` (600 s when unlocked) purely event-driven (WMS `NEW_MESSAGE` indications), add backoff on its `time.sleep(2)` exception loop, make `--dpm-open-port` on-demand, and add a "DSDS power-save" toggle (disable secondary subscription / prefer 2G / single-standby while Wi-Fi is the default route). | Measured DSDS active with LTE+GSM, no data bearer, DPM port open, `drx-cycle`/`mico-mode` unset. Directly reduces baseband wakeups. | Must not lose SIM2 calls/SMS; gate on user preference. | @@ -363,7 +365,7 @@ few places where the regression checks mandated by the analysis changed the impl | U3 | `statusbar_periodics` interval 60 -> 300 s; `sxmo_hook_statusbar.sh periodics` drops `mmcli`/`nmcli`/`rfkill` polls and only calls `xsetroot` when the status string changes. | | U4 | udev rule filtered to the two PM8916 supplies; `(sleep 2; ... recheck)` fork removed from `sxmo_hook_battery.sh`. | | U5 | `AutoEnable=false`, finite discoverable/pairable timeouts, `ControllerMode=bredr`, fewer reconnect attempts. BT was already off on the device, so this is a latent fix. | -| U6 | `etc/chrony/chrony.conf` uses `minpoll 12 maxpoll 16` and no `iburst`. | +| U6 | `etc/chrony/chrony.conf` uses `minpoll 12 maxpoll 16` with `iburst` restored; `bananarchy-time-sync.sh` refreshes the pool on network-up (section 16). | | U7 | Withdrawn (C1); the cosmetic `yesno` typo in `etc/init.d/wpa_supplicant` was fixed anyway. | | U8 | `[device] wifi.scan-interval=600` in `10-power.conf`; IPv6 disabled on new interfaces (K10). | | U9 | On-demand DPM port; `dsds-daemon` 600 s -> 1800 s poll and 2 s -> 30 s error backoff; `sxmo_modemmonitor.sh` 10/120 s -> 30/600 s. | @@ -784,3 +786,85 @@ that is the deliberate trade for its RF saving. boot-time `0xffff` transient only disappears after a reflash with the rebuilt image. Reloading `pm8916_bms_vm` at runtime from `/lib/modules` confirms the new module reports sane voltage and `health=Good`. + +--- + +## 16. Network-up NTP synchronisation (U6 follow-up) + +### 16.1 Symptom and measurement + +U6 set `pool pool.ntp.org minpoll 12 maxpoll 16` and removed `iburst` to avoid +the startup packet burst. A later live check on the connected Nokia 8110 4G +found the clock at `Mon Jan 12 12:09:59 UTC 1970` with Wi-Fi connected to +`OffOurStreets_`, USB RNDIS up, DNS working (`getent hosts pool.ntp.org` +returned 3 addresses) and `chronyd` reported `started` by OpenRC: + +| Probe | Result | +| --- | --- | +| `chronyd` processes | 2, `-f /etc/chrony/chrony.conf -F 1` | +| NTP/chrony UDP sockets | none (no source had an address) | +| `chronyc activity` over the root Unix socket | `4 sources online`, `4 sources with unknown address` | +| `chronyc tracking` | `Reference ID : 00000000 ()`, ref time 1970 | +| `/var/lib/chrony` | `root:root 0750`, empty; chronyd logged `Could not open /var/lib/chrony/chrony.drift.tmp : Permission denied` | +| `chronyd -Q -t 30 ... iburst` | clock wrong by `1793085783 s`, measured in 5 s | +| `chronyd -d -x -t 25 -F 1` with shipped config | no source activity for the whole 25 s | + +Three independent causes combined: + +1. **DNS is not ready when chronyd starts.** OpenRC's `need net` is satisfied + by NetworkManager `provide net` as soon as the service starts, not when a + connection has a working `resolv.conf`. chronyd resolves pool names once at + startup and does not retry; the four sources stay at `unknown address`. +2. **No `iburst` and `minpoll 12`.** Even with DNS working, the first poll is + scheduled up to 4096 s after startup. Restarting chronyd with the network + already up produced no NTP socket and no clock change within 15 s; adding + `iburst` produced four reachable sources at poll 12 within 6 s. +3. **`cmdport 0` was assumed to disable chronyc entirely.** It only closes the + UDP command port. chronyd 4.9 keeps a root/chrony-only Unix socket at + `/run/chrony/chronyd.sock`; `chronyc` defaults to that socket first, so + privileged commands (`refresh`, `burst`, `online`) work without any key or + network exposure. `chronyc -h 127.0.0.1 refresh` returns `501 Not + authorised`, while `chronyc -h /run/chrony/chronyd.sock refresh` returns + `200 OK`. + +A Wi-Fi disconnect/reconnect confirmed NetworkManager dispatcher scripts do +run even though the `networkmanager-dispatcher` OpenRC service is stopped: +D-Bus activates `org.freedesktop.nm_dispatcher`. Events observed: +`connectivity-change`, `dns-change`, `down`, `dns-change`, `dhcp4-change`, +`up`, `connectivity-change`. + +### 16.2 Fix + +- `etc/chrony/chrony.conf` restores `iburst` and keeps `minpoll 12 maxpoll 16`, + `rtcsync`, `cmdport 0` and `makestep 1 -1`. The startup burst is 4 sources x + a few packets once per boot, negligible next to 4096 s of a wrong clock. +- `usr/bin/bananarchy-time-sync.sh` (root, `doas -n` re-exec) waits for the + chronyd Unix socket, calls `chronyc refresh` until `activity` reports + `0 sources with unknown address`, then `online` and `burst 2/4`. It only + writes its throttle stamp once the pool resolves, so a later network event + can retry. Throttling uses `/proc/uptime` (monotonic): 15 s between attempts + while unsynchronised, 300 s between source refreshes once synchronised, and + a non-blocking `flock` serialises the several dispatcher events that fire + for one connection. `status` and `force` are available from the CLI. +- `etc/NetworkManager/dispatcher.d/20-bananarchy-time-sync.sh` handles + `up`, `dhcp4-change`, `dhcp6-change`, `dns-change`, `connectivity-change` + and `vpn-up`. `etc/init.d/bananarchy-time-sync` (`need chronyd net`, + `after networkmanager`) covers the boot race where dispatcher events fire + before chronyd is ready; both are backgrounded so they never block NM or + OpenRC. +- `/var/lib/chrony` is chowned to the `chrony` uid/gid in both + `rebuild-image.sh` and `apply-bananarchy-overlay.sh`, so the driftfile is + written and the frequency estimate survives reboots. +- `scripts/check-rebuild.sh` guards the four new files, the `iburst` / + `cmdport 0` pair, the runlevel hook and both ownership fixes. + +### 16.3 Verification + +On the live phone, `chronyc refresh` followed by `chronyc burst 2/4` over +`/run/chrony/chronyd.sock` resolved all four pool sources and stepped the +clock from `1970-01-12` to `Sat Nov 7 19:40:25 UTC 2026` within seconds; +`chronyc tracking` then showed `Reference ID : 53A84595 +(hosted-by.SkillHost.PL)` and `System time : 0.000000000 seconds fast of NTP +time`. No UDP chronyc port was opened. The steady-state poll interval remains +`minpoll 12 maxpoll 16`, so U6's battery saving is preserved; the only extra +traffic is one short burst at boot and on each network transition. diff --git a/CHANGELOG.md b/CHANGELOG.md index 3957b32..cd67570 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,44 @@ Release versions follow the `
` format (where `2026` is `a`, --- +## Unreleased — network-up NTP synchronisation + +- **Fixed the clock never being set over the network**: `chronyd` resolves + `pool.ntp.org` only at startup and, with `minpoll 12`, waits up to 4096 s + for its first measurement. On this device it starts before NetworkManager + has installed a working `resolv.conf`, so `chronyc activity` reported + `4 sources with unknown address` and the clock stayed at 1970 even with + Wi-Fi connected. `iburst` is back in `etc/chrony/chrony.conf` so a daemon + started with DNS already up syncs in seconds; the long + `minpoll 12 maxpoll 16` steady-state interval is unchanged. +- **New `bananarchy-time-sync.sh`**: on a network-up event it calls + `chronyc refresh` (re-resolve the pool), `online` and `burst 2/4` over + chronyd's root-only Unix command socket. `cmdport 0` stays in place, so no + UDP command port is exposed. The helper waits for chronyd and for the pool + to resolve, throttles repeats using monotonic `/proc/uptime` (a 56-year + clock step cannot invalidate the stamp), serialises concurrent events with + `flock`, and skips bursts once the clock is synchronised. `status` prints + `chronyc tracking` / `sources`; `force` triggers a sync on demand. +- **Triggers**: `/etc/NetworkManager/dispatcher.d/20-bananarchy-time-sync.sh` + handles `up`, `dhcp4-change`, `dhcp6-change`, `dns-change`, + `connectivity-change` and `vpn-up`; a new `bananarchy-time-sync` OpenRC + service (`need chronyd net`, `after networkmanager`) covers boot ordering + when NetworkManager events fire before chronyd is ready. Both are in the + default runlevel / dispatcher set and are guarded by + `scripts/check-rebuild.sh`. +- **Fixed the driftfile**: `/var/lib/chrony` was `root:root 0750`, so the + unprivileged `chrony` user could not write `chrony.drift` and its frequency + estimate was lost on every reboot. `rebuild-image.sh` and + `apply-bananarchy-overlay.sh` now chown it to the `chrony` uid/gid. +- Live verification on the connected phone: the shipped config left the + clock at `1970-01-12` with no NTP socket; `chronyc refresh` + `burst 2/4` + over `/run/chrony/chronyd.sock` resolved all four sources and stepped the + clock to `2026-11-07` within seconds. NetworkManager dispatcher events + were confirmed to fire (`dns-change`, `dhcp4-change`, `up`, + `connectivity-change`) on a Wi-Fi reconnect. + +--- + ## Unreleased — reproducible device initramfs module lists - `scripts/apply-bananarchy-overlay.sh` now installs the tracked diff --git a/MANUAL.md b/MANUAL.md index 95c2f2a..bc246e4 100644 --- a/MANUAL.md +++ b/MANUAL.md @@ -580,6 +580,15 @@ The **Xlibre** display server (`modesetting_drv.so`) uses **Glamor + DRI3 + GLX bananarchy-gps disable # stop standalone GNSS engine to save power ``` +### 5.7 Network time synchronisation (`bananarchy-time-sync.sh`) + +`chronyd` runs from the default runlevel with `pool pool.ntp.org iburst minpoll 12 maxpoll 16`: long polls keep Wi-Fi asleep, while `iburst` sets the clock within seconds whenever the pool resolves. Because NetworkManager installs `resolv.conf` after `chronyd` starts, a `bananarchy-time-sync` OpenRC service and `/etc/NetworkManager/dispatcher.d/20-bananarchy-time-sync.sh` re-resolve the pool and request an immediate burst when a connection comes up (`up`, `dhcp4-change`, `dhcp6-change`, `dns-change`, `connectivity-change`, `vpn-up`). The helper uses chronyd's root-only Unix command socket, so `cmdport 0` keeps the UDP command port closed, and it throttles repeats with a monotonic uptime stamp so a decades-long clock step cannot confuse it. `/var/lib/chrony` is owned by the `chrony` user so the driftfile survives reboots. + +```bash +bananarchy-time-sync.sh status # chronyc tracking + sources (via doas) +bananarchy-time-sync.sh force # re-resolve the pool and burst immediately +``` + --- ## 6. Rebuilding the OS from a clean clone diff --git a/README.md b/README.md index 9b926ac..79985fc 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ ### System and hardware support - **Mainline Linux 6.12 kernel**: custom patches and out-of-tree drivers for Qualcomm **Adreno 304 (`FD304`, 96 KB GMEM)** 3D GPU acceleration (`drm/msm` + `qcom-iommu`), Qualcomm QDSP6/Q6Voice audio routing, Dual-SIM modem control, the rear 2MP GalaxyCore `GC2395` / `GC2385` / `GC2365` camera (`/dev/video0`), the `SY7807` flashlight LED (`white:torch`), physical keypad/slider events, and clean PM8909 poweroff. The kernel image is built from `config-bananarchy-msm8909.armv7` with the known-good pmaports module ABI, and ships with idle-blanking power management; `BATTERY-OPTIMIZATION-ANALYSIS.md` records the measured idle state and explains why suspend-to-RAM is unavailable on this firmware (PSCI `SYSTEM_SUSPEND` missing) and why a full custom module tree rebuild is not worthwhile. Runtime Wi-Fi listen-interval, modem DRX and DSDS secondary-SIM power policies are configurable in `/etc/bananarchy-power-policy.conf`; see `MANUAL.md`. - **Alpine Linux Edge base**: full access to the Alpine package ecosystem with `< 95 MB` idle RAM usage on 512 MB devices and full local root access out of the box. +- **Network time synchronisation**: `chronyd` uses `iburst` with long `minpoll 12 maxpoll 16` polls; `bananarchy-time-sync.sh` re-resolves the NTP pool and requests an immediate burst over chronyd's root-only Unix socket when NetworkManager brings a connection up, so the clock is set within seconds of boot or a Wi-Fi reconnect without shortening the battery-friendly steady-state interval. See `MANUAL.md` section 5.7. - **Pure X11 graphical stack with GPU acceleration**: cross-compiled **Xlibre** (`X11Libre/xserver`) display server offloading 2D/3D screen rendering to the **Adreno 304 GPU** via **Glamor, DRI3, GLX, and Mesa Freedreno (`FD304`, OpenGL 3.1 / OpenGL ES 3.0)** over `panel-mipi-dbi` KMSRO, paired with a customized **Sxmo (`sxmo-de-dwm`)** window manager (`240x320` vertical stack layout, native `ParentRelative` desktop clock and softkey bar without Conky overhead, `< 2 ms` slider/key wake latency, and zero Wayland overhead). ### Keypad-first user experience and native DWM plugins diff --git a/overlay/rootfs/etc/NetworkManager/dispatcher.d/20-bananarchy-time-sync.sh b/overlay/rootfs/etc/NetworkManager/dispatcher.d/20-bananarchy-time-sync.sh new file mode 100755 index 0000000..7f0b983 --- /dev/null +++ b/overlay/rootfs/etc/NetworkManager/dispatcher.d/20-bananarchy-time-sync.sh @@ -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 diff --git a/overlay/rootfs/etc/chrony/chrony.conf b/overlay/rootfs/etc/chrony/chrony.conf index 0e25da3..4e252eb 100644 --- a/overlay/rootfs/etc/chrony/chrony.conf +++ b/overlay/rootfs/etc/chrony/chrony.conf @@ -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 diff --git a/overlay/rootfs/etc/doas.d/50-sxmo.conf b/overlay/rootfs/etc/doas.d/50-sxmo.conf index 1ed6e27..e2826cc 100644 --- a/overlay/rootfs/etc/doas.d/50-sxmo.conf +++ b/overlay/rootfs/etc/doas.d/50-sxmo.conf @@ -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 diff --git a/overlay/rootfs/etc/init.d/bananarchy-time-sync b/overlay/rootfs/etc/init.d/bananarchy-time-sync new file mode 100755 index 0000000..bb6dd50 --- /dev/null +++ b/overlay/rootfs/etc/init.d/bananarchy-time-sync @@ -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 +} diff --git a/overlay/rootfs/usr/bin/bananarchy-time-sync.sh b/overlay/rootfs/usr/bin/bananarchy-time-sync.sh new file mode 100755 index 0000000..8291633 --- /dev/null +++ b/overlay/rootfs/usr/bin/bananarchy-time-sync.sh @@ -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 diff --git a/rebuild-image.sh b/rebuild-image.sh index ab04074..c7fae87 100755 --- a/rebuild-image.sh +++ b/rebuild-image.sh @@ -328,6 +328,14 @@ EOF chown -R "${TOR_UID:-109}:${TOR_GID:-114}" "$ROOTFS/var/lib/tor" chmod 0700 "$ROOTFS/var/lib/tor" fi + # chronyd drops privileges to the chrony user; root-owned driftfile dirs + # make it log "Permission denied" and lose its frequency estimate on reboot. + if [ -d "$ROOTFS/var/lib/chrony" ]; then + CHRONY_UID="$(awk -F: "/^chrony:/ {print \$3}" "$ROOTFS/etc/passwd" 2>/dev/null || true)" + CHRONY_GID="$(awk -F: "/^chrony:/ {print \$4}" "$ROOTFS/etc/passwd" 2>/dev/null || true)" + chown -R "${CHRONY_UID:-101}:${CHRONY_GID:-102}" "$ROOTFS/var/lib/chrony" + chmod 0750 "$ROOTFS/var/lib/chrony" + fi depmod -b "$ROOTFS" 6.12.1-msm8916 diff --git a/scripts/apply-bananarchy-overlay.sh b/scripts/apply-bananarchy-overlay.sh index a5e9fdc..b5fca50 100755 --- a/scripts/apply-bananarchy-overlay.sh +++ b/scripts/apply-bananarchy-overlay.sh @@ -110,6 +110,24 @@ mkdir -p "$TARGET_ROOTFS/home/user/.local/share/sxmo/modem" \ chown -R 10000:10000 "$TARGET_ROOTFS/home/user" 2>/dev/null || true chown 0:0 "$TARGET_ROOTFS/etc/doas.d/50-sxmo.conf" 2>/dev/null || true chmod 640 "$TARGET_ROOTFS/etc/doas.d/50-sxmo.conf" 2>/dev/null || true +# The time-sync helper uses chronyd's root-only Unix command socket, so keep +# its scripts root-owned and executable, and let the chrony user write the +# driftfile (rebuild-image.sh re-applies this after normalising all UIDs). +chown 0:0 "$TARGET_ROOTFS/etc/chrony/chrony.conf" \ + "$TARGET_ROOTFS/usr/bin/bananarchy-time-sync.sh" \ + "$TARGET_ROOTFS/etc/init.d/bananarchy-time-sync" \ + "$TARGET_ROOTFS/etc/NetworkManager/dispatcher.d/20-bananarchy-time-sync.sh" \ + 2>/dev/null || true +chmod 0644 "$TARGET_ROOTFS/etc/chrony/chrony.conf" 2>/dev/null || true +chmod 0755 "$TARGET_ROOTFS/usr/bin/bananarchy-time-sync.sh" \ + "$TARGET_ROOTFS/etc/init.d/bananarchy-time-sync" \ + "$TARGET_ROOTFS/etc/NetworkManager/dispatcher.d/20-bananarchy-time-sync.sh" \ + 2>/dev/null || true +CHRONY_UID="$(awk -F: '/^chrony:/ {print $3}' "$TARGET_ROOTFS/etc/passwd" 2>/dev/null || true)" +CHRONY_GID="$(awk -F: '/^chrony:/ {print $4}' "$TARGET_ROOTFS/etc/passwd" 2>/dev/null || true)" +mkdir -p "$TARGET_ROOTFS/var/lib/chrony" +chown -R "${CHRONY_UID:-101}:${CHRONY_GID:-102}" "$TARGET_ROOTFS/var/lib/chrony" 2>/dev/null || true +chmod 0750 "$TARGET_ROOTFS/var/lib/chrony" 2>/dev/null || true mkdir -p "$TARGET_ROOTFS/etc/bananarchy" chown 0:0 "$TARGET_ROOTFS/etc/bananarchy" 2>/dev/null || true chmod 0700 "$TARGET_ROOTFS/etc/bananarchy" 2>/dev/null || true @@ -304,6 +322,7 @@ rm -rf \ "$TARGET_ROOTFS/var/log/logbookd.db"* ln -sf /etc/init.d/kill-plymouth "$TARGET_ROOTFS/etc/runlevels/default/kill-plymouth" ln -sf /etc/init.d/bananarchy-power-policy "$TARGET_ROOTFS/etc/runlevels/default/bananarchy-power-policy" +ln -sf /etc/init.d/bananarchy-time-sync "$TARGET_ROOTFS/etc/runlevels/default/bananarchy-time-sync" mkdir -p "$TARGET_ROOTFS/var/lib/tinydm" ln -sf /usr/share/xsessions/sxmo.desktop "$TARGET_ROOTFS/var/lib/tinydm/default-session.desktop" diff --git a/scripts/check-rebuild.sh b/scripts/check-rebuild.sh index bb3d6f0..08214d3 100755 --- a/scripts/check-rebuild.sh +++ b/scripts/check-rebuild.sh @@ -179,6 +179,34 @@ if ! grep -q 'MODULES_INITFS' "$BASE_DIR/scripts/apply-bananarchy-overlay.sh" || fail "apply-bananarchy-overlay.sh no longer installs the device initramfs module list" fi +echo "[check] Network time synchronisation..." +for f in overlay/rootfs/etc/chrony/chrony.conf \ + overlay/rootfs/usr/bin/bananarchy-time-sync.sh \ + overlay/rootfs/etc/init.d/bananarchy-time-sync \ + overlay/rootfs/etc/NetworkManager/dispatcher.d/20-bananarchy-time-sync.sh; do + if [ ! -f "$BASE_DIR/$f" ]; then + fail "missing $f" + fi +done +for f in overlay/rootfs/usr/bin/bananarchy-time-sync.sh \ + overlay/rootfs/etc/init.d/bananarchy-time-sync \ + overlay/rootfs/etc/NetworkManager/dispatcher.d/20-bananarchy-time-sync.sh; do + if [ -f "$BASE_DIR/$f" ] && ! sh -n "$BASE_DIR/$f" 2>/dev/null; then + fail "shell syntax: $f" + fi +done +if ! grep -q 'iburst' "$BASE_DIR/overlay/rootfs/etc/chrony/chrony.conf" || \ + ! grep -q 'cmdport 0' "$BASE_DIR/overlay/rootfs/etc/chrony/chrony.conf"; then + fail "chrony.conf must keep iburst and the closed UDP command port" +fi +if ! grep -q 'bananarchy-time-sync' "$BASE_DIR/scripts/apply-bananarchy-overlay.sh" || \ + ! grep -q 'var/lib/chrony' "$BASE_DIR/scripts/apply-bananarchy-overlay.sh"; then + fail "overlay no longer enables the time-sync service and chrony driftfile owner" +fi +if ! grep -q 'var/lib/chrony' "$BASE_DIR/rebuild-image.sh"; then + fail "rebuild-image.sh no longer fixes chrony driftfile ownership" +fi + echo "[check] Docker availability..." if ! command -v docker >/dev/null 2>&1; then fail "docker is not installed"