diff --git a/net/nut/Makefile b/net/nut/Makefile index bd96d27b7..afb9c0975 100644 --- a/net/nut/Makefile +++ b/net/nut/Makefile @@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=nut PKG_VERSION:=2.7.4 -PKG_RELEASE:=9 +PKG_RELEASE:=10 PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz PKG_SOURCE_URL:=http://www.networkupstools.org/source/2.7/ @@ -87,8 +87,7 @@ define Package/nut-common DEPENDS:= nut \ +NUT_DRIVER_SNMP:libnetsnmp \ +NUT_DRIVER_USB:libusb-compat \ - +NUT_SSL:libopenssl \ - +PACKAGE_libwrap:libwrap + +NUT_SSL:libopenssl endef define Package/nut-common/description diff --git a/net/nut/files/nut-monitor.init b/net/nut/files/nut-monitor.init index 56c12c0ae..72458f21a 100755 --- a/net/nut/files/nut-monitor.init +++ b/net/nut/files/nut-monitor.init @@ -1,16 +1,19 @@ #!/bin/sh /etc/rc.common -START=51 +START=82 +STOP=28 USE_PROCD=1 UPSMON_C=/var/etc/nut/upsmon.conf nut_upsmon_conf() { local cfg="$1" + local RUNAS val optval echo "# Config file automatically generated from UCI config" > "$UPSMON_C" - config_get runas "$cfg" runas - [ -n "$runas" ] && echo "RUN_AS_USER $runas" >> $UPSMON_C + config_get RUNAS "$cfg" runas + [ -n "$RUNAS" ] && echo "RUN_AS_USER $RUNAS" >> "$UPSMON_C" + runas="$RUNAS" config_get val "$cfg" minsupplies 1 echo "MINSUPPLIES $val" >> "$UPSMON_C" @@ -143,7 +146,6 @@ nut_upsmon_add() { } build_config() { - local runas mkdir -m 0750 -p "$(dirname "$UPSMON_C")" config_load nut_monitor @@ -151,7 +153,7 @@ build_config() { config_foreach nut_upsmon_add master master config_foreach nut_upsmon_add slave slave - [ ! -s "$(cat /var/etc/nut/nut.conf)" ] && { + [ ! -s /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 @@ -161,32 +163,65 @@ 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 build_config [ "$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 } reload_service() { if pgrep upsmon >/dev/null 2>/dev/null; then + local runas 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" } diff --git a/net/nut/files/nut-server.init b/net/nut/files/nut-server.init index d878e3641..5b003fb4c 100755 --- a/net/nut/files/nut-server.init +++ b/net/nut/files/nut-server.init @@ -4,7 +4,8 @@ # This is free software, licensed under the GNU General Public License v2. # See /LICENSE for more information. # -START=50 +START=70 +STOP=30 USERS_C=/var/etc/nut/upsd.users UPSD_C=/var/etc/nut/upsd.conf @@ -31,20 +32,18 @@ get_write_driver_config() { } upsd_statepath() { - local cfg="$1" local statepath - config_get statepath "$cfg" statepath "/var/run/nut" + config_get statepath upsd statepath /var/run/nut STATEPATH="$statepath" } upsd_runas() { - local cfg="$1" local runas - [ -n "$RUNAS" ] && return + [ -n "$RUNAS" ] && return 0 - config_get runas "$cfg" runas + config_get runas upsd runas RUNAS="$runas" } @@ -65,7 +64,7 @@ upsd_config() { config_get runas "$cfg" runas RUNAS="$runas" - config_get statepath "$cfg" statepath "/var/run/nut" + config_get statepath "$cfg" statepath /var/run/nut STATEPATH="$statepath" config_get maxage "$cfg" maxage @@ -111,7 +110,8 @@ nut_user_add() { } build_server_config() { - mkdir -m 0755 -p "$(dirname "$UPSD_C")" + mkdir -p "$(dirname "$UPSD_C")" + chmod 0640 "$UPS_C" rm -f "$USERS_C" rm -f "$UPSD_C" rm -f /var/etc/nut/nut.conf @@ -129,13 +129,14 @@ build_server_config() { chmod 0644 /var/etc/nut/nut.conf [ -d "${STATEPATH}" ] || { - mkdir -m 0750 -p "${STATEPATH}" + mkdir -p "${STATEPATH}" + chmod 0750 "${STATEPATH}" } if [ -n "$RUNAS" ]; then - chown $RUNAS:$(id -gn $RUNAS) "${STATEPATH}" - chgrp $(id -gn $RUNAS) "$USERS_C" - chgrp $(id -gn $RUNAS) "$UPSD_C" + chown "$RUNAS":"$(id -gn "$RUNAS")" "${STATEPATH}" + chgrp "$(id -gn "$RUNAS")" "$USERS_C" + chgrp "$(id -gn "$RUNAS")" "$UPSD_C" fi haveserver=1 } @@ -181,6 +182,7 @@ build_driver_config() { local overvar="$1" local defover="$2" local overtype="$(echo "$overvar" | tr '.' '_')" + local overval config_get overval "${defover}_${overtype}" value [ -n "$overval" ] && echo "${defover}.${overvar} = $overval" >>"$UPS_C" @@ -192,20 +194,19 @@ build_driver_config() { other() { local othervar="$1" local othervarflag="$2" + local otherval if [ "$othervarflag" = "otherflag" ]; then - config_get_bool otherval "${othervarflag}_${overtype}" value - [ "$otherval" = "1" ] && echo "${othervarflag}_${othervar}" >>"$UPS_C" + config_get_bool otherval "${othervarflag}_${othervar}" value + [ "$otherval" = "1" ] && echo "${othervar}" >>"$UPS_C" else - config_get otherval "${othervarflag}_${overtype}" value - [ -n "$otherval" ] && echo "${othervarflag}_${othervar} = $otherval" >>"$UPS_C" + config_get otherval "${othervarflag}_${othervar}" value + [ -n "$otherval" ] && echo "${othervar} = $otherval" >>"$UPS_C" fi } - config_list_foreach "$cfg" override defoverride override - config_list_foreach "$cfg" default defoverride default - config_list_foreach "$cfg" default other other - config_list_foreach "$cfg" default other otherflag + config_list_foreach "$cfg" other other + config_list_foreach "$cfg" other otherflag echo "" >>$UPS_C havedriver=1 } @@ -223,7 +224,6 @@ build_global_driver_config() { get_write_driver_config "$cfg" synchronous config_get runas "$cfg" user RUNAS="$runas" - upsd_runas echo "" >>$UPS_C } @@ -231,19 +231,19 @@ build_global_driver_config() { build_config() { local STATEPATH=/var/run/nut - mkdir -m 0755 -p "$(dirname "$UPS_C")" + mkdir -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 + upsd_runas config_foreach build_global_driver_config driver_global config_foreach build_driver_config driver - [ -n "$RUNAS" ] && chgrp $(id -gn $RUNAS) "$UPS_C" - + upsd_statepath build_server_config + [ -n "$RUNAS" ] && chgrp "$(id -gn "$RUNAS")" "$UPS_C" } start_driver_instance() { @@ -256,22 +256,26 @@ start_driver_instance() { [ "$havedriver" != 1 ] && return # If wanting a specific instance, only start it - [ "$requested" != "$cfg" ] && [ x"$requested" != x ] && return 0 + if [ "$requested" != "$cfg" ] && [ "$request" != "" ]; then + return 0 + fi - mkdir -m 0755 -p "$(dirname "$UPS_C")" + mkdir -p "$(dirname "$UPS_C")" + chmod 0755 "$UPS_C" - [ ! -s "$UPS_C" ] && build_config + upsd_statepath + 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 [ -d /var/run/nut ] && [ -f /var/run/nut/disable-hotplug ]; then + return 0 + fi if [ -n "$RUNAS" ]; then - chown $RUNAS:$(id -gn $RUNAS) "${STATEPATH}" + chown "$RUNAS":"$(id -gn "$RUNAS")" "${STATEPATH}" + chgrp "$(id -gn "$RUNAS")" "$UPS_C" fi config_get driver "$cfg" driver "usbhid-ups" @@ -283,13 +287,39 @@ start_driver_instance() { procd_close_instance } +interface_triggers() { + local action="$1" + local triggerlist trigger + + config_get triggerlist upsd triggerlist + + . /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-server reload + 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-server reload + else + ubus call network.device status | grep -q '"up": true' && return 0 + fi + fi + [ "$action" = "add_trigger" ] || return 1 +} + start_server_instance() { - local RUNAS - build_config + local cfg="$1" [ "$haveserver" != 1 ] && return + interface_triggers "check_interface_up" || return - procd_open_instance "upsd" + procd_open_instance "$cfg" procd_set_param respawn procd_set_param stderr 0 procd_set_param stdout 1 @@ -298,30 +328,39 @@ start_server_instance() { } start_service() { - local havedriver haveserver local STATEPATH=/var/run/nut - local RUNAS # Avoid hotplug inadvertenly restarting driver during # forced shutdown [ -f /var/run/killpower ] && return 0 - [ -f /var/run/nut/disable-hotplug ] && return 0 config_load nut_server build_config - config_foreach start_driver_instance driver "$@" - start_server_instance "upsd" + case $@ in + "") + config_foreach start_driver_instance driver "$@" + start_server_instance upsd + ;; + *upsd*) + start_server_instance upsd + ;; + *) + config_foreach start_driver_instance driver "$@" + ;; + esac } reload_service() { - stop + stop_service "$@" sleep 2 - local havedriver haveserver - start + start_service "$@" } service_triggers() { + config_load nut_server + + interface_triggers "add_trigger" procd_add_reload_trigger "nut_server" } diff --git a/net/nut/files/nutshutdown b/net/nut/files/nutshutdown index e4fd962cd..86fe01bb9 100755 --- a/net/nut/files/nutshutdown +++ b/net/nut/files/nutshutdown @@ -4,11 +4,6 @@ # See /LICENSE for more information. # -. /lib/functions.sh - -mount -o remount,ro /overlay /overlay -mount -o remount,ro / / - stop_instance() { /etc/init.d/nut-server stop "$1" } @@ -16,26 +11,41 @@ stop_instance() { shutdown_instance() { local cfg="$1" config_get driver "$cfg" driver "usbhid-ups" - /lib/nut/${driver} -a "$cfg" -k + + # Only FSD if killpower was indicated + if [ -f /var/run/killpower ]; then + /lib/nut/"${driver}" -a "$cfg" -k + fi } -[ -f /var/run/killpower ] && { - [ -f /etc/config/nut_server ] && { - config_load nut_server +do_fsd() { + if [ -f /var/run/killpower ]; then + # Only make FS readonly if we are doing an FSD + mount -o remount,ro /overlay /overlay + mount -o remount,ro / / - # Can't FSD unless drivers are stopped - config_foreach stop_instance driver - # Driver will wait 'offdelay' before shutting down - config_foreach shutdown_instance driver - # So this can happen + . ${IPKG_INSTOOT}/lib/functions.sh + + if [ -f /etc/config/nut_server ]; then + config_load nut_server + + # Can't FSD unless drivers are stopped + config_foreach stop_instance driver + # Driver will wait 'offdelay' before shutting down + config_foreach shutdown_instance driver + # So this can happen + rm -f /var/run/killpower + poweroff + # And just in case + sleep 120 + # Uh-oh failed to poweroff UPS + reboot -f + else + poweroff + fi + else poweroff - # And just in case - sleep 120 - # Uh-oh failed to poweroff UPS - reboot -f - } || { - poweroff - } -} || { - poweroff + fi } + +do_fsd