mirror of
https://github.com/alexbelgium/hassio-addons.git
synced 2026-01-11 02:11:01 +01:00
Use pid1
This commit is contained in:
@@ -7,10 +7,17 @@ echo "Starting..."
|
|||||||
# Starting scripts #
|
# Starting scripts #
|
||||||
####################
|
####################
|
||||||
|
|
||||||
|
# Detect if this is PID1 (main container process) — do this once at the start
|
||||||
|
PID1=false
|
||||||
|
if [ "$$" -eq 1 ]; then
|
||||||
|
PID1=true
|
||||||
|
fi
|
||||||
|
|
||||||
run_script() {
|
run_script() {
|
||||||
runfile="$1"
|
runfile="$1"
|
||||||
script_kind="$2"
|
script_kind="$2"
|
||||||
echo "$runfile: 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)" "$runfile"
|
chown "$(id -u)":"$(id -g)" "$runfile"
|
||||||
@@ -27,36 +34,40 @@ run_script() {
|
|||||||
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
|
||||||
|
|
||||||
# Prepare candidate shebangs: contenv first if PID1
|
# Shebang check/replacement (only if missing or invalid)
|
||||||
candidate_shebangs=()
|
currentshebang="$(sed -n '1{s/^#![[:blank:]]*//p;q}' "$runfile")"
|
||||||
if $PID1; then
|
command_path="${currentshebang%% *}"
|
||||||
candidate_shebangs+=("/command/with-contenv bashio" "/usr/bin/with-contenv bashio")
|
if [ ! -x "$command_path" ]; then
|
||||||
fi
|
candidate_shebangs=()
|
||||||
candidate_shebangs+=(
|
if $PID1; then
|
||||||
"/usr/bin/env bashio"
|
candidate_shebangs+=("/command/with-contenv bashio" "/usr/bin/with-contenv 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
|
|
||||||
fi
|
fi
|
||||||
done
|
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|" "$runfile"
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
# Use source to share env variables when requested
|
# Use source to share env variables when requested
|
||||||
if [[ "$script_kind" == service ]]; then
|
if [[ "$script_kind" == service ]]; then
|
||||||
@@ -93,7 +104,7 @@ for SCRIPTS in /etc/s6-overlay/s6-rc.d/*/run; do
|
|||||||
done || true
|
done || true
|
||||||
|
|
||||||
# Start services.d
|
# Start services.d
|
||||||
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
|
||||||
@@ -108,7 +119,7 @@ fi
|
|||||||
######################
|
######################
|
||||||
|
|
||||||
# If PID 1, keep alive and manage sigterm
|
# If PID 1, keep alive and manage sigterm
|
||||||
if [ "$$" -eq 1 ]; then
|
if $PID1; then
|
||||||
echo " "
|
echo " "
|
||||||
echo -e "\033[0;32mEverything started!\033[0m"
|
echo -e "\033[0;32mEverything started!\033[0m"
|
||||||
terminate() {
|
terminate() {
|
||||||
|
|||||||
Reference in New Issue
Block a user