Skip to content

Commit

Permalink
Always terminate the container if pihole-FTL binary exits. Either nat…
Browse files Browse the repository at this point in the history
…urally or via an error. Don't attempt to restart it, allow the container's restart policy to do this.

Signed-off-by: Adam Warner <[email protected]>
  • Loading branch information
PromoFaux committed Oct 6, 2024
1 parent 89eb7f6 commit 210a117
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 25 deletions.
16 changes: 8 additions & 8 deletions src/bash_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 48 additions & 17 deletions src/start.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash -e
#!/bin/bash

if [ "${PH_VERBOSE:-0}" -gt 0 ]; then
set -x
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit 210a117

Please sign in to comment.