]> git.proxmox.com Git - mirror_frr.git/blob - tools/frr.in
bgpd: Refactor subgroup_announce_table() to reuse an existing helpers
[mirror_frr.git] / tools / frr.in
1 #!/bin/bash
2 #
3 ### BEGIN INIT INFO
4 # Provides: frr
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 Frr routing suite
10 # Description: Frr is a routing suite for IP routing protocols like
11 # BGP, OSPF, RIP and others. This script contols the main
12 # daemon "frr" as well as the individual protocol daemons.
13 ### END INIT INFO
14 #
15
16 PATH=/bin:/usr/bin:/sbin:/usr/sbin
17 D_PATH="@CFG_SBIN@" # /usr/lib/frr
18 C_PATH="@CFG_SYSCONF@" # /etc/frr
19 V_PATH="@CFG_STATE@" # /var/run/frr
20 B_PATH="@CFG_BIN@"
21 VTYSH="@vtysh_bin@" # /usr/bin/vtysh
22 FRR_USER="@enable_user@" # frr
23 FRR_GROUP="@enable_group@" # frr
24 FRR_VTY_GROUP="@enable_vty_group@" # frrvty
25 FRR_CONFIG_MODE="@enable_configfile_mask@" # 0600
26 FRR_DEFAULT_PROFILE="@DFLT_NAME@" # traditional / datacenter
27
28 # Local Daemon selection may be done by using /etc/frr/daemons.
29 # See /usr/share/doc/frr/README.Debian.gz for further information.
30 # Keep zebra first and do not list watchfrr!
31 DAEMONS="mgmtd zebra bgpd ripd ripngd ospfd ospf6d isisd babeld pimd pim6d ldpd nhrpd eigrpd sharpd pbrd staticd bfdd fabricd vrrpd pathd"
32 MAX_INSTANCES=5
33 RELOAD_SCRIPT="$D_PATH/frr-reload.py"
34
35 if [ -e /lib/lsb/init-functions ]; then
36 . /lib/lsb/init-functions
37 fi
38
39 if [ -f $D_PATH/ssd ]; then
40 SSD=$D_PATH/ssd
41 else
42 SSD=`which start-stop-daemon`
43 fi
44
45 # Print the name of the pidfile.
46 pidfile()
47 {
48 echo "$V_PATH/$1.pid"
49 }
50
51 # Print the name of the vtysh.
52 vtyfile()
53 {
54 echo "$V_PATH/$1.vty"
55 }
56
57 # Check if daemon is started by using the pidfile.
58 started()
59 {
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
67 }
68
69 # Loads the config via vtysh -b if configured to do so.
70 vtysh_b ()
71 {
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
74 $VTYSH -b
75 fi
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
82 check_daemon()
83 {
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
100 install -g "$FRR_GROUP" -o "$FRR_USER" -m "$FRR_CONFIG_MODE" /dev/null "$C_PATH/$1-$2.conf"
101 fi
102 elif [ ! -r "$C_PATH/$1.conf" ]; then
103 install -g "$FRR_GROUP" -o "$FRR_USER" -m "$FRR_CONFIG_MODE" /dev/null "$C_PATH/$1.conf"
104 fi
105 fi
106 return 0
107 }
108
109 # Starts the server if it's not alrady running according to the pid file.
110 # The Frr daemons creates the pidfile when starting.
111 start()
112 {
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
119
120 # We may need to restart watchfrr if new daemons are added and/or
121 # removed
122 if started "$dmn" ; then
123 stop watchfrr
124 else
125 # Echo only once. watchfrr is printed in the stop above
126 echo -n " $dmn"
127 fi
128
129 eval "set - $watchfrr_options"
130 ${SSD} \
131 --start \
132 --pidfile=`pidfile $dmn` \
133 --exec "$D_PATH/$dmn" \
134 -- \
135 "$@"
136
137 elif [ -n "$inst" ]; then
138 echo -n " $dmn-$inst"
139 if ! check_daemon $dmn $inst ; then
140 echo -n " (binary does not exist)"
141 return;
142 fi
143
144 ${SSD} \
145 --start \
146 --pidfile=`pidfile $dmn-$inst` \
147 --exec "$D_PATH/$dmn" \
148 -- \
149 `eval echo "$""$dmn""_options"` $frr_global_options -n "$inst"
150 else
151 if ! check_daemon $dmn; then
152 echo -n " (binary does not exist)"
153 return;
154 fi
155
156 if [ "$valgrind_enable" = "yes" ]; then
157 ${SSD} \
158 --start \
159 --pidfile=`pidfile $dmn` \
160 --exec "$valgrind" \
161 -- --trace-children=no --leak-check=full --log-file=/var/log/frr/$dmn-valgrind.log $D_PATH/$dmn \
162 `eval echo "$""$dmn""_options"` $frr_global_options
163 else
164 ${SSD} \
165 --start \
166 --pidfile=`pidfile $dmn` \
167 --exec "$D_PATH/$dmn" \
168 -- \
169 `eval echo "$""$dmn""_options"` $frr_global_options
170 fi
171 fi
172
173 # Start the staticd automatically
174 if [ "$dmn" = "zebra" ]; then
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 -- \
186 `eval echo "$"staticd"_options"` $frr_global_options
187 fi
188 }
189
190 # Stop the daemon given in the parameter, printing its name to the terminal.
191 stop()
192 {
193 local inst
194
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
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
221 done
222 fi
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
230 fi
231 }
232
233 # Converts values from /etc/frr/daemons to all-numeric values.
234 convert_daemon_prios()
235 {
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}
239
240 # Daemon not activated or entry missing?
241 if [ "$value" = "no" -o "$value" = "" ]; then value=0; fi
242
243 # These strings parsed for backwards compatibility.
244 if [ "$value" = "yes" -o "$value" = "true" ]; then
245 value=1;
246 fi
247
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
251
252 # If e.g. name is zebra then we set "zebra=yes".
253 eval $name=$value
254 done
255 }
256
257 # Starts watchfrr for all wanted daemons.
258 start_watchfrr()
259 {
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
271 if declare -p watchfrr_options | grep -q '^declare \-a'; then
272 # old array support
273 watchfrr_options="${watchfrr_options[@]}"
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
287 watchfrr_options="$watchfrr_options ${daemon_name}-${inst}"
288 fi
289 fi
290 done
291 else
292 if check_daemon $daemon_name; then
293 watchfrr_options="$watchfrr_options $daemon_name"
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
302 start watchfrr
303 echo "."
304 fi
305 }
306
307 # Stopps watchfrr.
308 stop_watchfrr()
309 {
310 echo -n "Stopping Frr monitor daemon:"
311 stop watchfrr
312 echo "."
313 }
314
315 # Stops all daemons that have a lower level of priority than the given.
316 # (technically if daemon_prio >= wanted_prio)
317 stop_prio()
318 {
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
381 }
382
383 # Starts all daemons that have a higher level of priority than the given.
384 # (technically if daemon_prio <= wanted_prio)
385 start_prio()
386 {
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
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"
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
452 }
453
454 check_status()
455 {
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
491 }
492
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"
502 if [ -e "$C_PATH/daemons.conf" ]; then
503 . "$C_PATH/daemons.conf"
504 fi
505
506 # Read configuration variable file if it is present
507 [ -r /etc/default/frr ] && . /etc/default/frr
508
509 if 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
518 fi
519 test -n "$frr_profile" && frr_global_options="$frr_global_options -F $frr_profile"
520
521 MAX_INSTANCES=${MAX_INSTANCES:=5}
522
523 # Set priority of un-startable daemons to 'no' and substitute 'yes' to '0'
524 convert_daemon_prios
525
526 if [ ! -d $V_PATH ]; then
527 echo "Creating $V_PATH"
528 install -g "$FRR_GROUP" -o "$FRR_USER" -m "$FRR_CONFIG_MODE" -d "$V_PATH"
529 chmod gu+x "${V_PATH}"
530 fi
531
532 if [ -n "$3" ] && [ "$3" != "all" ]; then
533 dmn="$2"-"$3"
534 elif [ -n "$2" ] && [ "$2" != "all" ]; then
535 dmn="$2"
536 fi
537
538 case "$1" in
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
570 if [ -n "$dmn" -a "$dmn" != "zebra" ]; then
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"
586 "$RELOAD_SCRIPT" --reload --bindir "$B_PATH" --confdir "$C_PATH" --rundir "$V_PATH" "$C_PATH/frr.conf"
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 ;;
609 esac
610
611 echo "Exiting from the script"
612 exit 0