HEX
Server: Apache/2.4.63 (Unix)
System: Linux TOMS-220NAS 4.4.302+ #86009 SMP Wed Nov 26 18:19:17 CST 2025 x86_64
User: flavio87 (1026)
PHP: 8.3.27
Disabled: NONE
Upload Files
File: //usr/syno/bin/synoupscommon
#!/bin/sh
# Copyright (c) 2000-2012 Synology Inc. All rights reserved.
UPS_STATE="/run/ups_state"
UPS_ROOT="/tmp/ups"
UPS_LOCK=$UPS_ROOT"/ups.lock"
FLOCK_FILE=$UPS_LOCK
UPS_UDEVLOCK="/tmp/upsudevlock"
SYNOUPS_CONF="/usr/syno/etc/ups/synoups.conf"
UPSSCHED_CONF="/etc/ups/upssched.conf"
UPSMON_CONF="/etc/ups/upsmon.conf"
UPSMON_CONF_BACKUP="/etc.defaults/ups/upsmon.conf"
UPS_CONF="/etc/ups/ups.conf"
UPS_CONF_BACKUP="/etc.defaults/ups/ups.conf"
NUTSCAN_USB_H="/etc/ups/nutscan-usb.h"
UPS_TABLE="/lib/udev/devicetable/usb.nut-hid"
BIN_UPSMON="/usr/sbin/upsmon"
BIN_UPSD="/usr/sbin/upsd"
BIN_UPSC="/usr/bin/upsc"
UPSMON_PID="/run/upsmon.pid"
UPSD_PID="$UPS_STATE/upsd.pid"
BIN_FILE_UPSC="/usr/bin/upsc"
BIN_SYNONOTIFY="/usr/syno/bin/synonotify"
# for debug log
UPS_LOG=`/bin/get_key_value $SYNOUPS_CONF upslog`
if [ $? -ne 1 ]; then
	UPS_LOG="/dev/null"
fi
UPSMON_DBG=`/bin/get_key_value $SYNOUPS_CONF upsmon_dbg`
UPSD_DBG=`/bin/get_key_value $SYNOUPS_CONF upsd_dbg`
UPSDRV_DBG=`/bin/get_key_value $SYNOUPS_CONF upsdrv_dbg`

# Black List
# Reason:
# Belkin_F5U002: printer
# ...
Belkin_F5U002="050d/0002"
blackList="$BelkinF5U002"

if [ ! -d $UPS_ROOT ]; then
	mkdir -p $UPS_ROOT
fi

if [ ! -d $UPS_STATE ]; then
	mkdir -p $UPS_STATE
fi

SupportUPS=`/bin/get_key_value /etc.defaults/synoinfo.conf supportups`
case "$SupportUPS" in
[Yy][Ee][Ss])
	;;
*)
	echo "UPS is not support here."
	exit
	;;
esac

EnableUPS=`/bin/get_key_value $SYNOUPS_CONF ups_enabled`
case "$EnableUPS" in
[Yy][Ee][Ss])
	MasterEnabled=1;;
*)
	MasterEnabled=0;;
esac

EnableSlave=`/bin/get_key_value $SYNOUPS_CONF upsslave_enabled`
case "$EnableSlave" in
[Yy][Ee][Ss])
	SlaveEnabled=1;;
*)
	SlaveEnabled=0;;
esac

UpsMode=`/bin/get_key_value $SYNOUPS_CONF ups_mode`
# ups_mode will be empty when upgrade to 4.2
if [ "x" = "x${UpsMode}" ]; then
	UpsMode="usb"
fi

StatClient=`ps -aux |grep '/usr/sbin/upsmon'|grep -cv grep`

SafeMV() {
	mv $1 $2.$$
	mv $2.$$ $2
}

UpdateUpsdConf() {
	local RAD_F_LISTEN="/etc/ups/upsd.conf"

	echo "LISTEN 0.0.0.0" > $RAD_F_LISTEN
}

StartUpsd() {
	if [ -z ${UPSD_DBG} ]; then
		$BIN_UPSD
	else
		$BIN_UPSD -DDD >> ${UPSD_DBG} 2>&1 &
	fi
}

StartServer() {
	UpdateUpsdConf

	local cntDaemon=`ps -aux |grep \'$BIN_UPSD\'|grep -cv grep`
	if [ $cntDaemon -gt 0 ]; then
		$BIN_UPSD -c stop
	fi
	rm $UPSD_PID
	StartUpsd
}

RestartServer() {
	local cntDaemon=`ps -aux |grep \'$BIN_UPSD\'|grep -cv grep`
	if [ $cntDaemon -gt 0 ]; then
		$BIN_UPSD -c stop
	else
		return 0
	fi
	rm $UPSD_PID
	StartUpsd
}

StartClient() {
	local cntMon=`ps -aux |grep \'$BIN_UPSMON\'|grep -cv grep`
	if [ $cntMon -gt 0 ]; then
		$BIN_UPSMON -c stop
	fi
	rm $UPSMON_PID
	if [ -z ${UPSMON_DBG} ]; then
		$BIN_UPSMON
	else
		$BIN_UPSMON -DDD >> ${UPSMON_DBG} 2>&1 &
	fi
}

StopUps() {
	ShowLog "Stop UPS Daemon"

	killall upsmon > /dev/null 2>&1
	killall upssched > /dev/null 2>&1
	killall upsd > /dev/null 2>&1
	/usr/bin/upsdrvctl stop

	#we need to check the process stopped, in some low level DS, it sometimes costs much time
	WaitStop	

	return 0
}

ForceStopUps() {
	ShowLog "Stop UPS timeout, Force stop UPS"

	killall -9 upsmon > /dev/null 2>&1
	killall -9 upssched > /dev/null 2>&1
	killall -9 upsd > /dev/null 2>&1
	/usr/bin/upsdrvctl stop
	sleep 3
}

WaitStop() {
	for i in `seq 1 1 3`; do
		sleep 3
		local P_COUNT=`ps -aux |grep -E "(upsd|upsmon|a ups)"|grep -cv grep`
		if [ 0 -eq $P_COUNT ]; then
			return 0
		fi
	done
	ForceStopUps
	return 255
}

CheckUpsmonConf() {
	local conf_size=`stat $UPSMON_CONF |grep Size|cut -d" " -f 4`
	# mode: local/remote
	local mode=$1
	local server_ip="0.0.0.0"

	if [ 0 -eq $conf_size ]; then
		if [ -f $UPSMON_CONF_BACKUP ]; then
			/bin/cp $UPSMON_CONF_BACKUP $UPSMON_CONF
		else
			ShowLog "synoupscommon: cannot rescue $UPSMON_CONF"
		fi
	fi

	if [ "$mode" == "local" ]; then
		/bin/sed -i "/^MONITOR/c\\MONITOR ups@localhost 1 monuser secret master" $UPSMON_CONF
	elif [ "$mode" == "remote" ]; then
		if [ $# -ne 2 ]; then
			ShowLog "synoupscommon: CheckUpsmonConf(): remote mode need set server_ip"
			return 1
		fi
		server_ip=$2
		/bin/sed -i "/^MONITOR/c\\MONITOR ups@${server_ip} 1 monuser secret slave" $UPSMON_CONF
	fi

	return 0
}

ShowLog() {
	echo $1
	logger -p user.err -t upsmsg $1
}

isInBlackList()
{
	if [ -z "$1" ]; then
		return 1
	fi

	for dev in $blackList; do
		if [ "x$dev" = "x$1" ]; then
			echo "$1 is in the black list" >> $UPS_LOG
			return 0
		fi
	done
	return 1
}

CheckUPSSupport()
{
	local vendorID="$1"
	local productID="$2"

	echo $vendorID |grep "0x" > /dev/null 2>&1
	if [ 0 -ne $? ]; then
		vendorID="0x$vendorID"
	fi

	echo $productID |grep "0x" > /dev/null 2>&1
	if [ 0 -ne $? ]; then
		productID="0x$productID"
	fi

	if [ -f ${UPS_TABLE} ]; then
		grep "libhidups      0x0003      $vendorID   $productID" $UPS_TABLE > /dev/null 2>&1
		return $?
	fi
	return 1
}