#!/bin/ksh
#
#pragma ident     "@(#)udlmreconfig.m4 1.6 01/09/05"
#Copyright (C) 1997-2001 Sun Microsystems, Inc.
#

#
# udlmreconfig - Energizer reconfiguration Program for UDLM.
#
# Input:
#               All environment variables like currnode/localnodied
#		"clustname"  current "cmmstep".(CURRSTEP)
#
# Action:       Run Reconfiguration Programs based on current
#               "cmmstepN"|"cmmabort"|"cmmstart"|"cmmreturn".
#
# Output:       Return 0 if success.
#               Return 1 if failure
#               Return 200 if reconfigurtion program result is to
#               be ignored.
set -e
pre="SUNWcluster.reconf.udlm"
RECONF_SCRIPTS=${RECONF_SCRIPTS:-/opt/SUNWcluster/etc/reconf/scripts}
CLUSTERBIN=${CLUSTERBIN:-/opt/SUNWcluster/bin/}
CLUSTERVAR=${CLUSTERVAR:-/var/opt/SUNWcluster/}
CLUSTERETC=${CLUSTERETC:-/etc/opt/SUNWcluster/}
INCLUDE=.

#
# Include common files if needed by UDLM
#
${INCLUDE} ${RECONF_SCRIPTS}/reconf_ener.common

udlm_arch() {

	LKMGR_ARCH=$(LC_MESSAGES=C /bin/file ${CLUSTERBIN}/lkmgr | \
		/bin/awk '/(32|64)-/ {print substr($3,0,2);}')
	UDLMCTL="${CLUSTERBIN}/udlmctl_${LKMGR_ARCH}"

}

check_lkmgr_arch() {

	udlm_arch;
	if [ "$LKMGR_ARCH" -ne "32" ]; then
		MACH_ARCH=$(LC_MESSAGES=C /bin/isainfo -b)
		if [ "$LKMGR_ARCH" -ne "$MACH_ARCH" ]; then
			log_info "$pre.4004" "${CLUSTERBIN}/lkmgr: Oracle UNIX DLM wrong instruction set architecture."
			exit 1
		fi
	fi

}

udlmstartnode_cmd()
{

	if [ -d ${CLUSTERBIN}/lkmgr -o ! -x ${CLUSTERBIN}/lkmgr  ]; then
		log_info "$pre.4000" "${CLUSTERBIN}/lkmgr:  Oracle UNIX DLM is not installed."
		exit 1
	fi

}

udlmstart_cmd() {

	# if no log location is specified, assign a default
	if [ "${LKMGR_LOG}X" = "X" ]; then
		export LKMGR_LOG
		LKMGR_LOG=${CLUSTERVAR}
	fi
 
	# start the Unix DLM.  We can't do it before this point because
	# clustd has to determine the nodeid.
	if [ `/bin/ps -u 0 | /bin/grep lkmgr | /bin/grep -v grep | \
			/bin/wc -l` -ne 0 ]; then
		log_info "$pre.4001" "${clustname} Unix DLM already running"
		exit 1
	fi

	log_info "$pre.1000" "${clustname} starting the Unix DLM."
	if (( cpus == 1 )); then
		DFLT_CLASS=TS
		if [[ -f ${CONF_FILE} ]] ; then
		        DFLT_CLASS=$(cat ${CONF_FILE})
		fi

		/usr/bin/priocntl -c ${DFLT_CLASS} -p 59 -m 59 \
			-e ${CLUSTERBIN}/lkmgr -n 2 -c ${cdbfile} \
			-i ${localnodeid}  > /dev/console 2>&1
	else
		DFLT_CLASS=$(enmatch udlm.schedclass)
		if [[ ${DFLT_CLASS} = "TS" ]] ; then
			if [[ -f ${CONF_FILE} ]] ; then
				DFLT_CLASS=$(cat ${CONF_FILE})
			fi
		fi
		/usr/bin/priocntl -c ${DFLT_CLASS} -p `enmatch \
			udlm.schedpriority` -e ${CLUSTERBIN}/lkmgr -n 2 \
			-c ${cdbfile} -i ${localnodeid}  > /dev/console 2>&1
	fi

}


udlmabort_cmd() {

	set +e

	if [ `/bin/ps -u 0 | /bin/grep lkmgr | /bin/grep -v grep | \
			/bin/wc -l` -ne 0 ]; then
		if [ -x ${CLUSTERBIN}/ogmsctl -a -x ${CLUSTERBIN}/ogms -a -x ${CLUSTERBIN}/scogms ]; then
			${CLUSTERBIN}/scogms stop_pmf $clustname ; retval=$?
			if [ $? -ne 0 ]; then
				log_info "$pre.4002" \
					"${clustname} Oracle GMS abort failed"
			fi
		fi
		udlm_arch
		${UDLMCTL} abort $clustname \
			`enmatch udlm.abort_timeout` 
		if [ $? -ne 0 ]; then
			log_info "$pre.4002" \
				"${clustname} Unix DLM abort failed"
			exit 1
		fi
	fi
	set -e

}

udlmstep3_cmd() {

	# The Unix DLM is "lkmgr" before initialization, "dlmmon" after
        #    initialization, and multiple dlmmon processes during fork.

	if [ `/bin/ps -u 0 | /bin/grep lkmgr | /bin/grep -v grep | \
			/bin/wc -l` -eq 0 ]; then
               log_info "$pre.4003" "${clustname} Unix DLM no longer running"
               exit 1
         fi
	udlm_arch
       ${UDLMCTL} step1 $clustname `enmatch udlm.step1_timeout` 

}

cpus=$(LC_MESSAGES=C /usr/sbin/psrinfo | /bin/grep on-line | /bin/wc -l)

case ${CURRSTEP} in

	startnode)
		udlmstartnode_cmd;
		check_lkmgr_arch;;

	cmmstart)
		udlmstart_cmd;;

	cmmabort)
		udlmabort_cmd;;

	cmmreturn)
		return 0;;

	cmmstep3)
		udlmstep3_cmd;;

	cmmstep4)
		udlm_arch;
       		${UDLMCTL} step2 \
			$clustname `enmatch udlm.step2_timeout`;;

	cmmstep5)
		udlm_arch;
       		${UDLMCTL} step3 \
			$clustname `enmatch udlm.step3_timeout`;;

	cmmstep6)
		udlm_arch;
       		${UDLMCTL} step4 \
			$clustname `enmatch udlm.step4_timeout`;;

	cmmstep7)
		udlm_arch;
       		${UDLMCTL} step5 \
			$clustname `enmatch udlm.step5_timeout`;;

	*)	;;

esac

exit 0

