#!/bin/sh # Chandra's updater on the handset. It runs in a privileged pod on the phone (charts/chandra-handset # in nandi/cluster), one image per commit, with the phone's /usr/local at $HOST/usr/local. # # handset run install what can go in live, then wait to install the rest (the pod's command) # handset status what is installed and what is waiting # handset approve install the waiting compositor now: RESTARTS THE SESSION, closing its apps # handset ready exit 0 when the live part is dealt with (the pod's readiness probe) # # payload/live chandra-remote and its web page: installed at once; chandra-remote restarts in # place (SIGUSR2) and browsers reconnect. # payload/session the compositor, its host, the panel modules...: installed only when approved, or # when the phone has been locked with its screen off for $IDLE_MIN minutes. The # session is restarted (locked again if it was), and everything is put back if it # doesn't answer. A release that failed is not tried again unless approved. set -u HOST=${HOST:-/host} PAYLOAD=${PAYLOAD:-/payload} STATE=${STATE:-$HOST/var/lib/chandra-handset} RUN=${RUN:-/tmp/handset} RELEASE=${RELEASE:-$(cat "$PAYLOAD/RELEASE")} IDLE_MIN=${IDLE_MIN:-10} POLL=${POLL:-30} SESSION_USER=${SESSION_USER:-nandi} SESSION_UID=${SESSION_UID:-1000} MARKER=/run/chandra-start-locked # compositor/host/lock.mbt log() { echo "$(date -u +%FT%TZ) $*"; } # Run a command as the phone itself, not in the container. onhost() { if [ -n "${FAKE_HOST:-}" ]; then "$FAKE_HOST" "$@"; else nsenter -t 1 -m -u -i -n -p -- "$@"; fi; } ctl() { onhost runuser -u "$SESSION_USER" -- env "XDG_RUNTIME_DIR=/run/user/$SESSION_UID" \ timeout 5 /usr/local/bin/chandra-ctl --socket "/run/user/$SESSION_UID/chandra/wayland-moon-0.sock" "$@" 2>/dev/null } # Files of a group ("live" or "session") that differ from what is installed, relative to /. differing() { (cd "$PAYLOAD/$1" && find . -type f | sed 's|^\./||' | sort) | while IFS= read -r f; do cmp -s "$PAYLOAD/$1/$f" "$HOST/$f" || echo "$f" done } # Install FILES (a list, one per line) of GROUP, keeping what was there in $STATE/prev-GROUP. install_files() { group=$1 files=$2 prev="$STATE/prev-$1" rm -rf "${STATE:?}/prev-${group:?}.new" && mkdir -p "$prev.new" || return 1 printf '%s\n' "$files" > "$prev.new/FILES" printf '%s\n' "$files" | while IFS= read -r f; do if [ -e "$HOST/$f" ]; then mkdir -p "$prev.new/$(dirname "$f")" && cp -p "$HOST/$f" "$prev.new/$f" || exit 1 fi done || return 1 rm -rf "${STATE:?}/prev-${group:?}" && mv "$prev.new" "$prev" || return 1 printf '%s\n' "$files" | while IFS= read -r f; do mode=644 case "$f" in */bin/*|*/lib/*) mode=755 ;; esac mkdir -p "$HOST/$(dirname "$f")" && cp "$PAYLOAD/$group/$f" "$HOST/$f.handset-new" && chown 0:0 "$HOST/$f.handset-new" && chmod "$mode" "$HOST/$f.handset-new" && mv -f "$HOST/$f.handset-new" "$HOST/$f" || exit 1 done } # Put back what the last install_files GROUP replaced, and take out what it added. restore_files() { prev="$STATE/prev-$1" while IFS= read -r f; do [ -n "$f" ] || continue if [ -e "$prev/$f" ]; then cp -p "$prev/$f" "$HOST/$f.handset-new" && mv -f "$HOST/$f.handset-new" "$HOST/$f" else rm -f "${HOST:?}/${f:?}" fi done < "$prev/FILES" } # Whether a program of the payload starts on this phone (its libraries are there). starts() { check="usr/local/lib/.handset-check" mkdir -p "$HOST/usr/local/lib" && cp "$PAYLOAD/$1" "$HOST/$check" && chmod 755 "$HOST/$check" || return 1 onhost ldd "/$check" > "$RUN/ldd" 2>&1; rc=$? rm -f "${HOST:?}/usr/local/lib/.handset-check" if [ $rc -ne 0 ] || grep -q 'not found' "$RUN/ldd"; then log "ERROR: $1 doesn't start here:"; cat "$RUN/ldd" return 1 fi } remote_pid() { onhost pgrep -x -u "$SESSION_USER" chandra-remote | head -n 1; } remote_answers() { onhost curl -fsS -m 3 -o /dev/null http://127.0.0.1:6080/api/info 2>/dev/null; } # Whether a process catches SIGUSR2 (12). A chandra-remote from before it could restart in place # would be killed by it. catches_usr2() { cgt=$(onhost sed -n 's/^SigCgt:[[:space:]]*//p' "/proc/$1/status") [ -n "$cgt" ] && [ $((0x$(printf %s "$cgt" | tail -c 4) & 0x800)) -ne 0 ] } # 0: installed. 1: failed. 2: must wait for the session restart. apply_live() { files=$(differing live) [ -n "$files" ] || return 0 pid=$(remote_pid) if [ -n "$pid" ] && ! catches_usr2 "$pid"; then log "live part waits for the session restart: the running chandra-remote can't restart in place" return 2 fi starts live/usr/local/bin/chandra-remote || return 1 log "installing live: $(echo $files)" install_files live "$files" || { restore_files live; return 1; } if [ -n "$pid" ]; then case "$files" in *bin/chandra-remote*) onhost kill -USR2 "$pid" sleep 1 i=0 while ! remote_answers; do i=$((i + 1)) if [ $i -gt 15 ]; then log "ERROR: chandra-remote doesn't answer: putting the old one back" restore_files live pid=$(remote_pid); [ -n "$pid" ] && onhost kill -USR2 "$pid" return 1 fi sleep 1 done ;; esac fi log "live part installed" } session_up() { i=0 while [ $i -lt 20 ]; do sleep "${UP_POLL:-2}" onhost pgrep -x chandra-host > /dev/null && [ "$(ctl ping)" = "ok pong" ] && return 0 i=$((i + 1)) done return 1 } # Restart the login session; $1 = 1: it was locked, so it must come back locked, screen off. restart_session() { if [ "$1" = 1 ]; then onhost sh -c "echo 1 > $MARKER" || return 1; fi onhost systemctl restart greetd session_up || return 1 if [ "$1" = 1 ]; then # A compositor that doesn't know the marker is locked by hand. [ "$(ctl lock status)" = "ok locked" ] || ctl lock > /dev/null ctl screen off > /dev/null # The marker stays until the session is known to be locked. [ "$(ctl lock status)" = "ok locked" ] && onhost rm -f /run/chandra-start-locked fi return 0 } apply_session() { s_files=$(differing session) l_files=$(differing live) [ -n "$s_files$l_files" ] || return 0 # Anything but a clear "unlocked" counts as locked. locked=1 [ "$(ctl lock status)" = "ok unlocked" ] && locked=0 for p in session/usr/local/bin/chandra-host session/usr/local/lib/chandra/chandra-polkit-agent live/usr/local/bin/chandra-remote; do starts "$p" || { echo "$RELEASE" > "$STATE/failed"; return 1; } done log "installing session: $(echo $s_files $l_files)" ok=1 if [ -n "$s_files" ]; then install_files session "$s_files" || ok=0; fi if [ -n "$l_files" ] && [ $ok = 1 ]; then install_files live "$l_files" || ok=0; did_live=1; else did_live=0; fi if [ $ok = 1 ] && restart_session $locked; then rm -f "${STATE:?}/failed" log "session restarted on $RELEASE (lock: $(ctl lock status))" return 0 fi log "ERROR: the new session didn't come up: putting the old one back" echo "$RELEASE" > "$STATE/failed" [ -n "$s_files" ] && restore_files session [ $did_live = 1 ] && restore_files live restart_session $locked || log "ERROR: the old session doesn't answer either" return 1 } idle_now() { [ "$(ctl lock status)" = "ok locked" ] || return 1 case "$(ctl status)" in *" screen=off "*) return 0 ;; esac return 1 } status() { echo "release $RELEASE" live=$(differing live); session=$(differing session) if [ -z "$live" ]; then echo "live: installed"; else echo "live: waiting:"; echo "$live" | sed 's/^/ /'; fi if [ -z "$session" ]; then echo "session: installed"; else echo "session: waiting (handset approve):"; echo "$session" | sed 's/^/ /'; fi if [ "$(cat "$STATE/failed" 2>/dev/null)" = "$RELEASE" ]; then echo "session: this release FAILED; only 'handset approve' tries it again" fi echo "phone: $(ctl lock status) | $(ctl status)" } mkdir -p "$RUN" case "${1:-}" in run) mkdir -p "$STATE" && chmod 700 "$STATE" || exit 1 rm -f "${RUN:?}/live-ready" "${RUN:?}/approve" log "release $RELEASE" apply_live case $? in 0) echo installed > "$RUN/live-ready" ;; 2) echo waiting > "$RUN/live-ready" ;; *) log "ERROR: live part not installed"; exit 1 ;; esac status idle=0 rounds=0 while :; do if [ -n "$(differing session)$(differing live)" ]; then if [ -e "$RUN/approve" ]; then rm -f "${RUN:?}/approve" log "approved" apply_session; echo $? > "$RUN/approve-result" elif [ "$(cat "$STATE/failed" 2>/dev/null)" != "$RELEASE" ] && idle_now; then idle=$((idle + POLL)) if [ $idle -ge $((IDLE_MIN * 60)) ]; then log "locked and dark for $IDLE_MIN minutes" apply_session idle=0 fi else idle=0 fi elif [ -e "$RUN/approve" ]; then rm -f "${RUN:?}/approve"; echo 0 > "$RUN/approve-result" fi rounds=$((rounds + 1)) if [ -n "${ROUNDS:-}" ] && [ $rounds -ge "$ROUNDS" ]; then exit 0; fi # Wake at once for an approval. i=0 while [ $i -lt "$POLL" ] && [ ! -e "$RUN/approve" ]; do sleep 1; i=$((i + 1)); done done ;; approve) rm -f "${RUN:?}/approve-result"; touch "$RUN/approve" echo "installing $RELEASE and restarting the session..." i=0 while [ ! -e "$RUN/approve-result" ] && [ $i -lt 180 ]; do sleep 1; i=$((i + 1)); done status exit "$(cat "$RUN/approve-result" 2>/dev/null || echo 1)" ;; ready) [ -e "$RUN/live-ready" ] ;; status) status ;; *) sed -n '2,19p' "$0"; exit 2 ;; esac