From 59d12598b50edbc5a7a1f178c3c291524bd85b13 Mon Sep 17 00:00:00 2001 From: Aaron Goodman Date: Thu, 5 Nov 2020 20:37:37 -0500 Subject: [PATCH] mwan3: use procd for locking everywhere Replace locks on /var/run/mwan3.lock with locks via procd. This fixes a deadlock issue where mwan3 stop would have a procd lock, but a hotplug script would have the /var/run/mwan3.lock Locking can be removed from mwan3rtmon since: 1) procd will have sent the KILL signal to the process during shutdown, so it will not add routes to already removed interfaces on mwan3 shutdown and 2) mwan3rtmon checks if an interface is active based on the mwan3_iface_in_ entry in iptables, and the hotplug script always adds this before creating the route table and removes it before deleting the route table Fixes github issue #13704 (https://github.com/openwrt/packages/issues/13704) --- net/mwan3/files/etc/hotplug.d/iface/15-mwan3 | 16 ++++++---------- .../files/etc/hotplug.d/iface/16-mwan3-user | 7 +++---- net/mwan3/files/etc/init.d/mwan3 | 7 ------- net/mwan3/files/lib/mwan3/mwan3.sh | 10 ---------- net/mwan3/files/usr/sbin/mwan3rtmon | 4 ---- 5 files changed, 9 insertions(+), 35 deletions(-) diff --git a/net/mwan3/files/etc/hotplug.d/iface/15-mwan3 b/net/mwan3/files/etc/hotplug.d/iface/15-mwan3 index 5c60128d42..5fe3e33e0f 100644 --- a/net/mwan3/files/etc/hotplug.d/iface/15-mwan3 +++ b/net/mwan3/files/etc/hotplug.d/iface/15-mwan3 @@ -5,6 +5,10 @@ . /lib/mwan3/mwan3.sh . /lib/mwan3/common.sh +initscript=/etc/init.d/mwan3 +. /lib/functions/procd.sh + + SCRIPTNAME="mwan3-hotplug" [ "$ACTION" = "ifup" ] || [ "$ACTION" = "ifdown" ] || [ "$ACTION" = "connected" ] || [ "$ACTION" = "disconnected" ] || exit 1 [ -n "$INTERFACE" ] || exit 2 @@ -15,18 +19,17 @@ if { [ "$ACTION" = "ifup" ] || [ "$ACTION" = "connected" ] ; } && [ -z "$DEVICE" exit 3 fi -[ "$MWAN3_STARTUP" = 1 ] || mwan3_lock "$ACTION" "$INTERFACE" +[ "$MWAN3_STARTUP" = 1 ] || procd_lock config_load mwan3 /etc/init.d/mwan3 running || { - [ "$MWAN3_STARTUP" = 1 ] || mwan3_unlock "$ACTION" "$INTERFACE" + [ "$MWAN3_STARTUP" = 1 ] || procd_lock LOG notice "mwan3 hotplug $ACTION on $INTERFACE not called because globally disabled" mwan3_flush_conntrack "$INTERFACE" "$ACTION" exit 0 } $IPT4 -S mwan3_hook &>/dev/null || { - mwan3_unlock "$ACTION" "$INTERFACE" LOG warn "hotplug called on $INTERFACE before mwan3 has been set up" exit 0 } @@ -43,7 +46,6 @@ fi config_get_bool enabled $INTERFACE 'enabled' '0' [ "${enabled}" -eq 1 ] || { - [ "$MWAN3_STARTUP" = 1 ] || mwan3_unlock "$ACTION" "$INTERFACE" LOG notice "mwan3 hotplug on $INTERFACE not called because interface disabled" exit 0 } @@ -56,11 +58,6 @@ else status=online fi -if [ "$ACTION" = ifup ] || [ "$ACTION" = ifdown ]; then - initscript=/etc/init.d/mwan3 - . /lib/functions/procd.sh -fi - LOG notice "Execute $ACTION event on interface $INTERFACE (${DEVICE:-unknown})" case "$ACTION" in @@ -93,5 +90,4 @@ case "$ACTION" in mwan3_set_policies_iptables ;; esac -[ "$MWAN3_STARTUP" = 1 ] || mwan3_unlock "$ACTION" "$INTERFACE" 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 698fe09099..be857e8ff6 100644 --- a/net/mwan3/files/etc/hotplug.d/iface/16-mwan3-user +++ b/net/mwan3/files/etc/hotplug.d/iface/16-mwan3-user @@ -3,11 +3,12 @@ [ -f "/etc/mwan3.user" ] && { . /lib/functions.sh . /lib/mwan3/mwan3.sh + initscript=/etc/init.d/mwan3 + . /lib/functions/procd.sh - [ "$MWAN3_SHUTDOWN" != 1 ] && mwan3_lock "$ACTION" "$DEVICE-user" + [ "$MWAN3_SHUTDOWN" != 1 ] && procd_lock [ "$MWAN3_SHUTDOWN" != 1 ] && ! /etc/init.d/mwan3 running && { - mwan3_unlock "$ACTION" "$DEVICE-user" exit 0 } @@ -19,8 +20,6 @@ exit 0 } - [ "$MWAN3_SHUTDOWN" != 1 ] && mwan3_unlock "$ACTION" "$DEVICE-user" - env -i ACTION="$ACTION" INTERFACE="$INTERFACE" DEVICE="$DEVICE" \ /bin/sh /etc/mwan3.user } diff --git a/net/mwan3/files/etc/init.d/mwan3 b/net/mwan3/files/etc/init.d/mwan3 index b7cf91eb17..bd1ea72603 100644 --- a/net/mwan3/files/etc/init.d/mwan3 +++ b/net/mwan3/files/etc/init.d/mwan3 @@ -31,8 +31,6 @@ start_service() { mwan3_init config_foreach start_tracker interface - mwan3_lock "command" "mwan3" - mwan3_update_iface_to_table mwan3_set_connected_ipset mwan3_set_custom_ipset @@ -43,8 +41,6 @@ start_service() { mwan3_set_policies_iptables mwan3_set_user_rules - mwan3_unlock "command" "mwan3" - procd_open_instance rtmon_ipv4 procd_set_param command /usr/sbin/mwan3rtmon ipv4 procd_set_param respawn @@ -61,8 +57,6 @@ start_service() { stop_service() { local ipset rule IP IPTR IPT family table tid - mwan3_lock "command" "mwan3" - config_load mwan3 mwan3_init config_foreach mwan3_interface_shutdown interface @@ -108,7 +102,6 @@ stop_service() { rm -rf $MWAN3_STATUS_DIR $MWAN3TRACK_STATUS_DIR - mwan3_unlock "command" "mwan3" } reload_service() { diff --git a/net/mwan3/files/lib/mwan3/mwan3.sh b/net/mwan3/files/lib/mwan3/mwan3.sh index e6e43ebe85..8a5fe6f0d9 100644 --- a/net/mwan3/files/lib/mwan3/mwan3.sh +++ b/net/mwan3/files/lib/mwan3/mwan3.sh @@ -103,16 +103,6 @@ mwan3_count_one_bits() echo $count } -mwan3_lock() { - lock /var/run/mwan3.lock - #LOG debug "$1 $2 (lock)" -} - -mwan3_unlock() { - #LOG debug "$1 $2 (unlock)" - lock -u /var/run/mwan3.lock -} - mwan3_get_iface_id() { local _tmp diff --git a/net/mwan3/files/usr/sbin/mwan3rtmon b/net/mwan3/files/usr/sbin/mwan3rtmon index 569b4d1729..6ece581256 100644 --- a/net/mwan3/files/usr/sbin/mwan3rtmon +++ b/net/mwan3/files/usr/sbin/mwan3rtmon @@ -159,16 +159,13 @@ main() IP="$IP4" fi mwan3_init - mwan3_lock "mwan3rtmon" "start" sh -c "echo \$\$; exec $IP monitor route" | { read -r monitor_pid trap_with_arg func_trap "$monitor_pid" SIGINT SIGTERM SIGKILL while IFS='' read -r line; do [ -z "${line##*table*}" ] && continue LOG debug "handling route update $family '$line'" - mwan3_lock "service" "mwan3rtmon" mwan3_rtmon_route_handle "$line" "$family" - mwan3_unlock "service" "mwan3rtmon" done } & child=$! @@ -176,7 +173,6 @@ main() trap_with_arg func_trap "$child" SIGINT SIGTERM SIGKILL mwan3_set_connected_${family} mwan3_add_all_routes ${family} - mwan3_unlock "mwan3rtmon" "start" kill -SIGCONT $child wait $! } -- 2.30.2