#!/bin/sh
#
#pragma ident	"@(#)initfed	1.14	03/11/07 SMI"
#
# Copyright 1998-2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Start/stop processes required for init fed

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


# The following three variables are used
# to create the file name used by libsecurity.
TRANSPORT=ticotsord
PROGNUM=100142
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

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

	# no need to loop since we looped when we started rpc.pmfd
	for PROCESS in rpc.pmfd
	do
	    daemon_exists ${PROCESS}
	    if [ $? -ne 0 ]
	    then
                ${USRBIN}/logger -p local0.err -t INITFED "Error: ${PROCESS} \
is not running."
		exit 1
	    fi
	done

	# start the server
	${LIBSCDIR}/${SERVER}
	if [ $? -ne 0 ]; then
		${USRBIN}/logger -p local0.err -t INITFED "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.
		    ${USRBIN}/logger -p local0.err -t INITFED "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
			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 INITFED "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 INITFED "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 INITFED "Error: Can't stop \
${SERVER} outside of run control environment."
	    exit 1
	fi

	# Find if the server 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/initfed { start | stop }"
	;;
esac
