#!/bin/bash
#
# fence_sanlockd - daemon for fence_sanlock agent
#
# chkconfig: 2345 20 80
# description: starts and stops fence_sanlockd
#


### BEGIN INIT INFO
# Provides: fence_sanlockd
# Required-Start: $time $syslog
# Required-Stop: $syslog
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts and stops fence_sanlockd
# Description: starts and stops fence_sanlockd
### END INIT INFO

. /etc/rc.d/init.d/functions

prog="fence_sanlockd"
agent="fence_sanlock"
runfile="/run/$prog/$prog.pid"
fifofile="/run/$prog/$prog.fifo"
lockfile="/var/lock/subsys/$prog"
exec="/usr/sbin/$prog"

FENCESANLOCKDOPTS="-w"

[ -f /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

start() {
	# Check if the service is already running?
	if [ -f /var/lock/subsys/sanlock ]; then
		msg_already_running "sanlock"
		return
 	fi

	# start wdmd and sanlock daemons if they aren't running
	/etc/rc.d/init.d/wdmd status >/dev/null 2>&1 || /etc/rc.d/init.d/wdmd start
	/etc/rc.d/init.d/sanlock status >/dev/null 2>&1 || /etc/rc.d/init.d/sanlock start

	msg_starting "$prog"
	daemon $prog $FENCESANLOCKDOPTS
	retval=$?
	[ $retval -eq 0 ] && touch $lockfile
	return $retval
}

stop() {
	if [ ! -f /var/lock/subsys/$prog ]; then
		msg_not_running "$prog"
		return
	fi
	agent_ps="$(ps ax -o pid,args | grep fence_sanlock | grep -v grep | grep -v fence_sanlockd)"

	[ -n "$agent_ps" ] && {
		agent_pid="$(echo $agent_ps | awk '{print $1}')"
		echo "Cannot stop while $agent $agent_pid is running" >&2
		retval=1
		return $retval
	}

	# Ideally, we'd like a general way to check if anything
	# needs fencing to continue running, but without that,
	# check what we know, which is that dlm requires it.

	if [ -d /sys/kernel/dlm/ ]; then
		count="$(ls -A /sys/kernel/dlm/ | wc -l)"
		if [ $count -ne 0 ]; then
			echo "Cannot stop while dlm lockspaces exist" >&2
			retval=1
			return $retval
		fi
	fi

	if [ -d /sys/kernel/config/dlm/cluster ]; then
		# this dir exists while dlm_controld is running
		echo "Cannot stop while dlm is running" >&2
		retval=1
		return $retval
	fi

	PID=$(pidofproc -p $runfile $prog)

	# We have to use SIGHUP to mean stop because sanlock
	# uses SIGTERM to mean that the lockspace failed.

	killproc -p $runfile $prog -HUP
	retval=$?

	if [ $retval -ne 0 ]; then
		return $retval
	fi

	# fence_sanlockd won't see the SIGHUP if it's
	# still waiting for config from the fifo, so
	# send invalid config to the fifo to make it fail.

	if [ -p $fifofile ]; then
		echo "" > $fifofile
	fi

	show "Waiting for %s (%s) to stop:" "$prog" "$PID"

	timeout=10
	while checkpid $PID; do
		sleep 1
		timeout=$((timeout - 1))
		if [ "$timeout" -le 0 ]; then
			fail
			return 1
		fi
	done

	ok
	rm -f $lockfile

	# stop wdmd and sanlock daemons if they are running
	/etc/rc.d/init.d/sanlock status >/dev/null 2>&1 && /etc/rc.d/init.d/sanlock stop
	/etc/rc.d/init.d/wdmd status >/dev/null 2>&1 && /etc/rc.d/init.d/wdmd stop

	return $retval
}

condrestart() {
	if [ ! -f /var/lock/subsys/$prog ]; then
		msg_not_running "$prog"
		retval=$1
		return $retval
	fi
	stop && start
	retval=$?
	return $retval
}
 
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		stop && start
		;;
	reload)
		rh_status_q || exit 7
		$1
		;;
	force-reload)
		condrestart 7
		;;
	status)
		status --pidfile $runfile $prog
		retval=$?
		;;
	condrestart|try-restart)
		condrestart 0
		;;
	*)
		msg_usage "$0 {start|stop|status|restart|condrestart|try-restart|force-reload}"
		exit 3
esac
exit $?
