summaryrefslogtreecommitdiff
path: root/recipes/quagga/files/quagga.init
blob: cfc02d8b810b248aebf6e7f78c9fbdbcaf78d57b (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/sh
#
# /etc/init.d/quagga -- start/stop the Quagga routing daemons
#
# Based on debian version by Endre Hirling <endre@mail.elte.hu> and 
# Christian Hammers <ch@debian.org>.
#

# NOTE: sbin must be before bin so we get the iproute2 ip and not the
# busybox ip command. The busybox one flushes all routes instead of just
# the dynamic routes
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/sbin
D_PATH=/usr/lib/quagga
C_PATH=/etc/quagga
DAEMONS="zebra bgpd ripd ripngd ospfd ospf6d isisd"	# keep zebra first!

# Print the name of the pidfile.
pidfile()
{
	echo "/var/run/quagga/$1.pid"
}

# Check if daemon is started by using the pidfile.
started()
{
	[ -e `pidfile $1` ] && kill -0 `cat \`pidfile $1\`` 2> /dev/null && return 0
	return 1
}

# Loads the config via vtysh -b if configured to do so.
vtysh_b ()
{
	# Rember, that all variables have been incremented by 1 in convert_daemon_prios()
	if [ "$vtysh_enable" = 2 -a -f $C_PATH/Quagga.conf ]; then
		/usr/bin/vtysh -b
	fi
}

# Check if the daemon is activated and if its executable and config files 
# are in place.
# params: 	daemon name
# returns:	0=ok, 1=error
check_daemon()
{
	# If the integrated config file is used the others are not checked.
	if [ -r "$C_PATH/Quagga.conf" ]; then
	  return 0
	fi 

	# check for config file		  
	if [ ! -r "$C_PATH/$1.conf" ]; then
	  return 1
	fi
	return 0
}

# Starts the server if it's not alrady running according to the pid file.
# The Quagga daemons creates the pidfile when starting.
start()
{
	if ! check_daemon $1; then echo -n " (!$1)"; return; fi
	echo -n " $1"
	start-stop-daemon \
		--start \
		--pidfile=`pidfile $1` \
		--exec "$D_PATH/$1" \
		-- \
		`eval echo "$""$1""_options"`
		
}

# Stop the daemon given in the parameter, printing its name to the terminal.
stop()
{
    if ! started "$1" ; then
	echo -n " (!$1)"
	return 0
    else
	PIDFILE=`pidfile $1`
	PID=`cat $PIDFILE 2>/dev/null`
	start-stop-daemon --stop --quiet --exec "$D_PATH/$1"
	#
	#       Now we have to wait until $DAEMON has _really_ stopped.
	#
	if test -n "$PID" && kill -0 $PID 2>/dev/null; then
	    echo -n " (waiting) ."
	    cnt=0
	    while kill -0 $PID 2>/dev/null; do
		cnt=`expr $cnt + 1`
		if [ $cnt -gt 60 ]; then
		    # Waited 120 secs now, fail.
		    echo -n "Failed.. "
		    break
		fi
		sleep 2
		echo -n "."
		done
	    fi
	echo -n " $1"
	rm -f `pidfile $1`
    fi
}

stop_all() 
{
	local daemon_list
	daemon_list=${1:-$DAEMONS}

	echo -n "Stopping Quagga daemons:"
	for daemon_name in $daemon_list; do
		stop "$daemon_name"
	done
	echo "."
}

start_all()
{
	local daemon_list
	daemon_list=${1:-$DAEMONS}

	echo -n "Starting Quagga daemons:"
	for daemon_name in $daemon_list; do
		start "$daemon_name"
	done
	echo "."
}

status_all() 
{
	local daemon_list
	daemon_list=${1:-$DAEMONS}
	res=1

	echo -n "quagga: "
	for daemon_name in $daemon_list; do
	    if started "$daemon_name" ; then
		id=`cat \`pidfile $daemon_name\``
		echo -n "$daemon_name (pid $id) "
		res=0
	    fi
	done
	if [ $res -eq 0 ]; then
	    echo "is running..."
	else
	    echo "is stopped..."
	fi
	exit $res
}

#########################################################
# 		Main program 				#
#########################################################

# Load configuration
test -f /etc/default/quagga && . /etc/default/quagga

case "$1" in
    start)
	cd $C_PATH/
	start_all $2
	vtysh_b
    	;;
	
    stop)
  	stop_all $2
	echo "Removing all routes made by zebra."
	ip route flush proto zebra
   	;;

    status)
  	status_all $2
   	;;

    restart|force-reload)
	$0 stop $2
	sleep 1
	$0 start $2
	;;

    *)
    	echo "Usage: /etc/init.d/quagga {start|stop|restart|status|force-reload} [daemon]"
	exit 1
	;;
esac

exit 0