This commit is contained in:
Alexandre
2025-07-25 21:59:15 +02:00
committed by GitHub
parent 6ee20d3b6d
commit ff92c6cfdc

View File

@@ -7,22 +7,24 @@ echo "Starting..."
# Starting scripts # # Starting scripts #
#################### ####################
# Loop through /etc/cont-init.d/* run_script() {
for SCRIPTS in /etc/cont-init.d/*; do runfile="$1"
[ -e "$SCRIPTS" ] || continue script_kind="$2"
echo "$SCRIPTS: executing" echo "$runfile: executing"
# 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)" "$SCRIPTS" chown "$(id -u)":"$(id -g)" "$runfile"
chmod a+x "$SCRIPTS" chmod a+x "$runfile"
else else
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 with user $(id -u):$(id -g), things can break and chown won't work\e[0m"
# Disable chown and chmod in scripts # Disable chown and chmod in scripts
sed -i "s/^chown /true # chown /g" "$SCRIPTS" sed -i -E 's/^([[:space:]]*)chown /\1true # chown /' "$runfile"
sed -i "s/ chown / true # chown /g" "$SCRIPTS" sed -i -E 's/^([[:space:]]*)chmod /\1true # chmod /' "$runfile"
sed -i "s/^chmod /true # chmod /g" "$SCRIPTS" fi
sed -i "s/ chmod / true # chmod /g" "$SCRIPTS"
# Replace s6-setuidgid with su-based equivalent
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"
fi fi
# Get current shebang, if not available use another # Get current shebang, if not available use another
@@ -30,7 +32,7 @@ for SCRIPTS in /etc/cont-init.d/*; do
if [ ! -f "${currentshebang%% *}" ]; then if [ ! -f "${currentshebang%% *}" ]; then
for shebang in "/command/with-contenv bashio" "/usr/bin/with-contenv bashio" "/usr/bin/env bashio" "/usr/bin/bashio" "/usr/bin/bash" "/usr/bin/sh" "/bin/bash" "/bin/sh"; do for shebang in "/command/with-contenv bashio" "/usr/bin/with-contenv bashio" "/usr/bin/env bashio" "/usr/bin/bashio" "/usr/bin/bash" "/usr/bin/sh" "/bin/bash" "/bin/sh"; do
command_path="${shebang%% *}" command_path="${shebang%% *}"
if [ -x "$command_path" ] && "$command_path" echo "yes" >/dev/null 2>&1; then if [ -x "$command_path" ] && "$command_path" echo "yes" > /dev/null 2>&1; then
echo "Valid shebang: $shebang" echo "Valid shebang: $shebang"
break break
fi fi
@@ -39,64 +41,37 @@ for SCRIPTS in /etc/cont-init.d/*; do
fi fi
# Use source to share env variables when requested # Use source to share env variables when requested
if [ "${ha_entry_source:-null}" = true ] && command -v "source" &>/dev/null; then if [[ "$script_kind" == service ]]; then
sed -i "s/(.*\s|^)exit \([0-9]\+\)/ \1 return \2 || exit \2/g" "$SCRIPTS" (exec "$runfile") & true
sed -i "s/bashio::exit.nok/return 1/g" "$SCRIPTS"
sed -i "s/bashio::exit.ok/return 0/g" "$SCRIPTS"
# shellcheck disable=SC1090
source "$SCRIPTS" || echo -e "\033[0;31mError\033[0m : $SCRIPTS exiting $?"
else else
"$SCRIPTS" || echo -e "\033[0;31mError\033[0m : $SCRIPTS exiting $?" if [ "${ha_entry_source:-null}" = true ]; then
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.ok/return 0/g" "$runfile"
# shellcheck disable=SC1090
source "$runfile" || echo -e "\033[0;31mError\033[0m : $runfile exiting $?"
else
"$runfile" || echo -e "\033[0;31mError\033[0m : $runfile exiting $?"
fi
fi fi
# Cleanup # Cleanup
rm "$SCRIPTS" if [[ "$script_kind" != service ]]; then
rm "$runfile"
fi
}
# Loop through /etc/cont-init.d/*
for SCRIPTS in /etc/cont-init.d/*; do
[ -e "$SCRIPTS" ] || continue
run_script "$SCRIPTS" script
done done
# Loop through /etc/services.d/*/run # Start services.d
[[ -d /etc/services.d ]] && for SERVICE in /etc/services.d/*/run; do for service_dir in /etc/services.d/*; do
[ -e "$SERVICE" ] || continue SCRIPTS="${service_dir}/run"
echo "$SERVICE: executing" [ -e "$SCRIPTS" ] || continue
run_script "$SCRIPTS" service
# Check if run as root
if [ "$(id -u)" -eq 0 ]; then
chown "$(id -u)":"$(id -g)" "$SERVICE"
chmod a+x "$SERVICE"
else
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"
# Disable chown and chmod in scripts
sed -i "s/^chown /true # chown /g" "$SERVICE"
sed -i "s/ chown / true # chown /g" "$SERVICE"
sed -i "s/^chmod /true # chmod /g" "$SERVICE"
sed -i "s/ chmod / true # chmod /g" "$SERVICE"
fi
# Get current shebang, if not available use another
currentshebang="$(sed -n '1{s/^#![[:blank:]]*//p;q}' "$SERVICE")"
if [ ! -f "${currentshebang%% *}" ]; then
for shebang in "/command/with-contenv bashio" "/usr/bin/with-contenv bashio" "/usr/bin/env 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" "$SERVICE"
fi
# Use source to share env variables when requested
if [ "${ha_entry_source:-null}" = true ] && command -v "source" &>/dev/null; then
sed -i "s/(.*\s|^)exit \([0-9]\+\)/ \1 return \2 || exit \2/g" "$SERVICE"
sed -i "s/bashio::exit.nok/return 1/g" "$SERVICE"
sed -i "s/bashio::exit.ok/return 0/g" "$SERVICE"
# shellcheck disable=SC1090
source "$SERVICE" || echo -e "\033[0;31mError\033[0m : $SERVICE exiting $?"
else
"$SERVICE" || echo -e "\033[0;31mError\033[0m : $SERVICE exiting $?"
fi
# Cleanup
rm "$SERVICE"
done done
###################### ######################
@@ -110,32 +85,32 @@ if [ "$$" -eq 1 ]; then
terminate() { terminate() {
echo "Termination signal received, forwarding to subprocesses..." echo "Termination signal received, forwarding to subprocesses..."
# Terminate all 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 # 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%/}
if [[ "$pid" -ne 1 ]] && grep -q "^PPid:\s*$$" "/proc/$pid/status" 2>/dev/null; then if [[ "$pid" -ne 1 ]] && grep -q "^PPid:\s*$$" "/proc/$pid/status" 2> /dev/null; then
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"
fi fi
done done
fi fi
kill -TERM -$$ 2>/dev/null || true
sleep 5
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
while :; do wait -n
sleep infinity &
wait $!
done
else else
echo " " echo " "
echo -e "\033[0;32mStarting the upstream container\033[0m" echo -e "\033[0;32mStarting the upstream container\033[0m"