@ -117,7 +117,7 @@ build_server_config() {
chmod 0640 "$USERS_C"
chmod 0640 "$USERS_C"
chmod 0640 "$UPSD_C"
chmod 0640 "$UPSD_C"
chmod 0640 /var/etc/nut/nut.conf
chmod 0644 /var/etc/nut/nut.conf
[ -d "${STATEPATH}" ] || {
[ -d "${STATEPATH}" ] || {
mkdir -m 0750 -p "${STATEPATH}"
mkdir -m 0750 -p "${STATEPATH}"
@ -128,17 +128,14 @@ build_server_config() {
chgrp $(id -gn $RUNAS) "$USERS_C"
chgrp $(id -gn $RUNAS) "$USERS_C"
chgrp $(id -gn $RUNAS) "$UPSD_C"
chgrp $(id -gn $RUNAS) "$UPSD_C"
fi
fi
haveserver=1
}
}
build_driver_config() {
build_driver_config() {
local cfg="$1"
local cfg="$1"
local runas
echo "[$cfg]" >>"$UPS_C"
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" driver "usbhid-ups"
get_write_driver_config "$cfg" port "auto"
get_write_driver_config "$cfg" port "auto"
get_write_driver_config "$cfg" mfr
get_write_driver_config "$cfg" mfr
@ -166,12 +163,18 @@ build_driver_config() {
}
}
build_config() {
build_config() {
local RUNAS=nut
local STATEPATH=/var/run/nut
mkdir -m 0755 -p "$(dirname "$UPS_C")"
mkdir -m 0755 -p "$(dirname "$UPS_C")"
rm -f "$UPS_C"
rm -f "$UPS_C"
echo "# Config file automatically generated from UCI config" > "$UPS_C"
echo "# Config file automatically generated from UCI config" > "$UPS_C"
chmod 0640 "$UPS_C"
chmod 0640 "$UPS_C"
config_load nut_server
config_load nut_server
config_foreach upsd_statepath upsd
config_foreach build_global_driver_config driver_global
config_foreach build_driver_config driver
config_foreach build_driver_config driver
[ -n "$RUNAS" ] && chgrp $(id -gn $RUNAS) "$UPS_C"
[ -n "$RUNAS" ] && chgrp $(id -gn $RUNAS) "$UPS_C"
@ -181,26 +184,29 @@ build_config() {
start_driver_instance() {
start_driver_instance() {
local cfg="$1"
local cfg="$1"
local requested="$2"
local requested="$2"
local RUNAS=nut
local driver
local driver
local STATEPATH=/var/run/nut
local RUNAS=nut
[ "$havedriver" != 1 ] && return
# If wanting a specific instance, only start it
# 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")"
mkdir -m 0755 -p "$(dirname "$UPS_C")"
[ ! -s "$UPS_C" ] && build_config
[ ! -s "$UPS_C" ] && build_config
# Avoid hotplug inadvertenly restarting driver during
# Avoid hotplug inadvertenly restarting driver during
# forced shutdown
# forced shutdown
[ -f /var/run/killpower ] && return 0
[ -f /var/run/killpower ] && return 0
[ -d /var/run/nut ] && [ -f /var/run/nut/disable-hotplug ] && return 0
[ -d /var/run/nut ] && [ -f /var/run/nut/disable-hotplug ] && return 0
config_foreach upsd_statepath upsd
if [ -n "$RUNAS" ]; then
if [ -n "$RUNAS" ]; then
chown $RUNAS:$(id -gn $RUNAS) "${STATEPATH}"
chown $RUNAS:$(id -gn $RUNAS) "${STATEPATH}"
chown $RUNAS:$(id -gn $RUNAS) "$(dirname "$UPS_C")"
fi
fi
config_get driver "$cfg" driver "usbhid-ups"
config_get driver "$cfg" driver "usbhid-ups"
@ -213,9 +219,11 @@ start_driver_instance() {
}
}
start_server_instance() {
start_server_instance() {
local RUNAS STATEPATH
local RUNAS
build_config
build_config
[ "$haveserver" != 1 ] && return
procd_open_instance "upsd"
procd_open_instance "upsd"
procd_set_param respawn
procd_set_param respawn
procd_set_param stderr 0
procd_set_param stderr 0
@ -225,30 +233,26 @@ start_server_instance() {
}
}
start_service() {
start_service() {
local havedriver haveserver
local STATEPATH=/var/run/nut
local STATEPATH=/var/run/nut
# Avoid hotplug inadvertenly restarting driver during
# Avoid hotplug inadvertenly restarting driver during
# forced shutdown
# forced shutdown
[ -f /var/run/killpower ] && return 0
[ -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_load nut_server
config_foreach upsd_statepath upsd
[ -d "${STATEPATH}" ] || {
mkdir -m 0750 -p "${STATEPATH}"
}
build_config
build_config
config_foreach start_driver_instance driver "$@"
config_foreach start_driver_instance driver "$@"
[ "$1" != "upsd" ] && [ x"$1" != x"" ] && return 0
start_server_instance "upsd"
start_server_instance "upsd"
}
}
reload_service() {
reload_service() {
stop
stop
sleep 2
sleep 2
local havedriver haveserver
start
start
}
}