#!/bin/sh
#
#pragma ident	"@(#)initrgm	1.11	03/09/29 SMI"
#
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

# Start/stop resource group manager daemon

LIBSCDIR=/usr/cluster/lib/sc
BINDIR=/usr/cluster/bin
USRBIN=/usr/bin
SERVER=rgmd
PROGNUM=100142
VERSNUM=1

#
# daemon_exists daemonname
#
# Tests whether a daemon exists with the daemonname, running
# as root and with parent process id of 1.  We don't need
# the actual pid, just the boolean status of whether it
# exists.  Returns 0 for exists, non-zero otherwise.
#
daemon_exists()
{
	${USRBIN}/pgrep -u 0 -P 1 -x $1 >/dev/null 2>&1
	return $?
}


case "$1" in
'start')
	# test whether we are a cluster and exit if not a cluster
	/usr/sbin/clinfo > /dev/null 2>&1
	if [ $? != 0 ] ; then
		exit 0
	fi

	# exit if rgmd is already running
	pidlist=`${USRBIN}/ps -e | ${USRBIN}/grep ${SERVER} \
	    | ${USRBIN}/awk '{print $1}'`
	if [ -n "${pidlist}" ] ; then
		${USRBIN}/logger -p local0.err -t INITRGM "Error: ${SERVER} is already running."
		exit 1
	fi

        # check that the servers on which rgm depends are up
	# by doing a stat command
	# no need to loop since we looped when we started the other servers
	# check that rpc.pmfd is up
	${BINDIR}/pmfadm -L 2>/dev/null 1>/dev/null
	if [ $? -ne 0 ]; then
                ${USRBIN}/logger -p local0.err -t INITRGM "Error: rpc.pmfd is not running."
		exit 1
	fi

	# check that rpc.fed is up
       	${USRBIN}/rpcinfo -l 127.0.0.1 ${PROGNUM} ${VERSNUM} 2>/dev/null | \
            ${USRBIN}/grep ${PROGNUM} >/dev/null 2>&1
	if [ $? -ne 0 ]; then
                ${USRBIN}/logger -p local0.err -t INITRGM "Error: rpc.fed is not running."
		exit 1
	fi

	# check that pnmd is up
	${BINDIR}/pnmstat 2>/dev/null 1>/dev/null
	if [ $? -eq 5 ]; then
                ${USRBIN}/logger -p local0.err -t INITRGM "Error: pnmd is not running."
		exit 1
	fi

	${LIBSCDIR}/${SERVER}
	if [ $? -ne 0 ]; then
		${USRBIN}/logger -p local0.err -t INITRGM "Error: Can't start ${SERVER}."
		exit 1
	fi

	;;

'stop')
	# Extra sync's are to increase our chances of getting
	# stuff written out to disk.
	/sbin/sync &

	# If the rgmd is running on this node, attempt the
	# scswitch evacuate, in case the human admin forgot
	# to do it.
	if daemon_exists rgmd
	then
		echo "$0: Calling scswitch -S (evacuate)"
	 	${BINDIR}/scswitch -S -h `/usr/sbin/clinfo -n`
		# We proceed with the rest of the init transition
		# regardless of whether scswitch -S worked or not.

		# If all resources have been successfully evacuated
		# then failfasts can be disabled.
		if [ $? -eq 0 ]; then
			/usr/cluster/lib/sc/cmm_ctl -n `/usr/sbin/clinfo -n`
		fi
	fi
	/sbin/sync &

	# If any of the following user-space daemons that are
	# part of the rgm world exist, then it means that this
	# node had made it up as far as run-level 3.  It is beyond
	# the ability of the Sun Cluster algorithms to support
	# transitioning to run levels that are less fully up than
	# level 3 but where the host stays up indefinitely.  For
	# attempts to transition to those levels, we halt instead.
	if daemon_exists rpc.pmfd || \
	    daemon_exists rpc.fed  || \
	    daemon_exists rgmd || \
	    daemon_exists ucmmd
	then 
		if [ ${_INIT_RUN_LEVEL} = "S" ] || \
		   [ ${_INIT_RUN_LEVEL} = "s" ] || \
		   [ ${_INIT_RUN_LEVEL} = "1" ] || \
		   [ ${_INIT_RUN_LEVEL} = "2" ]
		then
			echo \
"$0: Sun Cluster does not support transitioning from run-level 3 to levels S (single-user), 1, or 2, so halting"
			/sbin/sync &
			/usr/sbin/halt
			# In case halt fails call uadmin:
			/sbin/uadmin 2 0
		fi
	fi
		
	;;

*)
	echo "Usage: /etc/init.d/initrgm { start | stop }"
	;;
esac
