Fix shebang validation and error handling

This commit is contained in:
Alexandre
2026-02-04 21:37:56 +01:00
committed by GitHub
parent f9ba24d617
commit bbbeecd174

View File

@@ -28,7 +28,7 @@ pick_exec_dir() {
if [ -d "$d" ] && [ -w "$d" ]; then if [ -d "$d" ] && [ -w "$d" ]; then
# Create a tiny test executable to confirm "exec" works # Create a tiny test executable to confirm "exec" works
local t="${d%/}/.exec_test_$$" local t="${d%/}/.exec_test_$$"
printf '#!/bin/sh\necho ok\n' > "$t" 2>/dev/null || { rm -f "$t" 2>/dev/null || true; continue; } printf '#!/bin/sh\necho ok\n' >"$t" 2>/dev/null || { rm -f "$t" 2>/dev/null || true; continue; }
chmod 700 "$t" 2>/dev/null || { rm -f "$t" 2>/dev/null || true; continue; } chmod 700 "$t" 2>/dev/null || { rm -f "$t" 2>/dev/null || true; continue; }
if "$t" >/dev/null 2>&1; then if "$t" >/dev/null 2>&1; then
rm -f "$t" 2>/dev/null || true rm -f "$t" 2>/dev/null || true
@@ -63,6 +63,8 @@ candidate_shebangs=(
"/bin/sh" "/bin/sh"
) )
SHEBANG_ERRORS=()
probe_script_content=' probe_script_content='
set -e set -e
@@ -87,32 +89,46 @@ bashio::addon.version
validate_shebang() { validate_shebang() {
local candidate="$1" local candidate="$1"
local tmp out rc local tmp out rc
local errfile msg
# shellcheck disable=SC2206 # shellcheck disable=SC2206
local cmd=( $candidate ) local cmd=( $candidate )
local exe="${cmd[0]}" local exe="${cmd[0]}"
if [ ! -x "$exe" ]; then if [ ! -x "$exe" ]; then
SHEBANG_ERRORS+=(" - FAIL (not executable): #!$candidate")
return 1 return 1
fi fi
tmp="${EXEC_DIR%/}/shebang_test.$$.$RANDOM" tmp="${EXEC_DIR%/}/shebang_test.$$.$RANDOM"
errfile="${EXEC_DIR%/}/shebang_probe_err.$$"
{ {
printf '#!%s\n' "$candidate" printf '#!%s\n' "$candidate"
printf '%s\n' "$probe_script_content" printf '%s\n' "$probe_script_content"
} > "$tmp" } >"$tmp"
chmod 700 "$tmp" 2>/dev/null || true chmod 700 "$tmp" 2>/dev/null || true
set +e set +e
out="$("$tmp" >/dev/null 2>&1)" out="$("$tmp" 2>"$errfile")"
rc=$? rc=$?
set -e set -e
rm -f "$tmp" 2>/dev/null || true rm -f "$tmp" 2>/dev/null || true
if [ "$rc" -eq 0 ] && [ -n "${out:-}" ] && [ "$out" != "null" ]; then if [ "$rc" -eq 0 ] && [ -n "${out:-}" ] && [ "$out" != "null" ]; then
rm -f "$errfile" 2>/dev/null || true
return 0 return 0
fi fi
msg=$' - FAIL: #!'"$candidate"$'\n'" rc=$rc, stdout='${out:-}'"$'\n'
if [ -s "$errfile" ]; then
msg+=$' stderr:\n'
msg+="$(sed -n '1,8p' "$errfile")"$'\n'
else
msg+=$' stderr: <empty>\n'
fi
SHEBANG_ERRORS+=("$msg")
rm -f "$errfile" 2>/dev/null || true
return 1 return 1
} }
@@ -128,11 +144,13 @@ if [ -z "$shebang" ]; then
echo "ERROR: No valid shebang found (unable to execute bashio::addon.version via candidates)." >&2 echo "ERROR: No valid shebang found (unable to execute bashio::addon.version via candidates)." >&2
echo "Tried:" >&2 echo "Tried:" >&2
printf ' - %s\n' "${candidate_shebangs[@]}" >&2 printf ' - %s\n' "${candidate_shebangs[@]}" >&2
if [ "${#SHEBANG_ERRORS[@]}" -gt 0 ]; then
echo "Probe failures:" >&2
printf '%s\n' "${SHEBANG_ERRORS[@]}" >&2
fi
exit 1 exit 1
fi fi
echo "Selected shebang: #!$shebang"
#################### ####################
# Starting scripts # # Starting scripts #
#################### ####################