From: Georgios Kontaxis Date: Mon, 22 Jun 2026 15:43:09 +0000 (+0000) Subject: etherwake: wait for the interface to come up before transmitting X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=c8b52a7f271ffea7480e2b55f9c1231164429090;p=openwrt-packages.git etherwake: wait for the interface to come up before transmitting Signed-off-by: Georgios Kontaxis --- diff --git a/net/etherwake/files/etherwake.config b/net/etherwake/files/etherwake.config index cf025dcad..34ac9aa40 100644 --- a/net/etherwake/files/etherwake.config +++ b/net/etherwake/files/etherwake.config @@ -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' diff --git a/net/etherwake/files/etherwake.init b/net/etherwake/files/etherwake.init index 8b8db51b7..3ab0e041c 100644 --- a/net/etherwake/files/etherwake.init +++ b/net/etherwake/files/etherwake.init @@ -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 $? }