]> git.99rst.org Git - openwrt-packages.git/commitdiff
watchcat: resolve logical interface names
authorDharmik Parmar <redacted>
Thu, 2 Jul 2026 03:31:19 +0000 (09:01 +0530)
committerJosef Schlehofer <redacted>
Thu, 9 Jul 2026 06:12:14 +0000 (08:12 +0200)
Allow logical interface references in watchcat interface options.

Resolve those names to real netifd devices for ping -I.

Keep logical names for ifup and real-device find_config behavior.

Use separate helpers for ping and restart values to avoid eval.

Treat ModemManager mm_iface_name=null as empty, not a modem name.

Signed-off-by: Dharmik Parmar <redacted>
utils/watchcat/Makefile
utils/watchcat/files/watchcat.sh

index d43f1d21e9e0afd2c5f1dc3b27410b66a87c87e0..ee03a5a37611a84e5d90d1b09fc7b5cd6e8a836a 100644 (file)
@@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
 
 PKG_NAME:=watchcat
 PKG_VERSION:=1
-PKG_RELEASE:=24
+PKG_RELEASE:=25
 
 PKG_MAINTAINER:=Roger D <rogerdammit@gmail.com>
 PKG_LICENSE:=GPL-2.0
index e0ecb5cf88a4d516bb919758462a09c57546250b..87c9678caae1b10c8af957427895dfa9dc9ce5d8 100644 (file)
@@ -7,6 +7,77 @@
 #
 
 . /lib/network/config.sh
+. /lib/functions/network.sh
+
+# Accept the historical real-device input while also handling @logical
+# interface references used by other OpenWrt configs.
+watchcat_resolve_ping_iface() {
+       local iface="$1"
+       local logical device network
+
+       [ -n "$iface" ] || return 1
+
+       case "$iface" in
+       @*)
+               logical="${iface#@}"
+               [ -n "$logical" ] || return 1
+               if network_get_device device "$logical"; then
+                       printf '%s\n' "$device"
+                       return 0
+               fi
+               printf '%s\n' "$iface"
+               return 1
+               ;;
+       esac
+
+       network="$(find_config "$iface")"
+       if [ -n "$network" ]; then
+               printf '%s\n' "$iface"
+               return 0
+       fi
+
+       if network_get_device device "$iface"; then
+               printf '%s\n' "$device"
+               return 0
+       fi
+
+       printf '%s\n' "$iface"
+       return 1
+}
+
+watchcat_resolve_restart_iface() {
+       local iface="$1"
+       local network device
+
+       [ -n "$iface" ] || return 1
+
+       case "$iface" in
+       @*)
+               network="${iface#@}"
+               [ -n "$network" ] || return 1
+               if ! network_get_device device "$network"; then
+                       printf '%s\n' "$network"
+                       return 1
+               fi
+               printf '%s\n' "$network"
+               return 0
+               ;;
+       esac
+
+       network="$(find_config "$iface")"
+       if [ -n "$network" ]; then
+               printf '%s\n' "$network"
+               return 0
+       fi
+
+       if network_get_device device "$iface"; then
+               printf '%s\n' "$iface"
+               return 0
+       fi
+
+       printf '%s\n' "$iface"
+       return 1
+}
 
 get_ping_size() {
        ps=$1
@@ -84,8 +155,11 @@ watchcat_restart_modemmanager_iface() {
 }
 
 watchcat_restart_network_iface() {
-       local network
-       network="$(find_config "$1")"
+       local iface="$1"
+       local network="$2"
+
+       [ -z "$network" ] && network="$(watchcat_resolve_restart_iface "$iface")"
+
        logger -p daemon.info -t "watchcat[$$]" "Restarting network interface: \"$1\" (network: \"$network\")."
        ifup "$network"
 }
@@ -110,11 +184,26 @@ watchcat_monitor_network() {
        mm_iface_unlock_bands="$7"
        address_family="$8"
        script="$9"
+       ping_iface=""
+       restart_iface=""
        reset_failure_timer=""
        if [ "$#" -gt 9 ]; then
                shift 9
                reset_failure_timer="$1"
        fi
+       [ "$mm_iface_name" = "null" ] && mm_iface_name=""
+       if [ "$iface" != "" ]; then
+               if ! ping_iface="$(watchcat_resolve_ping_iface "$iface")"; then
+                       logger -p daemon.warn -t "watchcat[$$]" "Could not resolve interface \"$iface\" for pinging."
+                       case "$iface" in
+                       @*) ping_iface="" ;;
+                       *) ping_iface="$iface" ;;
+                       esac
+               fi
+               if ! restart_iface="$(watchcat_resolve_restart_iface "$iface")"; then
+                       logger -p daemon.warn -t "watchcat[$$]" "Could not resolve interface \"$iface\" for restart."
+               fi
+       fi
 
        time_now="$(cat /proc/uptime)"
        time_now="${time_now%%.*}"
@@ -143,9 +232,9 @@ watchcat_monitor_network() {
                time_lastcheck="$time_now"
 
                for host in $ping_hosts; do
-                       if [ "$iface" != "" ]; then
+                       if [ "$ping_iface" != "" ]; then
                                ping_result="$(
-                                       ping $ping_family -I "$iface" -s "$ping_size" -c 1 "$host" &> /dev/null
+                                       ping $ping_family -I "$ping_iface" -s "$ping_size" -c 1 "$host" &> /dev/null
                                        echo $?
                                )"
                        else
@@ -178,7 +267,7 @@ watchcat_monitor_network() {
                                        watchcat_restart_modemmanager_iface "$mm_iface_name" "$mm_iface_unlock_bands"
                                fi
                                if [ "$iface" != "" ]; then
-                                       watchcat_restart_network_iface "$iface"
+                                       watchcat_restart_network_iface "$iface" "$restart_iface"
                                else
                                        watchcat_restart_all_network
                                fi
@@ -207,6 +296,16 @@ watchcat_ping() {
        ping_size="$5"
        address_family="$6"
        iface="$7"
+       ping_iface=""
+       if [ "$iface" != "" ]; then
+               if ! ping_iface="$(watchcat_resolve_ping_iface "$iface")"; then
+                       logger -p daemon.warn -t "watchcat[$$]" "Could not resolve interface \"$iface\" for pinging."
+                       case "$iface" in
+                       @*) ping_iface="" ;;
+                       *) ping_iface="$iface" ;;
+                       esac
+               fi
+       fi
 
        time_now="$(cat /proc/uptime)"
        time_now="${time_now%%.*}"
@@ -235,9 +334,9 @@ watchcat_ping() {
                time_lastcheck="$time_now"
 
                for host in $ping_hosts; do
-                       if [ "$iface" != "" ]; then
+                       if [ "$ping_iface" != "" ]; then
                                ping_result="$(
-                                       ping $ping_family -I "$iface" -s "$ping_size" -c 1 "$host" &> /dev/null
+                                       ping $ping_family -I "$ping_iface" -s "$ping_size" -c 1 "$host" &> /dev/null
                                        echo $?
                                )"
                        else
git clone https://git.99rst.org/PROJECT