blob: df1beb7123c036734caf817c804253f0c390e428 [file] [log] [blame]
Patrick Williamsb48b7b42016-08-17 15:04:38 -05001#!/bin/sh
2#
3# /etc/init.d/quagga -- start/stop the Quagga routing daemons
4#
5# Based on debian version by Endre Hirling <endre@mail.elte.hu> and
6# Christian Hammers <ch@debian.org>.
7#
8
9### BEGIN INIT INFO
10# Provides: quagga
11# Required-Start: $local_fs $network $remote_fs $syslog
12# Required-Stop: $local_fs $network $remote_fs $syslog
13# Default-Start: 2 3 4 5
14# Default-Stop: 0 1 6
15# Short-Description: start and stop the Quagga routing suite
16# Description: Quagga is a routing suite for IP routing protocols like
17# BGP, OSPF, RIP and others. This script contols the main
18# daemon "quagga" as well as the individual protocol daemons.
19### END INIT INFO
20
21# NOTE: sbin must be before bin so we get the iproute2 ip and not the
22# busybox ip command. The busybox one flushes all routes instead of just
23# the dynamic routes
24PATH=/sbin:/usr/sbin:/bin:/usr/bin:/sbin
25D_PATH=/usr/sbin
26C_PATH=/etc/quagga
27
28# Keep zebra first and do not list watchquagga!
Brad Bishop6e60e8b2018-02-01 10:27:11 -050029DAEMONS="zebra bgpd ripd ripngd ospfd ospf6d isisd"
Patrick Williamsb48b7b42016-08-17 15:04:38 -050030
31# Print the name of the pidfile.
32pidfile()
33{
34 echo "/var/run/quagga/$1.pid"
35}
36
37# Check if daemon is started by using the pidfile.
38started()
39{
40 [ -e `pidfile $1` ] && kill -0 `cat \`pidfile $1\`` 2> /dev/null && return 0
41 return 1
42}
43
44# Loads the config via vtysh -b if configured to do so.
45vtysh_b ()
46{
47 # Rember, that all variables have been incremented by 1 in convert_daemon_prios()
48 if [ "$vtysh_enable" = 2 -a -f $C_PATH/Quagga.conf ]; then
49 /usr/bin/vtysh -b
50 fi
51}
52
53# Check if the daemon is activated and if its executable and config files
54# are in place.
55# params: daemon name
56# returns: 0=ok, 1=error
57check_daemon()
58{
59 # If the integrated config file is used the others are not checked.
60 if [ -r "$C_PATH/Quagga.conf" ]; then
61 return 0
62 fi
63
64 # check for config file
65 if [ ! -r "$C_PATH/$1.conf" ]; then
66 return 1
67 fi
68 return 0
69}
70
71# Starts the server if it's not alrady running according to the pid file.
72# The Quagga daemons creates the pidfile when starting.
73start()
74{
75 if ! check_daemon $1; then echo -n " (!$1)"; return; fi
76 echo -n " $1"
77 start-stop-daemon \
78 --start \
79 --pidfile=`pidfile $1` \
80 --exec "$D_PATH/$1" \
81 -- \
82 `eval echo "$""$1""_options"`
83
84}
85
86# Stop the daemon given in the parameter, printing its name to the terminal.
87stop()
88{
89 if ! started "$1" ; then
90 echo -n " (!$1)"
91 return 0
92 else
93 PIDFILE=`pidfile $1`
94 PID=`cat $PIDFILE 2>/dev/null`
95 start-stop-daemon --stop --quiet --exec "$D_PATH/$1"
96 #
97 # Now we have to wait until $DAEMON has _really_ stopped.
98 #
99 if test -n "$PID" && kill -0 $PID 2>/dev/null; then
100 echo -n " (waiting) ."
101 cnt=0
102 while kill -0 $PID 2>/dev/null; do
103 cnt=`expr $cnt + 1`
104 if [ $cnt -gt 60 ]; then
105 # Waited 120 secs now, fail.
106 echo -n "Failed.. "
107 break
108 fi
109 sleep 2
110 echo -n "."
111 done
112 fi
113 echo -n " $1"
114 rm -f `pidfile $1`
115 fi
116}
117
118stop_all()
119{
120 local daemon_list
121 daemon_list=${1:-$DAEMONS}
122
123 echo -n "Stopping Quagga daemons:"
124 for daemon_name in $daemon_list; do
125 stop "$daemon_name"
126 done
127 echo "."
128}
129
130start_all()
131{
132 local daemon_list
133 daemon_list=${1:-$DAEMONS}
134
135 echo -n "Starting Quagga daemons:"
136 for daemon_name in $daemon_list; do
137 start "$daemon_name"
138 done
139 echo "."
140}
141
142status_all()
143{
144 local daemon_list
145 daemon_list=${1:-$DAEMONS}
146 res=1
147
148 echo -n "quagga: "
149 for daemon_name in $daemon_list; do
150 if started "$daemon_name" ; then
151 id=`cat \`pidfile $daemon_name\``
152 echo -n "$daemon_name (pid $id) "
153 res=0
154 fi
155 done
156 if [ $res -eq 0 ]; then
157 echo "is running..."
158 else
159 echo "is stopped..."
160 fi
161 exit $res
162}
163
164#########################################################
165# Main program #
166#########################################################
167
168# Load configuration
169test -f /etc/default/quagga && . /etc/default/quagga
170
171case "$1" in
172 start)
173 cd $C_PATH/
174 start_all $2
175 vtysh_b
176 ;;
177
178 stop)
179 stop_all $2
180 echo "Removing all routes made by zebra."
181 ip route flush proto zebra
182 ;;
183
184 status)
185 status_all $2
186 ;;
187
188 restart|force-reload)
189 $0 stop $2
190 sleep 1
191 $0 start $2
192 ;;
193
194 *)
195 echo "Usage: /etc/init.d/quagga {start|stop|restart|status|force-reload} [daemon]"
196 exit 1
197 ;;
198esac
199
200exit 0