testnet: avoid potential race condition

Instead of running a second instance of testnet once and in parallel
with the background task on interface up/down, the background task is
woken up. The avoids the potential for a race where under certain
circumstances a ubus event for internet up or down may never be sent.

Also remove unused include and add missing quotation.
This commit is contained in:
Erik Karlsson 2024-03-25 12:01:49 +01:00 committed by Sukru Senli
parent e383d1d145
commit ec7ba74900
2 changed files with 19 additions and 16 deletions

View file

@ -19,5 +19,5 @@ start_service() {
} }
reload_service() { reload_service() {
[ -n "$TESTNET" ] && procd_running testnet && "$TESTNET" once procd_send_signal testnet
} }

View file

@ -1,6 +1,7 @@
#!/bin/sh #!/bin/sh
. /lib/functions.sh RELOAD=0
WAITING=0
NETCON=0 NETCON=0
LASTSTATUS="" LASTSTATUS=""
@ -13,32 +14,32 @@ test_connection() {
local defroute="$(ip r | grep default | awk '{print$3}' | head -1)" local defroute="$(ip r | grep default | awk '{print$3}' | head -1)"
local def6route="$(ip -f inet6 r | grep default | awk '{print$3}')" local def6route="$(ip -f inet6 r | grep default | awk '{print$3}')"
local ping6dev="$(ip -f inet6 r | grep default | awk '{print$5}')" local ping6dev="$(ip -f inet6 r | grep default | awk '{print$5}')"
local resolvfile="$(uci -q get dhcp.@dnsmasq[0].resolvfile)" local resolvfile="$(uci -q get 'dhcp.@dnsmasq[0].resolvfile')"
if [ -n "$addr" ]; then if [ -n "$addr" ]; then
ping -q -w 5 -c 1 $addr >/dev/null 2>&1 && return 0 ping -q -w 5 -c 1 "$addr" >/dev/null 2>&1 && return 0
elif [ -n "$defroute" ]; then elif [ -n "$defroute" ]; then
ping -q -w 5 -c 1 $defroute >/dev/null 2>&1 && return 0 ping -q -w 5 -c 1 "$defroute" >/dev/null 2>&1 && return 0
if [ -e "$resolvfile" ]; then if [ -e "$resolvfile" ]; then
for nmsrv in $(grep nameserver "$resolvfile" | awk '{print$2}'); do for nmsrv in $(grep nameserver "$resolvfile" | awk '{print$2}'); do
ping -q -w 5 -c 1 $nmsrv >/dev/null 2>&1 && return 0 ping -q -w 5 -c 1 "$nmsrv" >/dev/null 2>&1 && return 0
done done
fi fi
elif [ -n "$def6route" -a -n "$ping6dev" ]; then elif [ -n "$def6route" -a -n "$ping6dev" ]; then
ndisc6 -w 5 -1 $def6route $ping6dev >/dev/null 2>&1 && return 0 ndisc6 -w 5 -1 "$def6route" "$ping6dev" >/dev/null 2>&1 && return 0
fi fi
return 1 return 1
} }
internet_test() { internet_test() {
local link dest local dest
# use the destination address given in config for connectivity check # use the destination address given in config for connectivity check
dest="$(uci -q get testnet.global.destination)" dest="$(uci -q get testnet.global.destination)"
# for backwards compatibility # for backwards compatibility
[ -n "$dest" ] || dest="$(uci -q get system.@system[0].netping_addr)" [ -n "$dest" ] || dest="$(uci -q get 'system.@system[0].netping_addr')"
test_connection $dest test_connection "$dest"
if [ "$?" -eq 0 ]; then if [ "$?" -eq 0 ]; then
NETCON=1 NETCON=1
@ -52,7 +53,7 @@ internet_test() {
connectivity_test() { connectivity_test() {
internet_test internet_test
if [ $NETCON -eq 1 ]; then if [ "$NETCON" -eq 1 ]; then
CURSTATUS=1 CURSTATUS=1
[ "$CURSTATUS" == "$LASTSTATUS" ] || ubus send internet '{"status" : "online"}' [ "$CURSTATUS" == "$LASTSTATUS" ] || ubus send internet '{"status" : "online"}'
LASTSTATUS=1 LASTSTATUS=1
@ -63,12 +64,14 @@ connectivity_test() {
fi fi
} }
if [ "$1" == "once" ]; then trap RELOAD=1 HUP
connectivity_test
exit 0
fi
while true; do while true; do
if [ "$WAITING" -eq 0 ]; then
sleep "$SLEEP_TIME" &
WAITING=1
fi
RELOAD=0
connectivity_test connectivity_test
sleep $SLEEP_TIME [ "$RELOAD" -eq 0 ] && wait && WAITING=0
done done