nut: Fix upsmon init actions
authorDaniel F. Dickinson <cshored@thecshore.com>
Tue, 27 Aug 2019 03:43:16 +0000 (23:43 -0400)
committerDaniel F. Dickinson <cshored@thecshore.com>
Tue, 27 Aug 2019 03:43:16 +0000 (23:43 -0400)
1) For upsmon start and stop were at wrong position in rc.d
2) Stop needs more than just killing the procd instead but rather
needs a  stop command to be issued.
3) Interface up/down was causing not to enter a crashloop (we fix this
with procd trigger on interface changes).

Signed-off-by: Daniel F. Dickinson <cshored@thecshore.com>
net/nut/files/nut-monitor.init

index f094a82bae96ad9f8f10aa81df6408c1a3af2a23..1e3ab2930f9fc3456e796c0dc2e075bdefd1be8c 100755 (executable)
@@ -1,6 +1,7 @@
 #!/bin/sh /etc/rc.common
 
-START=51
+START=82
+STOP=28
 USE_PROCD=1
 UPSMON_C=/var/etc/nut/upsmon.conf
 
@@ -162,6 +163,32 @@ build_config() {
        [ -s "$UPSMON_C" ] && chgrp $(id -gn ${runas:-root}) "$UPSMON_C"
 }
 
+interface_triggers() {
+       local action="$1"
+       local triggerlist trigger
+
+       config_get triggerlist "upsmon" triggerlist
+
+       . "${IPKG_INSTROOT}"/lib/functions/network.sh
+
+       if [ -n "$triggerlist" ]; then
+               for trigger in $triggerlist; do
+                       if [ "$action" = "add_trigger" ]; then
+                               procd_add_interface_trigger "interface.*" "$trigger" /etc/init.d/nut-monitor restart
+                       else
+                               network_is_up "$trigger" && return 0
+                       fi
+               done
+       else
+               if [ "$action" = "add_trigger" ]; then
+                       procd_add_raw_trigger "interface.*.up" 2000 /etc/init.d/nut-monitor restart
+               else
+                       ubus call network.device status | grep -q '"up": true' && return 0
+               fi
+       fi
+       [ "$action" = "add_trigger" ] || return 1
+}
+
 start_service() {
        local runas
        local havemon havems
@@ -169,11 +196,12 @@ start_service() {
 
        [ "$havemon" != 1 ] && return
        [ "$havems" != 1 ] && return
+       interface_triggers "check_interface_up" || return 0
 
        procd_open_instance "upsmon"
-       procd_set_param respawn
-       procd_set_param stderr 0
-       procd_set_param stdout 1
+       procd_set_param respawn 10 20 6
+       procd_set_param stderr 1
+       procd_set_param stdout 0
        procd_set_param command /usr/sbin/upsmon -D
        procd_close_instance
 }
@@ -184,12 +212,16 @@ reload_service() {
                build_config
                /usr/sbin/upsmon -c reload
        else
-               stop
-               sleep 2
-               start_service
+               restart
        fi
 }
 
+stop_service() {
+       upsmon -c stop
+}
+
 service_triggers() {
+       config_load nut_monitor
+       interface_triggers "add_trigger"
        procd_add_reload_trigger "nut_monitor"
 }