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
3 changes: 2 additions & 1 deletion default/splash/answer
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ exec 3<&0
exec 3<&-
"$@"
status=$?
printf '%s %s\n' "$id" "$status" >"$run/result"
# The dot is the byte ask-for-password waits for with a time limit.
printf '.%s %s\n' "$id" "$status" >"$run/result"
exit "$status"
) <&3 &
attempt=$!
Expand Down
7 changes: 6 additions & 1 deletion default/splash/plymouth
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,14 @@ ask_for_password() {
# session), and ends when the splash is gone for good. An attempt the
# splash started is waited for even then: the console's own attempt would
# find the volume it unlocked already open, and fail forever.
#
# Only the byte in front of the answer is read with a time limit: busybox's
# read drops what it has read when its time runs out, and an answer cut
# short would leave the boot waiting forever.
status=""
while [ -z "$status" ]; do
if read -r -t 1 -u 3 reply answer; then
if read -r -t 1 -n 1 -u 3 _; then
read -r -u 3 reply answer
[ "$reply" = "$id" ] && status=$answer
elif ! alive "$run/session.pid" && ! answering "$id"; then
exec 3>&-
Expand Down
61 changes: 61 additions & 0 deletions test/shell.d/boot-splash-answer-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

# An unlock's exit status always reaches the `plymouth ask-for-password` that
# waits for it. The initramfs's busybox drops what `read -t` has read when its
# time runs out, so a timed read of more than one byte can lose the start of an
# answer. The timed read here loses the first byte it is sent that way once.

test_tmp=$(mktemp -d)
run=$test_tmp/run
mkdir "$run"
sleep 60 >/dev/null &
session=$!
trap 'kill "$session" 2>/dev/null || true; rm -rf "$test_tmp"' EXIT
echo "$session" >"$run/session.pid"
touch "$run/ready"

for script in plymouth answer; do
sed -e "s|/run/omarchy-splash|$run|g" -e 's|read -r -t 1|timed_read|' \
"$ROOT/default/splash/$script" >"$test_tmp/$script"
done
grep -q timed_read "$test_tmp/plymouth" || fail "the wait for an answer reads with a time limit"

cat >"$test_tmp/timed-read.sh" <<'SH'
timed_read() {
if [[ $1 == "-n" ]]; then
read -r -t 1 "$@"
elif [[ ! -e $CUT ]]; then
IFS= read -r -t 1 -N 1 -u 3 _ && : >"$CUT"
return 1
else
read -r -t 1 "$@"
fi
}
SH

# Asks for a password whose unlock exits with status $1, and prints the status
# ask-for-password gives the hook.
ask() {
local status=0 asking
rm -f "$test_tmp/cut"
CUT=$test_tmp/cut BASH_ENV=$test_tmp/timed-read.sh timeout 10 \
bash "$test_tmp/plymouth" ask-for-password --command="exit-with $1" --number-of-tries=1 >/dev/null &
asking=$!
for _ in {1..250}; do
[[ ! -s $run/request ]] || break
sleep 0.02
done
timeout 10 bash "$test_tmp/answer" "$(sed -n 's/^id=//p' "$run/request")" bash -c "exit $1" </dev/null || true
wait "$asking" || status=$?
echo "$status"
}

for status in 0 2; do
got=$(ask "$status")
(( got == status )) || fail "an answer cut short by the read's time limit still arrives" "unlock exited $status, ask-for-password $got"
done
pass "an answer cut short by the read's time limit still arrives"