use procd for nodogsplash, thanks @lynxis
[feed/routing.git] / nodogsplash / files / nodogsplash.init
1 #!/bin/sh /etc/rc.common
2 #
3 # description: Startup/shutdown script for nodogsplash captive portal
4 #
5 # P. Kube 2007
6 #
7 # (Based on wifidog startup script
8 # Date : 2004-08-25
9 # Version : 1.0
10 # Comment by that author: Could be better, but it's working as expected)
11 #
12
13 START=95
14 STOP=95
15
16 USE_PROCD=1
17
18 IPT=/usr/sbin/iptables
19 WD_DIR=/usr/bin
20 NDS_CONF=/etc/nodogsplash/nodogsplash.conf
21 # -s -d 5 runs in background, with level 5 (not so verbose) messages to syslog
22 # -f -d 7 runs in foreground, with level 7 (verbose) debug messages to terminal
23 OPTIONS="-s -f -d 5"
24
25 start_service() {
26 if test_module ; then
27 procd_open_instance
28 procd_set_param command /usr/bin/nodogsplash $OPTIONS
29 procd_set_param respawn
30 procd_close_instance
31 else
32 logger -s -t nodogsplash -p daemon.error "nodogsplash is missing some kernel modules"
33 fi
34 }
35
36 stop_service() {
37 # nodogsplash doesn't exit fast enought, when procd terminates it.
38 # otherwise procd will restart nodogsplash twice. first time starting nodogsplash fails, second time it succeeds
39 sleep 1
40 }
41
42 status() {
43 $WD_DIR/ndsctl status
44 }
45
46 test_module() {
47
48 ### Test ipt_mark with iptables
49 test_ipt_mark () {
50 ($IPT -A FORWARD -m mark --mark 2 -j ACCEPT 2>&1) > /dev/null
51 IPTABLES_OK=$?
52 if [ "$IPTABLES_OK" -eq 0 ]; then
53 ($IPT -D FORWARD -m mark --mark 2 -j ACCEPT 2>&1) > /dev/null
54 return 0
55 else
56 return 1
57 fi
58 }
59 ### Test ipt_mac with iptables
60 test_ipt_mac () {
61 ($IPT -A INPUT -m mac --mac-source 00:00:00:00:00:00 -j ACCEPT 2>&1) > /dev/null
62 IPTABLES_OK=$?
63 if [ "$IPTABLES_OK" -eq 0 ]; then
64 ($IPT -D INPUT -m mac --mac-source 00:00:00:00:00:00 -j ACCEPT 2>&1) > /dev/null
65 return 0
66 else
67 return 1
68 fi
69 }
70
71 ### Test ipt_IMQ with iptables
72 test_ipt_IMQ () {
73 ($IPT -t mangle -A PREROUTING -j IMQ --todev 0 2>&1) > /dev/null
74 IPTABLES_OK=$?
75 if [ "$IPTABLES_OK" -eq 0 ]; then
76 ($IPT -t mangle -D PREROUTING -j IMQ --todev 0 2>&1) > /dev/null
77 return 0
78 else
79 return 1
80 fi
81 }
82
83 ### Test imq with ip
84 test_imq () {
85 (ip link set imq0 up 2>&1) > /dev/null
86 IMQ0_OK=$?
87 (ip link set imq1 up 2>&1) > /dev/null
88 IMQ1_OK=$?
89 if [ "$IMQ0_OK" -eq 0 -a "$IMQ1_OK" -eq 0 ]; then
90 (ip link set imq0 down 2>&1) > /dev/null
91 (ip link set imq1 down 2>&1) > /dev/null
92 return 0
93 else
94 return 1
95 fi
96 }
97
98 ### Test sch_htb with tc; requires imq0
99 test_sch_htb () {
100 (tc qdisc del dev imq0 root 2>&1) > /dev/null
101 (tc qdisc add dev imq0 root htb 2>&1) > /dev/null
102 TC_OK=$?
103 if [ "$TC_OK" -eq 0 ]; then
104 (tc qdisc del dev imq0 root 2>&1) > /dev/null
105 return 0
106 else
107 return 1
108 fi
109 }
110
111
112 ### Find a module on disk
113 module_exists () {
114 EXIST=$(find /lib/modules/`uname -r` -name $1.*o 2> /dev/null)
115 if [ -n "$EXIST" ]; then
116 return 0
117 else
118 return 1
119 fi
120 }
121
122 ### Test if a module is in memory
123 module_in_memory () {
124 MODULE=$(lsmod | grep $1 | awk '{print $1}')
125 if [ "$MODULE" = "$1" ]; then
126 return 0
127 else
128 return 1
129 fi
130 }
131
132 ### Test functionality of a module; load if necessary
133 do_module_tests () {
134 echo " Testing module $1 $2"
135 "test_$1"
136 if [ $? -ne 0 ]; then
137 echo " Module $1 $2 needed"
138 echo " Scanning disk for $1 module"
139 module_exists $1
140 if [ $? -ne 0 ]; then
141 echo " $1 module missing: please install it"
142 exit 1
143 else
144 echo " $1 exists, trying to load"
145 insmod $1 $2 > /dev/null
146 if [ $? -ne 0 ]; then
147 echo " Error: insmod $1 $2 failed"
148 exit 1
149 else
150 echo " $1 $2 loaded successfully"
151 fi
152 fi
153 else
154 echo " $1 is working"
155 fi
156
157 }
158
159 echo " Testing required modules"
160
161 do_module_tests "ipt_mac"
162 do_module_tests "ipt_mark"
163
164 # test for imq modules, only if TrafficControl is enabled in conf
165 if ( grep -q -E '^[[:space:]]*TrafficControl[[:space:]]+(yes|true|1)' "$NDS_CONF" ) ; then
166 do_module_tests "imq" "numdevs=2"
167 do_module_tests "ipt_IMQ"
168 do_module_tests "sch_htb"
169 fi
170 }