Skip to content

Commit

Permalink
Clean up the code - at the end of the start() function, wait for the …
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
PromoFaux committed Oct 7, 2024
1 parent 210a117 commit 9e37aa8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 37 deletions.
28 changes: 0 additions & 28 deletions src/bash_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 29 additions & 9 deletions src/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 9e37aa8

Please sign in to comment.