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