From 2dac1bcdc969a940fa34396db89ae3f1f2a3073f Mon Sep 17 00:00:00 2001 From: Florian Eckert Date: Wed, 22 Jul 2020 09:19:55 +0200 Subject: [PATCH] mwan3: fix race condition on mwan3 restart This adjustment of the locks fixes the race condition when a mwan3 hotplug script and a mwan3 command are running at the same time. Signed-off-by: Florian Eckert --- net/mwan3/files/etc/hotplug.d/iface/15-mwan3 | 14 +++++++----- net/mwan3/files/etc/hotplug.d/iface/16-mwan3 | 11 +++++++--- .../files/etc/hotplug.d/iface/16-mwan3-user | 16 ++++++++++++-- net/mwan3/files/usr/sbin/mwan3 | 22 ++++++++++++++----- 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/net/mwan3/files/etc/hotplug.d/iface/15-mwan3 b/net/mwan3/files/etc/hotplug.d/iface/15-mwan3 index 5bfbd24624..db2dc237a9 100644 --- a/net/mwan3/files/etc/hotplug.d/iface/15-mwan3 +++ b/net/mwan3/files/etc/hotplug.d/iface/15-mwan3 @@ -12,19 +12,24 @@ if [ "$ACTION" == "ifup" ]; then [ -n "$DEVICE" ] || exit 3 fi +mwan3_lock "$ACTION" "$INTERFACE" config_load mwan3 config_get_bool enabled globals 'enabled' '0' -[ ${enabled} -gt 0 ] || exit 0 +[ ${enabled} -gt 0 ] || { + mwan3_unlock "$ACTION" "$INTERFACE" + exit 0 +} -mwan3_lock "$ACTION" "$INTERFACE" mwan3_init mwan3_set_connected_iptables mwan3_set_custom_ipset -mwan3_unlock "$ACTION" "$INTERFACE" config_get enabled $INTERFACE enabled 0 config_get initial_state $INTERFACE initial_state "online" -[ "$enabled" == "1" ] || exit 0 +[ "${enabled}" == "1" ] || { + mwan3_unlock "$ACTION" "$INTERFACE" + exit 0 +} if [ "$ACTION" == "ifup" ]; then config_get family $INTERFACE family ipv4 @@ -58,7 +63,6 @@ else running=1 fi -mwan3_lock "$ACTION" "$INTERFACE" $LOG notice "Execute "$ACTION" event on interface $INTERFACE (${DEVICE:-unknown})" case "$ACTION" in diff --git a/net/mwan3/files/etc/hotplug.d/iface/16-mwan3 b/net/mwan3/files/etc/hotplug.d/iface/16-mwan3 index c243d55ff8..d68401fedc 100644 --- a/net/mwan3/files/etc/hotplug.d/iface/16-mwan3 +++ b/net/mwan3/files/etc/hotplug.d/iface/16-mwan3 @@ -4,14 +4,17 @@ . /lib/functions/network.sh . /lib/mwan3/mwan3.sh +mwan3_lock "$ACTION" "mwan3rtmon" + config_load mwan3 config_get_bool enabled globals 'enabled' '0' -[ ${enabled} -gt 0 ] || exit 0 +[ ${enabled} -gt 0 ] || { + mwan3_unlock "$ACTION" "mwan3rtmon" + exit 0 +} if [ "$ACTION" == "ifup" ]; then - mwan3_lock "$ACTION" "mwan3rtmon" mwan3_rtmon - mwan3_unlock "$ACTION" "mwan3rtmon" fi config_get enabled $INTERFACE enabled 0 @@ -19,4 +22,6 @@ config_get enabled $INTERFACE enabled 0 mwan3_flush_conntrack "$INTERFACE" "$ACTION" } +mwan3_unlock "$ACTION" "mwan3rtmon" + exit 0 diff --git a/net/mwan3/files/etc/hotplug.d/iface/16-mwan3-user b/net/mwan3/files/etc/hotplug.d/iface/16-mwan3-user index 9372c736e1..ef490e1833 100644 --- a/net/mwan3/files/etc/hotplug.d/iface/16-mwan3-user +++ b/net/mwan3/files/etc/hotplug.d/iface/16-mwan3-user @@ -2,13 +2,25 @@ [ -f "/etc/mwan3.user" ] && { . /lib/functions.sh + . /lib/mwan3/mwan3.sh + + mwan3_lock "$ACTION" "user" config_load mwan3 config_get_bool enabled globals 'enabled' '0' - [ ${enabled} -gt 0 ] || exit 0 + [ ${enabled} -gt 0 ] || { + mwan3_unlock "$ACTION" "user" + exit 0 + } config_get enabled "$INTERFACE" enabled 0 - [ "${enabled}" = "1" ] || exit 0 + [ "${enabled}" = "1" ] || { + mwan3_unlock "$ACTION" "user" + exit 0 + } + + mwan3_unlock "$ACTION" "user" + env -i ACTION="$ACTION" INTERFACE="$INTERFACE" DEVICE="$DEVICE" \ /bin/sh /etc/mwan3.user } diff --git a/net/mwan3/files/usr/sbin/mwan3 b/net/mwan3/files/usr/sbin/mwan3 index b9a5afb9e4..dc65279bec 100755 --- a/net/mwan3/files/usr/sbin/mwan3 +++ b/net/mwan3/files/usr/sbin/mwan3 @@ -45,30 +45,39 @@ ifup() { local device enabled up l3_device status + mwan3_lock "command" "mwan3" + config_load mwan3 config_get_bool enabled globals 'enabled' 0 + [ ${enabled} -gt 0 ] || { echo "The service mwan3 is global disabled." echo "Please execute \"/etc/init.d/mwan3 start\" first." + mwan3_unlock "command" "mwan3" exit 1 } if [ -z "$1" ]; then - echo "Expecting interface. Usage: mwan3 ifup " && exit 0 + echo "Expecting interface. Usage: mwan3 ifup " + mwan3_unlock "command" "mwan3" + exit 0 fi if [ -n "$2" ]; then - echo "Too many arguments. Usage: mwan3 ifup " && exit 0 + echo "Too many arguments. Usage: mwan3 ifup " + mwan3_unlock "command" "mwan3" + exit 0 fi + config_get enabled "$1" enabled 0 + mwan3_unlock "command" "mwan3" + status=$(ubus -S call network.interface.$1 status) [ -n "$status" ] && { json_load "$status" json_get_vars up l3_device } - config_get enabled "$1" enabled 0 - if [ "$up" = "1" ] \ && [ -n "$l3_device" ] \ && [ "$enabled" = "1" ]; then @@ -130,7 +139,9 @@ start() { local enabled + mwan3_lock "command" "mwan3" uci_toggle_state mwan3 globals enabled "1" + mwan3_unlock "command" "mwan3" config_load mwan3 config_foreach ifup interface @@ -141,6 +152,7 @@ stop() local ipset route rule table IP IPT pid mwan3_lock "command" "mwan3" + uci_toggle_state mwan3 globals enabled "0" for pid in $(pgrep -f "mwan3rtmon"); do kill -TERM "$pid" > /dev/null 2>&1 @@ -200,8 +212,6 @@ stop() mwan3_lock_clean rm -rf $MWAN3_STATUS_DIR $MWAN3TRACK_STATUS_DIR - - uci_toggle_state mwan3 globals enabled "0" } restart() { -- 2.30.2