Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 101 additions & 1 deletion tests/install-phone
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.."
SRC=$PWD
fail() { printf 'not ok - %s\n' "$1" >&2; exit 1; }
pass() { printf 'ok - %s\n' "$1"; }
for tool in debugfs mkfs.ext4 zstd openssl lz4 cpio; do command -v $tool >/dev/null || { echo "skip: no $tool"; exit 0; }; done
for tool in debugfs mkfs.ext4 zstd openssl lz4 cpio timeout; do command -v $tool >/dev/null || { echo "skip: no $tool"; exit 0; }; done
t=$(mktemp -d)
cleanup() {
[[ ! -d $t/gnupg ]] || gpgconf --homedir "$t/gnupg" --kill gpg-agent 2>/dev/null || true
Expand Down Expand Up @@ -450,6 +450,106 @@ if tinstall --verify-only --unsigned; then fail "--verify-only refuses a bundle
[[ ! -s $t/fastboot.log ]] || fail "a refused --verify-only never runs fastboot"
pass "as a bundle's tools/install-phone: finds the bundle, and --verify-only checks it with no phone"

# Two installs with one --work: the staged files have fixed names there, so a
# second install while the first waits at its codename would replace them (the
# first would flash the second owner's answers) and remove them when it ends.
# The second, by any spelling of that directory, stops before it writes in
# --work or reads the ramdisk (a mktemp and an lz4 that note their calls show
# either), so before it contacts the phone; --verify-only still runs beside
# the first; an install killed outright leaves the next one free to go; and
# the lock is beside --work, which ends empty, so a parent that can't be
# written stops the install, as does a lock that can't be taken (a python3
# that fails that one flock call stands in for a file system without flock).
second_install() {
local status=0
: > "$t/fastboot.log"
answers | HOME=$t/home PATH="$t/bin:$PATH" \
timeout --kill-after=2s 30s "$R/tools/install-phone" \
--device testdev --images "$t/images" --work "$t/work" "$@" \
> "$t/out" 2>&1 || status=$?
((status != 124 && status != 137)) ||
fail "a second install refuses promptly instead of waiting for the lock"
return "$status"
}
waiting() { # NAME: an install for user NAME, waiting at the codename; fd 9 types it
mkfifo "$t/$1.in"
HOME=$t/home PATH="$t/bin:$PATH" "$R/tools/install-phone" --device testdev --images "$t/images" --work "$t/work" \
< "$t/$1.in" > "$t/$1.out" 2>&1 &
exec 9> "$t/$1.in"
USERS=$1 answers | sed '$d' >&9
for _ in $(seq 300); do grep -c 'are not touched' "$t/$1.out" >/dev/null && return; sleep 0.1; done
cat "$t/$1.out"; fail "an install waits at the codename prompt"
}
staged() { (cd "$t/work" && find . -name 'verify.*' -prune -o -type f -print | LC_ALL=C sort | xargs cksum); }
flashed_user() {
local f=$t/userdata.flashed
if command -v simg2img >/dev/null && [[ $(head -c4 "$f" | od -An -tx1 | tr -d ' ') == 3aff26ed ]]; then
simg2img "$f" "$t/flashed.img"; f=$t/flashed.img
fi
debugfs -R 'cat /etc/omamobile/firstboot.conf' "$f" 2>/dev/null | sed -n 's/^USERNAME=//p'
}
mkdir -p "$t/watch"
cat > "$t/watch/lz4" <<'EOF'
#!/bin/bash
# lz4 or mktemp (by the name it is called), noting the call
echo "${0##*/} $*" >> "$T/watched.log"
PATH=${PATH//"$T/watch:"/} exec "${0##*/}" "$@"
EOF
chmod +x "$t/watch/lz4" && ln -s lz4 "$t/watch/mktemp"
waiting alice
first=$!
before=$(staged)
if PATH=$t/watch:$PATH USERS=bob second_install 9>&-; then cat "$t/out"; fail "a second install with the same --work is refused"; fi
grep -c "another install is using the work directory $t/work" "$t/out" >/dev/null || { cat "$t/out"; fail "says another install uses the work directory"; }
grep -c "About the phone's owner" "$t/out" >/dev/null && fail "the second install asks no questions"
[[ ! -s $t/fastboot.log ]] || fail "the second install sends the phone nothing"
ln -s work "$t/work-link"
if PATH=$t/watch:$PATH USERS=bob second_install --work "$t/work-link" 9>&-; then cat "$t/out"; fail "a second install with another spelling of the --work is refused"; fi
[[ ! -e $t/watched.log ]] || { cat "$t/watched.log"; fail "a second install stops before it writes in --work or reads the ramdisk"; }
[[ $(staged) == "$before" ]] || fail "the second install leaves the first one's staged files as they were"
install --verify-only 9>&- || { cat "$t/out"; fail "--verify-only runs beside a waiting install"; }
[[ $(staged) == "$before" ]] || fail "--verify-only leaves the waiting install's staged files as they were"
: > "$t/fastboot.log"
echo testdev >&9
exec 9>&-
wait $first || { cat "$t/alice.out"; fail "the waiting install goes ahead"; }
[[ $(flashed_user) == alice ]] || fail "the waiting install flashes its own owner's answers"
waiting carol
killed=$!
kill -KILL $killed $(cat /proc/$killed/task/*/children)
exec 9>&-
wait $killed 2>/dev/null || true
USERS=dave install || { cat "$t/out"; fail "an install killed outright leaves the next one free to go"; }
[[ $(flashed_user) == dave ]] || fail "the next install flashes its own answers"
[[ -z $(ls -A "$t/work") && -f $t/.work.install.lock ]] || fail "the work directory ends empty, the lock beside it"
if ((EUID != 0)); then
mkdir -p "$t/ro/work" && chmod 555 "$t/ro"
install --work "$t/ro/work" && refused=0 || refused=1
chmod 755 "$t/ro"
((refused)) || { cat "$t/out"; fail "an install whose lock can't be made beside --work is refused"; }
grep -c "can't create the lock file $t/ro/.work.install.lock beside the work directory" "$t/out" >/dev/null || { cat "$t/out"; fail "names the lock file it can't create"; }
[[ ! -s $t/fastboot.log ]] || fail "an install with no lock sends the phone nothing"
fi
mkdir -p "$t/nolock"
cat > "$t/nolock/python3" <<'EOF'
#!/bin/bash
# python3 ($PYTHON3), but the install lock's flock fails as on a file system without flock
if [[ $# == 3 && $1 == -I && $2 == - ]]; then
script=$(cat)
[[ $script != *'except BlockingIOError'* ]] ||
script=$'import errno, fcntl\ndef flock(fd, op): raise OSError(errno.EOPNOTSUPP, "Operation not supported")\nfcntl.flock = flock\n'$script
exec "$PYTHON3" -I -c "$script" "$3"
fi
exec "$PYTHON3" "$@"
EOF
chmod +x "$t/nolock/python3"
if PYTHON3=$(command -v python3) PATH=$t/nolock:$PATH install; then cat "$t/out"; fail "an install whose lock can't be taken is refused"; fi
grep -c "can't lock the work directory $t/work (give a --work DIR on a file system that supports flock)" "$t/out" >/dev/null ||
{ cat "$t/out"; fail "says it can't lock the work directory"; }
grep -c "About the phone's owner" "$t/out" >/dev/null && fail "an install with no lock asks no questions"
[[ ! -s $t/fastboot.log && -z $(ls -A "$t/work") ]] || fail "an install with no lock stages nothing and sends the phone nothing"
pass "one install at a time per work directory: a second one stops before the phone, --verify-only runs beside it, a killed one keeps none out, the lock is beside --work, no lock stops the install"

# The guided installer has asked the questions: they aren't printed again,
# the answers are still read and checked, and the closing hints stay out
OMAMOBILE_INSTALL_GUIDED=1 install --dry-run || { cat "$t/out"; fail "a dry run for the guided installer succeeds"; }
Expand Down
36 changes: 24 additions & 12 deletions tests/install-phone-initramfs
Original file line number Diff line number Diff line change
Expand Up @@ -207,29 +207,41 @@ for dev in "twelve gs201 0x9855001 0x800 vendor_kernel_boot" "six gs101 0x984500
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).
# run is live nothing is removed: here a --verify-only killed part-way, and
# another 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). The killed run is a
# --verify-only held in its lz4 by a stand-in that waits on a fifo, because a
# second install with the same --work stops before it reads the ramdisk.
held() { # name fd: an install of six waiting at the codename, which fd gives
mkfifo "$t/$1.fifo" && eval "exec $2<>\"\$t/$1.fifo\""
# Its output is in OUT; the job's own shell would print bash's "Killed"
# notice for the one killed below, which in a passing log reads as a failure
# notice for a killed run, which in a passing log reads as a failure
HOLD=$t/$1.fifo OUT=$t/$1.out install six 7>&- 8>&- 2>/dev/null &
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"
}
mkdir -p "$t/hold"
printf '#!/bin/bash\necho "$BASHPID" > "$T/hold.pid"\nread -r _ < "$T/hold.fifo" || exit 1\nexec %q "$@"\n' "$(command -v lz4)" > "$t/hold/lz4"
chmod +x "$t/hold/lz4"
held_verify() { # fd: a --verify-only of six, held in its lz4 (which fd gives)
mkfifo "$t/hold.fifo" && eval "exec $1<>\"\$t/hold.fifo\""
# As in held: no "Killed" notice from the job's own shell
PATH=$t/hold:$PATH OUT=$t/killed.out install six --verify-only 7>&- 8>&- 2>/dev/null &
for _ in $(seq 600); do [[ -s $t/hold.pid ]] && return; sleep 0.1; done
cat "$t/killed.out"; fail "a --verify-only is held part-way"
}
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")"
held_verify 8
kill -KILL "$(cat "$t/killed.out.pid")" "$(cat "$t/hold.pid")"
exec 8>&-
wait $! || true
both=$(verify_dirs)
[[ $(wc -w <<<"$both") == 3 ]] || fail "a second install, killed, leaves its directory and the waiting one's (got: $both)"
[[ $(wc -w <<<"$both") == 3 ]] || fail "a --verify-only, killed part-way, leaves its directory and the waiting install'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
Expand All @@ -238,11 +250,11 @@ wait $live || { cat "$t/live.out"; fail "the waiting install goes ahead after --
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"; }
install six --verify-only || { cat "$t/out"; fail "--verify-only passes after a killed run"; }
[[ $(verify_dirs) == 'verify.unmarked ' ]] ||
fail "a run with no other live removes a killed install's directory, and nothing unmarked (left: $(verify_dirs))"
fail "a run with no other live removes a killed run'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"
pass "a killed run'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
Expand Down
48 changes: 47 additions & 1 deletion tools/install-phone
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@
# Answers are read from standard input, so they can be piped for tests and by
# the guided installer, which sets OMAMOBILE_INSTALL_GUIDED=1: the questions
# are then not printed again (it has asked them), nor the closing hints.
#
# One install or dry run at a time uses a work directory: each holds a lock
# beside it (.NAME.install.lock in its parent directory), and a second one
# with the same work directory stops before it contacts the phone (give it
# its own --work DIR). --verify-only runs beside them.
# Its parent directory must be writable; the lock file is kept after the run.
set -euo pipefail
shopt -s nullglob
# Byte-wise character ranges in the checks below ([ -~] is printable ASCII)
Expand Down Expand Up @@ -412,12 +418,52 @@ if [[ -f $manifest ]]; then
if [[ -n $modules_release && -n $images_release ]]; then note "kernel $images_release, its modules in the root image"; fi
fi

# --- One install at a time per work directory --------------------------------
# The files staged below have fixed names in --work: a second install there
# would replace them while this one waits for its codename, and remove them
# when it ends. So an install (or a dry run; --verify-only takes no part)
# holds an exclusive flock until it exits, and a second one stops here, before
# it reads the ramdisk or writes in --work. The kernel drops the lock with the
# run's last process, however it ends. The lock file is beside the resolved
# work directory (as make-bundle's is beside the bundles), so two spellings of
# it lock one file and the directory still ends empty. It stays: unlinking it
# could let two runs lock different inodes. A parent that can't be written, or
# a lock that can't be taken for any other reason (a file system without
# flock), stops the install too: going ahead unlocked could let a second
# install replace this one's files.
work=${work:-${XDG_CACHE_HOME:-$HOME/.cache}/omamobile-install}
if ((!verify_only)); then
mkdir -p "$work" && work_real=$(cd -P -- "$work" && pwd -P) ||
die "can't create or write to the work directory $work (give one you can write to with --work DIR)"
install_lock=${work_real%/*}/.${work_real##*/}.install.lock
exec {install_fd}>>"$install_lock" ||
die "can't create the lock file $install_lock beside the work directory (give a --work DIR under a directory you can write to)"
if python3 -I - "$install_fd" <<'PY'
import fcntl, sys
try:
fcntl.flock(int(sys.argv[1]), fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError:
sys.exit(1)
except OSError as error:
print(f"flock: {error}", file=sys.stderr)
sys.exit(2)
PY
then
:
else
lock_status=$?
if ((lock_status == 1)); then
die "another install is using the work directory $work: let it finish, or give this one its own with --work DIR"
fi
die "can't lock the work directory $work (give a --work DIR on a file system that supports flock)"
fi
fi

# --- Slot B's shipped ramdisk ------------------------------------------------
# Read and listed before the phone is contacted, so --verify-only checks it
# too; the owner's key is appended to it further down. The read has a
# directory of its own in --work that only this run removes: another install
# may be waiting there for its answers, with its images staged.
work=${work:-${XDG_CACHE_HOME:-$HOME/.cache}/omamobile-install}
verify_dir=""
# vendor_ramdisk get IMG OUT, or vendor_ramdisk set IMG RAMDISK OUT (inline,
# because a bundle ships this script alone)
Expand Down