ea4b348f97d7ca35cf3696bd8b2e817f61ca5746
[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 babel_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 # Handle ignore options.
113 local _ignored
114 # This works because we loaded the whole configuration
115 # beforehand (see config_load below).
116 config_get_bool _ignored "$section" 'ignore' 0
117 if [ "$_ignored" -eq 1 ]
118 then
119 option_cb() { return; }
120 else
121 # Also include an empty "interface $interface" statement,
122 # so that babeld operates on this interface.
123 cfg_append "interface $interface"
124 fi
125 ;;
126 *)
127 # Don't use reset_cb, this would also reset config_cb
128 option_cb() { return; }
129 ;;
130 esac
131 }
132
133 start() {
134 mkdir -p /var/lib
135 # Start by emptying the generated config file
136 >"$CONFIGFILE"
137 # First load the whole config file, without callbacks, so that we are
138 # aware of all "ignore" options in the second pass.
139 config_load babeld
140 # Parse general and interface sections thanks to the "config_cb()"
141 # callback. This allows to loop over all options without having to
142 # know their name in advance.
143 config_cb() { babel_config_cb "$@"; }
144 config_load babeld
145 # Parse filters separately, since we know which options we expect
146 config_foreach babel_filter filter
147 # Using multiple config files is supported since babeld 1.5.1
148 /usr/sbin/babeld -D -I "$pidfile" -c "$OTHERCONFIGFILE" -c "$CONFIGFILE"
149 # Wait for the pidfile to appear
150 for i in 1 2
151 do
152 [ -f "$pidfile" ] || sleep 1
153 done
154 [ -f "$pidfile" ] || (echo "Failed to start babeld"; exit 42)
155 }
156
157 stop() {
158 [ -f "$pidfile" ] && kill $(cat $pidfile)
159 # avoid race-condition on restart: wait for
160 # babeld to die for real.
161 [ -f "$pidfile" ] && sleep 1
162 [ -f "$pidfile" ] && sleep 1
163 [ -f "$pidfile" ] && sleep 1
164 [ -f "$pidfile" ] && exit 42
165 }
166
167 status() {
168 [ -f "$pidfile" ] && kill -USR1 $(cat $pidfile)
169 }