]> git.proxmox.com Git - mirror_frr.git/blame - tools/frr.in
tools: Set correct directory of vtysh for frr-reload.py
[mirror_frr.git] / tools / frr.in
CommitLineData
df44cf00 1#!/bin/bash
651415bd
DS
2#
3### BEGIN INIT INFO
d8e4c438 4# Provides: frr
651415bd
DS
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
d8e4c438
DS
9# Short-Description: start and stop the Frr routing suite
10# Description: Frr is a routing suite for IP routing protocols like
651415bd 11# BGP, OSPF, RIP and others. This script contols the main
d8e4c438 12# daemon "frr" as well as the individual protocol daemons.
651415bd
DS
13### END INIT INFO
14#
df44cf00 15
651415bd 16PATH=/bin:/usr/bin:/sbin:/usr/sbin
40da52d8
DL
17D_PATH="@CFG_SBIN@" # /usr/lib/frr
18C_PATH="@CFG_SYSCONF@" # /etc/frr
19V_PATH="@CFG_STATE@" # /var/run/frr
c9bdc0c7 20B_PATH="@CFG_BIN@"
40da52d8
DL
21VTYSH="@vtysh_bin@" # /usr/bin/vtysh
22FRR_USER="@enable_user@" # frr
23FRR_GROUP="@enable_group@" # frr
24FRR_VTY_GROUP="@enable_vty_group@" # frrvty
5c906377 25FRR_CONFIG_MODE="@enable_configfile_mask@" # 0600
68495906 26FRR_DEFAULT_PROFILE="@DFLT_NAME@" # traditional / datacenter
651415bd 27
d8e4c438
DS
28# Local Daemon selection may be done by using /etc/frr/daemons.
29# See /usr/share/doc/frr/README.Debian.gz for further information.
9473e340 30# Keep zebra first and do not list watchfrr!
1c84efe4 31DAEMONS="mgmtd zebra bgpd ripd ripngd ospfd ospf6d isisd babeld pimd pim6d ldpd nhrpd eigrpd sharpd pbrd staticd bfdd fabricd vrrpd pathd"
651415bd 32MAX_INSTANCES=5
40da52d8 33RELOAD_SCRIPT="$D_PATH/frr-reload.py"
651415bd 34
915c81b3 35if [ -e /lib/lsb/init-functions ]; then
25e87a8f 36 . /lib/lsb/init-functions
915c81b3 37fi
651415bd 38
40da52d8 39if [ -f $D_PATH/ssd ]; then
25e87a8f 40 SSD=$D_PATH/ssd
d84fd25b 41else
25e87a8f 42 SSD=`which start-stop-daemon`
d84fd25b
DS
43fi
44
651415bd
DS
45# Print the name of the pidfile.
46pidfile()
47{
25e87a8f 48 echo "$V_PATH/$1.pid"
651415bd
DS
49}
50
51# Print the name of the vtysh.
52vtyfile()
53{
25e87a8f 54 echo "$V_PATH/$1.vty"
651415bd
DS
55}
56
57# Check if daemon is started by using the pidfile.
58started()
59{
25e87a8f
QY
60 [ ! -e `pidfile $1` ] && return 3
61 if [ -n "$2" ] && [ "$2" == "log" ]; then
62 status_of_proc -p `pidfile $1` $1 $1 && return 0 || return $?
63 else
64 kill -0 `cat \`pidfile $1\`` 2> /dev/null || return 1
65 return 0
66 fi
651415bd
DS
67}
68
69# Loads the config via vtysh -b if configured to do so.
70vtysh_b ()
71{
25e87a8f
QY
72 # Rember, that all variables have been incremented by 1 in convert_daemon_prios()
73 if [ "$vtysh_enable" = 2 -a -f $C_PATH/frr.conf ]; then
e220f954 74 $VTYSH -b
25e87a8f 75 fi
651415bd
DS
76}
77
78# Check if the daemon is activated and if its executable and config files
79# are in place.
80# params: daemon name
81# returns: 0=ok, 1=error
82check_daemon()
83{
25e87a8f
QY
84 if [ $1 != "watchfrr" -a $1 != "vtysh_enable" ]; then
85 # check for daemon binary
86 if [ ! -x "$D_PATH/$1" ]; then return 1; fi
87 fi
88
89 # If the integrated config file is used the others are not checked.
90 if [ -r "$C_PATH/frr.conf" ]; then
91 return 0
92 fi
93
94 # vtysh_enable has no config file nor binary so skip check.
95 # (Not sure why vtysh_enable is in this list but does not hurt)
96 if [ $1 != "watchfrr" -a $1 != "vtysh_enable" ]; then
97 # check for config file
98 if [ -n "$2" ]; then
99 if [ ! -r "$C_PATH/$1-$2.conf" ]; then
972cdc56 100 install -g "$FRR_GROUP" -o "$FRR_USER" -m "$FRR_CONFIG_MODE" /dev/null "$C_PATH/$1-$2.conf"
25e87a8f
QY
101 fi
102 elif [ ! -r "$C_PATH/$1.conf" ]; then
972cdc56 103 install -g "$FRR_GROUP" -o "$FRR_USER" -m "$FRR_CONFIG_MODE" /dev/null "$C_PATH/$1.conf"
25e87a8f
QY
104 fi
105 fi
106 return 0
651415bd
DS
107}
108
109# Starts the server if it's not alrady running according to the pid file.
d8e4c438 110# The Frr daemons creates the pidfile when starting.
651415bd 111start()
df44cf00 112{
10ea85ee
DL
113 local dmn inst
114 dmn="$1"
115 inst="$2"
116
117 ulimit -n $MAX_FDS > /dev/null 2> /dev/null
118 if [ "$dmn" = "watchfrr" ]; then
25e87a8f
QY
119
120 # We may need to restart watchfrr if new daemons are added and/or
121 # removed
10ea85ee 122 if started "$dmn" ; then
25e87a8f
QY
123 stop watchfrr
124 else
125 # Echo only once. watchfrr is printed in the stop above
10ea85ee 126 echo -n " $dmn"
25e87a8f
QY
127 fi
128
10ea85ee 129 eval "set - $watchfrr_options"
25e87a8f
QY
130 ${SSD} \
131 --start \
10ea85ee
DL
132 --pidfile=`pidfile $dmn` \
133 --exec "$D_PATH/$dmn" \
25e87a8f 134 -- \
10ea85ee 135 "$@"
25e87a8f 136
10ea85ee
DL
137 elif [ -n "$inst" ]; then
138 echo -n " $dmn-$inst"
139 if ! check_daemon $dmn $inst ; then
25e87a8f
QY
140 echo -n " (binary does not exist)"
141 return;
142 fi
143
144 ${SSD} \
145 --start \
10ea85ee
DL
146 --pidfile=`pidfile $dmn-$inst` \
147 --exec "$D_PATH/$dmn" \
25e87a8f 148 -- \
68495906 149 `eval echo "$""$dmn""_options"` $frr_global_options -n "$inst"
25e87a8f 150 else
10ea85ee 151 if ! check_daemon $dmn; then
25e87a8f
QY
152 echo -n " (binary does not exist)"
153 return;
154 fi
155
156 if [ "$valgrind_enable" = "yes" ]; then
157 ${SSD} \
158 --start \
10ea85ee 159 --pidfile=`pidfile $dmn` \
25e87a8f 160 --exec "$valgrind" \
10ea85ee 161 -- --trace-children=no --leak-check=full --log-file=/var/log/frr/$dmn-valgrind.log $D_PATH/$dmn \
68495906 162 `eval echo "$""$dmn""_options"` $frr_global_options
25e87a8f
QY
163 else
164 ${SSD} \
165 --start \
10ea85ee
DL
166 --pidfile=`pidfile $dmn` \
167 --exec "$D_PATH/$dmn" \
25e87a8f 168 -- \
68495906 169 `eval echo "$""$dmn""_options"` $frr_global_options
25e87a8f
QY
170 fi
171 fi
7ac1de8e
DS
172
173 # Start the staticd automatically
10ea85ee 174 if [ "$dmn" = "zebra" ]; then
25e87a8f
QY
175 echo -n "starting staticd since zebra is running"
176 if ! check_daemon staticd ; then
177 echo -n " (binary does not exist)"
178 return;
179 fi
180
181 ${SSD} \
182 --start \
183 --pidfile=`pidfile staticd` \
184 --exec "$D_PATH/staticd" \
185 -- \
68495906 186 `eval echo "$"staticd"_options"` $frr_global_options
7ac1de8e 187 fi
651415bd
DS
188}
189
190# Stop the daemon given in the parameter, printing its name to the terminal.
191stop()
192{
25e87a8f 193 local inst
7ac1de8e 194
25e87a8f
QY
195 if [ -n "$2" ]; then
196 inst="$1-$2"
197 else
198 inst="$1"
199 fi
200
201 if ! started "$inst" ; then
202 echo -n " ($inst)"
203 return 0
204 else
205 PIDFILE=`pidfile $inst`
206 PID=`cat $PIDFILE 2>/dev/null`
207 kill -2 $PID 2>/dev/null
208 #
209 # Now we have to wait until $DAEMON has _really_ stopped.
210 #
211 if test -n "$PID" && kill -0 $PID 2>/dev/null; then
25e87a8f
QY
212 cnt=0
213 while kill -0 $PID 2>/dev/null; do
214 cnt=`expr $cnt + 1`
215 if [ $cnt -gt 60 ]; then
216 # Waited 120 secs now, fail.
217 echo -n "Failed.. "
218 break
219 fi
220 sleep 2
25e87a8f
QY
221 done
222 fi
25e87a8f
QY
223 rm -f `pidfile $inst`
224 rm -f `vtyfile $inst`
225
226 if [ "$1" = "zebra" ]; then
227 echo -n "Stopping staticd since zebra is running"
228 stop staticd
229 fi
7ac1de8e 230 fi
df44cf00 231}
232
d8e4c438 233# Converts values from /etc/frr/daemons to all-numeric values.
651415bd 234convert_daemon_prios()
df44cf00 235{
25e87a8f
QY
236 for name in $DAEMONS zebra vtysh_enable watchfrr_enable; do
237 # First, assign the value set by the user to $value
238 eval value=\${${name}:0:3}
651415bd 239
25e87a8f
QY
240 # Daemon not activated or entry missing?
241 if [ "$value" = "no" -o "$value" = "" ]; then value=0; fi
651415bd 242
25e87a8f
QY
243 # These strings parsed for backwards compatibility.
244 if [ "$value" = "yes" -o "$value" = "true" ]; then
245 value=1;
246 fi
651415bd 247
25e87a8f
QY
248 # Zebra is threatened special. It must be between 0=off and the first
249 # user assigned value "1" so we increase all other enabled daemons' values.
250 if [ "$name" != "zebra" -a "$value" -gt 0 ]; then value=`expr "$value" + 1`; fi
651415bd 251
25e87a8f
QY
252 # If e.g. name is zebra then we set "zebra=yes".
253 eval $name=$value
254 done
df44cf00 255}
256
9473e340
DS
257# Starts watchfrr for all wanted daemons.
258start_watchfrr()
df44cf00 259{
25e87a8f
QY
260 local daemon_name
261 local daemon_prio
262 local found_one
263 local daemon_inst
264
265 # Start the monitor daemon only if desired.
266 if [ 0 -eq "$watchfrr_enable" ]; then
267 return
268 fi
269
270 # Check variable type
10ea85ee
DL
271 if declare -p watchfrr_options | grep -q '^declare \-a'; then
272 # old array support
273 watchfrr_options="${watchfrr_options[@]}"
25e87a8f
QY
274 fi
275
276 # Which daemons have been started?
277 found_one=0
278 for daemon_name in $DAEMONS; do
279 eval daemon_prio=\$$daemon_name
280 if [ "$daemon_prio" -gt 0 ]; then
281 eval "daemon_inst=\${${daemon_name}_instances//,/ }"
282 if [ -n "$daemon_inst" ]; then
283 for inst in ${daemon_inst}; do
284 eval "inst_disable=\${${daemon_name}_${inst}}"
285 if [ -z ${inst_disable} ] || [ ${inst_disable} != 0 ]; then
286 if check_daemon $daemon_name $inst; then
10ea85ee 287 watchfrr_options="$watchfrr_options ${daemon_name}-${inst}"
25e87a8f
QY
288 fi
289 fi
290 done
291 else
292 if check_daemon $daemon_name; then
10ea85ee 293 watchfrr_options="$watchfrr_options $daemon_name"
25e87a8f
QY
294 fi
295 fi
296 found_one=1
297 fi
298 done
299
300 # Start if at least one daemon is activated.
301 if [ $found_one -eq 1 ]; then
25e87a8f
QY
302 start watchfrr
303 echo "."
304 fi
df44cf00 305}
306
9473e340
DS
307# Stopps watchfrr.
308stop_watchfrr()
df44cf00 309{
25e87a8f
QY
310 echo -n "Stopping Frr monitor daemon:"
311 stop watchfrr
312 echo "."
651415bd
DS
313}
314
315# Stops all daemons that have a lower level of priority than the given.
316# (technically if daemon_prio >= wanted_prio)
317stop_prio()
318{
25e87a8f
QY
319 local wanted_prio
320 local daemon_prio
321 local daemon_list
322 local daemon_inst
323 local inst
324
325 if [ -n "$2" ] && [[ "$2" =~ (.*)-(.*) ]]; then
326 daemon=${BASH_REMATCH[1]}
327 inst=${BASH_REMATCH[2]}
328 else
329 daemon="$2"
330 fi
331
332 wanted_prio=$1
333 daemon_list=${daemon:-$DAEMONS}
334
335 echo -n "Stopping Frr daemons (prio:$wanted_prio):"
336
337 for prio_i in `seq 10 -1 $wanted_prio`; do
338 for daemon_name in $daemon_list; do
339 eval daemon_prio=\${${daemon_name}:0:3}
340 daemon_inst=""
341 if [ $daemon_prio -eq $prio_i ]; then
342 eval "daemon_inst=\${${daemon_name}_instances//,/ }"
343 if [ -n "$daemon_inst" ]; then
344 for i in ${daemon_inst}; do
345 if [ -n "$inst" ] && [ "$i" == "$inst" ]; then
346 stop "$daemon_name" "$inst"
347 elif [ x"$inst" == x ]; then
348 stop "$daemon_name" "$i"
349 fi
350 done
351 else
352 stop "$daemon_name"
353 fi
354 fi
355 done
356 done
357
358 echo "."
359 if [ -z "$inst" ]; then
360 # Now stop other daemons that're prowling, coz the daemons file changed
361 echo -n "Stopping other frr daemons"
362 if [ -n "$daemon" ]; then
363 eval "file_list_suffix="$V_PATH"/"$daemon*""
364 else
365 eval "file_list_suffix="$V_PATH/*""
366 fi
367 for pidfile in $file_list_suffix.pid; do
368 PID=`cat $pidfile 2>/dev/null`
369 ${SSD} --stop --quiet --oknodo --pidfile "$pidfile"
370 echo -n "."
371 rm -rf "$pidfile"
372 done
373 echo "."
374
375 echo -n "Removing remaining .vty files"
376 for vtyfile in $file_list_suffix.vty; do
377 rm -rf "$vtyfile"
378 done
379 echo "."
380 fi
651415bd
DS
381}
382
383# Starts all daemons that have a higher level of priority than the given.
384# (technically if daemon_prio <= wanted_prio)
385start_prio()
386{
25e87a8f
QY
387 local wanted_prio
388 local daemon_prio
389 local daemon_list
390 local daemon_name
391 local daemon_inst
392 local inst
393
394 if [ -n "$2" ] && [[ "$2" =~ (.*)-(.*) ]]; then
395 daemon=${BASH_REMATCH[1]}
396 inst=${BASH_REMATCH[2]}
397 else
398 daemon="$2"
399 fi
400
401 wanted_prio=$1
402 daemon_list=${daemon:-$DAEMONS}
403
25e87a8f
QY
404 for prio_i in `seq 1 $wanted_prio`; do
405 for daemon_name in $daemon_list; do
406 eval daemon_prio=\$${daemon_name}
407 daemon_inst=""
408 if [ $daemon_prio -eq $prio_i ]; then
409 eval "daemon_inst=\${${daemon_name}_instances//,/ }"
410 if [ -n "$daemon_inst" ]; then
411 if [ `echo "$daemon_inst" | wc -w` -gt ${MAX_INSTANCES} ]; then
412 echo "Max instances supported is ${MAX_INSTANCES}. Aborting"
413 exit 1
414 fi
415 # Check if we're starting again by switching from single instance
416 # to MI version
417 if started "$daemon_name"; then
418 PIDFILE=`pidfile $daemon_name`
419 ${SSD} \
420 --stop --quiet --oknodo \
421 --pidfile "$PIDFILE" \
422 --exec "$D_PATH/$daemon_name"
423
424 rm -f `pidfile $1`
425 rm -f `vtyfile $1`
426 fi
427
428 for i in ${daemon_inst}; do
429 if [ -n "$inst" ] && [ "$i" == "$inst" ]; then
430 start "$daemon_name" "$inst"
431 elif [ x"$inst" == x ]; then
432 start "$daemon_name" "$i"
433 fi
434 done
435 else
436 # Check if we're starting again by switching from
437 # single instance to MI version
438 eval "file_list_suffix="$V_PATH"/"$daemon_name-*""
439 for pidfile in $file_list_suffix.pid; do
440 ${SSD} --stop --quiet --oknodo --pidfile "$pidfile"
25e87a8f
QY
441 rm -rf "$pidfile"
442 done
443 for vtyfile in $file_list_suffix.vty; do
444 rm -rf "$vtyfile"
445 done
446
447 start "$daemon_name"
448 fi
449 fi
450 done
451 done
df44cf00 452}
453
651415bd 454check_status()
df44cf00 455{
25e87a8f
QY
456 local daemon_name
457 local daemon_prio
458 local daemon_inst
459 local failed_status=0
460
461 if [ -n "$1" ] && [[ "$1" =~ (.*)-(.*) ]]; then
462 daemon=${BASH_REMATCH[1]}
463 inst=${BASH_REMATCH[2]}
464 else
465 daemon="$1"
466 fi
467
468 daemon_list=${daemon:-$DAEMONS}
469
470 # Which daemons have been started?
471 for daemon_name in $daemon_list; do
472 eval daemon_prio=\$$daemon_name
473 if [ "$daemon_prio" -gt 0 ]; then
474 eval "daemon_inst=\${${daemon_name}_instances//,/ }"
475 if [ -n "$daemon_inst" ]; then
476 for i in ${daemon_inst}; do
477 if [ -n "$inst" -a "$inst" = "$i" ]; then
478 started "$1" "log" || failed_status=$?
479 elif [ -z "$inst" ]; then
480 started "$daemon_name-$i" "log" || failed_status=$?
481 fi
482 done
483 else
484 started "$daemon_name" "log" || failed_status=$?
485 fi
486 fi
487 done
488
489 # All daemons that need to have been started are up and running
490 return $failed_status
df44cf00 491}
492
651415bd
DS
493#########################################################
494# Main program #
495#########################################################
496
497# Config broken but script must exit silently.
498[ ! -r "$C_PATH/daemons" ] && exit 0
499
500# Load configuration
501. "$C_PATH/daemons"
a55578f6
CF
502if [ -e "$C_PATH/daemons.conf" ]; then
503 . "$C_PATH/daemons.conf"
504fi
651415bd
DS
505
506# Read configuration variable file if it is present
d8e4c438 507[ -r /etc/default/frr ] && . /etc/default/frr
651415bd 508
68495906
DL
509if test -z "$frr_profile"; then
510 # try to autodetect config profile
511 if test -d /etc/cumulus; then
512 frr_profile=datacenter
513 # elif test ...; then
514 # -- add your distro/system here
515 elif test -n "$FRR_DEFAULT_PROFILE"; then
516 frr_profile="$FRR_DEFAULT_PROFILE"
517 fi
518fi
519test -n "$frr_profile" && frr_global_options="$frr_global_options -F $frr_profile"
520
651415bd
DS
521MAX_INSTANCES=${MAX_INSTANCES:=5}
522
523# Set priority of un-startable daemons to 'no' and substitute 'yes' to '0'
524convert_daemon_prios
525
526if [ ! -d $V_PATH ]; then
25e87a8f 527 echo "Creating $V_PATH"
6f26cc24 528 install -g "$FRR_GROUP" -o "$FRR_USER" -m "$FRR_CONFIG_MODE" -d "$V_PATH"
972cdc56 529 chmod gu+x "${V_PATH}"
651415bd
DS
530fi
531
532if [ -n "$3" ] && [ "$3" != "all" ]; then
25e87a8f 533 dmn="$2"-"$3"
651415bd 534elif [ -n "$2" ] && [ "$2" != "all" ]; then
25e87a8f 535 dmn="$2"
651415bd
DS
536fi
537
df44cf00 538case "$1" in
25e87a8f
QY
539 start)
540 # Try to load this necessary (at least for 2.6) module.
541 if [ -d /lib/modules/`uname -r` ] ; then
542 echo "Loading capability module if not yet done."
543 set +e; LC_ALL=C modprobe -a capability 2>&1 | egrep -v "(not found|Can't locate)"; set -e
544 fi
545
546 # Start all daemons
547 cd $C_PATH/
548 if [ "$2" != "watchfrr" ]; then
549 start_prio 10 $dmn
550 fi
551 start_watchfrr
552 vtysh_b
553 ;;
554
555 1|2|3|4|5|6|7|8|9|10)
556 # Stop/start daemons for the appropriate priority level
557 stop_prio $1
558 start_prio $1
559 vtysh_b
560 ;;
561
562 stop|0)
563 # Stop all daemons at level '0' or 'stop'
564 stop_watchfrr
565 if [ "$dmn" != "watchfrr" ]; then
566 [ -n "${dmn}" ] && eval "${dmn/-/_}=0"
567 stop_prio 0 $dmn
568 fi
569
db6e0503 570 if [ -n "$dmn" -a "$dmn" != "zebra" ]; then
25e87a8f
QY
571 [ -n "$dmn" ] && eval "${dmn/-/_}=0"
572 start_watchfrr
573 fi
574 ;;
575
576 reload)
577 # Just apply the commands that have changed, no restart necessary
578 if [ ! -x "$RELOAD_SCRIPT" ]; then
579 echo "Please install frr-pythontools package. Required for reload"
580 exit 0
581 fi
582
583 NEW_CONFIG_FILE="${2:-$C_PATH/frr.conf}"
584 [ ! -r $NEW_CONFIG_FILE ] && echo "Unable to read new configuration file $NEW_CONFIG_FILE" && exit 1
585 echo "Applying only incremental changes to running configuration from frr.conf"
c9bdc0c7 586 "$RELOAD_SCRIPT" --reload --bindir "$B_PATH" --confdir "$C_PATH" --rundir "$V_PATH" "$C_PATH/frr.conf"
25e87a8f
QY
587 exit $?
588 ;;
589
590 status)
591 check_status $dmn
592 exit $?
593 ;;
594
595 restart|force-reload)
596 $0 stop $dmn
597 sleep 1
598 $0 start $dmn
599 ;;
600
601 *)
602 echo "Usage: /etc/init.d/frr {start|stop|status|reload|restart|force-reload|<priority>} [daemon]"
603 echo " E.g. '/etc/init.d/frr 5' would start all daemons with a prio 1-5."
604 echo " reload applies only modifications from the running config to all daemons."
605 echo " reload neither restarts starts any daemon nor starts any new ones."
606 echo " Read /usr/share/doc/frr/README.Debian for details."
607 exit 1
608 ;;
df44cf00 609esac
610
651415bd 611echo "Exiting from the script"
df44cf00 612exit 0