mwan3: use procd for locking everywhere
authorAaron Goodman <aaronjg@stanford.edu>
Fri, 6 Nov 2020 01:37:37 +0000 (20:37 -0500)
committerFlorian Eckert <fe@dev.tdt.de>
Fri, 6 Nov 2020 09:40:57 +0000 (10:40 +0100)
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_<IFACE> 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
net/mwan3/files/etc/hotplug.d/iface/16-mwan3-user
net/mwan3/files/etc/init.d/mwan3
net/mwan3/files/lib/mwan3/mwan3.sh
net/mwan3/files/usr/sbin/mwan3rtmon

index 5c60128d4261faf0aa311988bdf0f6158b77f6ae..5fe3e33e0f1c1b6ad65b0afb5cf53928857f8b2d 100644 (file)
@@ -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
index 698fe0909943cc0d66bbd9aeafe181a745481446..be857e8ff693d0f8f80d24cba038f7944fc665b0 100644 (file)
@@ -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
 }
index b7cf91eb175dbb5fa1e041fb81d4914f2428aa35..bd1ea72603778189d0e798963dfd729874989396 100644 (file)
@@ -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() {
index e6e43ebe8520b49c7e41052495a3fa923c0b1140..8a5fe6f0d98e0815fb375ffafd26d1f9f168674b 100644 (file)
@@ -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
index 569b4d1729d5032c2b9fa6c5de8a4a66166bf76c..6ece58125675b81a649a31e03fa3a79de8b59034 100644 (file)
@@ -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 $!
 }