mwan3: return via variable
authorEtienne Champetier <[email protected]>
Fri, 27 Jun 2025 23:18:52 +0000 (19:18 -0400)
committerFlorian Eckert <[email protected]>
Fri, 1 Aug 2025 11:03:01 +0000 (13:03 +0200)
Using $(...) to call an internal function causes a fork().
Pass the name of the return variable as first parameter,
and write to it using 'export -n'.

Signed-off-by: Etienne Champetier <[email protected]>
net/mwan3/files/lib/mwan3/common.sh
net/mwan3/files/lib/mwan3/mwan3.sh
net/mwan3/files/usr/libexec/rpcd/mwan3
net/mwan3/files/usr/sbin/mwan3rtmon

index caf546e66512d5383d9f460f8951d6fa377f627a..b99ccfd0e89b9173e497573bb32c21cff175512b 100644 (file)
@@ -94,7 +94,7 @@ readfile() {
 
 mwan3_get_mwan3track_status()
 {
-       local interface=$1
+       local interface=$2
        local track_ips pid cmdline started
        mwan3_list_track_ips()
        {
@@ -103,29 +103,29 @@ mwan3_get_mwan3track_status()
        config_list_foreach "$interface" track_ip mwan3_list_track_ips
 
        if [ -z "$track_ips" ]; then
-               echo "disabled"
+               export -n "$1=disabled"
                return
        fi
        readfile pid $MWAN3TRACK_STATUS_DIR/$interface/PID 2>/dev/null
        if [ -z "$pid" ]; then
-               echo "down"
+               export -n "$1=down"
                return
        fi
        readfile cmdline /proc/$pid/cmdline 2>/dev/null
        if [ $cmdline != "/bin/sh/usr/sbin/mwan3track${interface}" ]; then
-               echo "down"
+               export -n "$1=down"
                return
        fi
        readfile started $MWAN3TRACK_STATUS_DIR/$interface/STARTED
        case "$started" in
                0)
-                       echo "paused"
+                       export -n "$1=paused"
                        ;;
                1)
-                       echo "active"
+                       export -n "$1=active"
                        ;;
                *)
-                       echo "down"
+                       export -n "$1=down"
                        ;;
        esac
 }
@@ -208,17 +208,21 @@ mwan3_count_one_bits()
 }
 
 get_uptime() {
-       local uptime
-       readfile uptime /proc/uptime
-       echo "${uptime%%.*}"
+       local _tmp
+       readfile _tmp /proc/uptime
+       if [ $# -eq 0 ]; then
+               echo "${_tmp%%.*}"
+       else
+               export -n "$1=${_tmp%%.*}"
+       fi
 }
 
 get_online_time() {
        local time_n time_u iface
-       iface="$1"
+       iface="$2"
        readfile time_u "$MWAN3TRACK_STATUS_DIR/${iface}/ONLINE" 2>/dev/null
        [ -z "${time_u}" ] || [ "${time_u}" = "0" ] || {
-               time_n="$(get_uptime)"
-               echo $((time_n-time_u))
+               get_uptime time_n
+               export -n "$1=$((time_n-time_u))"
        }
 }
index c270b8a0e139b1f517ec4d3b9ba6c0452a4d9811..8858f9312181f8487f909bae3080c478890601be 100644 (file)
@@ -1108,7 +1108,7 @@ mwan3_report_iface_status()
        fi
 
        if [ "$status" = "online" ]; then
-               online=$(get_online_time "$1")
+               get_online_time online "$1"
                network_get_uptime uptime "$1"
                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)))"
@@ -1128,7 +1128,7 @@ mwan3_report_iface_status()
                [ "$result" = "0" ] && result=""
        fi
 
-       tracking="$(mwan3_get_mwan3track_status $1)"
+       mwan3_get_mwan3track_status tracking $1
        if [ -n "$result" ]; then
                echo " interface $1 is $status and tracking is $tracking ($result)"
        else
index 30c25501606646e3bd001fad44af327ef517dae4..dbc5f57640b02e1556063e40000ded5add9a55b7 100755 (executable)
@@ -67,21 +67,21 @@ report_policies_v6() {
 
 get_age() {
        local time_p time_u
-       iface="$1"
+       iface="$2"
        readfile time_p "$MWAN3TRACK_STATUS_DIR/${iface}/TIME"
        [ -z "${time_p}" ] || {
-               time_n="$(get_uptime)"
-               echo $((time_n-time_p))
+               get_uptime time_n
+               export -n "$1=$((time_n-time_p))"
        }
 }
 
 get_offline_time() {
        local time_n time_d iface
-       iface="$1"
+       iface="$2"
        readfile time_d "$MWAN3TRACK_STATUS_DIR/${iface}/OFFLINE"
        [ -z "${time_d}" ] || [ "${time_d}" = "0" ] || {
-               time_n="$(get_uptime)"
-               echo $((time_n-time_d))
+               get_uptime time_n
+               export -n "$1=$((time_n-time_d))"
        }
 }
 
@@ -98,11 +98,11 @@ get_mwan3_status() {
                return
        fi
 
-       track_status="$(mwan3_get_mwan3track_status "$1")"
+       mwan3_get_mwan3track_status track_status "$1"
        [ "$track_status" = "active" ] && running="1"
-       age=$(get_age "$iface")
-       online=$(get_online_time "$iface")
-       offline=$(get_offline_time "$iface")
+       get_age age "$iface"
+       get_online_time online "$iface"
+       get_offline_time offline "$iface"
 
        config_get_bool enabled "$iface" enabled 0
 
index 5f21d0203c7a271336c364371dff6d2299c79909..cb7e3492f8c772440f8eafbb0b5e0bbd575bd65a 100755 (executable)
@@ -107,7 +107,10 @@ mwan3_rtmon_route_handle()
                local iface=$1
                tbl=$($IP route list table $tid 2>/dev/null)$'\n'
 
-               if [ -n "$iface" ] && [ "$(mwan3_get_mwan3track_status $iface)" != "active" ]; then
+               local status
+               mwan3_get_mwan3track_status status $iface
+
+               if [ -n "$iface" ] && [ "$status" != "active" ]; then
                        LOG debug "interface $iface is disabled - skipping '$route_line'";
                        return
                fi