mirror of
https://github.com/novatiq/packages.git
synced 2026-04-30 15:38:40 +01:00
nut: Backport fixes from master
Backport and squash the following commits from master:5790053ebnut: Add missing conffilesceff68837nut: Reorganize nut-server to clarify nut-driverf6a2a97d2nut: Use 'real' procd init for nut-monitor918a62f91nut: Make FSD really worka2f64b3banut: Reduce user error with POWERDOWNFLAG461393810nut: Use quotes around filenames1b6dbe7a7nut: Remove duplicate/extraneous lines0a49d0ffbnut: Fix checking for path before it exists3b5a8eee8nut: Various startup fixes for monitor and server44e57d4bdnut: Fix variables for NUT drivers36fd59dc7nut: Fix extraneous config_get192b0f164nut: Fix a typo in setting a driver parameterf48b060fanut: Fix upsd runs as root And bump PKG_RELEASE Signed-off-by: Daniel F. Dickinson <cshored@thecshore.com>
This commit is contained in:
+258
-89
@@ -6,47 +6,79 @@
|
||||
#
|
||||
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"
|
||||
}
|
||||
|
||||
upsd_runas() {
|
||||
local cfg="$1"
|
||||
local runas
|
||||
|
||||
[ -n "$RUNAS" ] && return
|
||||
|
||||
config_get runas "$cfg" runas "nut"
|
||||
RUNAS="$runas"
|
||||
}
|
||||
|
||||
listen_address() {
|
||||
local cfg="$1"
|
||||
|
||||
config_get address "$cfg" address "::1"
|
||||
config_get port "$cfg" port
|
||||
echo "LISTEN $address $port" >>$UPSD_C
|
||||
}
|
||||
|
||||
upsd_statepath() {
|
||||
local cfg="$1"
|
||||
config_get statepath "$cfg" statepath
|
||||
echo "LISTEN $address $port" >>"$UPSD_C"
|
||||
}
|
||||
|
||||
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
|
||||
[ -n "$maxage" ] && echo "MAXAGE $maxage" >>"$UPSD_C"
|
||||
|
||||
config_get statepath "$cfg" statepath
|
||||
[ -n "$statepath" ] && echo "STATEPATH $statepath" >>$UPSD_C
|
||||
[ -n "$statepath" ] && echo "STATEPATH $statepath" >>"$UPSD_C"
|
||||
|
||||
config_get maxconn "$cfg" maxconn
|
||||
[ -n "$maxconn" ] && echo "MAXCONN $maxconn" >>$UPSD_C
|
||||
[ -n "$maxconn" ] && echo "MAXCONN $maxconn" >>"$UPSD_C"
|
||||
|
||||
#NOTE: certs only apply to SSL-enabled version
|
||||
config_get certfile "$cfg" certfile
|
||||
[ -n "$certfile" ] && echo "CERTFILE $certfile" >>$UPSD_C
|
||||
[ -n "$certfile" ] && echo "CERTFILE $certfile" >>"$UPSD_C"
|
||||
}
|
||||
|
||||
nut_user_add() {
|
||||
@@ -55,104 +87,241 @@ nut_user_add() {
|
||||
local val
|
||||
|
||||
config_get val "$cfg" username "$1"
|
||||
echo "[$val]" >> $USERS_C
|
||||
echo "[$val]" >> "$USERS_C"
|
||||
|
||||
config_get val "$cfg" password
|
||||
echo " password = $val" >> $USERS_C
|
||||
echo " password = $val" >> "$USERS_C"
|
||||
|
||||
config_get val "$cfg" actions
|
||||
for a in $val; do
|
||||
echo " actions = $a" >> $USERS_C
|
||||
echo " actions = $a" >> "$USERS_C"
|
||||
done
|
||||
|
||||
instcmd() {
|
||||
local val="$1"
|
||||
echo " instcmds = $val" >> $USERS_C
|
||||
echo " instcmds = $val" >> "$USERS_C"
|
||||
}
|
||||
|
||||
config_list_foreach "$cfg" instcmd instcmd
|
||||
|
||||
config_get val "$cfg" upsmon
|
||||
if [ -n "$val" ]; then
|
||||
echo " upsmon $val" >> $USERS_C
|
||||
echo " upsmon $val" >> "$USERS_C"
|
||||
fi
|
||||
}
|
||||
|
||||
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" > "$USERS_C"
|
||||
echo "# Config file automatically generated from UCI config" > "$UPSD_C"
|
||||
|
||||
config_foreach nut_user_add user
|
||||
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 "$UPSD_C"
|
||||
chmod 0644 /var/etc/nut/nut.conf
|
||||
|
||||
[ -d "${STATEPATH}" ] || {
|
||||
mkdir -m 0750 -p "${STATEPATH}"
|
||||
}
|
||||
|
||||
if [ -n "$RUNAS" ]; then
|
||||
chown $RUNAS:$(id -gn $RUNAS) "${STATEPATH}"
|
||||
chgrp $(id -gn $RUNAS) "$USERS_C"
|
||||
chgrp $(id -gn $RUNAS) "$UPSD_C"
|
||||
fi
|
||||
haveserver=1
|
||||
}
|
||||
|
||||
build_driver_config() {
|
||||
local cfg="$1"
|
||||
|
||||
echo "[$cfg]" >>"$UPS_C"
|
||||
|
||||
get_write_driver_config "$cfg" driver "usbhid-ups"
|
||||
get_write_driver_config "$cfg" port "auto"
|
||||
get_write_driver_config "$cfg" sdorder
|
||||
get_write_driver_config "$cfg" desc
|
||||
get_write_driver_config "$cfg" nolock 0 1
|
||||
get_write_driver_config "$cfg" ignorelb 0 1
|
||||
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
|
||||
get_write_driver_config "$cfg" ondelay
|
||||
get_write_driver_config "$cfg" pollfreq
|
||||
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
|
||||
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
|
||||
get_write_driver_config "$cfg" snmp_timeout
|
||||
get_write_driver_config "$cfg" notransferoids 0 1
|
||||
get_write_driver_config "$cfg" maxstartdelay
|
||||
get_write_driver_config "$cfg" retrydelay
|
||||
get_write_driver_config "$cfg" synchronous
|
||||
get_write_driver_config "$cfg" other
|
||||
get_write_driver_config "$cfg" otherflag
|
||||
|
||||
defoverride() {
|
||||
local overvar="$1"
|
||||
local defover="$2"
|
||||
local overtype="$(echo "$overvar" | tr '.' '_')"
|
||||
|
||||
config_get overval "${defover}_${overtype}" value
|
||||
[ -n "$overval" ] && echo "${defover}.${overvar} = $overval" >>"$UPS_C"
|
||||
}
|
||||
|
||||
config_list_foreach "$cfg" override defoverride override
|
||||
config_list_foreach "$cfg" default defoverride default
|
||||
|
||||
other() {
|
||||
local othervar="$1"
|
||||
local othervarflag="$2"
|
||||
|
||||
if [ "$othervarflag" = "otherflag" ]; then
|
||||
config_get_bool otherval "${othervarflag}_${overtype}" value
|
||||
[ "$otherval" = "1" ] && echo "${othervarflag}_${othervar}" >>"$UPS_C"
|
||||
else
|
||||
config_get otherval "${othervarflag}_${overtype}" value
|
||||
[ -n "$otherval" ] && echo "${othervarflag}_${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
|
||||
echo "" >>$UPS_C
|
||||
havedriver=1
|
||||
}
|
||||
|
||||
build_global_driver_config() {
|
||||
local cfg="$1"
|
||||
|
||||
# Global driver config
|
||||
get_write_driver_config "$cfg" chroot
|
||||
get_write_driver_config "$cfg" driverpath
|
||||
get_write_driver_config "$cfg" maxstartdelay
|
||||
get_write_driver_config "$cfg" maxretry
|
||||
get_write_driver_config "$cfg" retrydelay
|
||||
get_write_driver_config "$cfg" pollinterval
|
||||
get_write_driver_config "$cfg" synchronous
|
||||
config_get runas "$cfg" user "nut"
|
||||
RUNAS="$runas"
|
||||
upsd_runas
|
||||
|
||||
echo "" >>$UPS_C
|
||||
}
|
||||
|
||||
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"
|
||||
|
||||
build_server_config
|
||||
}
|
||||
|
||||
start_driver_instance() {
|
||||
local cfg="$1"
|
||||
local requested="$2"
|
||||
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
|
||||
|
||||
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}"
|
||||
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
|
||||
build_config
|
||||
|
||||
[ "$haveserver" != 1 ] && return
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
start_service() {
|
||||
local runas statepath
|
||||
local havedriver haveserver
|
||||
local STATEPATH=/var/run/nut
|
||||
|
||||
mkdir -p /var/etc/nut
|
||||
chmod -R 750 /var/etc/nut
|
||||
|
||||
rm -f $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
|
||||
}
|
||||
# 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
|
||||
|
||||
config_foreach nut_user_add user
|
||||
config_foreach upsd_config upsd
|
||||
config_foreach listen_address listen_address
|
||||
build_config
|
||||
config_foreach start_driver_instance driver "$@"
|
||||
|
||||
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}"
|
||||
}
|
||||
|
||||
if [ -n "$runas" ]; then
|
||||
chown -R $runas:$(id -gn $runas) /var/etc/nut
|
||||
fi
|
||||
|
||||
if [ "$have_drivers" = "true" ]; then
|
||||
$DEBUG /usr/sbin/upsd ${runas:+-u $runas} $OPTIONS
|
||||
$DEBUG /usr/sbin/upsdrvctl ${runas:+-u $runas} start
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
nut_driver_stop() {
|
||||
local cfg="$1"
|
||||
local driver
|
||||
|
||||
config_get driver "$cfg" driver
|
||||
|
||||
[ -r ${statepath:-/var/run/nut}/$driver-$cfg ] && /usr/sbin/upsdrvctl stop $cfg
|
||||
}
|
||||
|
||||
stop_service() {
|
||||
[ -r $PID_F ] && /usr/sbin/upsd -c stop
|
||||
config_load ups
|
||||
config_foreach upsd_statepath upsd
|
||||
config_foreach nut_driver_stop driver
|
||||
start_server_instance "upsd"
|
||||
}
|
||||
|
||||
reload_service() {
|
||||
upsd -c reload
|
||||
stop
|
||||
sleep 2
|
||||
local havedriver haveserver
|
||||
start
|
||||
}
|
||||
|
||||
service_triggers() {
|
||||
procd_add_reload_trigger "nut_server"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user