Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pihole-FTL.service « Templates « advanced - github.com/pi-hole/pi-hole.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9eb183ed37b4fe75af8c97436d50bad278a4da40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/bash
### 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

FTLUSER=pihole
PIDFILE=/var/run/pihole-FTL.pid

get_pid() {
    pidof "pihole-FTL"
}

is_running() {
    ps "$(get_pid)" > /dev/null 2>&1
}


# Start the service
start() {
  if is_running; then
    echo "pihole-FTL is already running"
  else
    # Touch files to ensure they exist (create if non-existing, preserve if existing)
    touch /var/log/pihole-FTL.log /var/log/pihole.log
    touch /run/pihole-FTL.pid /run/pihole-FTL.port
    touch /etc/pihole/dhcp.leases
    mkdir -p /var/run/pihole
    mkdir -p /var/log/pihole
    chown pihole:pihole /var/run/pihole /var/log/pihole
    # Remove possible leftovers from previous pihole-FTL processes
    rm -f /dev/shm/FTL-* 2> /dev/null
    rm /var/run/pihole/FTL.sock 2> /dev/null
    # Ensure that permissions are set so that pihole-FTL can edit all necessary files
    chown pihole:pihole /run/pihole-FTL.pid /run/pihole-FTL.port
    chown pihole:pihole /etc/pihole /etc/pihole/dhcp.leases 2> /dev/null
    chown pihole:pihole /var/log/pihole-FTL.log /var/log/pihole.log
    chmod 0644 /var/log/pihole-FTL.log /run/pihole-FTL.pid /run/pihole-FTL.port /var/log/pihole.log
    echo "nameserver 127.0.0.1" | /sbin/resolvconf -a lo.piholeFTL
    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
    echo
  fi
}

# Stop the service
stop() {
  if is_running; then
    /sbin/resolvconf -d lo.piholeFTL
    kill "$(get_pid)"
    for i in {1..5}; do
      if ! is_running; then
        break
      fi

      echo -n "."
      sleep 1
    done
    echo

    if is_running; then
      echo "Not stopped; may still be shutting down or shutdown may have failed, killing now"
      kill -9 "$(get_pid)"
      exit 1
    else
      echo "Stopped"
    fi
  else
    echo "Not running"
  fi
  echo
}

# 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
}


### main logic ###
case "$1" in
  stop)
        stop
        ;;
  status)
        status
        ;;
  start|restart|reload|condrestart)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac

exit 0