Clean up the code - at the end of the start() function, wait for the start_ftl process and pass it's exit code to stop.

Add in additional while/wait to ensure that after the FTL log exists, it contains the "FTL Started" line

Signed-off-by: Adam Warner <me@adamwarner.co.uk>
This commit is contained in:
Adam Warner
2024-10-07 21:47:58 +01:00
parent 210a1172ff
commit 9e37aa8f59
2 changed files with 29 additions and 37 deletions

View File

@@ -199,34 +199,6 @@ setup_web_password() {
fi
}
start_ftl() {
echo " [i] pihole-FTL pre-start checks"
# Remove possible leftovers from previous pihole-FTL processes
rm -f /dev/shm/FTL-* 2>/dev/null
rm -f /run/pihole/FTL.sock
fix_capabilities
sh /opt/pihole/pihole-FTL-prestart.sh
echo " [i] Starting pihole-FTL ($FTL_CMD) as ${DNSMASQ_USER}"
echo ""
capsh --user=$DNSMASQ_USER --keep=1 -- -c "/usr/bin/pihole-FTL $FTL_CMD >/dev/null"
ftl_exit_code=$?
# Store the exit code so that we can exit the container with the same code from start.sh
echo $ftl_exit_code >/pihole-FTL.exit
# pihole-FTL has exited, so we should stop the rest of the container
killall --signal 15 start.sh
# Notes on above:
# - DNSMASQ_USER default of pihole is in Dockerfile & can be overwritten by runtime container env
# - /var/log/pihole/pihole*.log has FTL's output that no-daemon would normally print in FG too
# prevent duplicating it in docker logs by sending to dev null
}
fix_capabilities() {
# Testing on Docker 20.10.14 with no caps set shows the following caps available to the container:
# Current: cap_chown,cap_dac_override,cap_fowner,cap_fsetid,cap_kill,cap_setgid,cap_setuid,cap_setpcap,cap_net_bind_service,cap_net_raw,cap_sys_chroot,cap_mknod,cap_audit_write,cap_setfcap=ep

View File

@@ -53,14 +53,34 @@ start() {
#migrate Gravity Database if needed:
migrate_gravity
# Start pihole-FTL in the background
start_ftl &
echo " [i] pihole-FTL pre-start checks"
# Remove possible leftovers from previous pihole-FTL processes
rm -f /dev/shm/FTL-* 2>/dev/null
rm -f /run/pihole/FTL.sock
fix_capabilities
sh /opt/pihole/pihole-FTL-prestart.sh
echo " [i] Starting pihole-FTL ($FTL_CMD) as ${DNSMASQ_USER}"
echo ""
capsh --user=$DNSMASQ_USER --keep=1 -- -c "/usr/bin/pihole-FTL $FTL_CMD >/dev/null" &
# Notes on above:
# - DNSMASQ_USER default of pihole is in Dockerfile & can be overwritten by runtime container env
# - /var/log/pihole/pihole*.log has FTL's output that no-daemon would normally print in FG too
# prevent duplicating it in docker logs by sending to dev null
ftl_pid=$!
# Wait until the log file exists before continuing
while [ ! -f /var/log/pihole/FTL.log ]; do
sleep 0.5
done
# Wait until the FTL log contains the "FTL started" message before continuing
while ! grep -q '########## FTL started' /var/log/pihole/FTL.log; do
sleep 0.5
done
# If we are migrating from v5 to v6, we now need to run the basic configuration step that we deferred earlier
# This is because pihole-FTL needs to migrate the config files before we can perform the basic configuration checks
if [[ ${v5_volume} -eq 1 ]]; then
@@ -83,11 +103,13 @@ start() {
echo " [i] FTL log output is disabled. Remove the Environment variable TAIL_FTL_LOG, or set it to 1 to enable FTL log output."
fi
# https://stackoverflow.com/a/49511035
wait $!
# Wait for the ftl process to finish and handle its return
wait $ftl_pid
stop $?
}
stop() {
local FTL_EXIT_CODE=$1
# Only attempt to close pihole-FTL if it is running, it may already have crashed
if pgrep pihole-FTL >/dev/null; then
@@ -104,15 +126,13 @@ stop() {
while test -d /proc/"${ftl_pid}"; do
sleep 0.5
done
# Return from this function, it will be called again once FTL finishes
return
fi
# Wait for a few seconds to allow the FTL log tail to catch up before exiting the container
sleep 2
# read the FTL exit code from the file created in the `start_ftl` function
FTL_EXIT_CODE=$(cat /pihole-FTL.exit)
rm /pihole-FTL.exit
# ensure the exit code is an integer, if not set it to 1
if ! [[ "${FTL_EXIT_CODE}" =~ ^[0-9]+$ ]]; then
FTL_EXIT_CODE=1