2019-11-05 21:11:47 +00:00
|
|
|
#!/usr/bin/env bash
|
2017-02-21 10:18:47 +00:00
|
|
|
### BEGIN INIT INFO
|
|
|
|
# Provides: pihole-FTL
|
|
|
|
# Required-Start: $remote_fs $syslog
|
|
|
|
# Required-Stop: $remote_fs $syslog
|
|
|
|
# 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
|
|
|
|
|
2017-02-21 10:50:08 +00:00
|
|
|
FTLUSER=pihole
|
2017-02-21 10:18:47 +00:00
|
|
|
PIDFILE=/var/run/pihole-FTL.pid
|
|
|
|
|
|
|
|
get_pid() {
|
2019-05-31 07:18:12 +00:00
|
|
|
# First, try to obtain PID from PIDFILE
|
|
|
|
if [ -s "${PIDFILE}" ]; then
|
|
|
|
cat "${PIDFILE}"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If the PIDFILE is empty or not available, obtain the PID using pidof
|
2019-05-30 13:26:27 +00:00
|
|
|
pidof "pihole-FTL" | awk '{print $(NF)}'
|
2017-02-21 10:18:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
is_running() {
|
2017-04-12 17:34:13 +00:00
|
|
|
ps "$(get_pid)" > /dev/null 2>&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)
|
2018-08-19 11:53:34 +00:00
|
|
|
touch /var/log/pihole-FTL.log /var/log/pihole.log
|
|
|
|
touch /run/pihole-FTL.pid /run/pihole-FTL.port
|
|
|
|
touch /etc/pihole/dhcp.leases
|
2018-01-01 10:26:03 +00:00
|
|
|
mkdir -p /var/run/pihole
|
|
|
|
mkdir -p /var/log/pihole
|
|
|
|
chown pihole:pihole /var/run/pihole /var/log/pihole
|
2019-02-08 13:56:44 +00:00
|
|
|
# Remove possible leftovers from previous pihole-FTL processes
|
|
|
|
rm -f /dev/shm/FTL-* 2> /dev/null
|
2018-02-24 12:23:06 +00:00
|
|
|
rm /var/run/pihole/FTL.sock 2> /dev/null
|
2018-08-19 17:55:47 +00:00
|
|
|
# Ensure that permissions are set so that pihole-FTL can edit all necessary files
|
2018-08-19 11:53:34 +00:00
|
|
|
chown pihole:pihole /run/pihole-FTL.pid /run/pihole-FTL.port
|
2018-11-19 16:38:19 +00:00
|
|
|
chown pihole:pihole /etc/pihole /etc/pihole/dhcp.leases 2> /dev/null
|
2018-08-19 11:53:34 +00:00
|
|
|
chown pihole:pihole /var/log/pihole-FTL.log /var/log/pihole.log
|
2017-12-08 22:25:01 +00:00
|
|
|
chmod 0644 /var/log/pihole-FTL.log /run/pihole-FTL.pid /run/pihole-FTL.port /var/log/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
|
|
|
|
chown pihole:pihole /etc/pihole/pihole-FTL.db /etc/pihole/gravity.db 2> /dev/null
|
2018-02-25 09:11:11 +00:00
|
|
|
echo "nameserver 127.0.0.1" | /sbin/resolvconf -a lo.piholeFTL
|
2018-08-19 12:32:19 +00:00
|
|
|
if setcap CAP_NET_BIND_SERVICE,CAP_NET_RAW,CAP_NET_ADMIN+eip "$(which pihole-FTL)"; then
|
|
|
|
su -s /bin/sh -c "/usr/bin/pihole-FTL" "$FTLUSER"
|
|
|
|
else
|
|
|
|
echo "Warning: Starting pihole-FTL as root because setting capabilities is not supported on this system"
|
|
|
|
pihole-FTL
|
|
|
|
fi
|
2017-02-21 10:18:47 +00:00
|
|
|
echo
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Stop the service
|
|
|
|
stop() {
|
|
|
|
if is_running; then
|
2018-02-25 09:11:11 +00:00
|
|
|
/sbin/resolvconf -d lo.piholeFTL
|
2017-04-12 21:13:18 +00:00
|
|
|
kill "$(get_pid)"
|
|
|
|
for i in {1..5}; do
|
2017-02-21 10:18:47 +00:00
|
|
|
if ! is_running; then
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo -n "."
|
|
|
|
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"
|
|
|
|
kill -9 "$(get_pid)"
|
2017-02-21 10:18:47 +00:00
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "Stopped"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "Not running"
|
|
|
|
fi
|
|
|
|
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 ###
|
|
|
|
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
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo $"Usage: $0 {start|stop|restart|reload|status}"
|
|
|
|
exit 1
|
|
|
|
esac
|
|
|
|
|
|
|
|
exit 0
|