#!/bin/sh
#
#ident	"@(#)clccrad	1.2	04/01/07 SMI"
#
# Copyright 2001-2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# usr/src/cmd/initpkg/init.d/clccrad

# Start/stop clccrad



SERVER_NAME=cl_ccrad
SERVER_PATH=/usr/cluster/lib/sc
SERVER=${SERVER_PATH}/${SERVER_NAME}
PMFADM=/usr/cluster/bin/pmfadm 
CLCCRAD_TAG_NAME=cl_ccrad

ECHO=/usr/bin/echo
LOGGER=/usr/bin/logger
KILL=/usr/bin/kill
CLINFO=/usr/sbin/clinfo
PGREP=/usr/bin/pgrep
PMFD_NAME=rpc.pmfd

#
# wait for deamon's death , to be sure that pmf really killed it 
# before trying to restart it
#

wait_for_daemon_death () {
   
   stop=1

   while [ $stop -gt 0 ]
   do
	daemon_is_running
	diag_run=$?
	daemon_is_monitored
	diag_mon=$?
	
	stop=`expr $diag_run + $diag_mon`
   done

}

#
# check if monitoring daemon is running 
# to avoid to be stuck with pmfadm command
#

daemon_pmf_is_running() {
    ${PGREP} -u 0 -P 1 -x ${PMFD_NAME} > /dev/null 2>&1
    if [ $? -eq 0 ]
    then
	return 1
    fi
    
    return 0
}

daemon_is_monitored() {
    
    daemon_pmf_is_running
    if [ $? -eq 0 ]
    then
	return 0
    fi

    ${PMFADM} -q ${CLCCRAD_TAG_NAME}
    if [ $? -eq 0 ]
    then
	return 1
    fi

    return 0
}

daemon_is_running() {
    
    #the server may have been started manualy
    
    #usual cmd is to test -u 0 -P 1 
    #here we need to know even if daemon was launched manually 
    deamon_pid=`/usr/bin/pgrep -x ${SERVER_NAME}`
    if [ -z "$deamon_pid" ] ; then
	return 0
    fi
    
    return $deamon_pid

}

daemon_start() {
        
        # test whether we are a cluster and exit if not a cluster
	${CLINFO} > /dev/null 2>&1
	if [ $? != 0 ] ; then
		exit 0
	fi


        # We only allow one clccrad running at a time
	
	daemon_is_running
	if [ $? -gt 0 ] 
	then
	    ${LOGGER} -p local0.err -t CL_CCRAD "Error: ${SERVER_NAME} is already running."
	    return 1
	fi
	
	#
	# Start under PMF with 4 retries and a two minute failure period.
	# If there are more than 4 failures in a two minute period, the pmf
	# will stop attempting to restart the daemon.
	#
	
	daemon_pmf_is_running
	if [ $? -ne 1 ]
	then
	    ${LOGGER} -p local0.err -t CL_CCRAD "Cannot start ${SERVER_NAME}, ${PMFD_NAME}is not running."
	    return 1;
	fi

	
	${PMFADM} -c ${CLCCRAD_TAG_NAME} -t 2 -n 4 ${SERVER};

	if [ $? -ne 0 ]; then
		${LOGGER} -p local0.err -t CL_CCRAD "Error: Can't start ${SERVER}.";
		return 1;
	fi

	return 0
}

daemon_stop() {
    
    

    # test whether we are a cluster
    # if not don't try to contact pmf
    ${CLINFO} > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
      	  daemon_is_monitored
	  if [ $? -eq 1 ]
	  then
		# Send SIGKILL via PMF
		${PMFADM} -s ${CLCCRAD_TAG_NAME} 9 > /dev/null 2>&1;
		if [ $? -eq 0 ] ; then
		    return 1
		fi
	    return 0
	  fi
    fi

  
    
    daemon_is_running
    pid=$?
    if [ $pid -gt 0 ]
    then
	${KILL} -9 $pid
    fi
    
    return 0
}

daemon_usage() {
    ${ECHO} "Usage: /etc/init.d/clccrad { start | stop | restart | status}";
}





if [ -z "$1" ]
then
    daemon_usage
    exit 1  
fi

case $1 in
'start')
        daemon_start
	if [ $? -eq 1 ]
	then
	    ${LOGGER} -p local0.err -t CL_CCRAD "Error: ${SERVER_NAME} cannot be started."
	    exit 1
	fi
	;;
'stop')
        daemon_stop
	;;
'restart')
        daemon_stop
	wait_for_daemon_death
	daemon_start
	if [ $? -eq 1 ]
	then
	    ${LOGGER} -p local0.err -t CL_CCRAD "Error: ${SERVER_NAME} cannot be started."
	    exit 1
	fi
	;;
'status')
        daemon_is_running
	pid=$?
	if [ "$pid" -gt 0 ]
	then
	    ${ECHO} "${SERVER_NAME} is running (pid=${pid})."
	    daemon_is_monitored
	    if [ $? -eq 1 ]
	    then
		${ECHO} "and monitored."
	    fi
	else
	    ${ECHO} "${SERVER_NAME} is not running."
	fi
	
	;;
'help')
	daemon_usage
	exit 0
	;;
*)
	daemon_usage
	exit 1
	;;
esac








