From b4cb0f94801de09ba25000f20d83380d466048da Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Thu, 12 Nov 2026 01:21:33 +0000 Subject: [PATCH] install-phone: the next run removes the verify directory a killed run left Since #648 the shipped ramdisk is read in a verify.XXXXXX directory of the run's own in --work, removed when the run exits. A run killed outright (SIGKILL, a closed terminal's kill, a crash) never exits that way, so its directory, with the decompressed ramdisk, stayed in --work for good, one more for every such run. A run can't tell a dead run's directory from a live one's by age (an install can wait at the codename prompt for hours), and a full install needs its directory until it exits. So each run holds a shared flock on --work itself (a descriptor on the directory, no file of its own there) until it exits, and marks its directory only once it holds the lock. A run that gets the lock exclusively, so no other run of this installer is live, first removes the marked directories: their runs have all ended. A directory not yet marked, or left by an installer from before this change (whose run may still be going), is never removed. flock comes from python3's fcntl, which the installer already needs; flock(1) is util-linux, not in its tool list, and the bundle ships install alone, so the helper is inline (L-497). Where --work can't be locked (some network file systems) nothing is removed and nothing is marked. Tested: tests/install-phone-initramfs gains the case (an install waiting at the codename, a second one killed with SIGKILL there, --verify-only beside the waiting one, the waiting one then flashing the key's append, and a later --verify-only alone), which fails with main's tool at "a run with no other live removes a killed install's directory"; mutants without the exclusive check, with the shared lock let go after marking, without the mark check, or with no mark written each fail it, and a blocking exclusive lock hangs it. Both install-phone suites pass, the initramfs one under umask 077, 022 and 002; tools/pr-check origin/main; tests/all. A bundle's ./install --unsigned, killed during --verify-only's read and at the codename prompt, then run again: the leftovers pile up on main and are gone on this branch, and an install waiting at the codename keeps its directory and flashes. Not tested: on a phone (laptop-side only); a network-mounted home; a run killed between creating its directory and marking it (its directory stays, as on main). Co-Authored-By: Claude Opus 5.5 --- tests/install-phone-initramfs | 42 +++++++++++++++++++++++++++++++++-- tools/install-phone | 24 ++++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/tests/install-phone-initramfs b/tests/install-phone-initramfs index 769dc3ce2..9ebdc1ef1 100755 --- a/tests/install-phone-initramfs +++ b/tests/install-phone-initramfs @@ -145,8 +145,10 @@ install() { # codename [args]; RAMDISK_PART_SIZE: vendor_kernel_boot_b's size done } > "$t/vars" # user, password twice, host, time zone, name, email, Wi-Fi, key, confirmation - printf '%s\n' alice 1234 1234 "" UTC "" "" "" "$t/owner.pub" "$cn" | - (umask 077; HOME=$t/home PATH="$t/fbin:$PATH" "$BUNDLE/install" --unsigned --work "$t/work" "$@") > "$t/out" 2>&1 + # (read from the fifo HOLD when set); output to OUT, the install's PID to OUT.pid + { printf '%s\n' alice 1234 1234 "" UTC "" "" "" "$t/owner.pub"; if [[ -n ${HOLD:-} ]]; then cat "$HOLD"; else echo "$cn"; fi; } | + (umask 077; echo "$BASHPID" > "${OUT:-$t/out}.pid" + HOME=$t/home PATH="$t/fbin:$PATH" exec "$BUNDLE/install" --unsigned --work "$t/work" "$@") > "${OUT:-$t/out}" 2>&1 } refused() { # why message [args]: install refused, said why, flashed nothing if install twelve "${@:3}"; then fail "refuses $1"; fi @@ -204,6 +206,42 @@ for dev in "twelve gs201 0x9855001 0x800 vendor_kernel_boot" "six gs101 0x984500 pass "$cn: the bundle's install flashes the hook's own $rimg.img (the owner's key appended) and the hook's other images" done +# A run killed outright leaves its verify.XXXXXX in --work. While another +# run is live nothing is removed: here --verify-only beside an install waiting +# at the codename, which then appends the key from its own directory. The +# next run with no other live removes the killed one's, but never a directory +# no run marked (as the installer before the sweep left them, whose run may +# still be live). +held() { # name fd: an install of six waiting at the codename, which fd gives + mkfifo "$t/$1.fifo" && eval "exec $2<>\"\$t/$1.fifo\"" + HOLD=$t/$1.fifo OUT=$t/$1.out install six 7>&- 8>&- & + for _ in $(seq 600); do grep -c "Type the phone's codename" "$t/$1.out" >/dev/null 2>&1 && return; sleep 0.1; done + cat "$t/$1.out"; fail "an install waits at the codename prompt" +} +verify_dirs() { (cd "$t/work" && ls -d verify.* | LC_ALL=C sort | tr '\n' ' '); } +mkdir -p "$t/work/verify.unmarked" +held live 7 +live=$! +held killed 8 +kill -KILL "$(cat "$t/killed.out.pid")" +exec 8>&- +{ wait $!; } 2>/dev/null || true +both=$(verify_dirs) +[[ $(wc -w <<<"$both") == 3 ]] || fail "a second install, killed, leaves its directory and the waiting one's (got: $both)" +install six --verify-only || { cat "$t/out"; fail "--verify-only passes while an install waits"; } +[[ $(verify_dirs) == "$both" ]] || fail "--verify-only beside a waiting install removes nothing (had: $both; now: $(verify_dirs))" +echo six >&7 +exec 7>&- +wait $live || { cat "$t/live.out"; fail "the waiting install goes ahead after --verify-only"; } +cmp -s "$t/flashed/vendor_boot_b" "$t/hook-images-six/vendor_boot.img" || + fail "the waiting install appends the key from its own directory after --verify-only" +[[ $(wc -w <<<"$(verify_dirs)") == 2 ]] || fail "the install removes its own directory and no other" +install six --verify-only || { cat "$t/out"; fail "--verify-only passes after a killed install"; } +[[ $(verify_dirs) == 'verify.unmarked ' ]] || + fail "a run with no other live removes a killed install's directory, and nothing unmarked (left: $(verify_dirs))" +rm -r "$t/work" +pass "a killed install's verify directory is removed by the next run alone; a live install's and an unmarked one stay" + # An install waiting at "Type the phone's codename" has its images staged in # --work. A --verify-only run on the same --work, or a second install refused # before the owner's answers (here another phone's --serial), reads the diff --git a/tools/install-phone b/tools/install-phone index 6b5a906e8..4ca51f615 100755 --- a/tools/install-phone +++ b/tools/install-phone @@ -468,6 +468,30 @@ if [[ -n $ramdisk_image ]]; then mkdir -p "$work" verify_dir=$(mktemp -d "$work/verify.XXXXXX") trap 'rm -rf "$verify_dir"' EXIT + # A run killed outright leaves its directory behind. Each run holds a shared + # lock on --work itself (no file of its own there) until it exits, and marks + # its directory only once it holds that lock; a run that gets it alone + # removes the marked ones, whose runs have all ended. A directory not yet + # marked, or left by an installer without the lock (whose run may still be + # going), is never removed. + if exec {work_lock}<"$work"; then + python3 -I - "$work_lock" "$work" "$verify_dir" <<'PY' || true +import fcntl, os, shutil, sys +lock, work, mine = int(sys.argv[1]), sys.argv[2], sys.argv[3] +mark = "sweepable" +try: + fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) +except OSError: + pass # another run holds it (or the file system can't lock): remove nothing +else: + for name in os.listdir(work): + d = os.path.join(work, name) + if name.startswith("verify.") and not os.path.islink(d) and os.path.isfile(os.path.join(d, mark)): + shutil.rmtree(d, ignore_errors=True) +fcntl.flock(lock, fcntl.LOCK_SH) +open(os.path.join(mine, mark), "x").close() +PY + fi vendor_ramdisk get "$images/$ramdisk_image.img" "$verify_dir/shipped.cpio.lz4" || die "can't read the ramdisk of $ramdisk_image.img" lz4 -dc "$verify_dir/shipped.cpio.lz4" > "$verify_dir/shipped.cpio" || die "lz4 couldn't decompress $ramdisk_image.img's ramdisk" # With --quiet, cpio -it says nothing when it lists the archive; given bytes