#!/bin/sh
#
#pragma ident	"@(#)initpmf	1.16	03/11/13 SMI"
#
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#


# Start/stop processes required for init pmf

LIBSCDIR=/usr/cluster/lib/sc
BINDIR=/usr/cluster/bin
USRBIN=/usr/bin
SERVER=rpc.pmfd

# The following three variables are used
# to create the file name used by libsecurity.
TRANSPORT=ticotsord
PROGNUM=100248
VERSNUM=2
FILE=/var/run/scrpc/${PROGNUM}.${VERSNUM}.${TRANSPORT}

#
# 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 $?
}


#
# daemon_kills daemonname
#
# Kill the daemon named daemon name
# (if several process are named daemonname then all
# of them are killed, however this should not happen)
daemon_kills()
{
	PIDS=`${USRBIN}/pgrep -u 0 -P 1 -x $1`
	if [ -n "${PIDS}" ]
	then
		# kill all processes in list
                kill -TERM ${PIDS}
		echo "Notice: ${1} is being stopped."
	fi
}


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

	# Find if an instance of the server is already running and if yes exit
	# We only allow one rpc.pmfd at a time.
	if daemon_exists ${SERVER}
	then
		${USRBIN}/logger -p local0.err -t INITPMF "Error: ${SERVER} is already running."
                exit 0;
	fi

	# Find out if host is running 32 or 64 bit Solaris
	# and start the appropriate server
	version=`${USRBIN}/optisa sparcv9 sparc`
	if [ "${version}" = "sparcv9" ] ; then
		${LIBSCDIR}/sparcv9/${SERVER}
	else
		${LIBSCDIR}/${SERVER}
	fi

	if [ $? -ne 0 ]; then
		${USRBIN}/logger -p local0.err -t INITPMF "Error: Startup of ${SERVER} failed."
		exit 1
	fi

	# Loop for 2 min checking that the server is up.
	#
	# We need to wait until the server is up and registered before
	# moving on, so that init scripts that follow us will be able
	# to succeed if they depend on us being up.
	# We rely on the file created by libsecurity to detect if the
	# server is ready
	COUNTER=120
	while [ ${COUNTER} -gt 0 ]
	do
		COUNTER=`expr ${COUNTER} - 1`

		daemon_exists ${SERVER}
		if [ $? -ne 0 ]
		then
		    # if the process disappeared there is no need to
		    # wait for the file to be created. startup failed.
		    ${USRBIN}/logger -p local0.err -t INITPMF "Error: Startup of ${SERVER} failed."
		    exit 1
		fi

		# As soon as the file is created we assume that the server is up
		# and running.
		if [ -f ${FILE} ]
		then
		    # check that rpc.pmfd is accessible with pmfadm command
		    ${BINDIR}/pmfadm -L 2>/dev/null 1>/dev/null
		    if [ $? -ne 0 ]; then
			${USRBIN}/logger -p local0.err -t INITPMF "Error: ${SERVER} is running but not accessible."
			# kill rpc.pmfd to force the other init scripts to fail
			daemon_kills ${SERVER}
			exit 1
		    fi

		    # rpc.pmfd is ready
		    exit 0
		fi

		# we log an error every 30 seconds in case of failure
		if [ `expr ${COUNTER} % 30` -eq 0 ]
		then
			${USRBIN}/logger -p local0.warning -t INITPMF "Waiting for ${SERVER} to be ready."
		fi

		${USRBIN}/sleep 1

	done

	# startup failed
	# We log an error
	daemon_kills ${SERVER}
	${USRBIN}/logger -p local0.err -t INITPMF "Error: Startup of ${SERVER} failed."
	exit 1
	;;

'stop')
	if [ -z "${_INIT_RUN_LEVEL}" ]
	then
	    # If the environment variable _INIT_RUN_LEVEL is not set
	    # it means that the command is called outside of run
	    # control environment
	    ${USRBIN}/logger -p local0.err -t INITPMF "Error: Can't stop \
${SERVER} outside of run control environment."
	    exit 1
	fi

	# Find if rpc.pmfd is running and if yes kill it.
	# This works on a list, but there shouldn't be more than one
	# instance running (see start case).
	daemon_kills ${SERVER}
	;;
*)
	echo "Usage: /etc/init.d/initpmf { start | stop }"
	;;
esac
