]> git.proxmox.com Git - mirror_frr.git/blob - debian/quagga.init.d
Debian Packaging Files
[mirror_frr.git] / debian / quagga.init.d
1 #!/bin/bash
2 #
3 ### BEGIN INIT INFO
4 # Provides: quagga
5 # Required-Start: $local_fs $network $remote_fs $syslog
6 # Required-Stop: $local_fs $network $remote_fs $syslog
7 # Default-Start: 2 3 4 5
8 # Default-Stop: 0 1 6
9 # Short-Description: start and stop the Quagga routing suite
10 # Description: Quagga is a routing suite for IP routing protocols like
11 # BGP, OSPF, RIP and others. This script contols the main
12 # daemon "quagga" as well as the individual protocol daemons.
13 ### END INIT INFO
14 #
15
16 PATH=/bin:/usr/bin:/sbin:/usr/sbin
17 D_PATH=/usr/lib/quagga
18 C_PATH=/etc/quagga
19
20 # Local Daemon selection may be done by using /etc/quagga/daemons.
21 # See /usr/share/doc/quagga/README.Debian.gz for further information.
22 # Keep zebra first and do not list watchquagga!
23 DAEMONS="zebra bgpd ripd ripngd ospfd ospf6d isisd babeld"
24
25 # Print the name of the pidfile.
26 pidfile()
27 {
28 echo "/var/run/quagga/$1.pid"
29 }
30
31 # Check if daemon is started by using the pidfile.
32 started()
33 {
34 [ -e `pidfile $1` ] && kill -0 `cat \`pidfile $1\`` 2> /dev/null && return 0
35 return 1
36 }
37
38 # Loads the config via vtysh -b if configured to do so.
39 vtysh_b ()
40 {
41 # Rember, that all variables have been incremented by 1 in convert_daemon_prios()
42 if [ "$vtysh_enable" = 2 -a -f $C_PATH/Quagga.conf ]; then
43 /usr/bin/vtysh -b
44 fi
45 }
46
47 # Check if the daemon is activated and if its executable and config files
48 # are in place.
49 # params: daemon name
50 # returns: 0=ok, 1=error
51 check_daemon()
52 {
53 # If the integrated config file is used the others are not checked.
54 if [ -r "$C_PATH/Quagga.conf" ]; then
55 return 0
56 fi
57
58 # vtysh_enable has no config file nor binary so skip check.
59 # (Not sure why vtysh_enable is in this list but does not hurt)
60 if [ $1 != "watchquagga" -a $1 != "vtysh_enable" ]; then
61 # check for daemon binary
62 if [ ! -x "$D_PATH/$1" ]; then return 1; fi
63
64 # check for config file
65 if [ ! -r "$C_PATH/$1.conf" ]; then
66 echo -n " (not started without config file)"
67 return 1
68 fi
69 fi
70 return 0
71 }
72
73 # Starts the server if it's not alrady running according to the pid file.
74 # The Quagga daemons creates the pidfile when starting.
75 start()
76 {
77 echo -n " $1"
78 if ! check_daemon $1; then return; fi
79
80 if [ "$1" = "watchquagga" ]; then
81 start-stop-daemon \
82 --start \
83 --pidfile=`pidfile $1` \
84 --exec "$D_PATH/$1" \
85 -- \
86 "${watchquagga_options[@]}"
87 else
88 start-stop-daemon \
89 --start \
90 --pidfile=`pidfile $1` \
91 --exec "$D_PATH/$1" \
92 -- \
93 `eval echo "$""$1""_options"`
94 fi
95 }
96
97 # Stop the daemon given in the parameter, printing its name to the terminal.
98 stop()
99 {
100 if ! started "$1" ; then
101 echo -n " ($1)"
102 return 0
103 else
104 PIDFILE=`pidfile $1`
105 PID=`cat $PIDFILE 2>/dev/null`
106 start-stop-daemon --stop --quiet --oknodo --exec "$D_PATH/$1"
107 #
108 # Now we have to wait until $DAEMON has _really_ stopped.
109 #
110 if test -n "$PID" && kill -0 $PID 2>/dev/null; then
111 echo -n " (waiting) ."
112 cnt=0
113 while kill -0 $PID 2>/dev/null; do
114 cnt=`expr $cnt + 1`
115 if [ $cnt -gt 60 ]; then
116 # Waited 120 secs now, fail.
117 echo -n "Failed.. "
118 break
119 fi
120 sleep 2
121 echo -n "."
122 done
123 fi
124 echo -n " $1"
125 rm -f `pidfile $1`
126 fi
127 }
128
129 # Converts values from /etc/quagga/daemons to all-numeric values.
130 convert_daemon_prios()
131 {
132 for name in $DAEMONS zebra vtysh_enable watchquagga_enable; do
133 # First, assign the value set by the user to $value
134 eval value=\$$name
135
136 # Daemon not activated or entry missing?
137 if [ "$value" = "no" -o "$value" = "" ]; then value=0; fi
138
139 # These strings parsed for backwards compatibility.
140 if [ "$value" = "yes" -o "$value" = "true" ]; then value=1; fi
141
142 # Zebra is threatened special. It must be between 0=off and the first
143 # user assigned value "1" so we increase all other enabled daemons' values.
144 if [ "$name" != "zebra" -a "$value" -gt 0 ]; then value=`expr "$value" + 1`; fi
145
146 # If e.g. name is zebra then we set "zebra=yes".
147 eval $name=$value
148 done
149 }
150
151 # Starts watchquagga for all wanted daemons.
152 start_watchquagga()
153 {
154 local daemon_name
155 local daemon_prio
156 local found_one
157
158 # Start the monitor daemon only if desired.
159 if [ 0 -eq "$watchquagga_enable" ]; then
160 return
161 fi
162
163 # Check variable type
164 if ! declare -p watchquagga_options | grep -q '^declare \-a'; then
165 echo
166 echo "ERROR: The variable watchquagga_options from /etc/quagga/debian.cnf must be a BASH array!"
167 echo "ERROR: Please convert config file and restart!"
168 exit 1
169 fi
170
171 # Which daemons have been started?
172 found_one=0
173 for daemon_name in $DAEMONS; do
174 eval daemon_prio=\$$daemon_name
175 if [ "$daemon_prio" -gt 0 ]; then
176 watchquagga_options+=($daemon_name)
177 found_one=1
178 fi
179 done
180
181 # Start if at least one daemon is activated.
182 if [ $found_one -eq 1 ]; then
183 echo -n "Starting Quagga monitor daemon:"
184 start watchquagga
185 echo "."
186 fi
187 }
188
189 # Stopps watchquagga.
190 stop_watchquagga()
191 {
192 echo -n "Stopping Quagga monitor daemon:"
193 stop watchquagga
194 echo "."
195 }
196
197 # Stops all daemons that have a lower level of priority than the given.
198 # (technically if daemon_prio >= wanted_prio)
199 stop_prio()
200 {
201 local wanted_prio
202 local daemon_prio
203 local daemon_list
204
205 wanted_prio=$1
206 daemon_list=${2:-$DAEMONS}
207
208 echo -n "Stopping Quagga daemons (prio:$wanted_prio):"
209
210 for prio_i in `seq 10 -1 $wanted_prio`; do
211 for daemon_name in $daemon_list; do
212 eval daemon_prio=\$$daemon_name
213 if [ $daemon_prio -eq $prio_i ]; then
214 stop "$daemon_name"
215 fi
216 done
217 done
218 echo "."
219 }
220
221 # Starts all daemons that have a higher level of priority than the given.
222 # (technically if daemon_prio <= wanted_prio)
223 start_prio()
224 {
225 local wanted_prio
226 local daemon_prio
227 local daemon_list
228
229 wanted_prio=$1
230 daemon_list=${2:-$DAEMONS}
231
232 echo -n "Starting Quagga daemons (prio:$wanted_prio):"
233
234 for prio_i in `seq 1 $wanted_prio`; do
235 for daemon_name in $daemon_list; do
236 eval daemon_prio=\$$daemon_name
237 if [ $daemon_prio -eq $prio_i ]; then
238 start "$daemon_name"
239 fi
240 done
241 done
242 echo "."
243 }
244
245 #########################################################
246 # Main program #
247 #########################################################
248
249 # Config broken but script must exit silently.
250 [ ! -r "$C_PATH/daemons" ] && exit 0
251
252 # Load configuration
253 . "$C_PATH/daemons"
254 . "$C_PATH/debian.conf"
255
256 # Set priority of un-startable daemons to 'no' and substitute 'yes' to '0'
257 convert_daemon_prios
258
259 if [ ! -d /var/run/quagga ]; then
260 mkdir -p /var/run/quagga
261 chown quagga:quagga /var/run/quagga
262 chmod 755 /var/run/quagga
263 fi
264
265 case "$1" in
266 start)
267 # Try to load this necessary (at least for 2.6) module.
268 if [ -d /lib/modules/`uname -r` ] ; then
269 echo "Loading capability module if not yet done."
270 set +e; LC_ALL=C modprobe -a capability 2>&1 | egrep -v "(not found|Can't locate)"; set -e
271 fi
272
273 # Start all daemons
274 cd $C_PATH/
275 if [ "$2" != "watchquagga" ]; then
276 start_prio 10 $2
277 fi
278 vtysh_b
279 start_watchquagga
280 ;;
281
282 1|2|3|4|5|6|7|8|9|10)
283 # Stop/start daemons for the appropriate priority level
284 stop_prio $1
285 start_prio $1
286 vtysh_b
287 ;;
288
289 stop|0)
290 # Stop all daemons at level '0' or 'stop'
291 stop_watchquagga
292 if [ "$2" != "watchquagga" ]; then
293 stop_prio 0 $2
294 fi
295
296 if [ -z "$2" -o "$2" = "zebra" ]; then
297 echo "Removing all routes made by zebra."
298 ip route flush proto zebra
299 fi
300 ;;
301
302 restart|force-reload)
303 $0 stop $2
304 sleep 1
305 $0 start $2
306 ;;
307
308 *)
309 echo "Usage: /etc/init.d/quagga {start|stop|restart|force-reload|<priority>} [daemon]"
310 echo " E.g. '/etc/init.d/quagga 5' would start all daemons with a prio 1-5."
311 echo " Read /usr/share/doc/quagga/README.Debian for details."
312 exit 1
313 ;;
314 esac
315
316 exit 0