#!/bin/sh
#
# Copyright (C) 2012 Red Hat, Inc. All rights reserved.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions
# of the GNU General Public License v.2.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# This file is part of LVM2.
# It is required for the proper handling of failures of LVM2 mirror
# devices that were created using the -m option of lvcreate.
#
#
# chkconfig: 12345 02 99
# description: Starts and stops LVM poll daemon
#
# For Red-Hat-based distributions such as Fedora, RHEL, CentOS.
#
### BEGIN INIT INFO
# Provides: lvm2-lvmpolld
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: 1 2 3 4 5
# Default-Stop: 0 6
# Short-Description: A daemon that is responsible for monitoring in-progress
#                    and possibly longer term operations on logical volumes. 
#                    It helps to reduce the number of spawned processes if same
#                    logical volume is requested to get monitored multiple times.
#                    Also avoids unsolicited termination due to external factors.
### END INIT INFO

. /etc/init.d/functions

DAEMON="lvmpolld"
NAME="LVM poll daemon"

LOCK_FILE="/var/lock/subsys/lvm2-lvmpolld"
PID_FILE="/var/run/lvmpolld.pid"

start()
{
	if [ -f $LOCK_FILE ]; then
		msg_already_running "$NAME"
		return
	fi
	msg_starting "$NAME"
	daemon "/sbin/$DAEMON" </dev/null
	RETVAL=$?
	[ "$RETVAL" -eq 0 ] && touch "$LOCK_FILE"
}

stop()
{
	if [ ! -f "$LOCK_FILE" ]; then
		msg_not_running "$NAME"
		return
	fi
	msg_stopping "$NAME"
	killproc --pidfile "$PID_FILE" "$DAEMON"
	rm -f "$LOCK_FILE"
}

condrestart() {
	if [ ! -f "$LOCK_FILE" ]; then
		msg_not_running "$NAME"
		RETVAL=$1
		return
	fi
	stop
	start
}

reload() {
	if [ ! -f "$LOCK_FILE" ]; then
		msg_not_running "$NAME"
		RETVAL=7
		return
	fi
	msg_reloading "$NAME"
	killproc "$DAEMON" -HUP
	RETVAL=$?
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
	start
	;;

  stop)
	stop
	;;

  restart)
	stop
	start
	;;

  condrestart|try-restart)
	condrestart 0
	;;

  status)
	status $DAEMON
	exit $?
	;;

  *)
	msg_usage "$0 {start|stop|force-stop|restart|condrestart|try-restart|status}"
	;;
esac

exit $RETVAL
