From: Daniel F. Dickinson Date: Mon, 27 Aug 2018 13:50:34 +0000 (-0400) Subject: nut: Reorganize nut-server to clarify nut-driver X-Git-Url: http://git.lede-project.org./?a=commitdiff_plain;h=ceff68837d4b8d5a9bd8bf1962e913b5203d95e5;p=feed%2Fpackages.git nut: Reorganize nut-server to clarify nut-driver Clarify the different between the common nut-driver portions of nut-server and the nut-server (upsd) proper. nut-driver can't be used without nut-server so there is no compelling reason to split into a separate package, but the reorganization makes such a move easier and makes it clear what each bit is for. While we continue to use a single initscript, it's a properly procdified one which handles the upsd daemon as a different instance than the driver instances. This resolves a race condition in which upsd and drivers fail to start do to a degree of interdepedence. Also properly 'procd'ifies the nut-server initscript. Signed-off-by: Daniel F. Dickinson --- diff --git a/net/nut/Makefile b/net/nut/Makefile index 640d7a5953..972056959c 100644 --- a/net/nut/Makefile +++ b/net/nut/Makefile @@ -63,6 +63,7 @@ define Package/nut/install endef define Package/nut-server/install + # Server portion $(INSTALL_DIR) $(1)/etc/nut $(INSTALL_DIR) $(1)/usr/sbin $(INSTALL_DIR) $(1)/etc/init.d @@ -70,14 +71,15 @@ define Package/nut-server/install $(INSTALL_DIR) $(1)/etc/init.d $(INSTALL_BIN) ./files/nut-server.init $(1)/etc/init.d/nut-server $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/sbin/upsd $(1)/usr/sbin - $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/sbin/upsdrvctl $(1)/usr/sbin $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/share/nut/cmdvartab $(1)/usr/share/nut/ - $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/share/nut/driver.list $(1)/usr/share/nut/ $(INSTALL_DIR) $(1)/etc/config $(INSTALL_CONF) ./files/nut_server $(1)/etc/config/nut_server - ln -sf /var/etc/nut/ups.conf $(1)/etc/nut/ups.conf ln -sf /var/etc/nut/upsd.users $(1)/etc/nut/upsd.users ln -sf /var/etc/nut/upsd.conf $(1)/etc/nut/upsd.conf + # Driver common portion + $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/sbin/upsdrvctl $(1)/usr/sbin + $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/share/nut/driver.list $(1)/usr/share/nut/ + ln -sf /var/etc/nut/ups.conf $(1)/etc/nut/ups.conf endef define Package/nut-common @@ -110,6 +112,7 @@ define Package/nut-server $(call Package/nut/Default) TITLE+= (server) DEPENDS:=nut +nut-common + USERID:=nut=113:nut=113 endef define Package/nut-server/description @@ -123,9 +126,9 @@ endef define Package/nut-server/conffiles /etc/config/nut_server -/etc/nut/ups.conf /etc/nut/upsd.conf /etc/nut/upsd.users +/etc/nut/ups.conf endef define Package/nut-upsmon @@ -349,7 +352,7 @@ define DriverPackage define Package/nut-driver-$(2) $(call Package/nut/Default) TITLE:=$(2) (NUT $(1) driver) - DEPENDS:=nut +nut-common + DEPENDS:=nut +nut-server $(if $(filter $(1),snmp),DEPENDS+= @NUT_DRIVER_SNMP) $(if $(filter $(1),usb),DEPENDS+= @NUT_DRIVER_USB) $(if $(filter $(1),serial),DEPENDS+= @NUT_DRIVER_SERIAL) diff --git a/net/nut/files/nut-server.init b/net/nut/files/nut-server.init index e064dd7213..ad39b78e9f 100755 --- a/net/nut/files/nut-server.init +++ b/net/nut/files/nut-server.init @@ -6,14 +6,38 @@ # START=50 -RUN_D=/var/run -PID_F=$RUN_D/upsd.pid -UPS_C=/var/etc/nut/ups.conf USERS_C=/var/etc/nut/upsd.users UPSD_C=/var/etc/nut/upsd.conf +UPS_C=/var/etc/nut/ups.conf USE_PROCD=1 +get_write_driver_config() { + local cfg="$1" + local var="$2" + local def="$3" + local flag="$4" + local val + + [ -z "$flag" ] && { + config_get val "$cfg" "$var" "$def" + [ -n "$val" ] && [ "$val" != "0" ] && echo "$var = $val" >>"$UPS_C" + } + + [ -n "$flag" ] && { + config_get_bool val "$cfg" "$var" "$def" + [ "$val" = 1 ] && echo "$var" >>"$UPS_C" + } +} + +upsd_statepath() { + local cfg="$1" + local statepath + + config_get statepath "$cfg" statepath "/var/run/nut" + STATEPATH="$statepath" +} + listen_address() { local cfg="$1" @@ -22,18 +46,17 @@ listen_address() { echo "LISTEN $address $port" >>$UPSD_C } -upsd_statepath() { - local cfg="$1" - config_get statepath "$cfg" statepath -} - upsd_config() { local cfg="$1" - local maxage maxconn certfile + local maxage maxconn certfile runas statepath # Note runas support requires you make sure USB device file is readable by # the runas user - config_get runas "$cfg" runas + config_get runas "$cfg" runas "nut" + RUNAS="$runas" + + config_get statepath "$cfg" statepath "/var/run/nut" + STATEPATH="$statepath" config_get maxage "$cfg" maxage [ -n "$maxage" ] && echo "MAXAGE $maxage" >>$UPSD_C @@ -78,81 +101,158 @@ nut_user_add() { fi } -start_service() { - local runas statepath - - mkdir -p /var/etc/nut - chmod -R 750 /var/etc/nut - - rm -f $UPSD_C - rm -f $USERS_C - rm -f $UPSD_C +build_server_config() { + mkdir -m 0755 -p "$(dirname "$UPSD_C")" + rm -f "$USERS_C" + rm -f "$UPSD_C" rm -f /var/etc/nut/nut.conf - echo "# Config file automatically generated from UCI config" > $UPS_C echo "# Config file automatically generated from UCI config" > $USERS_C echo "# Config file automatically generated from UCI config" > $UPSD_C - local in_driver have_drivers - config_cb() { - if [ "$1" != "driver" ]; then - in_driver= - else - echo "[$2]" >> $UPS_C - in_driver=true - have_drivers=true - fi - } - option_cb() { - if [ "$in_driver" = "true" ]; then - echo " $1 = $2" >> $UPS_C - fi - } - - config_load nut_server - config_foreach nut_user_add user - config_foreach upsd_config upsd config_foreach listen_address listen_address - + config_foreach upsd_config upsd echo "MODE=netserver" >>/var/etc/nut/nut.conf chmod 0640 $USERS_C chmod 0640 $UPS_C chmod 0640 $UPSD_C chmod 0640 /var/etc/nut/nut.conf - [ -d "${statepath:-/var/run/nut}" ] || { - mkdir -m 0750 -p "${statepath:-/var/run/nut}" - chown $runas:$(id -gn $runas) "${statepath:-/var/run/nut}" + + [ -d "${STATEPATH}" ] || { + mkdir -m 0750 -p "${STATEPATH}" } - if [ -n "$runas" ]; then - chown -R $runas:$(id -gn $runas) /var/etc/nut + if [ -n "$RUNAS" ]; then + chown $RUNAS:$(id -gn $RUNAS) "${STATEPATH}" + chgrp $(id -gn $RUNAS) "$USERS_C" + chgrp $(id -gn $RUNAS) "$UPSD_C" fi +} - if [ "$have_drivers" = "true" ]; then - $DEBUG /usr/sbin/upsd ${runas:+-u $runas} $OPTIONS - $DEBUG /usr/sbin/upsdrvctl ${runas:+-u $runas} start - fi +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 + get_write_driver_config "$cfg" model + get_write_driver_config "$cfg" serial + get_write_driver_config "$cfg" sdtime + get_write_driver_config "$cfg" offdelay 20 + get_write_driver_config "$cfg" ondelay 30 + get_write_driver_config "$cfg" pollfreq 30 + get_write_driver_config "$cfg" vendor + get_write_driver_config "$cfg" product + get_write_driver_config "$cfg" bus + get_write_driver_config "$cfg" interruptonly 0 1 + get_write_driver_config "$cfg" interruptsize 0 + get_write_driver_config "$cfg" maxreport + get_write_driver_config "$cfg" vendorid + get_write_driver_config "$cfg" productid + get_write_driver_config "$cfg" community + get_write_driver_config "$cfg" snmp_version + get_write_driver_config "$cfg" snmp_retries 0 + get_write_driver_config "$cfg" snmp_timeout 0 + get_write_driver_config "$cfg" notransferoids 0 1 + get_write_driver_config "$cfg" other + echo "" >>$UPS_C } +build_config() { + 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 build_driver_config driver + [ -n "$RUNAS" ] && chgrp $(id -gn $RUNAS) "$UPS_C" -nut_driver_stop() { + build_server_config +} + +start_driver_instance() { local cfg="$1" + local requested="$2" + local RUNAS=nut local driver - config_get driver "$cfg" driver + # If wanting a specific instance, only start it + [ "$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 + - [ -r ${statepath:-/var/run/nut}/$driver-$cfg ] && /usr/sbin/upsdrvctl stop $cfg + 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" + procd_open_instance "$cfg" + procd_set_param respawn + procd_set_param stderr 0 + procd_set_param stdout 1 + procd_set_param command /lib/nut/${driver} -D -a "$cfg" ${RUNAS:+-u $RUNAS} + procd_close_instance +} + +start_server_instance() { + local RUNAS STATEPATH + build_config + + procd_open_instance "upsd" + procd_set_param respawn + procd_set_param stderr 0 + procd_set_param stdout 1 + procd_set_param command /usr/sbin/upsd -D ${RUNAS:+-u $RUNAS} + procd_close_instance } -stop_service() { - [ -r $PID_F ] && /usr/sbin/upsd -c stop - config_load ups +start_service() { + 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 + + config_load nut_server config_foreach upsd_statepath upsd - config_foreach nut_driver_stop driver + + [ -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() { - upsd -c reload + stop + sleep 2 + start +} + +service_triggers() { + procd_add_reload_trigger "nut_server" } diff --git a/net/nut/files/nut_server b/net/nut/files/nut_server index bebd169db6..464d56303b 100644 --- a/net/nut/files/nut_server +++ b/net/nut/files/nut_server @@ -19,5 +19,6 @@ # option maxage 15 # option statepath /var/run/nut # option maxconn 1024 +# option runas nut # NB: certificates only apply to SSL-enabled version # option certfile /usr/local/etc/upsd.pem