Update pihole-FTL.service
Make this script a bourne shell script, which requires the removal of only a single bashism, the "{n..m}" expansion. Furthermore, since POSIX echo has no reliable command line options, switch to printf when line breaks shall be omitted. On most distros/setups "sh" calls a much lighter bourne shell like dash, which inits and runs much faster than bash.
Remove unused PIDFILE variable, remove the single case of FTLUSER call and remove it as well. Using variables here might give the wrong impression that there is a change these can be varied. But both are hardcoded in many places throughout Pi-hole, so in this service script.
Consolidate and merge the commands to pre-create and set permissions for required files and directories. The /var/log/pihole directory is and was never used, the touch, chmod and chown call can be merged into one each to reduce overhead. Use "-f" option to to fail on missing database files instead of redirecting STDERR, which is otherwise helpful to debug other possible errors, like missing or corrupted commands, filesystem errors and such.
Do not use "which pihole-FTL" when setting capabilities when the hardcoded path /usr/bin/pihole-FTL is used for the actual daemon call. It makes sense to use the full path here, as the Pi-hole installer and updater installs it explicitly there, and so we prevent users from e.g. overriding it via /usr/local/bin/pihole-FTL too easily.
On pgrep and pkill calls, add the "-x" flag to assure that only "pihole-FTL" is matched and not "foo-pihole-FTL" or "pihole-FTL-bar".
Do not remove possible leftovers from previous pihole-FTL processes on start, but on stop instead. Since "start" includes a proceeding "stop" as well, on service start nothing changes, but on service stop, some resources are now freed.
Remove leading "$" from usage message. In bash this was omitted, as $'...' is a special syntax for escape sequence expansion, which is not applicable here. In dash it would be printed literally. To keep previous behaviour, it is hence removed.
Signed-off-by: MichaIng <micha@dietpi.com>
2021-07-23 18:43:13 +00:00
|
|
|
#!/usr/bin/env sh
|
2017-02-21 10:18:47 +00:00
|
|
|
### BEGIN INIT INFO
|
|
|
|
# Provides: pihole-FTL
|
2020-04-03 17:05:59 +00:00
|
|
|
# Required-Start: $remote_fs $syslog $network
|
|
|
|
# Required-Stop: $remote_fs $syslog $network
|
2017-02-21 10:18:47 +00:00
|
|
|
# Default-Start: 2 3 4 5
|
|
|
|
# Default-Stop: 0 1 6
|
|
|
|
# Short-Description: pihole-FTL daemon
|
|
|
|
# Description: Enable service provided by pihole-FTL daemon
|
|
|
|
### END INIT INFO
|
|
|
|
|
2022-05-11 23:03:44 +00:00
|
|
|
# Global variables
|
|
|
|
FTLCONFFILE="/etc/pihole/pihole-FTL.conf"
|
|
|
|
DEFAULT_PID_FILE="/run/pihole-FTL.pid"
|
|
|
|
DEFAULT_PORT_FILE="/run/pihole-FTL.port"
|
|
|
|
FTL_PID=''
|
|
|
|
|
|
|
|
# Get the file path of the pihole-FTL.pid file
|
|
|
|
getFTLPIDFile() {
|
|
|
|
if [ -s "${FTLCONFFILE}" ]; then
|
|
|
|
# if PIDFILE is not set in pihole-FTL.conf, use the default path
|
|
|
|
FTL_PID_FILE="$({ grep '^PIDFILE=' "${FTLCONFFILE}" || echo "${DEFAULT_PID_FILE}"; } | cut -d'=' -f2-)"
|
|
|
|
else
|
|
|
|
# if there is no pihole-FTL.conf, use the default path
|
|
|
|
FTL_PID_FILE="${DEFAULT_PID_FILE}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get the PID of the FTL process based on the content of the pihole-FTL.pid file
|
|
|
|
getFTLPID() {
|
|
|
|
if [ -s "${FTL_PID_FILE}" ]; then
|
|
|
|
# -s: FILE exists and has a size greater than zero
|
|
|
|
FTL_PID="$(cat "${FTL_PID_FILE}")"
|
|
|
|
# Exploit prevention: unset the variable if there is malicious content
|
|
|
|
# Verify that the value read from the file is numeric
|
|
|
|
expr "${FTL_PID}" : "[^[:digit:]]" > /dev/null && unset FTL_PID
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If FTL is not running, or the PID file contains malicious stuff, substitute
|
|
|
|
# negative PID to signal this
|
|
|
|
FTL_PID=${FTL_PID:=-1}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get the file path of the pihole-FTL.port file
|
|
|
|
getFTLPortFile() {
|
|
|
|
if [ -s "${FTLCONFFILE}" ]; then
|
|
|
|
# if PORTFILE is not set in pihole-FTL.conf, use the default path
|
|
|
|
FTL_PORT_FILE="$({ grep '^PORTFILE=' "${FTLCONFFILE}" || echo "${DEFAULT_PORT_FILE}"; } | cut -d'=' -f2-)"
|
|
|
|
else
|
|
|
|
# if there is no pihole-FTL.conf, use the default path
|
|
|
|
FTL_PORT_FILE="${DEFAULT_PORT_FILE}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-21 10:18:47 +00:00
|
|
|
is_running() {
|
2022-05-11 23:03:44 +00:00
|
|
|
if [ -d "/proc/${FTL_PID}" ]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
return 1
|
2017-02-21 10:18:47 +00:00
|
|
|
}
|
|
|
|
|
2018-02-25 09:11:11 +00:00
|
|
|
|
2017-02-21 10:18:47 +00:00
|
|
|
# Start the service
|
|
|
|
start() {
|
|
|
|
if is_running; then
|
|
|
|
echo "pihole-FTL is already running"
|
|
|
|
else
|
2018-08-19 17:55:47 +00:00
|
|
|
# Touch files to ensure they exist (create if non-existing, preserve if existing)
|
2022-05-29 14:15:18 +00:00
|
|
|
mkdir -pm 0755 /run/pihole /var/log/pihole
|
2022-05-11 23:03:44 +00:00
|
|
|
[ ! -f "${FTL_PID_FILE}" ] && install -m 644 -o pihole -g pihole /dev/null "${FTL_PID_FILE}"
|
|
|
|
[ ! -f "${FTL_PORT_FILE}" ] && install -m 644 -o pihole -g pihole /dev/null "${FTL_PORT_FILE}"
|
2020-09-05 13:15:03 +00:00
|
|
|
[ ! -f /var/log/pihole/pihole-FTL.log ] && install -m 644 -o pihole -g pihole /dev/null /var/log/pihole/pihole-FTL.log
|
2022-06-19 21:09:05 +00:00
|
|
|
[ ! -f /var/log/pihole/pihole.log ] && install -m 640 -o pihole -g pihole /dev/null /var/log/pihole/pihole.log
|
2022-04-01 21:17:57 +00:00
|
|
|
[ ! -f /etc/pihole/dhcp.leases ] && install -m 644 -o pihole -g pihole /dev/null /etc/pihole/dhcp.leases
|
2018-08-19 17:55:47 +00:00
|
|
|
# Ensure that permissions are set so that pihole-FTL can edit all necessary files
|
2022-05-15 20:10:40 +00:00
|
|
|
chown pihole:pihole /run/pihole /etc/pihole /var/log/pihole /var/log/pihole/pihole-FTL.log /var/log/pihole/pihole.log /etc/pihole/dhcp.leases
|
2021-10-08 21:54:23 +00:00
|
|
|
# Ensure that permissions are set so that pihole-FTL can edit the files. We ignore errors as the file may not (yet) exist
|
2022-06-19 21:09:05 +00:00
|
|
|
chmod -f 0644 /etc/pihole/macvendor.db /etc/pihole/dhcp.leases /var/log/pihole/pihole-FTL.log
|
|
|
|
chmod -f 0640 /var/log/pihole/pihole.log
|
2019-12-09 12:17:55 +00:00
|
|
|
# Chown database files to the user FTL runs as. We ignore errors as the files may not (yet) exist
|
2021-08-13 19:24:35 +00:00
|
|
|
chown -f pihole:pihole /etc/pihole/pihole-FTL.db /etc/pihole/gravity.db /etc/pihole/macvendor.db
|
2021-10-08 21:54:23 +00:00
|
|
|
# Chown database file permissions so that the pihole group (web interface) can edit the file. We ignore errors as the files may not (yet) exist
|
|
|
|
chmod -f 0664 /etc/pihole/pihole-FTL.db
|
2022-05-15 20:10:40 +00:00
|
|
|
|
|
|
|
# Backward compatibility for user-scripts that still expect log files in /var/log instead of /var/log/pihole/
|
|
|
|
# Should be removed with Pi-hole v6.0
|
2022-05-18 20:06:36 +00:00
|
|
|
if [ ! -f /var/log/pihole.log ]; then
|
|
|
|
ln -s /var/log/pihole/pihole.log /var/log/pihole.log
|
2022-05-18 20:19:19 +00:00
|
|
|
chown -h pihole:pihole /var/log/pihole.log
|
2022-05-18 20:06:36 +00:00
|
|
|
|
|
|
|
fi
|
|
|
|
if [ ! -f /var/log/pihole-FTL.log ]; then
|
|
|
|
ln -s /var/log/pihole/pihole-FTL.log /var/log/pihole-FTL.log
|
2022-05-18 20:19:19 +00:00
|
|
|
chown -h pihole:pihole /var/log/pihole-FTL.log
|
2022-05-18 20:06:36 +00:00
|
|
|
fi
|
2022-05-15 20:10:40 +00:00
|
|
|
|
2021-09-11 20:35:11 +00:00
|
|
|
if setcap CAP_NET_BIND_SERVICE,CAP_NET_RAW,CAP_NET_ADMIN,CAP_SYS_NICE,CAP_IPC_LOCK,CAP_CHOWN+eip "/usr/bin/pihole-FTL"; then
|
Update pihole-FTL.service
Make this script a bourne shell script, which requires the removal of only a single bashism, the "{n..m}" expansion. Furthermore, since POSIX echo has no reliable command line options, switch to printf when line breaks shall be omitted. On most distros/setups "sh" calls a much lighter bourne shell like dash, which inits and runs much faster than bash.
Remove unused PIDFILE variable, remove the single case of FTLUSER call and remove it as well. Using variables here might give the wrong impression that there is a change these can be varied. But both are hardcoded in many places throughout Pi-hole, so in this service script.
Consolidate and merge the commands to pre-create and set permissions for required files and directories. The /var/log/pihole directory is and was never used, the touch, chmod and chown call can be merged into one each to reduce overhead. Use "-f" option to to fail on missing database files instead of redirecting STDERR, which is otherwise helpful to debug other possible errors, like missing or corrupted commands, filesystem errors and such.
Do not use "which pihole-FTL" when setting capabilities when the hardcoded path /usr/bin/pihole-FTL is used for the actual daemon call. It makes sense to use the full path here, as the Pi-hole installer and updater installs it explicitly there, and so we prevent users from e.g. overriding it via /usr/local/bin/pihole-FTL too easily.
On pgrep and pkill calls, add the "-x" flag to assure that only "pihole-FTL" is matched and not "foo-pihole-FTL" or "pihole-FTL-bar".
Do not remove possible leftovers from previous pihole-FTL processes on start, but on stop instead. Since "start" includes a proceeding "stop" as well, on service start nothing changes, but on service stop, some resources are now freed.
Remove leading "$" from usage message. In bash this was omitted, as $'...' is a special syntax for escape sequence expansion, which is not applicable here. In dash it would be printed literally. To keep previous behaviour, it is hence removed.
Signed-off-by: MichaIng <micha@dietpi.com>
2021-07-23 18:43:13 +00:00
|
|
|
su -s /bin/sh -c "/usr/bin/pihole-FTL" pihole
|
2018-08-19 12:32:19 +00:00
|
|
|
else
|
|
|
|
echo "Warning: Starting pihole-FTL as root because setting capabilities is not supported on this system"
|
Update pihole-FTL.service
Make this script a bourne shell script, which requires the removal of only a single bashism, the "{n..m}" expansion. Furthermore, since POSIX echo has no reliable command line options, switch to printf when line breaks shall be omitted. On most distros/setups "sh" calls a much lighter bourne shell like dash, which inits and runs much faster than bash.
Remove unused PIDFILE variable, remove the single case of FTLUSER call and remove it as well. Using variables here might give the wrong impression that there is a change these can be varied. But both are hardcoded in many places throughout Pi-hole, so in this service script.
Consolidate and merge the commands to pre-create and set permissions for required files and directories. The /var/log/pihole directory is and was never used, the touch, chmod and chown call can be merged into one each to reduce overhead. Use "-f" option to to fail on missing database files instead of redirecting STDERR, which is otherwise helpful to debug other possible errors, like missing or corrupted commands, filesystem errors and such.
Do not use "which pihole-FTL" when setting capabilities when the hardcoded path /usr/bin/pihole-FTL is used for the actual daemon call. It makes sense to use the full path here, as the Pi-hole installer and updater installs it explicitly there, and so we prevent users from e.g. overriding it via /usr/local/bin/pihole-FTL too easily.
On pgrep and pkill calls, add the "-x" flag to assure that only "pihole-FTL" is matched and not "foo-pihole-FTL" or "pihole-FTL-bar".
Do not remove possible leftovers from previous pihole-FTL processes on start, but on stop instead. Since "start" includes a proceeding "stop" as well, on service start nothing changes, but on service stop, some resources are now freed.
Remove leading "$" from usage message. In bash this was omitted, as $'...' is a special syntax for escape sequence expansion, which is not applicable here. In dash it would be printed literally. To keep previous behaviour, it is hence removed.
Signed-off-by: MichaIng <micha@dietpi.com>
2021-07-23 18:43:13 +00:00
|
|
|
/usr/bin/pihole-FTL
|
2018-08-19 12:32:19 +00:00
|
|
|
fi
|
2017-02-21 10:18:47 +00:00
|
|
|
echo
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Stop the service
|
|
|
|
stop() {
|
|
|
|
if is_running; then
|
2022-05-11 23:03:44 +00:00
|
|
|
kill "${FTL_PID}"
|
Update pihole-FTL.service
Make this script a bourne shell script, which requires the removal of only a single bashism, the "{n..m}" expansion. Furthermore, since POSIX echo has no reliable command line options, switch to printf when line breaks shall be omitted. On most distros/setups "sh" calls a much lighter bourne shell like dash, which inits and runs much faster than bash.
Remove unused PIDFILE variable, remove the single case of FTLUSER call and remove it as well. Using variables here might give the wrong impression that there is a change these can be varied. But both are hardcoded in many places throughout Pi-hole, so in this service script.
Consolidate and merge the commands to pre-create and set permissions for required files and directories. The /var/log/pihole directory is and was never used, the touch, chmod and chown call can be merged into one each to reduce overhead. Use "-f" option to to fail on missing database files instead of redirecting STDERR, which is otherwise helpful to debug other possible errors, like missing or corrupted commands, filesystem errors and such.
Do not use "which pihole-FTL" when setting capabilities when the hardcoded path /usr/bin/pihole-FTL is used for the actual daemon call. It makes sense to use the full path here, as the Pi-hole installer and updater installs it explicitly there, and so we prevent users from e.g. overriding it via /usr/local/bin/pihole-FTL too easily.
On pgrep and pkill calls, add the "-x" flag to assure that only "pihole-FTL" is matched and not "foo-pihole-FTL" or "pihole-FTL-bar".
Do not remove possible leftovers from previous pihole-FTL processes on start, but on stop instead. Since "start" includes a proceeding "stop" as well, on service start nothing changes, but on service stop, some resources are now freed.
Remove leading "$" from usage message. In bash this was omitted, as $'...' is a special syntax for escape sequence expansion, which is not applicable here. In dash it would be printed literally. To keep previous behaviour, it is hence removed.
Signed-off-by: MichaIng <micha@dietpi.com>
2021-07-23 18:43:13 +00:00
|
|
|
for i in 1 2 3 4 5; do
|
2017-02-21 10:18:47 +00:00
|
|
|
if ! is_running; then
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
|
Update pihole-FTL.service
Make this script a bourne shell script, which requires the removal of only a single bashism, the "{n..m}" expansion. Furthermore, since POSIX echo has no reliable command line options, switch to printf when line breaks shall be omitted. On most distros/setups "sh" calls a much lighter bourne shell like dash, which inits and runs much faster than bash.
Remove unused PIDFILE variable, remove the single case of FTLUSER call and remove it as well. Using variables here might give the wrong impression that there is a change these can be varied. But both are hardcoded in many places throughout Pi-hole, so in this service script.
Consolidate and merge the commands to pre-create and set permissions for required files and directories. The /var/log/pihole directory is and was never used, the touch, chmod and chown call can be merged into one each to reduce overhead. Use "-f" option to to fail on missing database files instead of redirecting STDERR, which is otherwise helpful to debug other possible errors, like missing or corrupted commands, filesystem errors and such.
Do not use "which pihole-FTL" when setting capabilities when the hardcoded path /usr/bin/pihole-FTL is used for the actual daemon call. It makes sense to use the full path here, as the Pi-hole installer and updater installs it explicitly there, and so we prevent users from e.g. overriding it via /usr/local/bin/pihole-FTL too easily.
On pgrep and pkill calls, add the "-x" flag to assure that only "pihole-FTL" is matched and not "foo-pihole-FTL" or "pihole-FTL-bar".
Do not remove possible leftovers from previous pihole-FTL processes on start, but on stop instead. Since "start" includes a proceeding "stop" as well, on service start nothing changes, but on service stop, some resources are now freed.
Remove leading "$" from usage message. In bash this was omitted, as $'...' is a special syntax for escape sequence expansion, which is not applicable here. In dash it would be printed literally. To keep previous behaviour, it is hence removed.
Signed-off-by: MichaIng <micha@dietpi.com>
2021-07-23 18:43:13 +00:00
|
|
|
printf "."
|
2017-02-21 10:18:47 +00:00
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
echo
|
|
|
|
|
|
|
|
if is_running; then
|
2017-04-12 21:13:18 +00:00
|
|
|
echo "Not stopped; may still be shutting down or shutdown may have failed, killing now"
|
2022-05-11 23:03:44 +00:00
|
|
|
kill -9 "${FTL_PID}"
|
2017-02-21 10:18:47 +00:00
|
|
|
else
|
|
|
|
echo "Stopped"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "Not running"
|
|
|
|
fi
|
Update pihole-FTL.service
Make this script a bourne shell script, which requires the removal of only a single bashism, the "{n..m}" expansion. Furthermore, since POSIX echo has no reliable command line options, switch to printf when line breaks shall be omitted. On most distros/setups "sh" calls a much lighter bourne shell like dash, which inits and runs much faster than bash.
Remove unused PIDFILE variable, remove the single case of FTLUSER call and remove it as well. Using variables here might give the wrong impression that there is a change these can be varied. But both are hardcoded in many places throughout Pi-hole, so in this service script.
Consolidate and merge the commands to pre-create and set permissions for required files and directories. The /var/log/pihole directory is and was never used, the touch, chmod and chown call can be merged into one each to reduce overhead. Use "-f" option to to fail on missing database files instead of redirecting STDERR, which is otherwise helpful to debug other possible errors, like missing or corrupted commands, filesystem errors and such.
Do not use "which pihole-FTL" when setting capabilities when the hardcoded path /usr/bin/pihole-FTL is used for the actual daemon call. It makes sense to use the full path here, as the Pi-hole installer and updater installs it explicitly there, and so we prevent users from e.g. overriding it via /usr/local/bin/pihole-FTL too easily.
On pgrep and pkill calls, add the "-x" flag to assure that only "pihole-FTL" is matched and not "foo-pihole-FTL" or "pihole-FTL-bar".
Do not remove possible leftovers from previous pihole-FTL processes on start, but on stop instead. Since "start" includes a proceeding "stop" as well, on service start nothing changes, but on service stop, some resources are now freed.
Remove leading "$" from usage message. In bash this was omitted, as $'...' is a special syntax for escape sequence expansion, which is not applicable here. In dash it would be printed literally. To keep previous behaviour, it is hence removed.
Signed-off-by: MichaIng <micha@dietpi.com>
2021-07-23 18:43:13 +00:00
|
|
|
# Cleanup
|
2022-05-11 23:03:44 +00:00
|
|
|
rm -f /run/pihole/FTL.sock /dev/shm/FTL-* "${FTL_PID_FILE}" "${FTL_PORT_FILE}"
|
2017-02-21 10:18:47 +00:00
|
|
|
echo
|
|
|
|
}
|
|
|
|
|
2018-06-02 05:30:51 +00:00
|
|
|
# Indicate the service status
|
|
|
|
status() {
|
|
|
|
if is_running; then
|
|
|
|
echo "[ ok ] pihole-FTL is running"
|
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
echo "[ ] pihole-FTL is not running"
|
|
|
|
exit 1
|
|
|
|
fi
|
2018-08-19 11:53:34 +00:00
|
|
|
}
|
2018-06-02 05:30:51 +00:00
|
|
|
|
|
|
|
|
2017-02-21 10:18:47 +00:00
|
|
|
### main logic ###
|
2022-05-11 23:03:44 +00:00
|
|
|
|
|
|
|
# Get file paths
|
|
|
|
getFTLPIDFile
|
|
|
|
getFTLPortFile
|
|
|
|
|
|
|
|
# Get FTL's current PID
|
|
|
|
getFTLPID
|
|
|
|
|
2017-02-21 10:18:47 +00:00
|
|
|
case "$1" in
|
|
|
|
stop)
|
|
|
|
stop
|
|
|
|
;;
|
|
|
|
status)
|
2018-06-02 05:30:51 +00:00
|
|
|
status
|
2017-02-21 10:18:47 +00:00
|
|
|
;;
|
2017-04-12 21:13:18 +00:00
|
|
|
start|restart|reload|condrestart)
|
2017-02-21 10:18:47 +00:00
|
|
|
stop
|
|
|
|
start
|
|
|
|
;;
|
|
|
|
*)
|
Update pihole-FTL.service
Make this script a bourne shell script, which requires the removal of only a single bashism, the "{n..m}" expansion. Furthermore, since POSIX echo has no reliable command line options, switch to printf when line breaks shall be omitted. On most distros/setups "sh" calls a much lighter bourne shell like dash, which inits and runs much faster than bash.
Remove unused PIDFILE variable, remove the single case of FTLUSER call and remove it as well. Using variables here might give the wrong impression that there is a change these can be varied. But both are hardcoded in many places throughout Pi-hole, so in this service script.
Consolidate and merge the commands to pre-create and set permissions for required files and directories. The /var/log/pihole directory is and was never used, the touch, chmod and chown call can be merged into one each to reduce overhead. Use "-f" option to to fail on missing database files instead of redirecting STDERR, which is otherwise helpful to debug other possible errors, like missing or corrupted commands, filesystem errors and such.
Do not use "which pihole-FTL" when setting capabilities when the hardcoded path /usr/bin/pihole-FTL is used for the actual daemon call. It makes sense to use the full path here, as the Pi-hole installer and updater installs it explicitly there, and so we prevent users from e.g. overriding it via /usr/local/bin/pihole-FTL too easily.
On pgrep and pkill calls, add the "-x" flag to assure that only "pihole-FTL" is matched and not "foo-pihole-FTL" or "pihole-FTL-bar".
Do not remove possible leftovers from previous pihole-FTL processes on start, but on stop instead. Since "start" includes a proceeding "stop" as well, on service start nothing changes, but on service stop, some resources are now freed.
Remove leading "$" from usage message. In bash this was omitted, as $'...' is a special syntax for escape sequence expansion, which is not applicable here. In dash it would be printed literally. To keep previous behaviour, it is hence removed.
Signed-off-by: MichaIng <micha@dietpi.com>
2021-07-23 18:43:13 +00:00
|
|
|
echo "Usage: $0 {start|stop|restart|reload|status}"
|
2017-02-21 10:18:47 +00:00
|
|
|
exit 1
|
|
|
|
esac
|
|
|
|
|
|
|
|
exit 0
|