#!/sbin/openrc-run

description="Starts and stops xen driver domain daemon"

depend() 
{
        after syslog
}

start() 
{
        # not running in Xen dom0 or domU
        if ! test -d /proc/xen ; then
                exit 0
        fi

        # mount xenfs in dom0 or domU with a pv_ops kernel
        if [ ! -f /proc/xen/capabilities  ] && \
                ! grep '^xenfs ' /proc/mounts >/dev/null;
        then
                mount -t xenfs xenfs /proc/xen
        fi

        # run this script only in domU:
        # no capabilities file in xenlinux domU kernel
        # empty capabilities file in pv_ops domU kernel
        if [ ! -f /proc/xen/capabilities ] || \
                grep -q "control_d" /proc/xen/capabilities ; then
                exit 0
        fi

        [ -n "${PIDFILE}" ] || PIDFILE=/run/xldevd.pid

        ebegin "Starting xendriverdomain"

        start-stop-daemon --start --quiet \
                --exec /usr/sbin/xl \
                -- devd --pidfile "${PIDFILE}" $XLDEVD_ARGS
        eend $? "Failed to start xendriverdomain"
}

stop() 
{
        [ -n "${PIDFILE}" ] || PIDFILE=/run/xldevd.pid

        ebegin "Stopping xendriverdomain"

        start-stop-daemon --stop --quiet \
                --pidfile "${PIDFILE}"

        eend $? "Failed to stop xendriverdomain"
}
