From ebe635501d7210a0fad62b7d622ce6c511ed1149 Mon Sep 17 00:00:00 2001 From: jknapp Date: Sun, 29 Nov 2026 07:31:56 -0800 Subject: [PATCH] pkgcache host: lock-free 'mode', atomic apply/revert file writes, shared repo lock - health 'mode' took the 30 s flock and mkdir'd the state dir; a held lock gave an empty answer, which sync (OFF) read as 'not off' and reverted from the newest backup. mode is now read-only and lock-free. - apply's rewrite() truncated repo files in place and revert used cp -a over the file; both now write a temp file and rename(2), so a concurrent dnf never sees half a file. - apply/revert take the same lock as the health timer so a health run cannot rewrite repo files while they edit them. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_0185ukXi4YX5FxJGqhi6qab6 --- scripts/host/pkgcache-host-health.sh | 8 +++++++ scripts/host/pkgcache-host-repos.sh | 31 ++++++++++++++++++++++------ scripts/tests/pkgcache-host.test.sh | 4 ++++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/scripts/host/pkgcache-host-health.sh b/scripts/host/pkgcache-host-health.sh index ba1d648..87582cc 100755 --- a/scripts/host/pkgcache-host-health.sh +++ b/scripts/host/pkgcache-host-health.sh @@ -33,6 +33,14 @@ MARK='#pkgcache-cachefirst ' MANAGED_RE='^baseurl=https://[^ /]+(:[0-9]+)?/(almalinux|epel|remi)/[^ ]+ https://[^ ]+$' log() { logger -t pkgcache-host-health -p "daemon.$1" -- "${*:2}" 2>/dev/null || true; echo "$1: ${*:2}" >&2; } +# "mode" is read-only and is used by repos.sh sync/status: no lock (a 30 s wait that ends in an +# empty answer would make `sync` think the host is not OFF and revert it), no state dir needed. +if [ "${1:-mode}" = mode ]; then + if grep -q -s -F "$MARK" "$REPO_DIR"/*.repo 2>/dev/null; then echo origin-only + elif grep -q -s -E "$MANAGED_RE" "$REPO_DIR"/*.repo 2>/dev/null; then echo cache-first + else echo off; fi + exit 0 +fi mkdir -p "$STATE_DIR" exec 9>"$STATE_DIR/lock"; flock -w 30 9 || { echo "lock busy" >&2; exit 0; } SF="$STATE_DIR/state" diff --git a/scripts/host/pkgcache-host-repos.sh b/scripts/host/pkgcache-host-repos.sh index fcad9df..a1e6b5b 100755 --- a/scripts/host/pkgcache-host-repos.sh +++ b/scripts/host/pkgcache-host-repos.sh @@ -79,6 +79,10 @@ BKROOT="${PKGCACHE_BACKUP_ROOT:-/root}" update_ca() { [ "${PKGCACHE_NO_CATRUST:-0}" = 1 ] || update-ca-trust; } dnf_clean() { [ "${PKGCACHE_NO_CATRUST:-0}" = 1 ] || dnf clean metadata -q || true; } die() { echo "FATAL(pkgcache-host-repos): $*" >&2; exit 1; } +# Same lock as pkgcache-host-health.sh, so a health run can never rewrite repo files while +# apply/revert do (child processes that take the lock themselves must NOT be called inside). +with_lock() { mkdir -p "$LOCKDIR"; ( flock -w 60 8 || die "repo lock busy"; "$@" ) 8>"$LOCKDIR/lock"; } +LOCKDIR="${PKGCACHE_STATE_DIR:-/var/lib/pkgcache-host}" same() { [ "$(sha256sum < "$1")" = "$(sha256sum < "$2")" ]; } # cmp/diffutils is not on minimal hosts rewrite() { # $1 file $2 cache url @@ -122,7 +126,13 @@ for line in open(f).read().split('\n'): break sec.append(line) flush() -open(f, 'w').write('\n'.join(out)) +import os, tempfile +st = os.stat(f) +fd, tmp = tempfile.mkstemp(dir=os.path.dirname(os.path.abspath(f)), prefix='.pkgcache-', suffix='.tmp') +with os.fdopen(fd, 'w') as fh: + fh.write('\n'.join(out)); fh.flush(); os.fsync(fh.fileno()) +os.chmod(tmp, st.st_mode & 0o7777); os.chown(tmp, st.st_uid, st.st_gid) +os.rename(tmp, f) # atomic: a concurrent dnf sees the old or the new file, never half of one PY } @@ -152,10 +162,13 @@ case "$MODE" in cp -a "$REPOS"/*.repo "$B/yum.repos.d/" [ -e "$ANCHOR" ] && cp -a "$ANCHOR" "$B/" || touch "$B/.no-anchor-before" install -m 0644 "$CA_SRC" "$ANCHOR" && update_ca - for f in "$REPOS"/almalinux*.repo "$REPOS"/epel*.repo "$REPOS"/remi*.repo; do - [ -f "$f" ] || continue - grep -q -E '^#?\s*baseurl\s*=\s*https?://(repo\.almalinux\.org|download\.example|rpms\.remirepo\.net)/' "$f" && rewrite "$f" "$C" - done + rewrite_all_repos() { + for f in "$REPOS"/almalinux*.repo "$REPOS"/epel*.repo "$REPOS"/remi*.repo; do + [ -f "$f" ] || continue + if grep -q -E '^#?\s*baseurl\s*=\s*https?://(repo\.almalinux\.org|download\.example|rpms\.remirepo\.net)/' "$f"; then rewrite "$f" "$C"; fi + done + } + with_lock rewrite_all_repos install_timer echo "backup: $B" echo "changed files:"; for f in "$B"/yum.repos.d/*.repo; do same "$f" "$REPOS/$(basename "$f")" || echo " $REPOS/$(basename "$f")"; done @@ -164,7 +177,13 @@ case "$MODE" in remove_timer; rm -f "${PKGCACHE_STATE_DIR:-/var/lib/pkgcache-host}/state" B="${2:-$(ls -d "$BKROOT"/pkgcache-repo-backup-* 2>/dev/null | sort | tail -1)}" [ -d "$B/yum.repos.d" ] || die "no backup dir ($B)" - for f in "$B"/yum.repos.d/*.repo; do same "$f" "$REPOS/$(basename "$f")" || { cp -a "$f" "$REPOS/"; echo "restored $REPOS/$(basename "$f")"; }; done + restore_repos() { + for f in "$B"/yum.repos.d/*.repo; do + d="$REPOS/$(basename "$f")" + same "$f" "$d" || { cp -a "$f" "$d.pkgcache-restore" && mv -f "$d.pkgcache-restore" "$d"; echo "restored $d"; } # atomic for a concurrent dnf + done + } + with_lock restore_repos if [ -e "$B/.no-anchor-before" ]; then rm -f "$ANCHOR"; update_ca; echo "removed $ANCHOR"; fi dnf_clean echo "reverted from $B" diff --git a/scripts/tests/pkgcache-host.test.sh b/scripts/tests/pkgcache-host.test.sh index 23b16c4..f22c726 100755 --- a/scripts/tests/pkgcache-host.test.sh +++ b/scripts/tests/pkgcache-host.test.sh @@ -82,6 +82,10 @@ FAKE_CACHE=refused FAKE_ORIGIN=down "$HL" run 2>/dev/null; FAKE_CACHE=refused FA [ "$("$HL" mode)" = cache-first ] && ok "cache+origin both down: no fallback" || bad "network outage caused a fallback" FAKE_CACHE=refused "$HL" run 2>/dev/null; FAKE_CACHE=refused "$HL" run 2>/dev/null; [ "$("$HL" mode)" = origin-only ] && ok "connection refused counts as failure" || bad "refused not counted" +# mode must not wait for the health lock (a held lock once turned `mode` into an empty answer) +( flock 9; sleep 4 ) 9>"$T/state/lock" & sleep 0.5 +m=$(timeout 3 "$HL" mode || true); [ "$m" = origin-only ] && ok "mode answers while the health lock is held" || bad "mode blocked on the lock (got '$m')" +wait # ---- OFF via setting reverts cleanly, even from origin-only ---- echo "PKGCACHE_ENABLED=0" >"$T/pkgcache-host.conf"; printf 'PKGCACHE_CA=%s\n' "$T/ca.crt" >>"$T/pkgcache-host.conf" "$R" sync >/dev/null