From 9416c80b1419b82da423e850bfe3cda178f56f0d Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Wed, 30 Jul 2025 06:23:58 +0200 Subject: [PATCH] Test shebangs --- .templates/ha_entrypoint.sh | 45 ++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/.templates/ha_entrypoint.sh b/.templates/ha_entrypoint.sh index 64dcf9fa3..7e28f106a 100755 --- a/.templates/ha_entrypoint.sh +++ b/.templates/ha_entrypoint.sh @@ -27,18 +27,36 @@ run_script() { sed -i -E 's|s6-setuidgid[[:space:]]+([a-zA-Z0-9._-]+)[[:space:]]+(.*)$|su -s /bin/bash \1 -c "\2"|g' "$runfile" fi - # Get current shebang, if not available use another - currentshebang="$(sed -n '1{s/^#![[:blank:]]*//p;q}' "$SCRIPTS")" - if [ ! -f "${currentshebang%% *}" ]; then - for shebang in "/usr/bin/env bashio" "/usr/bin/with-contenv bashio" "/command/with-contenv bashio" "/usr/bin/bashio" "/usr/bin/bash" "/usr/bin/sh" "/bin/bash" "/bin/sh"; do - command_path="${shebang%% *}" - if [ -x "$command_path" ] && "$command_path" echo "yes" > /dev/null 2>&1; then - echo "Valid shebang: $shebang" + # Prepare candidate shebangs: contenv first if PID1 + candidate_shebangs=() + if $PID1; then + candidate_shebangs+=("/command/with-contenv bashio" "/usr/bin/with-contenv bashio") + fi + candidate_shebangs+=( + "/usr/bin/env bashio" + "/usr/bin/bashio" + "/usr/bin/bash" + "/usr/bin/sh" + "/bin/bash" + "/bin/sh" + ) + + for shebang in "${candidate_shebangs[@]}"; do + command_path="${shebang%% *}" + if [ -x "$command_path" ]; then + tmpfile="$(mktemp)" + echo "#!$shebang" > "$tmpfile" + echo "bashio::addon.version" >> "$tmpfile" + chmod +x "$tmpfile" + output="$("$tmpfile" 2>/dev/null)" + rm -f "$tmpfile" + if [[ -n "$output" ]]; then + echo "Valid shebang: $shebang (output: $output)" + sed -i "1s|^#![^\n]*|#!$shebang|" "$SCRIPTS" break fi - done - sed -i "s|$currentshebang|$shebang|g" "$SCRIPTS" - fi + fi + done # Use source to share env variables when requested if [[ "$script_kind" == service ]]; then @@ -62,11 +80,18 @@ run_script() { } # Loop through /etc/cont-init.d/* +[[ -d /etc/cont-init.d ]] && \ for SCRIPTS in /etc/cont-init.d/*; do [ -e "$SCRIPTS" ] || continue run_script "$SCRIPTS" script done +[[ -d /etc/s6-overlay/s6-rc.d ]] && \ +for SCRIPTS in /etc/s6-overlay/s6-rc.d/*/run; do + [ -e "$SCRIPTS" ] || continue + run_script "$SCRIPTS" script +done || true + # Start services.d if [ "$$" -eq 1 ]; then for service_dir in /etc/services.d/*; do