babeld: Print an error when startup fails
[feed/routing.git] / babeld / files / babeld.init
1 #!/bin/sh /etc/rc.common
2
3 . /lib/functions/network.sh
4
5 START=70
6
7 pidfile='/var/run/babeld.pid'
8 CONFIGFILE='/var/etc/babeld.conf'
9 OTHERCONFIGFILE="/etc/babeld.conf"
10 EXTRA_COMMANDS="status"
11 EXTRA_HELP=" status Dump Babel's table to the log file."
12
13 # Append a line to the configuration file
14 cfg_append() {
15 local value="$1"
16 echo "$value" >> $CONFIGFILE
17 }
18
19 cfg_append_option() {
20 local section="$1"
21 local option="$2"
22 local value
23 config_get value "$section" "$option"
24 # babeld convention for options is '-', not '_'
25 [ -n "$value" ] && cfg_append "${option//_/-} $value"
26 }
27
28 # Append to the "$buffer" variable
29 append_ifname() {
30 local section="$1"
31 local option="$2"
32 local switch="$3"
33 local _name
34 config_get _name "$section" "$option"
35 [ -z "$_name" ] && return 0
36 local ifname=$(uci_get_state network "$_name" ifname "$_name")
37 append buffer "$switch $ifname"
38 }
39
40 append_bool() {
41 local section="$1"
42 local option="$2"
43 local value="$3"
44 local _loctmp
45 config_get_bool _loctmp "$section" "$option" 0
46 [ "$_loctmp" -gt 0 ] && append buffer "$value"
47 }
48
49 append_parm() {
50 local section="$1"
51 local option="$2"
52 local switch="$3"
53 local _loctmp
54 config_get _loctmp "$section" "$option"
55 [ -z "$_loctmp" ] && return 0
56 append buffer "$switch $_loctmp"
57 }
58
59 babel_filter() {
60 local cfg="$1"
61 local _loctmp
62
63 local _ignored
64 config_get_bool _ignored "$cfg" 'ignore' 0
65 [ "$_ignored" -eq 1 ] && return 0
66
67 unset buffer
68 append_parm "$cfg" 'type' ''
69
70 append_bool "$cfg" 'local' 'local'
71
72 append_parm "$cfg" 'ip' 'ip'
73 append_parm "$cfg" 'eq' 'eq'
74 append_parm "$cfg" 'le' 'le'
75 append_parm "$cfg" 'ge' 'ge'
76 append_parm "$cfg" 'neigh' 'neigh'
77 append_parm "$cfg" 'id' 'id'
78 append_parm "$cfg" 'proto' 'proto'
79
80 append_ifname "$cfg" 'if' 'if'
81
82 append_parm "$cfg" 'action' ''
83
84 cfg_append "$buffer"
85 }
86
87 # Only one of babeld's options is allowed multiple times, "import-table".
88 # We just append it multiple times.
89 list_cb() {
90 option_cb "$@"
91 }
92
93 config_cb() {
94 local type="$1"
95 local section="$2"
96 case "$type" in
97 "general")
98 option_cb() {
99 local option="$1"
100 local value="$2"
101 cfg_append "${option//_/-} $value"
102 }
103 ;;
104 "interface")
105 unset interface
106 network_get_device interface "$section" || interface="$section"
107 option_cb() {
108 local option="$1"
109 local value="$2"
110 cfg_append "interface $interface ${option//_/-} $value"
111 }
112 # Also include an empty "interface $interface" statement, so
113 # that babeld operates on this interface.
114 cfg_append "interface $interface"
115 ;;
116 *)
117 # Don't use reset_cb, this would also reset config_cb
118 option_cb() { return; }
119 ;;
120 esac
121 }
122
123 start() {
124 mkdir -p /var/lib
125 # Start by emptying the generated config file
126 >"$CONFIGFILE"
127 # Parse general and interface sections thanks to the "config_cb()"
128 # callback. This allows to loop over all options without having to
129 # know their name in advance.
130 config_load babeld
131 # Parse filters separately, since we know which options we expect
132 config_foreach babel_filter filter
133 # Using multiple config files is supported since babeld 1.5.1
134 /usr/sbin/babeld -D -I "$pidfile" -c "$OTHERCONFIGFILE" -c "$CONFIGFILE"
135 # Wait for the pidfile to appear
136 for i in 1 2
137 do
138 [ -f "$pidfile" ] || sleep 1
139 done
140 [ -f "$pidfile" ] || (echo "Failed to start babeld"; exit 42)
141 }
142
143 stop() {
144 [ -f "$pidfile" ] && kill $(cat $pidfile)
145 # avoid race-condition on restart: wait for
146 # babeld to die for real.
147 [ -f "$pidfile" ] && sleep 1
148 [ -f "$pidfile" ] && sleep 1
149 [ -f "$pidfile" ] && sleep 1
150 [ -f "$pidfile" ] && exit 42
151 }
152
153 status() {
154 [ -f "$pidfile" ] && kill -USR1 $(cat $pidfile)
155 }