#!/bin/bash
#
# Copyright (c) 2016 SUSE Linux GmbH
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of version 2.1 of the GNU Lesser General Public
# License as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; If not, see <http://www.gnu.org/licenses/>.
#

XENSTORED=@XENSTORED@

. @XEN_SCRIPT_DIR@/hotplugpath.sh

test_xenstore () {
	test -f @XEN_RUN_DIR@/xenstored.pid
	return $?
}

timeout_xenstore () {
	local time=0
	local timeout=30

	while [ $time -lt $timeout ] && ! test_xenstore ; do
		echo -n .
		time=$(($time+1))
		sleep 1
	done
	echo
 
	# Exit if we timed out
	if ! [ $time -lt $timeout ] ; then
		echo "Could not start $@"
		return 1
	fi

	return 0
}

test_xenstore && exit 0

test -f @CONFIG_DIR@/@CONFIG_LEAF_DIR@/xencommons && . @CONFIG_DIR@/@CONFIG_LEAF_DIR@/xencommons

[ "$XENSTORETYPE" = "" ] && XENSTORETYPE=daemon

/bin/mkdir -p @XEN_RUN_DIR@

[ "$XENSTORETYPE" = "daemon" ] && {
	[ -z "$XENSTORED_TRACE" ] || XENSTORED_ARGS="$XENSTORED_ARGS -T @XEN_LOG_DIR@/xenstored-trace.log"
	[ -z "$XENSTORED_MAX_OPEN_FDS" ] && XENSTORED_MAX_OPEN_FDS=unlimited
	[ -z "$XENSTORED" ] && XENSTORED=@XENSTORED@
	[ -x "$XENSTORED" ] || {
		echo "No xenstored found"
		exit 1
	}
	XS_OOM_SCORE=-$((${XENSTORED_OOM_MEM_THRESHOLD:-50} * 10))

	[ "$XENSTORED_MAX_OPEN_FDS" = "unlimited" ] || {
		[ -z "${XENSTORED_MAX_OPEN_FDS//[0-9]}" ] &&
		[ -n "$XENSTORED_MAX_OPEN_FDS" ] || {
			echo "XENSTORED_MAX_OPEN_FDS=$XENSTORED_MAX_OPEN_FDS invalid"
			echo "Setting to default \"unlimited\"."
			XENSTORED_MAX_OPEN_FDS=unlimited
		}
	}
	[ -r /proc/sys/fs/nr_open ] && {
		MAX_FDS=`cat /proc/sys/fs/nr_open`
		[ "$XENSTORED_MAX_OPEN_FDS" = "unlimited" ] && XENSTORED_MAX_OPEN_FDS=$MAX_FDS
		[ $XENSTORED_MAX_OPEN_FDS -gt $MAX_FDS ] && {
			echo "XENSTORED_MAX_OPEN_FDS exceeds system limit."
			echo "Setting to \"$MAX_FDS\"."
			XENSTORED_MAX_OPEN_FDS=$MAX_FDS
		}
	}

	rm -f @XEN_RUN_DIR@/xenstored.pid

	echo -n Starting $XENSTORED...
	prlimit --nofile=$XENSTORED_MAX_OPEN_FDS $XENSTORED --pid-file @XEN_RUN_DIR@/xenstored.pid $XENSTORED_ARGS

	systemd-notify --booted 2>/dev/null || timeout_xenstore $XENSTORED || exit 1
	XS_PID=`cat @XEN_RUN_DIR@/xenstored.pid`
	echo $XS_OOM_SCORE >/proc/$XS_PID/oom_score_adj

	exit 0
}

[ "$XENSTORETYPE" = "domain" ] && {
	[ -z "$XENSTORE_DOMAIN_KERNEL" ] && XENSTORE_DOMAIN_KERNEL=@LIBEXEC@/boot/xenstore-stubdom.gz
	XENSTORE_DOMAIN_ARGS="$XENSTORE_DOMAIN_ARGS --kernel $XENSTORE_DOMAIN_KERNEL"
	[ -z "$XENSTORE_DOMAIN_SIZE" ] && XENSTORE_DOMAIN_SIZE=8
	XENSTORE_DOMAIN_ARGS="$XENSTORE_DOMAIN_ARGS --memory $XENSTORE_DOMAIN_SIZE"
	[ -z "$XENSTORE_MAX_DOMAIN_SIZE" ] || XENSTORE_DOMAIN_ARGS="$XENSTORE_DOMAIN_ARGS --maxmem $XENSTORE_MAX_DOMAIN_SIZE"
	[ -z "$XENSTORED_TRACE" ] || XENSTORE_DOMAIN_ARGS="$XENSTORE_DOMAIN_ARGS -T xenstored-trace.log"

	echo -n Starting $XENSTORE_DOMAIN_KERNEL...
	${LIBEXEC_BIN}/init-xenstore-domain $XENSTORE_DOMAIN_ARGS || exit 1
	systemd-notify --ready 2>/dev/null

	exit 0
}

echo "illegal value $XENSTORETYPE for XENSTORETYPE"
exit 1
