babeld: add dynamic config files to the procd watch list
[feed/routing.git] / babeld / files / babeld.init
index d6a6f1ea264fba31ea4814e5f64105fef3794656..276e67f2a0ca86c059b14e178ae64808219228ce 100755 (executable)
@@ -1,19 +1,24 @@
 #!/bin/sh /etc/rc.common
 
-. /lib/functions/network.sh
+. $IPKG_INSTROOT/lib/functions/network.sh
 
+USE_PROCD=1
 START=70
 
-pidfile='/var/run/babeld.pid'
 CONFIGFILE='/var/etc/babeld.conf'
 OTHERCONFIGFILE="/etc/babeld.conf"
+OTHERCONFIGDIR="/tmp/babeld.d/"
 EXTRA_COMMANDS="status"
 EXTRA_HELP="        status Dump Babel's table to the log file."
 
+# Options to ignore for the global section (old options that are translated
+# for backward compatibility with old configuration files)
+ignored_options="carrier_sense assume_wireless no_split_horizon random_router_id multicast_address port hello_interval wired_hello_interval smoothing_half_time duplication_priority local_server conf_file conf_dir"
+
 # Append a line to the configuration file
 cfg_append() {
         local value="$1"
-        echo "$value" >> $CONFIGFILE
+        echo "$value" >> "$CONFIGFILE"
 }
 
 cfg_append_option() {
@@ -56,6 +61,63 @@ append_parm() {
        append buffer "$switch $_loctmp"
 }
 
+
+# Provides backward compatibility for old option names in the global section.
+translate_option() {
+       local section="$1"
+       local old_option="$2"
+       local new_option="$3"
+       local _value
+       config_get _value "$section" "$old_option"
+       [ -z "$_value" ] && return
+       cfg_append "${new_option//_/-} $_value"
+}
+
+translate_bool() {
+       local section="$1"
+       local old_option="$2"
+       local new_option="$3"
+       local _bool
+       local _value
+       config_get_bool _bool "$section" "$old_option" 0
+       [ "$_bool" -eq 0 ] && return
+       cfg_append "${new_option//_/-} true"
+}
+
+# Adds a new interface section for setting default interface options.
+add_default_option() {
+       local option="$1"
+       local value="$2"
+       cfg_append "default ${option//_/-} $value"
+}
+
+# Global 'hello_interval' and 'wired_hello_interval' options are ignored,
+# because they have no direct equivalent: you should use
+# interface-specific settings.
+parse_old_global_options() {
+       local section="$1"
+       translate_bool   "$section" 'carrier_sense'     'link_detect'
+       translate_bool   "$section" 'random_router_id'  'random_id'
+       translate_option "$section" 'multicast_address' 'protocol_group'
+       translate_option "$section" 'port'              'protocol_port'
+       translate_option "$section" 'local_server'      'local_port'
+       translate_option "$section" 'smoothing_half_time'       'smoothing_half_life'
+       translate_option "$section" 'duplication_priority'      'allow_duplicates'
+       # These two global options are turned into default interface options.
+       local _bool
+       config_get_bool _bool "$section" 'assume_wireless' 0
+       [ "$_bool" -eq 1 ] && add_default_option "wired" "false"
+       config_get_bool _bool "$section" 'no_split_horizon' 0
+       [ "$_bool" -eq 1 ] && add_default_option "split_horizon" "false"
+        # Configure alternative configuration file and directory
+       local conf_file
+       config_get conf_file "$section" "conf_file"
+       [ -n "$conf_file" ] && OTHERCONFIGFILE="$conf_file"
+       local conf_dir
+       config_get conf_dir "$section" "conf_dir"
+       [ -n "$conf_dir" ] && OTHERCONFIGDIR="$conf_dir"
+}
+
 babel_filter() {
        local cfg="$1"
        local _loctmp
@@ -73,6 +135,10 @@ babel_filter() {
        append_parm "$cfg" 'eq' 'eq'
        append_parm "$cfg" 'le' 'le'
        append_parm "$cfg" 'ge' 'ge'
+       append_parm "$cfg" 'src_ip' 'src-ip'
+       append_parm "$cfg" 'src_eq' 'src-eq'
+       append_parm "$cfg" 'src_le' 'src-le'
+       append_parm "$cfg" 'src_ge' 'src-ge'
        append_parm "$cfg" 'neigh' 'neigh'
        append_parm "$cfg" 'id' 'id'
        append_parm "$cfg" 'proto' 'proto'
@@ -98,12 +164,20 @@ babel_config_cb() {
                option_cb() {
                        local option="$1"
                        local value="$2"
+                       # Ignore old options
+                       list_contains ignored_options "$option" && return
+                       # Skip lists. They will be taken care of by list_cb
+                       test "${option#*_ITEM}" != "$option" && return
+                       test "${option#*_LENGTH}" != "$option" && return
                        cfg_append "${option//_/-} $value"
                }
        ;;
        "interface")
                local _ifname
                config_get _ifname "$section" 'ifname'
+               # Backward compatibility: try to use the section name
+               # if no "option ifname" was used.
+               [ -z "$_ifname" -a "${section:0:3}" != "cfg" ] && _ifname="$section"
                # Try to resolve the logical interface name
                unset interface
                network_get_device interface "$_ifname" || interface="$_ifname"
@@ -138,10 +212,17 @@ babel_config_cb() {
        esac
 }
 
-start() {
+start_service() {
        mkdir -p /var/lib
+       mkdir -p /var/etc
+       mkdir -p "$OTHERCONFIGDIR"
+
        # Start by emptying the generated config file
        >"$CONFIGFILE"
+       # Import dynamic config files
+       for f in "$OTHERCONFIGDIR"/*.conf; do
+               [ -f "$f" ] && cat "$f" >> "$CONFIGFILE"
+       done
        # First load the whole config file, without callbacks, so that we are
        # aware of all "ignore" options in the second pass.
        config_load babeld
@@ -150,28 +231,24 @@ start() {
        # know their name in advance.
        config_cb() { babel_config_cb "$@"; }
        config_load babeld
+       # Backward compatibility
+       config_foreach parse_old_global_options general
        # Parse filters separately, since we know which options we expect
        config_foreach babel_filter filter
+       procd_open_instance
        # Using multiple config files is supported since babeld 1.5.1
-       /usr/sbin/babeld -D -I "$pidfile" -c "$OTHERCONFIGFILE" -c "$CONFIGFILE"
-       # Wait for the pidfile to appear
-       for i in 1 2
-       do
-               [ -f "$pidfile" ] || sleep 1
-       done
-       [ -f "$pidfile" ] || (echo "Failed to start babeld"; exit 42)
+       procd_set_param command /usr/sbin/babeld -I "" -c "$OTHERCONFIGFILE" -c "$CONFIGFILE"
+       procd_set_param stdout 1
+       procd_set_param stderr 1
+       procd_set_param file "$OTHERCONFIGFILE" "$OTHERCONFIGDIR"/*.conf "$CONFIGFILE"
+       procd_set_param respawn
+       procd_close_instance
 }
 
-stop() {
-       [ -f "$pidfile" ] && kill $(cat $pidfile)
-  # avoid race-condition on restart: wait for
-  # babeld to die for real.
-  [ -f "$pidfile" ] && sleep 1
-  [ -f "$pidfile" ] && sleep 1
-  [ -f "$pidfile" ] && sleep 1
-  [ -f "$pidfile" ] && exit 42
+service_triggers() {
+       procd_add_reload_trigger babeld
 }
 
 status() {
-       [ -f "$pidfile" ] && kill -USR1 $(cat $pidfile)
+       kill -USR1 $(pgrep -P 1 babeld)
 }