From 210a1172ff8d80c20af5bed9bd1536051016d82a Mon Sep 17 00:00:00 2001 From: Adam Warner Date: Sun, 6 Oct 2024 22:39:04 +0100 Subject: [PATCH] Always terminate the container if pihole-FTL binary exits. Either naturally or via an error. Don't attempt to restart it, allow the container's restart policy to do this. Signed-off-by: Adam Warner --- src/bash_functions.sh | 16 +++++------ src/start.sh | 65 ++++++++++++++++++++++++++++++++----------- 2 files changed, 56 insertions(+), 25 deletions(-) diff --git a/src/bash_functions.sh b/src/bash_functions.sh index b5f8671f..5a605cdf 100644 --- a/src/bash_functions.sh +++ b/src/bash_functions.sh @@ -202,25 +202,25 @@ setup_web_password() { start_ftl() { echo " [i] pihole-FTL pre-start checks" - echo "" # Remove possible leftovers from previous pihole-FTL processes rm -f /dev/shm/FTL-* 2>/dev/null rm -f /run/pihole/FTL.sock - # Is /var/run/pihole used anymore? Or is this just a hangover from old container version - # /var/log sorted by running pihole-FTL-prestart.sh - # mkdir -p /var/run/pihole /var/log/pihole - # touch /var/log/pihole/FTL.log /var/log/pihole/pihole.log - # chown -R pihole:pihole /var/run/pihole /var/log/pihole /etc/pihole - fix_capabilities sh /opt/pihole/pihole-FTL-prestart.sh echo " [i] Starting pihole-FTL ($FTL_CMD) as ${DNSMASQ_USER}" - capsh --user=$DNSMASQ_USER --keep=1 -- -c "/usr/bin/pihole-FTL $FTL_CMD >/dev/null" & 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 diff --git a/src/start.sh b/src/start.sh index 4f7b8f37..9b8c58f5 100644 --- a/src/start.sh +++ b/src/start.sh @@ -1,4 +1,4 @@ -#!/bin/bash -e +#!/bin/bash if [ "${PH_VERBOSE:-0}" -gt 0 ]; then set -x @@ -53,11 +53,13 @@ start() { #migrate Gravity Database if needed: migrate_gravity - # Start pihole-FTL - start_ftl + # Start pihole-FTL in the background + start_ftl & - # Give FTL a couple of seconds to start up - sleep 2 + # Wait until the log file exists before continuing + while [ ! -f /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 @@ -75,8 +77,8 @@ start() { # Start tailing the FTL log from the most recent "FTL Started" message # Get the line number startFrom=$(grep -n '########## FTL started' /var/log/pihole/FTL.log | tail -1 | cut -d: -f1) - # Start the tail from the line number - tail -f -n +${startFrom} /var/log/pihole/FTL.log & + # Start the tail from the line number and background it + tail --follow=name -n +${startFrom} /var/log/pihole/FTL.log & else 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 @@ -86,22 +88,51 @@ start() { } stop() { - # Ensure pihole-FTL shuts down cleanly on SIGTERM/SIGINT - ftl_pid=$(pgrep pihole-FTL) - killall --signal 15 pihole-FTL - # Wait for pihole-FTL to exit - while test -d /proc/"${ftl_pid}"; do - sleep 0.5 - done + # Only attempt to close pihole-FTL if it is running, it may already have crashed + if pgrep pihole-FTL >/dev/null; then + echo "" + echo " [i] Container stop requested..." + echo " [i] pihole-FTL is running - Attempting to shut it down cleanly" + echo "" + # Ensure pihole-FTL shuts down cleanly on SIGTERM/SIGINT + ftl_pid=$(pgrep pihole-FTL) + + killall --signal 15 pihole-FTL + + # Wait for pihole-FTL to exit + while test -d /proc/"${ftl_pid}"; do + sleep 0.5 + done + fi - # If we are running pytest, keep the container alive for a little longer - # to allow the tests to complete + # 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 + fi + + echo "" + echo " [i] pihole-FTL exited with status $FTL_EXIT_CODE" + echo "" + echo " [i] Container will now stop or restart depending on your restart policy" + echo " https://docs.docker.com/engine/containers/start-containers-automatically/#use-a-restart-policy" + echo "" + + # # If we are running pytest, keep the container alive for a little longer + # # to allow the tests to complete if [[ ${PYTEST} ]]; then sleep 10 fi - exit + exit ${FTL_EXIT_CODE} + } start