From 9e37aa8f59143635d4dff5f100b19cfb8de1ef30 Mon Sep 17 00:00:00 2001 From: Adam Warner Date: Mon, 7 Oct 2024 21:47:58 +0100 Subject: [PATCH] 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 --- src/bash_functions.sh | 28 ---------------------------- src/start.sh | 38 +++++++++++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 37 deletions(-) diff --git a/src/bash_functions.sh b/src/bash_functions.sh index 5a605cd..3a82717 100644 --- a/src/bash_functions.sh +++ b/src/bash_functions.sh @@ -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 diff --git a/src/start.sh b/src/start.sh index 9b8c58f..1ad3508 100644 --- a/src/start.sh +++ b/src/start.sh @@ -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,14 +126,12 @@ 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 + sleep 2 # ensure the exit code is an integer, if not set it to 1 if ! [[ "${FTL_EXIT_CODE}" =~ ^[0-9]+$ ]]; then