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