#!/bin/bash
#
# xend		Script to start and stop the Xen control daemon.
#
# Author:       Keir Fraser <keir.fraser@cl.cam.ac.uk>
#
# chkconfig: 2345 98 01
# description: Starts and stops the Xen control daemon.
### BEGIN INIT INFO
# Provides:          xend
# Required-Start:    $syslog $remote_fs
# Should-Start:
# Required-Stop:     $syslog $remote_fs
# Should-Stop:
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6
# Default-Enabled:   yes
# Short-Description: Start/stop xend
# Description:       Starts and stops the Xen control daemon.
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

if [ ! -d /proc/xen ]; then
	exit 0
fi
if ! grep -q "control_d" /proc/xen/capabilities ; then
	exit 0
fi

# Default config params
XENSTORED_PID="/var/run/xenstore.pid"
XENSTORED_ARGS=

XENCONSOLED_LOG_HYPERVISOR=no
XENCONSOLED_LOG_GUESTS=no
XENCONSOLED_LOG_DIR=/var/log/xen/console
XENCONSOLED_ARGS=

BLKTAPCTRL_ARGS=

# User customized params
test -f /etc/sysconfig/xend && . /etc/sysconfig/xend

XENCONSOLED_LOG=none
if [ "$XENCONSOLED_LOG_HYPERVISOR" = "yes" ]
then
        if [ "$XENCONSOLED_LOG_GUESTS" = "yes" ]
        then
                XENCONSOLED_LOG=all
        else
                XENCONSOLED_LOG=hv
        fi
else
        if [ "$XENCONSOLED_LOG_GUESTS" = "yes" ]
        then
                XENCONSOLED_LOG=guest
        fi
fi

start() {
	echo -n $"Starting xen daemons: "

	echo -n "xenstored "
        /usr/sbin/xenstored --pid-file $XENSTORED_PID $XENSTORED_ARGS
	rc=$?
	test $rc = 0 || RETVAL=$rc

	echo -n "blktapctrl "
        /usr/sbin/blktapctrl $BLKTAPCTRL_ARGS
	rc=$?
	test $rc = 0 || RETVAL=$rc

	echo -n "xenconsoled "
        /usr/sbin/xenconsoled --log=$XENCONSOLED_LOG --log-dir=$XENCONSOLED_LOG_DIR $XENCONSOLED_ARGS
	rc=$?
	test $rc = 0 || RETVAL=$rc

        echo -n "xend"
	/usr/sbin/xend
	rc=$?
	test $rc = 0 || RETVAL=$rc

	test $RETVAL = 0 && echo_success || echo_failure
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/xend
}

stop() {
	echo -n $"Stopping xen daemons: "

	# NB not safe to stop xenstored, or blktapctrl if guests are active
	# or backend driver modules are loaded, so we skip them

	echo -n "xenconsoled "
	killproc xenconsoled > /dev/null
	rc=$?
	test $rc = 0 || RETVAL=$rc

	echo -n "xend "
	killproc xend > /dev/null
	rc=$?
	test $rc = 0 || RETVAL=$rc

	test $RETVAL = 0 && echo_success || echo_failure
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/xend
}

rcstatus() {
        status xenstored
        rc=$?
        test $rc != 0 && RETVAL=$rc

        status blktapctrl
        rc=$?
        test $rc != 0 && RETVAL=$rc

        status xenconsoled
        rc=$?
        test $rc != 0 && RETVAL=$rc

        status xend
        rc=$?
        test $rc != 0 && RETVAL=$rc
	test $RETVAL = 0 && echo_success || echo_failure
	echo
}

reload() {
	echo -n $"Reloading xen daemons: "

	echo -n "xenconsoled "
	killproc xenconsoled -HUP > /dev/null
	rc=$?
	test $rc = 0 || RETVAL=$rc

	echo -n "xend "
	killproc xend -HUP > /dev/null
	rc=$?
	test $rc = 0 || RETVAL=$rc

	test $RETVAL = 0 && echo_success || echo_failure
        echo
}

RETVAL=0
case "$1" in
  start)
        start
	;;
  stop)
        stop
	;;
  status)
        rcstatus
	;;
  reload)
	reload
        ;;
  restart|force-reload)
	stop
        start
	;;
  condrestart)
        if [ -f /var/lock/subsys/xend ]
        then
                stop
                start
        fi
        ;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|condrestart|reload|force-reload}"
	exit 1
esac

exit $RETVAL

