etherwake: wait for the interface to come up before transmitting
authorGeorgios Kontaxis <redacted>
Mon, 22 Jun 2026 15:43:09 +0000 (15:43 +0000)
committerGeorgios Kontaxis <redacted>
Mon, 22 Jun 2026 16:09:12 +0000 (16:09 +0000)
Signed-off-by: Georgios Kontaxis <redacted>
net/etherwake/files/etherwake.config
net/etherwake/files/etherwake.init

index cf025dcad91374a649f2fd52bab1775e923b67c0..34ac9aa404c38ba167b493fb97dfa13815f4e7f6 100644 (file)
@@ -1,6 +1,8 @@
 config 'etherwake' 'setup'
        # interface to use, defaults to 'eth0'
        #option 'interface' 'eth0'
+       # wait if the interface is not already up, defaults to 60 secs
+       #option 'interface_timeout' '60'
        # send wake-up packet to the broadcast address, defaults to off
        #option 'broadcast' 'off'
 
index 8b8db51b7c75ec3f40af212a1b6bc6829b34c80e..3ab0e041c10a78733dbc120dca20f0cdacad5c61 100644 (file)
@@ -2,12 +2,13 @@
 # Copyright (C) 2009 OpenWrt.org
 
 NAME='etherwake'
-START=60
+START=99
 PROGRAM='/usr/bin/etherwake'
 
 start()
 {
        local value=''
+       local timeout=''
 
        config_load "${NAME}"
 
@@ -15,6 +16,20 @@ start()
        config_get value 'setup' 'interface' 'eth0'
        [ -n "${value}" ] && append PROGRAM "-i ${value}"
 
+       # interface timeout
+       config_get timeout 'setup' 'interface_timeout'
+       case "$timeout" in
+               ''|*[!0-9]*)
+                       timeout=60
+                       ;;
+       esac
+
+       # wait for interface to come up
+       if ! etherwake_wait_for_iface "${value}" "${timeout}"; then
+               echo "Interface ${value} did not come up within ${timeout} seconds"
+               exit 1
+       fi
+
        # broadcast
        config_get_bool value 'setup' 'broadcast' '0'
        [ "${value}" -ne 0 ] && append PROGRAM '-b'
@@ -23,6 +38,28 @@ start()
        config_foreach etherwake_start target $*
 }
 
+etherwake_wait_for_iface()
+{
+       local iface="$1"
+       local timeout="${2:-1}"
+       local interval=1
+       local elapsed=0
+
+    [ -n "${iface}" ] || return 1
+
+       while [ "${elapsed}" -lt "${timeout}" ]; do
+               if [ "$(ubus call network.interface."${iface}" status 2> /dev/null |
+                       jsonfilter -e '@.up' 2> /dev/null)" = "true" ]; then
+                       return 0
+               fi
+
+               sleep "${interval}"
+               elapsed=$((elapsed + interval))
+       done
+
+       return 1
+}
+
 etherwake_start()
 {
        local section="$1"
@@ -84,11 +121,11 @@ do_etherwake()
 
        # mac address
        config_get value "${section}" 'mac'
-       [ -z "${value}" ] && { echo "${initscript}: Target ${section_name} has no MAC address"; return 1; }
+       [ -z "${value}" ] && { echo "Target ${section_name} has no MAC address"; return 1; }
        append args "${value}"
 
        # execute command
-       echo "${initscript}: Waking up ${section_name} via ${PROGRAM}${args:+ ${args}}"
+       echo "Waking up ${section_name} via ${PROGRAM}${args:+ ${args}}"
        ${PROGRAM} ${args}
        return $?
 }
git clone https://git.99rst.org/PROJECT