#!/usr/bin/env bash # Offline test for warden_console_shell (qemu/rootfs/etc/warden-lib.sh), the # warden.shell debug shell: with cttyhack it runs `setsid cttyhack sh` as # before; without it (the panel's BusyBox, RIG_BUSYBOX=panel) it opens the # console's tty from sysfs and runs `setsid -c sh` on it; with no tty it says # so and runs a plain sh (flare-edge#2210). Stub commands record each call. # # bash qemu/tests/console-shell-test.sh (WARDEN_TEST_SH="busybox sh" for the rig's shell) set -uo pipefail HERE="$(cd "$(dirname "$0")" && pwd)" LIB="$HERE/../rootfs/etc/warden-lib.sh" SH="${WARDEN_TEST_SH:-sh}" WORK="$(mktemp -d "${TMPDIR:-/tmp}/console-shell.XXXXXX")" || exit 1 trap 'rm -rf "$WORK"' EXIT FAIL=0 pass() { printf '[PASS] %s\n' "$1"; } fail() { printf '[FAIL] %s\n' "$1"; FAIL=1; } # Stubs: setsid and sh record their argv and where stdin points. mkdir -p "$WORK/bin" "$WORK/bin-ctty" for c in setsid sh; do # The single-quoted text is the stub's own source. # shellcheck disable=SC2016 printf '#!/bin/sh\necho "%s $*" >> "$CALLS"\nreadlink /proc/self/fd/0 >> "$CALLS"\n' "$c" > "$WORK/bin/$c" chmod 0755 "$WORK/bin/$c" done cp "$WORK/bin/setsid" "$WORK/bin/sh" "$WORK/bin-ctty/" printf '#!/bin/sh\nexit 0\n' > "$WORK/bin-ctty/cttyhack" chmod 0755 "$WORK/bin-ctty/cttyhack" # run_case : the calls the helper made. # The helper runs with only the stub dir and the host's basic tools on PATH, # so a host cttyhack (none expected) cannot leak in. run_case() { local calls="$WORK/calls.$RANDOM" : > "$calls" # $SH may carry arguments ("busybox sh"): split on purpose. # shellcheck disable=SC2086 CALLS="$calls" WARDEN_CONSOLE_ACTIVE="$2" WARDEN_DEV_DIR=/dev \ $SH -c ". '$LIB'; PATH='$1':/usr/bin:/bin; warden_console_shell" < /dev/zero > "$WORK/out" 2>&1 cat "$calls" } printf 'tty1 null\n' > "$WORK/active" got="$(run_case "$WORK/bin-ctty" "$WORK/active")" if [ "$(head -n 1 <<<"$got")" = "setsid cttyhack sh" ]; then pass "with cttyhack: setsid cttyhack sh" else fail "with cttyhack the helper ran: $got" fi got="$(run_case "$WORK/bin" "$WORK/active")" if [ "$got" = "$(printf 'setsid -c sh\n/dev/null')" ]; then pass "without cttyhack: setsid -c sh on the last console tty (/dev/null stand-in)" else fail "without cttyhack the helper ran: $got" fi got="$(run_case "$WORK/bin" "$WORK/no-such-active")" if [ "$(head -n 1 <<<"$got")" = "sh " ] && grep -q 'no console tty found' "$WORK/out"; then pass "no console tty: says so and runs a plain sh" else fail "with no console tty the helper ran: $got ($(cat "$WORK/out"))" fi printf 'ttyNOPE\n' > "$WORK/active-bad" got="$(run_case "$WORK/bin" "$WORK/active-bad")" if [ "$(head -n 1 <<<"$got")" = "sh " ]; then pass "a console name with no device node: plain sh, no setsid -c" else fail "a missing tty node ran: $got" fi if [ "$FAIL" -ne 0 ]; then echo "console-shell: FAILED" exit 1 fi echo "console-shell: all checks passed"