olsr: init-script: minor style changes
[feed/routing.git] / olsrd / files / olsrd.init
index 43c7e67311cd488a3edd965bf9a6a159d2030a28..f5b866093f789b76331518852123077eff9059e9 100644 (file)
@@ -1,5 +1,5 @@
 #!/bin/sh /etc/rc.common
-# Copyright (C) 2008-2013 OpenWrt.org
+# Copyright (C) 2008-2017 OpenWrt.org
 
 START=65
 
@@ -15,19 +15,17 @@ OLSRD_INTERFACE_DEFAULTS_SCHEMA='AutoDetectChanges:bool'
 T='    '
 N='
 '
-#6and4: backward compatibility - it MUST be different from /etc/init.d/olsrd6 PID variable
-PID6=/var/run/olsrd6and4.pid
 
 log() {
-       logger -t olsrd -p daemon.info -s "${initscript}: $@"
+       logger -t olsrd -p daemon.info -s "${initscript}: $*"
 }
 
 error() {
-        logger -t olsrd -p daemon.err -s "${initscript}: ERROR: $@"
+        logger -t olsrd -p daemon.err -s "${initscript}: ERROR: $*"
 }
 
 warn() {
-        logger -t olsrd -p daemon.warn -s "${initscript}: WARNING: $@"
+        logger -t olsrd -p daemon.warn -s "${initscript}: WARNING: $*"
 }
 
 validate_varname() {
@@ -126,22 +124,21 @@ olsrd_write_option() {
                fi
        fi
 
-       echo -n "${N}$param$option $value"
-
-       return 0
+       printf '%s' "${N}$param$option $value"
 }
 
 olsrd_write_plparam() {
        local funcname="olsrd_write_plparam"
        local param="$1"
        local cfg="$2"
-       validate_varname "$cfg" || return 1
        local option="$3"
-       validate_varname "$option" || return 1
        local value="$4"
        local option_type="$5"
        local _option oldIFS
 
+       validate_varname "$cfg" || return 1
+       validate_varname "$option" || return 1
+
        if [ "$option_type" = bool ]; then
                case "$value" in
                        1|on|true|enabled|yes) value=yes;;
@@ -182,9 +179,7 @@ olsrd_write_plparam() {
                [ -z "$ifname" ] || value=$ifname
        fi
 
-       echo -n "${N}${param}PlParam \"$option\" \"$value\""
-
-       return 0
+       printf '%s' "${N}${param}PlParam \"$option\" \"$value\""
 }
 
 config_update_schema() {
@@ -276,7 +271,7 @@ config_write_options() {
        already_in_schema "speed" && {
                get_value_for_entry "speed"
 
-               if [ 2>/dev/null $value -gt 0 -a $value -le 20 ]; then
+               if test 2>/dev/null "$value" -gt 0 -a "$value" -le 20 ; then
                        speed="$value"
                else
                        log "$funcname() Warning: invalid speed-value: '$value' - allowed integers: 1...20, fallback to 6"
@@ -292,30 +287,30 @@ config_write_options() {
                if [ -n "$speed" ]; then                # like sven-ola freifunk firmware fff-1.7.4
                        case "$schema_entry" in
                                HelloInterval)
-                                       value="$(( $speed / 2 + 1 )).0"
+                                       value="$(( speed / 2 + 1 )).0"
                                ;;
                                HelloValidityTime)
-                                       value="$(( $speed * 25 )).0"
+                                       value="$(( speed * 25 )).0"
                                ;;
-                               TcInterval)     # todo: not fisheye? -> $(( $speed * 2 ))
-                                       value=$(( $speed / 2 ))
+                               TcInterval)     # todo: not fisheye? -> $(( speed * 2 ))
+                                       value=$(( speed / 2 ))
                                        [ $value -eq 0 ] && value=1
                                        value="$value.0"
                                ;;
                                TcValidityTime)
-                                       value="$(( $speed * 100 )).0"
+                                       value="$(( speed * 100 )).0"
                                ;;
                                MidInterval)
-                                       value="$(( $speed * 5 )).0"
+                                       value="$(( speed * 5 )).0"
                                ;;
                                MidValidityTime)
-                                       value="$(( $speed * 100 )).0"
+                                       value="$(( speed * 100 )).0"
                                ;;
                                HnaInterval)
-                                       value="$(( $speed * 2 )).0"
+                                       value="$(( speed * 2 )).0"
                                ;;
                                HnaValidityTime)
-                                       value="$(( $speed * 25 )).0"
+                                       value="$(( speed * 25 )).0"
                                ;;
                                *)
                                        get_value_for_entry "$schema_entry" || continue
@@ -396,19 +391,6 @@ olsrd_write_olsrd() {
 
        [ "$OLSRD_COUNT" -gt 0 ] && return 0
 
-       config_get ipversion "$cfg" IpVersion
-       if [ "$UCI_CONF_NAME" = "olsrd6" ]; then
-               OLSRD_OLSRD_SCHEMA="$OLSRD_OLSRD_SCHEMA IpVersion=6"
-               if [ "$ipversion" = "6and4" ]; then
-                       error "IpVersion 6and4 not supported in olsrd6"
-                       return 1
-               fi
-       else
-               if [ "$ipversion" = "6and4" ]; then
-                       OLSRD_IPVERSION_6AND4=1
-                       config_set "$cfg" IpVersion '6'
-               fi
-       fi
        config_get smartgateway "$cfg" SmartGateway
        config_get smartgatewayuplink "$cfg" SmartGatewayUplink
 
@@ -428,12 +410,10 @@ olsrd_write_ipcconnect() {
 
        [ "$IPCCONNECT_COUNT" -gt 0 ] && return 0
 
-       echo -n "${N}IpcConnect${N}{"
+       printf '%s' "${N}IpcConnect${N}{"
        config_write_options "$OLSRD_IPCCONNECT_SCHEMA" "$cfg" olsrd_write_option "${T}"
        echo "${N}}"
        IPCCONNECT_COUNT=$((IPCCONNECT_COUNT + 1))
-
-       return 0
 }
 
 olsrd_write_hna4() {
@@ -456,11 +436,9 @@ olsrd_write_hna4() {
                return 0
        fi
 
-       [ "$HNA4_COUNT" -le 0 ] && echo -n "${N}Hna4${N}{"
-       echo -n "${N}${T}${T}$netaddr $netmask"
+       [ "$HNA4_COUNT" -le 0 ] && printf '%s' "${N}Hna4${N}{"
+       printf '%s' "${N}${T}${T}$netaddr $netmask"
        HNA4_COUNT=$((HNA4_COUNT + 1))
-
-       return 0
 }
 
 olsrd_write_hna6() {
@@ -483,39 +461,53 @@ olsrd_write_hna6() {
                return 0
        fi
 
-       [ "$HNA6_COUNT" -le 0 ] && echo -n "${N}Hna6${N}{"
-       echo -n "${N}${T}${T}$netaddr $prefix"
+       [ "$HNA6_COUNT" -le 0 ] && printf '%s' "${N}Hna6${N}{"
+       printf '%s' "${N}${T}${T}$netaddr $prefix"
        HNA6_COUNT=$((HNA6_COUNT + 1))
+}
 
-       return 0
+find_most_recent_plugin_libary()
+{
+       local library="$1"
+       local file unixtime
+
+       for file in "/lib/$library"* "/usr/lib/$library"* "/usr/local/lib/$library"*; do {
+               [ -f "$file" ] && {
+                       unixtime="$( date +%s -r "$file" )"
+                       echo "$unixtime $file"
+               }
+       } done | sort -n | tail -n1 | cut -d' ' -f2
 }
 
-olsrd_write_loadplugin() {
-       local funcname="olsrd_write_loadplugin"
+olsrd_write_loadplugin()
+{
+       local funcname='olsrd_write_loadplugin'
        local cfg="$1"
+       local ignore name suffix lat lon latlon_infile
+
        validate_varname "$cfg" || return 0
-       local ignore
-       local name
-       local suffix
-       local lat
-       local lon
-       local latlon_infile
 
        config_get_bool ignore "$cfg" ignore 0
        [ "$ignore" -ne 0 ] && return 0
 
+       # e.g. olsrd_txtinfo.so.1.1 or 'olsrd_txtinfo'
        config_get library "$cfg" library
-       if ! validate_olsrd_option "$library"; then
-               warning_invalid_value olsrd "$cfg" "library"
-               return 0
-       fi
-       if ! [ -x "/lib/$library" -o -x "/usr/lib/$library" -o -x "/usr/local/lib/$library" ]; then
+
+       library="$( find_most_recent_plugin_libary "$library" )"
+       if [ -z "$library" ]; then
                log "$funcname() Warning: Plugin library '$library' not found, skipped"
                return 0
+       else
+               library="$( basename "$library" )"
        fi
 
+       validate_olsrd_option "$library" || {
+               warning_invalid_value olsrd "$cfg" 'library'
+               return 0
+       }
+
        case "$library" in
-               olsrd_nameservice.*)
+               'olsrd_nameservice.'*)
                        config_get name "$cfg" name
                        [ -z "$name" ] && config_set "$cfg" name $SYSTEM_HOSTNAME
 
@@ -542,16 +534,14 @@ olsrd_write_loadplugin() {
 
                        [ -z "$latlon_file" ] && config_set "$cfg" latlon_file '/var/run/latlon.js'
                ;;
-               olsrd_watchdog.*)
+               'olsrd_watchdog.'*)
                        config_get wd_file "$cfg" file
                ;;
        esac
 
-       echo -n "${N}LoadPlugin \"$library\"${N}{"
+       printf '%s' "${N}LoadPlugin \"$library\"${N}{"
        config_write_options "$OLSRD_LOADPLUGIN_SCHEMA" "$cfg" olsrd_write_plparam "${T}"
        echo "${N}}"
-
-       return 0
 }
 
 olsrd_write_interface() {
@@ -590,19 +580,17 @@ olsrd_write_interface() {
 
        [ -z "$ifnames" ] && return 0
 
-       echo -n "${N}Interface$ifnames${N}{"
+       printf '%s' "${N}Interface$ifnames${N}{"
        config_write_options "$OLSRD_INTERFACE_SCHEMA" "$cfg" olsrd_write_option "${T}"
        echo "${N}}"
        INTERFACES_COUNT=$((INTERFACES_COUNT + 1))
-
-       return 0
 }
 
 olsrd_write_interface_defaults() {
        local cfg="$1"
        validate_varname "$cfg" || return 0
 
-       echo -n "${N}InterfaceDefaults$ifnames${N}{"
+       printf '%s' "${N}InterfaceDefaults$ifnames${N}{"
        config_write_options "$OLSRD_INTERFACE_DEFAULTS_SCHEMA" "$cfg" olsrd_write_option "${T}"
        echo "${N}}"
 
@@ -632,7 +620,6 @@ olsrd_update_schema() {
 }
 
 olsrd_write_config() {
-       OLSRD_IPVERSION_6AND4=0
        OLSRD_COUNT=0
        config_foreach olsrd_write_olsrd olsrd
        IPCCONNECT_COUNT=0
@@ -800,53 +787,16 @@ start() {
 
        [ -z "$OLSRD_CONFIG_FILE" ] && return 1
 
-       #6and4: backward compatibility
-       local bindv6only='0'
-       if [ "$OLSRD_IPVERSION_6AND4" -ne 0 ]; then
-               warn "IpVersion 6and4 is deprecated and will be removed in future!"
-               warn "You must use /etc/config/olsrd and /etc/init.d/olsrd for IPv4"
-               warn " /etc/config/olsrd6 and /etc/init.d/olsrd6 for IPv6"
-
-               bindv6only="$(sysctl -n net.ipv6.bindv6only)"
-               sysctl -w net.ipv6.bindv6only=1 > /dev/null
-               sed -e '/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/d' < "$OLSRD_CONFIG_FILE" > /var/etc/olsrd.conf.ipv6
-               sed -e 's/^IpVersion[ ][ ]*6$/IpVersion 4/' -e 's/^\t\t[A-Fa-f0-9.:]*[:][A-Fa-f0-9.:]*[ ][0-9]*$//' < "$OLSRD_CONFIG_FILE" > /var/etc/olsrd.conf.ipv4
-               rm $OLSRD_CONFIG_FILE
-
-               # some filenames should get the suffix .ipv6
-               for file in $latlon_file $hosts_file $services_file $resolv_file $macs_file $wd_file;do
-                       f=$(echo $file|sed 's/\//\\\//g')
-                       sed -i "s/$f/$f.ipv6/g" /var/etc/olsrd.conf.ipv6
-               done
-
-               SERVICE_PID_FILE="$PID6"
-               if service_check /usr/sbin/olsrd; then
-                       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 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
-
-               sleep 3
-               sysctl -w net.ipv6.bindv6only="$bindv6only" > /dev/null
+       SERVICE_PID_FILE="$PID"
+       if service_check /usr/sbin/olsrd; then
+               error "there is already an instance of $UCI_CONF_NAME running (pid: '$(cat $PID)'), not starting."
+               return 1
        else
-               SERVICE_PID_FILE="$PID"
-               if service_check /usr/sbin/olsrd; then
-                       error "there is already an instance of $UCI_CONF_NAME running (pid: '$(cat $PID)'), not starting."
-                       return 1
-               else
-                       service_start /usr/sbin/olsrd -f "$OLSRD_CONFIG_FILE" -nofork
-                       sleep 1
-                       service_check /usr/sbin/olsrd || {
-                               log "startup-error: check via: '/usr/sbin/olsrd -f \"$OLSRD_CONFIG_FILE\" -nofork'"
-                       }
-               fi
+               service_start /usr/sbin/olsrd -f "$OLSRD_CONFIG_FILE" -nofork
+               sleep 1
+               service_check /usr/sbin/olsrd || {
+                       log "startup-error: check via: '/usr/sbin/olsrd -f \"$OLSRD_CONFIG_FILE\" -nofork'"
+               }
        fi
 
        olsrd_setup_smartgw_rules
@@ -855,8 +805,4 @@ start() {
 stop() {
        SERVICE_PID_FILE="$PID"
        service_stop /usr/sbin/olsrd
-
-       #6and4: backward compatibility
-       SERVICE_PID_FILE="$PID6"
-       service_stop /usr/sbin/olsrd
 }