#!/bin/sh /etc/rc.common
-START=60
+START=51
USE_PROCD=1
UPSMON_C=/var/etc/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() {
system="$system:$port";
fi
echo "MONITOR $system $powervalue $username $password $type" >> "$UPSMON_C"
+
+ havems=1
}
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
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"
}
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}"
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
}
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"
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"
}
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
}
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
}