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: //var/packages/SynologyApplicationService/scripts/utils
#!/bin/sh

SynoSettingsGet()
{
	synogetkeyvalue /var/packages/SynologyApplicationService/etc/settings.conf $1
}

SynoSettingsSet()
{
	synosetkeyvalue /var/packages/SynologyApplicationService/etc/settings.conf $1 $2
}

SynoService()
{
	if [ -z "$SYNOPKG_DSM_VERSION_MAJOR" ]; then
		echo "Require SYNOPKG_DSM_VERSION_MAJOR";
	fi;

	if [[ "$SYNOPKG_DSM_VERSION_MAJOR" -le "6" ]]; then
		SynoUpstartService "$@"
	else
		SynoSystemdService "$@"
	fi;

	return $?;
}

SynoUpstartService()
{
	if (( $# < 2 )); then
		echo "Fatal, Wrong params." > $SYNOPKG_TEMP_LOGFILE
		return 2
	fi

	NEED_GREP=$( [ $1 == 'stop' ] && echo 'stop' || echo 'running' )
	if [ "$1" != "status" ]; then
		initctl $1 $2
		sleep 1
	fi

	if (( $# > 2 )); then
		initctl status $2 | grep $NEED_GREP;
		while (( $? == 0 )); do
			result=true
			for (( idx = 3 ; $idx <= $# ; ++idx )); do
				if ! ${!idx}; then
					result=false
					break;
				fi
			done
			if $result; then
				return 0
			fi
			sleep 0.5
			initctl status $2 | grep $NEED_GREP >/dev/null 2>&1;
		done
		echo "Service \"$2\" $1 failed." > $SYNOPKG_TEMP_LOGFILE
		return 1
	fi
	initctl status $2 | grep $NEED_GREP >/dev/null 2>&1;
	return $?
}

SynoSystemdService()
{
	if (( $# < 2 )); then
		echo "Fatal, Wrong params." > $SYNOPKG_TEMP_LOGFILE
		return 2
	fi

	NEED_GREP=$( [ $1 == 'stop' ] && echo 'inactive' || echo 'active' )
	if [ "$1" != "status" ]; then
		/usr/syno/bin/synosystemctl "$1" "$2"
		sleep 3
	fi

	if (( $# > 2 )); then
		/usr/syno/bin/synosystemctl get-active-status "$2" | grep -w "${NEED_GREP}"
		while (( $? == 0 )); do
			result=true
			for (( idx = 3 ; $idx <= $# ; ++idx )); do
				if ! ${!idx}; then
					result=false
					break;
				fi
			done
			if $result; then
				return 0
			fi
			sleep 0.5
			/usr/syno/bin/synosystemctl get-active-status "$2" | grep -w "${NEED_GREP}" >/dev/null 2>&1;
		done
		echo "Service \"$2\" $1 failed." > $SYNOPKG_TEMP_LOGFILE
		return 1
	fi
	/usr/syno/bin/synosystemctl get-active-status "$2" | grep -w "${NEED_GREP}" >/dev/null 2>&1;
	return $?
}

SynoSetVolume()
{
	volume="$(SynoSettingsGet volume)"

	volume_name=$(realpath /var/packages/SynologyApplicationService/target | /usr/bin/cut -d'/' -f2)
	install_volume="/$volume_name/@SynologyApplicationService"

	if [ ! -d ${install_volume} ]; then
		/bin/mkdir -p ${install_volume}
	fi

	if [[ "" != ${volume} && -d ${volume} && ${volume} != ${install_volume} ]]; then
		/bin/rm -rf ${install_volume}
		/bin/mv -f ${volume} ${install_volume}
	fi

	if [ ! -L /usr/local/SynologyApplicationService ]; then
		/bin/rm -rf /usr/local/SynologyApplicationService
	fi

	/bin/unlink /usr/local/SynologyApplicationService
	/bin/ln -s ${install_volume} /usr/local/SynologyApplicationService

	SynoSettingsSet volume ${install_volume}
}

GSMIsSupported()
{
	if [[ "$(/usr/syno/bin/synogetkeyvalue /etc.defaults/synoinfo.conf support_hyper_converged)" == "yes" ]]; then
		return 0
	fi
	return 1
}

GSMIsController()
{
	local SOFS_ROLE
	SOFS_ROLE=$1
	if [[ "$SOFS_ROLE" == "controller" ]]; then
		return 0
	elif [[ "$SOFS_ROLE" == "agent" ]]; then
		return 1
	fi
	if [[ "$(/usr/syno/bin/hcm-sentinel-tool get-role)" == "controller" ]]; then
		return 0
	fi
	return 1
}

FSDNIsSupported()
{
	if [[ ! -f "/usr/syno/sbin/syno_feature_check.sh" ]]; then
		return 1
	fi
	if [[ -z "$G_FSDN_IS_SUPPORT" ]];then
		if /usr/syno/sbin/syno_feature_check.sh SYNO_PRODUCT_FSDN ; then
			G_FSDN_IS_SUPPORT=0
		else
			G_FSDN_IS_SUPPORT=1
		fi
	fi
	return $G_FSDN_IS_SUPPORT
}

FSDNIsAdminNode()
{
	if [[ "$(/usr/syno/bin/aacore is-admin-node)" == "yes" ]]; then
		return 0
	fi
	return 1
}

IsSASServiceRunningNode()
{
	local role=$1
	# GSM agent node
	if GSMIsSupported && ! GSMIsController "${role}"; then
		return 1
	fi
	# PAM non-admin node
	if FSDNIsSupported && ! FSDNIsAdminNode; then
		return 1
	fi
	return 0
}