#!/bin/bash
#
# Corosync daemon init script for LSB-compliant Linux distributions.
#
# openais       Start the openais (corosync) cluster service
#
# chkconfig: - 20 20
# processname:  corosync
# pidfile:      /var/run/corosync.pid
# description:  Corosync/OpenAIS
### BEGIN INIT INFO
# Description: Manages the openais cluster services.
#
# Short-Description: openais cluster services.
# Provides: openais
# Required-Start: $network
# Should-Start: $syslog sshd drbd $named $remote_fs logd xendomains xend iscsi libvirtd
# Required-Stop: $network
# Default-Start: 3 5
# Default-Stop: 0 6
# Should-Stop: $syslog sshd drbd $named $remote_fs logd xendomains xend iscsi libvirtd
### END INIT INFO

do_force=0
prog="corosync"
exec="/usr/sbin/corosync"

OPENAIS_SYSCONFIG=/etc/sysconfig/openais
if [ -f $OPENAIS_SYSCONFIG ]; then
	. $OPENAIS_SYSCONFIG
fi
PACEMAKER_SYSCONFIG=/etc/sysconfig/pacemaker
if [ -f $PACEMAKER_SYSCONFIG ]; then
        . $PACEMAKER_SYSCONFIG
fi
LRMADMIN="/usr/sbin/lrmadmin"

SBD_CONFIG=/etc/sysconfig/sbd
SBD_BIN="/usr/sbin/sbd"
if [ -f $SBD_CONFIG ]; then
        . $SBD_CONFIG
fi

[ -x "$exec" ] || exit 0

SBD_DEVS=${SBD_DEVICE%;}
SBD_DEVICE=${SBD_DEVS//;/ -d }

StartSBD() {
        test -x $SBD_BIN || return
        if [ -n "$SBD_DEVICE" ]; then
		if ! pidofproc $SBD_BIN >/dev/null 2>&1 ; then
			echo -n "Starting SBD - "
			if ! $SBD_BIN -d $SBD_DEVICE -D $SBD_OPTS watch ; then
				echo "SBD failed to start; aborting."
				exit 1
			fi
		fi
        fi
}

StopSBD() {
        test -x $SBD_BIN || return
        if [ -n "$SBD_DEVICE" ]; then
		echo -n "Stopping SBD - "
                if ! $SBD_BIN -d $SBD_DEVICE -D $SBD_OPTS message LOCAL exit ; then
			echo "SBD failed to stop; aborting."
			exit 1
                fi
        fi
	while pidofproc $SBD_BIN >/dev/null 2>&1 ; do
		sleep 1
	done
	echo -n "done "
}

wait_for_lrmd() {
	local maxwait=30
	local i=0
	while [ $i -lt $maxwait ]; do
		$LRMADMIN -C > /dev/null 2>&1 &&
			break
		sleep 1
		i=$(($i+1))
	done
	if [ $i -lt $maxwait ]; then
		return 0
	else
		echo "lrmd apparently didn't start"
		return 1
	fi
}
set_lrmd_options() {
	test -x $LRMADMIN || return
	if [ -n "$LRMD_MAX_CHILDREN" ]; then
		wait_for_lrmd || return
		$LRMADMIN -p max-children $LRMD_MAX_CHILDREN
	fi
}

internal_status() {
    checkproc $exec > /dev/null 2>&1
    return $?
}

status() {
	if internal_status; then
		echo "Running"
		return 0
    else
		echo "Stopped"
		return 7
	fi
}

start() {
    export COROSYNC_DEFAULT_CONFIG_IFACE
    : ${COROSYNC_DEFAULT_CONFIG_IFACE="openaisserviceenableexperimental:corosync_parser"}
    echo -n $"Starting OpenAIS/Corosync daemon ($prog): "
    if ! internal_status; then
	StartSBD
	echo -n "starting... "
	startproc $exec
    fi

    sleep 2 # give it time to fail... $? isn't definitive

    if internal_status; then
	set_lrmd_options
	echo "OK"
	return 0
    fi

    echo "Failed"
    return 1
}

do_force=0
do_forever=1

stop() {
    local waitcount=0
    local iter=0
    local stopsbd=1
    local sig=TERM
    echo -n $"Stopping OpenAIS/corosync daemon ($prog): "

    while internal_status; do
	if [ $waitcount -eq 0 ]; then
           c_pid="`pidofproc $exec`"
	   if [ $iter -gt 0 ]; then
	      if [ $do_force = 1 ]; then
	         echo -n " Escalating... "
	         sig=KILL
	         # Do not stop SBD if we forcibly killed corosync; other
	         # nodes may still have a legitimate desire to STONITH us.
	         stopsbd=0
	      fi
	      if [ $do_forever = 0 ]; then
	     	 echo "Failed"
		 return 1
	      fi
	   fi
	   # Start a new cycle:
	   kill -$sig $c_pid
	   waitcount=0
	   iter=$[iter+1]
	fi

	waitcount=$[waitcount+1]
	echo -n "."
	sleep 2
    done

    if [ $stopsbd = 1 ]; then
    	StopSBD
    fi

    echo "OK"
    return 0
}

restart() {
    stop
    start
}

case "$1" in
    start|stop|restart)
        $1
        ;;
    force-stop)
	do_force=1
        stop
        ;;
    reload|force-reload)
        restart
        ;;
    condrestart|try-restart)
        internal_status && restart
        ;;
    status)
        status $prog
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|try-restart|condrestart|reload|force-reload|force-stop|status}"
        exit 2
esac
