From b7e26dd431a35a28b64c82ad3bde1fd996af6d39 Mon Sep 17 00:00:00 2001 From: Aaron Goodman Date: Mon, 31 Aug 2020 19:15:09 -0400 Subject: [PATCH] mwan3: fixup some extra spaces and shellcheck warnings Signed-off-by: Aaron Goodman --- net/mwan3/files/etc/hotplug.d/iface/15-mwan3 | 6 +- net/mwan3/files/lib/mwan3/common.sh | 2 +- net/mwan3/files/lib/mwan3/mwan3.sh | 112 ++++++++++--------- net/mwan3/files/usr/sbin/mwan3 | 21 ++-- net/mwan3/files/usr/sbin/mwan3rtmon | 9 +- net/mwan3/files/usr/sbin/mwan3track | 34 +++--- 6 files changed, 92 insertions(+), 92 deletions(-) diff --git a/net/mwan3/files/etc/hotplug.d/iface/15-mwan3 b/net/mwan3/files/etc/hotplug.d/iface/15-mwan3 index 6a7e95ac4..803d3ea27 100644 --- a/net/mwan3/files/etc/hotplug.d/iface/15-mwan3 +++ b/net/mwan3/files/etc/hotplug.d/iface/15-mwan3 @@ -7,9 +7,9 @@ . /lib/mwan3/common.sh SCRIPTNAME="mwan3-hotplug" -[ "$ACTION" = "ifup" ] || [ "$ACTION" = "ifdown" ] || [ "$ACTION" = "connected" ] || [ "$ACTION" = "disconnected" ] || exit 1 +[ "$ACTION" = "ifup" ] || [ "$ACTION" = "ifdown" ] || [ "$ACTION" = "connected" ] || [ "$ACTION" = "disconnected" ] || exit 1 [ -n "$INTERFACE" ] || exit 2 -if ( [ "$ACTION" = "ifup" ] || [ "$ACTION" = "connected" ] ) && [ -z "$DEVICE" ]; then +if { [ "$ACTION" = "ifup" ] || [ "$ACTION" = "connected" ] ; } && [ -z "$DEVICE" ]; then LOG notice "$ACTION called on $INTERFACE with no device set" exit 3 fi @@ -62,7 +62,7 @@ fi binary_status=$status [ "$binary_status" = "online" ] || binary_status=offline -LOG notice "Execute "$ACTION" event on interface $INTERFACE (${DEVICE:-unknown})" +LOG notice "Execute $ACTION event on interface $INTERFACE (${DEVICE:-unknown})" case "$ACTION" in ifup|connected) diff --git a/net/mwan3/files/lib/mwan3/common.sh b/net/mwan3/files/lib/mwan3/common.sh index bb26327d5..62a736b37 100644 --- a/net/mwan3/files/lib/mwan3/common.sh +++ b/net/mwan3/files/lib/mwan3/common.sh @@ -13,5 +13,5 @@ LOG() # when this release is out of beta, the comment in the line below # should be removed [ "$facility" = "debug" ] && return - logger -t "$SCRIPTNAME[$$]" -p $facility "$*" + logger -t "${SCRIPTNAME}[$$]" -p $facility "$*" } diff --git a/net/mwan3/files/lib/mwan3/mwan3.sh b/net/mwan3/files/lib/mwan3/mwan3.sh index 14af3ded2..e9b3feda8 100644 --- a/net/mwan3/files/lib/mwan3/mwan3.sh +++ b/net/mwan3/files/lib/mwan3/mwan3.sh @@ -50,7 +50,9 @@ $*"; mwan3_update_dev_to_table() { local _tid + # shellcheck disable=SC2034 mwan3_dev_tbl_ipv4=" " + # shellcheck disable=SC2034 mwan3_dev_tbl_ipv6=" " update_table() @@ -99,7 +101,7 @@ mwan3_route_line_dev() { # must have mwan3 config already loaded # arg 1 is route device - local _tid route_line route_device route_family entry curr_table + local _tid route_line route_device route_family entry curr_table route_line=$2 route_family=$3 route_device=$(echo "$route_line" | sed -ne "s/.*dev \([^ ]*\).*/\1/p") @@ -170,7 +172,7 @@ mwan3_init() bitcnt=$(mwan3_count_one_bits MMX_MASK) mmdefault=$(((1< /dev/null)" ] && \ [ -n "$($IP route list table $id default dev $device 2> /dev/null)" ]; then json_init @@ -1183,11 +1184,12 @@ mwan3_report_iface_status() json_get_vars online uptime json_select .. json_select .. - online="$(printf '%02dh:%02dm:%02ds\n' $(($online/3600)) $(($online%3600/60)) $(($online%60)))" - uptime="$(printf '%02dh:%02dm:%02ds\n' $(($uptime/3600)) $(($uptime%3600/60)) $(($uptime%60)))" + online="$(printf '%02dh:%02dm:%02ds\n' $((online/3600)) $((online%3600/60)) $((online%60)))" + uptime="$(printf '%02dh:%02dm:%02ds\n' $((uptime/3600)) $((uptime%3600/60)) $((uptime%60)))" result="$(mwan3_get_iface_hotplug_state $1) $online, uptime $uptime" - elif [ -n "$($IP rule | awk '$1 == "'$(($id+1000)):'"')" ] || \ - [ -n "$($IP rule | awk '$1 == "'$(($id+2000)):'"')" ] || \ + elif [ -n "$($IP rule | awk '$1 == "'$((id+1000)):'"')" ] || \ + [ -n "$($IP rule | awk '$1 == "'$((id+2000)):'"')" ] || \ + [ -n "$($IP rule | awk '$1 == "'$((id+3000)):'"')" ] || \ [ -n "$($IPT -S mwan3_iface_in_$1 2> /dev/null)" ] || \ [ -n "$($IP route list table $id default dev $device 2> /dev/null)" ]; then result="error" @@ -1225,10 +1227,10 @@ mwan3_report_policies() total_weight=$($ipt -S "$policy" | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | head -1 | awk '{print $3}') - if [ ! -z "${total_weight##*[!0-9]*}" ]; then + if [ -n "${total_weight##*[!0-9]*}" ]; then for iface in $($ipt -S "$policy" | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | awk '{print $1}'); do weight=$($ipt -S "$policy" | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | awk '$1 == "'$iface'"' | awk '{print $2}') - percent=$(($weight*100/$total_weight)) + percent=$((weight*100/total_weight)) echo " $iface ($percent%)" done else @@ -1306,6 +1308,6 @@ mwan3_flush_conntrack() mwan3_track_clean() { - rm -rf "$MWAN3_STATUS_DIR/${1}" &> /dev/null + rm -rf "${MWAN3_STATUS_DIR:?}/${1}" &> /dev/null rmdir --ignore-fail-on-non-empty "$MWAN3_STATUS_DIR" } diff --git a/net/mwan3/files/usr/sbin/mwan3 b/net/mwan3/files/usr/sbin/mwan3 index fd6b5204f..af66a70e1 100755 --- a/net/mwan3/files/usr/sbin/mwan3 +++ b/net/mwan3/files/usr/sbin/mwan3 @@ -44,7 +44,7 @@ ifdown() ifup() { - local device enabled up l3_device status interface true_iface + local enabled up l3_device status interface true_iface if [ -z "$1" ]; then echo "Expecting interface. Usage: mwan3 ifup " @@ -104,40 +104,40 @@ interfaces() echo "Interface status:" config_foreach mwan3_report_iface_status interface - echo -e + echo } policies() { echo "Current ipv4 policies:" mwan3_report_policies_v4 - echo -e + echo [ $NO_IPV6 -ne 0 ] && return echo "Current ipv6 policies:" mwan3_report_policies_v6 - echo -e + echo } connected() { echo "Directly connected ipv4 networks:" mwan3_report_connected_v4 - echo -e + echo [ $NO_IPV6 -ne 0 ] && return echo "Directly connected ipv6 networks:" mwan3_report_connected_v6 - echo -e + echo } rules() { echo "Active ipv4 user rules:" mwan3_report_rules_v4 - echo -e + echo [ $NO_IPV6 -ne 0 ] && return echo "Active ipv6 user rules:" mwan3_report_rules_v6 - echo -e + echo } status() @@ -205,12 +205,12 @@ stop() IP="$IP6" fi - for tid in $(ip route list table all | sed -ne 's/.*table \([0-9]\+\).*/\1/p'|sort -u); do + for tid in $(ip route list table all | sed -ne 's/.*table \([0-9]\+\).*/\1/p' | sort -u); do [ $tid -gt $MWAN3_INTERFACE_MAX ] && continue $IP route flush table $tid &> /dev/null done - for rule in $($IP rule list | egrep '^[1-2][0-9]{3}\:' | cut -d ':' -f 1); do + for rule in $($IP rule list | grep -E '^[1-3][0-9]{3}\:' | cut -d ':' -f 1); do $IP rule del pref $rule &> /dev/null done table="$($IPT -S)" @@ -255,6 +255,7 @@ restart() { case "$1" in ifup|ifdown|interfaces|policies|connected|rules|status|start|stop|restart) mwan3_init + # shellcheck disable=SC2048 $* ;; *) diff --git a/net/mwan3/files/usr/sbin/mwan3rtmon b/net/mwan3/files/usr/sbin/mwan3rtmon index 98a5c4b89..8a7da7a8f 100755 --- a/net/mwan3/files/usr/sbin/mwan3rtmon +++ b/net/mwan3/files/usr/sbin/mwan3rtmon @@ -8,8 +8,7 @@ mwan3_rtmon_route_handle() { config_load mwan3 - local section action route_line family tbl device metric tos dst line - local route_device tid + local section action route_line family tbl device metric tos dst line tid route_line=${1##"Deleted "} route_family=$2 @@ -48,7 +47,7 @@ mwan3_rtmon_route_handle() [ $action = "add" ] && [ -z "${tbl##*$route_line*}" ] && return [ $action = "del" ] && [ -n "${tbl##*$route_line*}" ] && return network_get_device device "$section" - LOG debug "adjusting route $device: $IP route "$action" table $tid $route_line" + LOG debug "adjusting route $device: $IP route \"$action\" table $tid $route_line" $IP route "$action" table $tid $route_line || LOG warn "failed: $IP route $action table $tid $route_line" } @@ -69,7 +68,7 @@ mwan3_rtmon_route_handle() dst=${route_line%% *} grep_line="$dst ${tos:+tos $tos}.*table [0-9].*${metric:+metric $metric}" - $IP route list table all | grep "$grep_line" | while read line; do + $IP route list table all | grep "$grep_line" | while read -r line; do tbl=${line##*table } tbl=${tbl%% *} [ $tbl -gt $MWAN3_INTERFACE_MAX ] && continue @@ -99,7 +98,7 @@ main() fi mwan3_init - $IP monitor route | while read line; do + $IP monitor route | while read -r line; do [ -z "${line##*table*}" ] && continue LOG debug "handling route update $family $line" mwan3_lock "service" "mwan3rtmon" diff --git a/net/mwan3/files/usr/sbin/mwan3track b/net/mwan3/files/usr/sbin/mwan3track index 5cbf5f75d..a48df72b5 100755 --- a/net/mwan3/files/usr/sbin/mwan3track +++ b/net/mwan3/files/usr/sbin/mwan3track @@ -44,7 +44,7 @@ validate_track_method() { LOG warn "Missing httping. Please install httping package." return 1 } - [ -n "$2" -a "$2" != "0.0.0.0" -a "$2" != "::" ] || { + [ -n "$2" ] && { [ "$2" = "0.0.0.0" ] || [ "$2" = "::" ]; } && { LOG warn "Cannot determine source IP for the interface which is required by httping." return 1 } @@ -64,10 +64,10 @@ validate_track_method() { disconnected() { echo "offline" > /var/run/mwan3track/$INTERFACE/STATUS - echo "$(get_uptime)" > /var/run/mwan3track/$INTERFACE/OFFLINE + get_uptime > /var/run/mwan3track/$INTERFACE/OFFLINE echo "0" > /var/run/mwan3track/$INTERFACE/ONLINE score=0 - [ "$1" == 1 ] && return + [ "$1" = 1 ] && return LOG notice "Interface $INTERFACE ($DEVICE) is offline" env -i ACTION="disconnected" INTERFACE="$INTERFACE" DEVICE="$DEVICE" /sbin/hotplug-call iface } @@ -75,12 +75,12 @@ disconnected() { connected() { echo "online" > /var/run/mwan3track/$INTERFACE/STATUS echo "0" > /var/run/mwan3track/$INTERFACE/OFFLINE - echo "$(get_uptime)" > /var/run/mwan3track/$INTERFACE/ONLINE + get_uptime > /var/run/mwan3track/$INTERFACE/ONLINE host_up_count=0 lost=0 turn=0 loss=0 - [ "$1" == 1 ] && return + [ "$1" = 1 ] && return LOG notice "Interface $INTERFACE ($DEVICE) is online" env -i ACTION="connected" INTERFACE="$INTERFACE" DEVICE="$DEVICE" /sbin/hotplug-call iface } @@ -94,11 +94,9 @@ firstconnect() { } update_status() { - local status track_ip - track_ip=$1 - status=$2 + local track_ip=$1 - echo "$1" > /var/run/mwan3track/$INTERFACE/TRACK_${track_ip} + echo "$2" > /var/run/mwan3track/$INTERFACE/TRACK_${track_ip} [ -z "$3" ] && return echo "$3" > /var/run/mwan3track/$INTERFACE/LATENCY_${track_ip} echo "$4" > /var/run/mwan3track/$INTERFACE/LOSS_${track_ip} @@ -151,13 +149,13 @@ main() { config_get failure_loss $INTERFACE failure_loss 40 config_get recovery_loss $INTERFACE recovery_loss 10 - local score=$(($down+$up)) + local score=$((down+up)) local track_ips=$(echo $* | cut -d ' ' -f 5-99) local host_up_count=0 local lost=0 local turn=0 local ping_protocol=4 - local sleep_time result ping_result ping_result_raw ping_status loss latency + local sleep_time result ping_result ping_result_raw ping_status loss latency firstconnect while true; do @@ -183,7 +181,7 @@ main() { ping_result_raw="$($PING -$ping_protocol -I ${SRC_IP:-$DEVICE} -c $count -W $timeout -s $size -t $max_ttl -q $track_ip 2>/dev/null)" ping_status=$? ping_result=$(echo "$ping_result_raw" | tail -n2) - loss="$(echo "$ping_result" | grep "packet loss" | cut -d "," -f3 | awk '{print $1}' | sed -e 's/%//')" + loss="$(echo "$ping_result" | grep "packet loss" | cut -d "," -f3 | awk '{print $1}' | sed -e 's/%//')" if [ "$ping_status" -ne 0 ] || [ "$loss" -eq 100 ]; then latency=999999 loss=100 @@ -234,14 +232,14 @@ main() { fi fi else - if [ "$loss" -ge "$failure_loss" -o "$latency" -ge "$failure_latency" ]; then + if [ "$loss" -ge "$failure_loss" ] || [ "$latency" -ge "$failure_latency" ]; then let lost++ update_status "$track_ip" "down" $latency $loss if [ $score -gt $up ]; then LOG info "Check (${track_method}: latency=${latency}ms loss=${loss}%) failed for target \"$track_ip\" on interface $INTERFACE ($DEVICE). Current score: $score" fi - elif [ "$loss" -le "$recovery_loss" -a "$latency" -le "$recovery_latency" ]; then + elif [ "$loss" -le "$recovery_loss" ] && [ "$latency" -le "$recovery_latency" ]; then let host_up_count++ update_status "$track_ip" "up" $latency $loss @@ -274,8 +272,8 @@ main() { score=0 fi else - if [ $score -lt $(($down+$up)) ] && [ $lost -gt 0 ]; then - LOG info "Lost $(($lost*$count)) ping(s) on interface $INTERFACE ($DEVICE). Current score: $score" + if [ $score -lt $((down+up)) ] && [ $lost -gt 0 ]; then + LOG info "Lost $((lost*count)) ping(s) on interface $INTERFACE ($DEVICE). Current score: $score" fi let score++ @@ -283,7 +281,7 @@ main() { if [ $score -gt $up ]; then echo "online" > /var/run/mwan3track/$INTERFACE/STATUS - score=$(($down+$up)) + score=$((down+up)) elif [ $score -le $up ]; then sleep_time=$recovery_interval fi @@ -298,7 +296,7 @@ main() { echo "${lost}" > /var/run/mwan3track/$INTERFACE/LOST echo "${score}" > /var/run/mwan3track/$INTERFACE/SCORE echo "${turn}" > /var/run/mwan3track/$INTERFACE/TURN - echo "$(get_uptime)" > /var/run/mwan3track/$INTERFACE/TIME + get_uptime > /var/run/mwan3track/$INTERFACE/TIME host_up_count=0 sleep "${sleep_time}" &