nut: Various startup fixes for monitor and server
authorDaniel F. Dickinson <redacted>
Wed, 10 Oct 2018 02:07:11 +0000 (22:07 -0400)
committerDaniel F. Dickinson <redacted>
Mon, 19 Nov 2018 21:40:39 +0000 (16:40 -0500)
Various path and permissions fixes to properly allow nut-server and nut-monitor to
start properly.

Signed-off-by: Daniel F. Dickinson <redacted>
net/nut/files/nut-monitor.init
net/nut/files/nut-server.init

index 513d703058b27ebe64f8a5c59213fd8726843f57..21e7002eaad4ab3db2ea71724b6c28cac4ee7a15 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/sh /etc/rc.common
 
-START=60
+START=51
 USE_PROCD=1
 UPSMON_C=/var/etc/nut/upsmon.conf
 
@@ -114,6 +114,8 @@ nut_upsmon_conf() {
 
        config_get_bool val "$cfg" forcessl 0
        if [ -n "$val" ]; then echo "FORCESSL $val" >> "$UPSMON_C"; fi
+
+       havemon=1
 }
 
 nut_upsmon_add() {
@@ -138,6 +140,8 @@ nut_upsmon_add() {
            system="$system:$port";
        fi
        echo "MONITOR $system $powervalue $username $password $type" >> "$UPSMON_C"
+
+       havems=1
 }
 
 build_config() {
@@ -149,19 +153,24 @@ build_config() {
        config_foreach nut_upsmon_add master master
        config_foreach nut_upsmon_add slave slave
 
-       [ -z "$(cat /var/etc/nut/nut.conf)" ] && {
+       [ ! -s "$(cat /var/etc/nut/nut.conf)" ] && {
                echo "MODE=netclient" >>/var/etc/nut/nut.conf
                chmod 640 /var/etc/nut/nut.conf
                chgrp $(id -gn ${runas:-root}) /var/etc/nut/nut.conf
        }
 
-       chmod 640 "$UPSMON_C"
-       chgrp $(id -gn ${runas:-root}) "$UPSMON_C"
+       [ -s "$UPSMON_C" ] && chmod 640 "$UPSMON_C"
+       [ -s "$UPSMON_C" ] && chgrp $(id -gn ${runas:-root}) "$UPSMON_C"
 }
 
 start_service() {
+       local havemon havems
        build_config
-       procd_open_instance
+
+       [ "$havemon" != 1 ] && return
+       [ "$havems" != 1 ] && return
+
+       procd_open_instance "upsmon"
        procd_set_param respawn
        procd_set_param stderr 0
        procd_set_param stdout 1
@@ -172,14 +181,14 @@ start_service() {
 reload_service() {
        if pgrep upsmon >/dev/null 2>/dev/null; then
                build_config
-               upsmon -c reload
+               /usr/sbin/upsmon -c reload
        else
                stop
-               sleep 1
-               start
+               sleep 2
+               start_service
        fi
 }
 
 service_triggers() {
-       procd_add_reload_trigger nut_monitor
+       procd_add_reload_trigger "nut_monitor"
 }
index 73bf30ea183410fad316cb754dab013ba4546d03..54958e23044f3dd466d7c2a75dad5bf277ec44c5 100755 (executable)
@@ -117,7 +117,7 @@ build_server_config() {
 
        chmod 0640 "$USERS_C"
        chmod 0640 "$UPSD_C"
-       chmod 0640 /var/etc/nut/nut.conf
+       chmod 0644 /var/etc/nut/nut.conf
 
        [ -d "${STATEPATH}" ] || {
                mkdir -m 0750 -p "${STATEPATH}"
@@ -128,17 +128,14 @@ build_server_config() {
                chgrp $(id -gn $RUNAS) "$USERS_C"
                chgrp $(id -gn $RUNAS) "$UPSD_C"
        fi
+       haveserver=1
 }
 
 build_driver_config() {
        local cfg="$1"
-       local runas
 
        echo "[$cfg]" >>"$UPS_C"
 
-       config_get runas "$cfg" runas "nut"
-       RUNAS="$runas"
-
        get_write_driver_config "$cfg" driver "usbhid-ups"
        get_write_driver_config "$cfg" port "auto"
        get_write_driver_config "$cfg" mfr
@@ -166,12 +163,18 @@ build_driver_config() {
 }
 
 build_config() {
+       local RUNAS=nut
+       local STATEPATH=/var/run/nut
+
         mkdir -m 0755 -p "$(dirname "$UPS_C")"
        rm -f "$UPS_C"
        echo "# Config file automatically generated from UCI config" > "$UPS_C"
        chmod 0640 "$UPS_C"
 
        config_load nut_server
+       config_foreach upsd_statepath upsd
+
+       config_foreach build_global_driver_config driver_global
        config_foreach build_driver_config driver
        [ -n "$RUNAS" ] && chgrp $(id -gn $RUNAS) "$UPS_C"
 
@@ -181,26 +184,29 @@ build_config() {
 start_driver_instance() {
        local cfg="$1"
        local requested="$2"
-       local RUNAS=nut
        local driver
+       local STATEPATH=/var/run/nut
+       local RUNAS=nut
+
+       [ "$havedriver" != 1 ] && return
 
        # If wanting a specific instance, only start it
-       [ "$requested" != "$cfg" ] && [ x"$requested" != x"" ] && return 0
+       [ "$requested" != "$cfg" ] && [ x"$requested" != x ] && return 0
 
        mkdir -m 0755 -p "$(dirname "$UPS_C")"
 
        [ ! -s "$UPS_C" ] && build_config
 
-
        # Avoid hotplug inadvertenly restarting driver during
        # forced shutdown
        [ -f /var/run/killpower ] && return 0
        [ -d /var/run/nut ] && [ -f /var/run/nut/disable-hotplug ] && return 0
 
 
+       config_foreach upsd_statepath upsd
+
        if [ -n "$RUNAS" ]; then
                chown $RUNAS:$(id -gn $RUNAS) "${STATEPATH}"
-               chown $RUNAS:$(id -gn $RUNAS) "$(dirname "$UPS_C")"
        fi
 
        config_get driver "$cfg" driver "usbhid-ups"
@@ -213,9 +219,11 @@ start_driver_instance() {
 }
 
 start_server_instance() {
-       local RUNAS STATEPATH
+       local RUNAS
        build_config
 
+       [ "$haveserver" != 1 ] && return
+
        procd_open_instance "upsd"
        procd_set_param respawn
        procd_set_param stderr 0
@@ -225,30 +233,26 @@ start_server_instance() {
 }
 
 start_service() {
+       local havedriver haveserver
        local STATEPATH=/var/run/nut
 
        # Avoid hotplug inadvertenly restarting driver during
        # forced shutdown
        [ -f /var/run/killpower ] && return 0
-       [ -d /var/run/nut ] && [ -f /var/run/nut/disable-hotplug ] && return 0
+       [ -f /var/run/nut/disable-hotplug ] && return 0
 
        config_load nut_server
-       config_foreach upsd_statepath upsd
-
-       [ -d "${STATEPATH}" ] || {
-               mkdir -m 0750 -p "${STATEPATH}"
-       }
 
        build_config
        config_foreach start_driver_instance driver "$@"
 
-       [ "$1" != "upsd" ] && [ x"$1" != x"" ] && return 0
        start_server_instance "upsd"
 }
 
 reload_service() {
        stop
        sleep 2
+       local havedriver haveserver
        start
 }
 
git clone https://git.99rst.org/PROJECT