[packages] olsrd: init: for better reuse, make a function out of a procedure get_valu...
[feed/routing.git] / files / olsrd.init
index 7578316c1311be0150e1e6b48e502db80ee092cc..6ed369fc473ece892417b1e81c81436537976896 100644 (file)
@@ -20,6 +20,11 @@ T='  '
 N='
 '
 
+log()
+{
+       logger -t olsrd -p daemon.info -s "$1"
+}
+
 validate_varname() {
        local varname="$1"
        [ -z "$varname" -o "$varname" != "${varname%%[!A-Za-z0-9_]*}" ] && return 1
@@ -75,6 +80,7 @@ olsrd_find_config_file() {
 }
 
 warning_invalid_value() {
+       local funcname="warning_invalid_value"
        local package="$1"
        validate_varname "$package" || package=
        local config="$2"
@@ -82,14 +88,12 @@ warning_invalid_value() {
        local option="$3"
        validate_varname "$option" || option=
 
-       echo -n "Warning: Invalid value" 1>&2
-
        if [ -n "$package" -a -n "$config" ]; then
-               echo -n " in option '$package.$config${option:+.}$option'" 1>&2
+               log "$funcname() in option '$package.$config${option:+.}$option', skipped"
+       else
+               log "$funcname() skipped"
        fi
 
-       echo ", skipped" 1>&2
-
        return 0
 }
 
@@ -127,6 +131,7 @@ olsrd_write_option() {
 }
 
 olsrd_write_plparam() {
+       local funcname="olsrd_write_plparam"
        local param="$1"
        local cfg="$2"
        validate_varname "$cfg" || return 1
@@ -163,9 +168,9 @@ olsrd_write_plparam() {
        if [ "$option" = 'NonOlsrIf' ]; then
                if validate_varname "$value"; then
                        if network_get_device ifname "$value"; then
-                               echo "Info: mdns Interface '$value' ifname '$ifname' found" 1>&2
+                               log "$funcname() Info: mdns Interface '$value' ifname '$ifname' found"
                        else
-                               echo "Warning: mdns Interface '$value' not found, skipped" 1>&2
+                               log "$funcname() Warning: mdns Interface '$value' not found, skipped"
                        fi
                else
                        warning_invalid_value olsrd "$cfg" "NonOlsrIf"
@@ -227,6 +232,7 @@ config_write_options() {
        local write_func="$3"
        [ -z "$write_func" ] && output_func=echo
        local write_param="$4"
+
        local schema_entry
        local option
        local option_length
@@ -239,20 +245,30 @@ config_write_options() {
        local i
        local position
 
-       for schema_entry in $schema; do
+       get_value_for_entry()
+       {
+               local schema_entry="$1"
+
                default="${schema_entry#*[=]}"
                [ "$default" = "$schema_entry" ] && default=
                option="${schema_entry%%[=]*}"
-               IFS=':'
-               set -- $option
-               unset IFS
+
+               IFS=':'; set -- $option; unset IFS
                option="$1"
                option_type="$2"
-               validate_varname "$option" || continue
-               [ -z "$option_type" ] || validate_varname "$option_type" || continue
-               [ "$option_type" = internal ] && continue
+
+               validate_varname "$option" || return 1
+               [ -z "$option_type" ] || validate_varname "$option_type" || return 1
+               [ "$option_type" = internal ] && return 1
+
                config_get value "$cfg" "$option"
 
+               return 0
+       }
+
+       for schema_entry in $schema; do
+               get_value_for_entry "$schema_entry" || continue
+
                if [ -z "$value" ]; then
                        IFS='+'
                        set -- $default
@@ -399,6 +415,7 @@ olsrd_write_hna6() {
 }
 
 olsrd_write_loadplugin() {
+       local funcname="olsrd_write_loadplugin"
        local cfg="$1"
        validate_varname "$cfg" || return 0
        local ignore
@@ -417,7 +434,7 @@ olsrd_write_loadplugin() {
                return 0
        fi
        if ! [ -x "/lib/$library" -o -x "/usr/lib/$library" -o -x "/usr/local/lib/$library" ]; then
-               echo "Warning: Plugin library '$library' not found, skipped" 1>&2
+               log "$funcname() Warning: Plugin library '$library' not found, skipped"
                return 0
        fi
 
@@ -462,6 +479,7 @@ olsrd_write_loadplugin() {
 }
 
 olsrd_write_interface() {
+       local funcname="olsrd_write_interface"
        local cfg="$1"
        validate_varname "$cfg" || return 0
        local ignore
@@ -474,13 +492,14 @@ olsrd_write_interface() {
 
        ifnames=
        config_get interfaces "$cfg" interface
+
        for interface in $interfaces; do
                if validate_varname "$interface"; then
                        if network_get_device IFNAME "$interface"; then
                                ifnames="$ifnames \"$IFNAME\""
                                ifsglobal="$ifsglobal $IFNAME"
                        else
-                               echo "Warning: Interface '$interface' not found, skipped" 1>&2
+                               log "$funcname() Warning: Interface '$interface' not found, skipped"
                        fi
                else
                        warning_invalid_value olsrd "$cfg" "interface"
@@ -551,14 +570,51 @@ olsrd_write_config() {
        return 0
 }
 
+get_wan_ifnames()
+{
+       local wanifnames word catch_next
+
+       which ip >/dev/null || return 1
+
+       set -- $( ip route list exact 0.0.0.0/0 table all )
+       for word in $*; do
+               case "$word" in
+                       dev)
+                               catch_next="true"
+                       ;;
+                       *)
+                               [ -n "$catch_next" ] && {
+                                       case "$wanifnames" in
+                                               *" $word "*)
+                                               ;;
+                                               *)
+                                                       wanifnames="$wanifnames $word "
+                                               ;;
+                                       esac
+
+                                       catch_next=
+                               }
+                       ;;
+               esac
+       done
+
+       echo "$wanifnames"
+}
+
 olsrd_setup_smartgw_rules() {
+       local funcname="olsrd_setup_smartgw_rules"
        # Check if ipip is installed
-       [ ! -e /etc/modules.d/[0-9]*-ipip ] && echo "Warning: kmod-ipip is missing. SmartGateway will not work until you install it."
+       [ -e /etc/modules.d/[0-9]*-ipip ] || {
+               log "$funcname() Warning: kmod-ipip is missing. SmartGateway will not work until you install it."
+               return 1
+       }
+
+       local wanifnames="$( get_wan_ifnames )"
 
-       wanifnames=$(ip r l e 0/0 t all | sed -e 's/^.* dev //' |cut -d " " -f 1 | sort | uniq)
-       nowan=0
        if [ -z "$wanifnames" ]; then
                nowan=1
+       else
+               nowan=0
        fi
 
        IP4T=$(which iptables)
@@ -577,7 +633,7 @@ olsrd_setup_smartgw_rules() {
        while $IP4T -t nat -D postrouting_rule -o tnl_+ -j MASQUERADE 2> /dev/null; do :;done
 
        if [ "$smartgateway" == "yes" ]; then
-               echo "Notice: Inserting firewall rules for SmartGateway"
+               log "$funcname() Notice: Inserting firewall rules for SmartGateway"
                if [ ! "$smartgatewayuplink" == "none" ]; then
                        if [ "$smartgatewayuplink" == "ipv4" ]; then
                                # Allow everything to be forwarded to tnl_+ and use NAT for it
@@ -622,7 +678,7 @@ olsrd_setup_smartgw_rules() {
 }
 
 error() {
-       echo "${initscript}:" "$@" 1>&2
+       log "error() ${initscript}: $@"
 }
 
 start() {
@@ -674,14 +730,14 @@ start() {
 
                SERVICE_PID_FILE="$PID6"
                if service_check /usr/sbin/olsrd; then
-                       error "there already is an IPv6 instance of olsrd running ($(cat $PID6)), not starting."
+                       error "there is already an IPv6 instance of olsrd running (pid: '$(cat $PID6)'), not starting."
                else
                        service_start /usr/sbin/olsrd -f /var/etc/olsrd.conf.ipv6 -nofork
                fi
 
                SERVICE_PID_FILE="$PID"
                if service_check /usr/sbin/olsrd; then
-                       error "there already is an IPv4 instance of olsrd running ($(cat $PID)), not starting."
+                       error "there is already an IPv4 instance of olsrd running (pid: '$(cat $PID)'), not starting."
                else
                        service_start /usr/sbin/olsrd -f /var/etc/olsrd.conf.ipv4 -nofork
                fi
@@ -697,7 +753,7 @@ start() {
 
                SERVICE_PID_FILE="$PID"
                if service_check /usr/sbin/olsrd; then
-                       error "there already is an IPv4 instance of olsrd running ($(cat $PID)), not starting."
+                       error "there is already an IPv4 instance of olsrd running (pid: '$(cat $PID)'), not starting."
                        return 1
                else
                        service_start /usr/sbin/olsrd -f "$OLSRD_CONFIG_FILE" -nofork