aboutsummaryrefslogtreecommitdiff
path: root/src/ifplugd/ifplugd
blob: 354238fe2047a4ed66fe979368e44c5324ae6d2c (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
#!/bin/bash
#
# ifplugd daemon script for Arch Linux

. /etc/rc.conf
. /etc/rc.d/functions

shopt -s extglob

# env vars
daemonname=ifplugd
cfg=/etc/ifplugd/ifplugd.conf
PID=$(pidof -o %PPID ifplugd)

# source configuration file
[[ -r $cfg ]] && . "$cfg"

# discover interfaces to monitor
net_ifs=($INTERFACES)

case $1 in
  start)
    stat_busy "Starting $daemonname: ${net_ifs[*]}"

    for nic in "${net_ifs[@]}"; do
      # only start if a PID doesn't already exist
      if [[ ! -f /var/run/ifplugd.$nic.pid ]]; then
        /usr/bin/ifplugd-daemon $nic

        # use presence of PID file to check for start success
        [[ -f /var/run/ifplugd.$nic.pid ]] || (( ++err ))
      fi
    done
    unset nic

    if (( err )); then
      stat_fail
      exit 1
    else
      add_daemon $daemonname
      stat_done
    fi
    ;;
  stop)
    stat_busy "Stopping $daemonname: ${net_ifs[*]}"

    for nic in /var/run/ifplugd.*.pid; do
      [[ -f $nic ]] || { (( ++err )); break; }
      nic=${nic%.pid}
      nic=${nic##*.}
      ifplugd -k -i "$nic" || (( ++err ))
    done

    if (( err )); then
      stat_fail
      exit 1
    else
      rm_daemon $daemonname
      stat_done
    fi
    ;;
  restart)
    $0 stop
    sleep 1
    $0 start
    ;;
  status)
    for nic in "${net_ifs[@]}"; do
      ifplugd -c -i "$nic"
    done
    unset nic
    ;;
  suspend)
    stat_busy "Suspending $daemonname: ${net_ifs[*]}"
    for nic in "${net_ifs[@]}"; do
      ifplugd -S -i $nic || (( ++err ))
    done
    unset nic

    if (( err )); then
      stat_fail
      exit 1
    else
      stat_done
    fi
    ;;
  resume)
    stat_busy "Resuming $daemonname ${net_ifs[*]}"

    for nic in "${net_ifs[@]}"; do
      ifplugd -R -i $nic || (( ++err ))
    done
    unset nic

    if (( err )); then
      stat_fail
      exit 1
    else
      stat_done
    fi
    ;;
  *)
    echo "usage: $0 {start|stop|restart|status|suspend|resume}"
esac
exit 0