From 5df7ed2f3229908267e75cc30630b6393339c73f Mon Sep 17 00:00:00 2001 From: DL6ER Date: Wed, 14 Apr 2021 18:33:10 +0200 Subject: [PATCH] Obtain FTL's PID from the PID file (#4103) * Try to obtain FTL's PID from the PID file. If this fails, try to identify the main process using pgrep --oldest (instead of relying on pkill finding the right one by itself). This allows the script to work in even when FTL is running inside the memory checker valgrind. * Rename FTL_PID -> FTL_PID_FILE * Remove the pgrep fallback after discussions about that it should be more obvious to users if something strange happened to their PID file. Also, simplify the routine using a bashism in the end. * Shorten if [[ regex ]] * Use unset instead of emptying the PID variable Signed-off-by: DL6ER Co-authored-by: Dan Schaper --- pihole | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/pihole b/pihole index 2b2b7bc2..f8085c8e 100755 --- a/pihole +++ b/pihole @@ -16,6 +16,7 @@ readonly PI_HOLE_SCRIPT_DIR="/opt/pihole" # error due to modifying a readonly variable. setupVars="/etc/pihole/setupVars.conf" PI_HOLE_BIN_DIR="/usr/local/bin" +readonly FTL_PID_FILE="/run/pihole-FTL.pid" readonly colfile="${PI_HOLE_SCRIPT_DIR}/COL_TABLE" source "${colfile}" @@ -98,8 +99,25 @@ versionFunc() { exit 0 } +# Get PID of main pihole-FTL process +getFTLPID() { + local pid + + if [ -s "${FTL_PID_FILE}" ]; then + # -s: FILE exists and has a size greater than zero + pid="$(<"$FTL_PID_FILE")" + # Exploit prevention: unset the variable if there is malicious content + # Verify that the value read from the file is numeric + [[ "$pid" =~ [^[:digit:]] ]] && unset pid + fi + + # If FTL is not running, or the PID file contains malicious stuff, substitute + # negative PID to signal this to the caller + echo "${pid:=-1}" +} + restartDNS() { - local svcOption svc str output status + local svcOption svc str output status pid icon svcOption="${1:-restart}" # Determine if we should reload or restart @@ -108,17 +126,34 @@ restartDNS() { # Note 1: This will NOT re-read any *.conf files # Note 2: We cannot use killall here as it does # not know about real-time signals - svc="pkill -RTMIN pihole-FTL" - str="Reloading DNS lists" + pid="$(getFTLPID)" + if [[ "$pid" -eq "-1" ]]; then + svc="true" + str="FTL is not running" + icon="${INFO}" + else + svc="kill -RTMIN ${pid}" + str="Reloading DNS lists" + icon="${TICK}" + fi elif [[ "${svcOption}" =~ "reload" ]]; then # Reloading of the DNS cache has been requested # Note: This will NOT re-read any *.conf files - svc="pkill -HUP pihole-FTL" - str="Flushing DNS cache" + pid="$(getFTLPID)" + if [[ "$pid" -eq "-1" ]]; then + svc="true" + str="FTL is not running" + icon="${INFO}" + else + svc="kill -HUP ${pid}" + str="Flushing DNS cache" + icon="${TICK}" + fi else # A full restart has been requested svc="service pihole-FTL restart" str="Restarting DNS server" + icon="${TICK}" fi # Print output to Terminal, but not to Web Admin @@ -128,7 +163,7 @@ restartDNS() { status="$?" if [[ "${status}" -eq 0 ]]; then - [[ -t 1 ]] && echo -e "${OVER} ${TICK} ${str}" + [[ -t 1 ]] && echo -e "${OVER} ${icon} ${str}" return 0 else [[ ! -t 1 ]] && local OVER=""