This commit is contained in:
Alexandre
2025-07-29 22:57:02 +02:00
committed by GitHub
parent 6e8860220d
commit 434c19641f

View File

@@ -1,8 +1,5 @@
#!/command/with-contenv bashio #!/command/with-contenv bashio
# shellcheck shell=bash # shellcheck shell=bash
# shellcheck disable=SC1090
set -Eeuo pipefail
echo "Starting..." echo "Starting..."
@@ -10,74 +7,57 @@ echo "Starting..."
# Starting scripts # # Starting scripts #
#################### ####################
PID1=false
if [ "$$" -eq 1 ]; then
PID1=true
fi
run_script() { run_script() {
local runfile="$1" runfile="$1"
local script_kind="$2" script_kind="$2"
echo "$runfile: executing" echo "$runfile: executing"
# FIX: Correct current shebang parsing
local currentshebang
currentshebang="$(sed -n '1{s/^#![[:blank:]]*//p;q}' "$runfile")"
# IMPROVED: Fix shebang if interpreter missing
if [ ! -f "${currentshebang%% *}" ]; then
local shebanglist="/usr/bin/bashio /usr/bin/bash /usr/bin/sh /bin/bash /bin/sh"
if ! "$PID1"; then
shebanglist="/usr/bin/with-contenv bashio /command/with-contenv bashio $shebanglist"
fi
for shebang in $shebanglist; do
local command_path="${shebang%% *}"
if [ -x "$command_path" ] && "$command_path" echo "yes" > /dev/null 2>&1; then
echo "Valid shebang: $shebang"
sed -i "1s|.*|#!$shebang|" "$runfile"
break
fi
done
fi
# Check if run as root # Check if run as root
if [ "$(id -u)" -eq 0 ]; then if [ "$(id -u)" -eq 0 ]; then
chown "$(id -u)":"$(id -g)" "$runfile" chown "$(id -u)":"$(id -g)" "$runfile"
chmod a+x "$runfile" chmod a+x "$runfile"
else else
if [ -t 1 ]; then echo -e "\e[38;5;214m$(date) WARNING: Script executed with user $(id -u):$(id -g), things can break and chown won't work\e[0m"
echo -e "\e[38;5;214m$(date) WARNING: Script executed as UID $(id -u), chown/chmod may fail\e[0m" # Disable chown and chmod in scripts
else
echo "$(date) WARNING: Script executed as UID $(id -u), chown/chmod may fail"
fi
# Disable chown/chmod in script
sed -i -E 's/^([[:space:]]*)chown /\1true # chown /' "$runfile" sed -i -E 's/^([[:space:]]*)chown /\1true # chown /' "$runfile"
sed -i -E 's/^([[:space:]]*)chmod /\1true # chmod /' "$runfile" sed -i -E 's/^([[:space:]]*)chmod /\1true # chmod /' "$runfile"
fi fi
# Replace s6-setuidgid with su fallback if s6-setuidgid is missing # Replace s6-setuidgid with su-based equivalent
if ! command -v s6-setuidgid > /dev/null 2>&1; then if ! command -v s6-setuidgid >/dev/null 2>&1; then
sed -i -E 's|s6-setuidgid[[:space:]]+([a-zA-Z0-9._-]+)[[:space:]]+(.*)$|su -s /bin/bash \1 -c "\2"|g' "$runfile" sed -i -E 's|s6-setuidgid[[:space:]]+([a-zA-Z0-9._-]+)[[:space:]]+(.*)$|su -s /bin/bash \1 -c "\2"|g' "$runfile"
fi fi
# Execute script # 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"
break
fi
done
sed -i "s|$currentshebang|$shebang|g" "$SCRIPTS"
fi
# Use source to share env variables when requested
if [[ "$script_kind" == service ]]; then if [[ "$script_kind" == service ]]; then
"$runfile" & (exec "$runfile") & true
else else
if [ "${ha_entry_source:-null}" = true ]; then if [ "${ha_entry_source:-null}" = true ]; then
sed -Ei 's/(^|[[:space:]])exit ([0-9]+)/\1return \2 || exit \2/g' "$runfile" sed -Ei 's/(^|[[:space:]])exit ([0-9]+)/\1return \2 || exit \2/g' "$runfile"
sed -i "s/bashio::exit.nok/return 1/g" "$runfile" sed -i "s/bashio::exit.nok/return 1/g" "$runfile"
sed -i "s/bashio::exit.ok/return 0/g" "$runfile" sed -i "s/bashio::exit.ok/return 0/g" "$runfile"
# shellcheck disable=SC1090
source "$runfile" || echo -e "\033[0;31mError\033[0m : $runfile exiting $?" source "$runfile" || echo -e "\033[0;31mError\033[0m : $runfile exiting $?"
else else
"$runfile" || echo -e "\033[0;31mError\033[0m : $runfile exiting $?" "$runfile" || echo -e "\033[0;31mError\033[0m : $runfile exiting $?"
fi fi
fi fi
# Cleanup only temporary scripts # Cleanup
if [[ "$script_kind" != service && "$runfile" == /tmp/* ]]; then if [[ "$script_kind" != service ]]; then
rm -f "$runfile" rm "$runfile"
fi fi
} }
@@ -88,35 +68,34 @@ for SCRIPTS in /etc/cont-init.d/*; do
done done
# Start services.d # Start services.d
if [ -d /etc/services.d ]; then if [ "$$" -eq 1 ]; then
if "$PID1"; then for service_dir in /etc/services.d/*; do
for service_dir in /etc/services.d/*; do SCRIPTS="${service_dir}/run"
SCRIPTS="${service_dir}/run" [ -e "$SCRIPTS" ] || continue
[ -e "$SCRIPTS" ] || continue run_script "$SCRIPTS" service
run_script "$SCRIPTS" service done
done else
else echo "Not PID 1 — skipping service startup"
echo "Not PID 1 — skipping service startup"
fi
fi fi
###################### ######################
# Starting container # # Starting container #
###################### ######################
if "$PID1"; then # If PID 1, keep alive and manage sigterm
echo if [ "$$" -eq 1 ]; then
echo " "
echo -e "\033[0;32mEverything started!\033[0m" echo -e "\033[0;32mEverything started!\033[0m"
terminate() { terminate() {
echo "Termination signal received, forwarding to subprocesses..." echo "Termination signal received, forwarding to subprocesses..."
# Terminate all subprocesses
if command -v pgrep &> /dev/null; then if command -v pgrep &> /dev/null; then
for pid in $(pgrep -P "$$"); do for pid in $(pgrep -P $$); do
echo "Terminating child PID $pid" echo "Terminating child PID $pid"
kill -TERM "$pid" 2> /dev/null || echo "Failed to terminate PID $pid" kill -TERM "$pid" 2> /dev/null || echo "Failed to terminate PID $pid"
done done
else else
# Fallback to iterating through /proc if pgrep is not available
for pid in /proc/[0-9]*/; do for pid in /proc/[0-9]*/; do
pid=${pid#/proc/} pid=${pid#/proc/}
pid=${pid%/} pid=${pid%/}
@@ -126,19 +105,20 @@ if "$PID1"; then
fi fi
done done
fi fi
kill -TERM -$$ 2>/dev/null || true
sleep 5 sleep 5
kill -KILL -$$ 2> /dev/null || true kill -KILL -$$ 2>/dev/null || true
wait wait
echo "All subprocesses terminated. Exiting." echo "All subprocesses terminated. Exiting."
exit 0 exit 0
} }
trap terminate SIGTERM SIGINT trap terminate SIGTERM SIGINT
wait -n wait -n
else else
echo echo " "
echo -e "\033[0;32mStarting the upstream container\033[0m" echo -e "\033[0;32mStarting the upstream container\033[0m"
echo echo " "
# Launch lsio mods
if [ -f /docker-mods ]; then exec /docker-mods; fi if [ -f /docker-mods ]; then exec /docker-mods; fi
fi fi