@ -16,6 +16,7 @@ readonly PI_HOLE_SCRIPT_DIR="/opt/pihole"
# error due to modifying a readonly variable.
# error due to modifying a readonly variable.
setupVars="/etc/pihole/setupVars.conf"
setupVars="/etc/pihole/setupVars.conf"
PI_HOLE_BIN_DIR="/usr/local/bin"
PI_HOLE_BIN_DIR="/usr/local/bin"
readonly FTL_PID_FILE="/run/pihole-FTL.pid"
readonly colfile="${PI_HOLE_SCRIPT_DIR}/COL_TABLE"
readonly colfile="${PI_HOLE_SCRIPT_DIR}/COL_TABLE"
source "${colfile}"
source "${colfile}"
@ -98,8 +99,25 @@ versionFunc() {
exit 0
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() {
restartDNS() {
local svcOption svc str output status
local svcOption svc str output status pid icon
svcOption="${1:-restart}"
svcOption="${1:-restart}"
# Determine if we should reload or restart
# Determine if we should reload or restart
@ -108,17 +126,34 @@ restartDNS() {
# Note 1: This will NOT re-read any *.conf files
# Note 1: This will NOT re-read any *.conf files
# Note 2: We cannot use killall here as it does
# Note 2: We cannot use killall here as it does
# not know about real-time signals
# not know about real-time signals
svc="pkill -RTMIN pihole-FTL"
pid="$(getFTLPID)"
str="Reloading DNS lists"
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
elif [[ "${svcOption}" =~ "reload" ]]; then
# Reloading of the DNS cache has been requested
# Reloading of the DNS cache has been requested
# Note: This will NOT re-read any *.conf files
# Note: This will NOT re-read any *.conf files
svc="pkill -HUP pihole-FTL"
pid="$(getFTLPID)"
str="Flushing DNS cache"
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
else
# A full restart has been requested
# A full restart has been requested
svc="service pihole-FTL restart"
svc="service pihole-FTL restart"
str="Restarting DNS server"
str="Restarting DNS server"
icon="${TICK}"
fi
fi
# Print output to Terminal, but not to Web Admin
# Print output to Terminal, but not to Web Admin
@ -128,7 +163,7 @@ restartDNS() {
status="$?"
status="$?"
if [[ "${status}" -eq 0 ]]; then
if [[ "${status}" -eq 0 ]]; then
[[ -t 1 ]] && echo -e "${OVER} ${TICK } ${str}"
[[ -t 1 ]] && echo -e "${OVER} ${icon } ${str}"
return 0
return 0
else
else
[[ ! -t 1 ]] && local OVER=""
[[ ! -t 1 ]] && local OVER=""