|
|
@ -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 "nut" |
|
|
|
config_get runas upsd runas nut |
|
|
|
RUNAS="$runas" |
|
|
|
} |
|
|
|
|
|
|
@ -62,10 +61,10 @@ upsd_config() { |
|
|
|
|
|
|
|
# Note runas support requires you make sure USB device file is readable by |
|
|
|
# the runas user |
|
|
|
config_get runas "$cfg" runas "nut" |
|
|
|
config_get runas "$cfg" runas nut |
|
|
|
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 |
|
|
|
} |
|
|
@ -179,6 +180,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" |
|
|
@ -190,6 +192,7 @@ build_driver_config() { |
|
|
|
other() { |
|
|
|
local othervar="$1" |
|
|
|
local othervarflag="$2" |
|
|
|
local otherval |
|
|
|
|
|
|
|
if [ "$othervarflag" = "otherflag" ]; then |
|
|
|
config_get_bool otherval "${othervarflag}_${othervar}" value |
|
|
@ -217,29 +220,28 @@ build_global_driver_config() { |
|
|
|
get_write_driver_config "$cfg" retrydelay |
|
|
|
get_write_driver_config "$cfg" pollinterval |
|
|
|
get_write_driver_config "$cfg" synchronous |
|
|
|
config_get runas "$cfg" user "nut" |
|
|
|
config_get runas "$cfg" user nut |
|
|
|
RUNAS="$runas" |
|
|
|
upsd_runas |
|
|
|
|
|
|
|
echo "" >>$UPS_C |
|
|
|
echo "" >>"$UPS_C" |
|
|
|
} |
|
|
|
|
|
|
|
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() { |
|
|
@ -252,22 +254,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" |
|
|
@ -275,7 +281,7 @@ start_driver_instance() { |
|
|
|
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_set_param command /lib/nut/"${driver}" -D -a "$cfg" ${RUNAS:+-u "$RUNAS"} |
|
|
|
procd_close_instance |
|
|
|
} |
|
|
|
|
|
|
@ -283,7 +289,7 @@ interface_triggers() { |
|
|
|
local action="$1" |
|
|
|
local triggerlist trigger |
|
|
|
|
|
|
|
config_get triggerlist "upsd" triggerlist |
|
|
|
config_get triggerlist upsd triggerlist |
|
|
|
|
|
|
|
. /lib/functions/network.sh |
|
|
|
|
|
|
@ -306,43 +312,47 @@ interface_triggers() { |
|
|
|
} |
|
|
|
|
|
|
|
start_server_instance() { |
|
|
|
local RUNAS=nut |
|
|
|
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 |
|
|
|
procd_set_param command /usr/sbin/upsd -D ${RUNAS:+-u $RUNAS} |
|
|
|
procd_set_param command /usr/sbin/upsd -D ${RUNAS:+-u "$RUNAS"} |
|
|
|
procd_close_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 |
|
|
|
[ -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() { |
|
|
|