#!/bin/sh
#
# vzeventd	This shell script takes care of starting and stopping
#               vzeventd daemon for OpenVZ.
#
# chkconfig: 2345 95 89
# description: vzeventd is OpenVZ events daemon. \
# It takes care of events sent by the OpenVZ kernel and performs required \
# actions associated with those events.

### BEGIN INIT INFO
# Provides: vzeventd
# Required-start: $remote_fs
# Required-stop: $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Start-Before: vz
# X-Stop-After: vz
# Short-Description: start and stop vzeventd
# Description: vzeventd is the OpenVZ events daemon.
#              It takes care of events sent by the OpenVZ kernel
#              and performs required actions associated with those events.
### END INIT INFO

. @SCRIPTDIR@/initd-functions

prog=vzeventd
lockfile=$VARLOCK/$prog

load_module() {
	modprobe vzevent reboot_event=1
}

check() {
	local param=/sys/module/vzevent/parameters/reboot_event

	__echo "Checking vzevent kernel module ..."

	if ! lsmod | fgrep -qw vzevent; then
		print_failure
		return 1
	fi
	echo 1 > $param
	if ! cat $param | fgrep -qw 1; then
		print_failure
		print_warning "vzevent module should be loaded with reboot_event=1 parameter"
		echo
		return 1
	fi
	print_success
	return 0
}

start() {
	[ "$(id -u)" != "0" ] && exit 4
	[ -x @SBINDIR@/vzeventd ] || exit 5
	check_vzkernel > /dev/null
	[ -r /etc/sysconfig/vzeventd ] && . /etc/sysconfig/vzeventd
	[ -r /etc/default/vzeventd ] && . /etc/default/vzeventd

	[ -f $lockfile ] && exit 0 # Already running

	load_module
	check || exit 1

	echo -n "Starting $prog: "
	vzdaemon_start $prog $OPTIONS
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch $lockfile
	return $RETVAL
}

stop() {
	[ "$(id -u)" != "0" ] && exit 4
	echo -n "Shutting down $prog: "
	vzdaemon_stop $prog
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f $lockfile
	return $RETVAL
}

# See how we were called.
case "$1" in
   start)
	start
	;;
   stop)
	stop
	;;
   status)
	vzdaemon_status $prog
	;;
   restart|force-reload)
	stop
	start
	;;
   try-restart|condrestart)
	if vzdaemon_status $prog >/dev/null 2>&1; then
		stop
		start
	fi
	;;
   reload)
	exit 3
	;;
   *)
	echo "Usage: $0 {start|stop|status|restart|try-restart|force-reload}"
	exit 2
esac
