summarylogtreecommitdiffstats
path: root/00-pdnsd
blob: ed7ab1ce87b3381437f2ab78439caf3a65663334 (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
#!/bin/sh

INTERFACE=$1
STATUS=$2

# $INIT should be either "init" (System V) or "systemd".
INIT=`cat /proc/1/comm`

no_valid_init() {
  echo 'No valid init system set. Please select either "init" or "systemd".'
  exit 1
}

pdnsd_is_running() {
  # Check whether pDNSd is running.
  case "$INIT" in
    'systemd')
      systemctl is-active pdnsd.service -q
      return $?
    ;;
    'init')
      [ 'STARTED' = `rc.d list|grep pdnsd|cut -f1 -d']'|cut -d'[' -f2` ]
      return $?
    ;;
    *)
      no_valid_init
    ;;
  esac
}

pdnsd_init_command() {
  # Give pDNSd a command through the init system
  case "$INIT" in
    'systemd')
      exec systemctl $1 pdnsd.service
    ;;
    'init')
      exec rc.d $1 pdnsd
    ;;
    *)
      no_valid_init
    ;;
  esac
}

pdnsd_going_up() {
  if ! pdnsd_is_running; then
    # pDNSd isn't running. Start it.
    pdnsd_init_command start
  else
    # pDNSd is running. Restart it.
    pdnsd_init_command restart
  fi
}

pdnsd_going_down() {
  # Check for active interface, take offline if none is active
  if [ 'connected' != `nm-tool|grep State|cut -f2 -d' '` ]; then
    pdnsd_init_command stop
  else
    # If another interface is active, restart so we're sure we're using
    # the active connection.
    pdnsd_init_command restart
  fi
}

case "$STATUS" in
  up)
    pdnsd_going_up
  ;;
  vpn-up)
    pdnsd_going_up
  ;;
  down)
    pdnsd_going_down
  ;;
  vpn-down)
    pdnsd_going_down
  ;;
esac