#! /usr/bin/ksh
#
# ident	"@(#)udlmreconfig.sh	1.9	03/04/17 SMI"
#
# Copyright 1997-2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# udlmreconfig - reconfiguration program for Oracle Unix DLM
#
# Input:
#	Environment variables:
#		${CLUSTNAME}
#		${CURRSTEP}
#		${CURRNODES}
#		${LOCALNODEID}
#		${RECONF_SCRIPTS}
#		${LKMGR_LOG}
#		${STEP_VERSION}
#
# Action:       Run Reconfiguration Programs based on current ucmm step
#
# Output:       Return 0 if success
#               Return 1 if failure
#               Return 200 if error result is to be ignored
#

#
# set some flags (NOTE: they are not set implicitly in subroutines)
#
# set -x # print commands as executed
# set -e # execute ERR trap on command error
# set -u # treat unset variables as an error
# set -f # disable file name generation

#
# Local variables
#
pre="SUNWudlm.udlmreconfig"
udlmctlbin="/opt/SUNWudlm/bin"
udlm_cfgfile="/opt/SUNWudlm/etc/udlm.conf"

udlmbin="/opt/SUNWcluster/bin";
default_lkmgr_log="/var/cluster/ucmm";
num_dlmds=2;
udlm_dbg_flag="-d 0xfff";
udlm_dbg_flag="";
udlmctl_dbg_flag="-d 0xfff";
udlmctl_dbg_flag="";

#
# Include common files if needed by UDLM
#
include=.
RECONF_SCRIPTS=${RECONF_SCRIPTS:-/usr/cluster/lib/ucmm}
${include} ${RECONF_SCRIPTS}/ucmm_reconf.common

udlm_cfg_match()
{

	${RECONF_SCRIPTS}/cfgmatch $1 ${udlm_cfgfile}

}

udlmstart_cmd() {

	check_lkmgr_arch;
	# if no log location is specified, assign a default
	export LKMGR_LOG=${LKMGR_LOG:-${default_lkmgr_log}}

	if [ "${LKMGR_LOG}X" = "X" ]; then
		export LKMGR_LOG=${default_lkmgr_log}
	fi
 
	# The DLM should not be running yet, error if it is
	/bin/pgrep -u 0 lkmgr >/dev/null 2>&1
	if [ $? -eq 0 ]; then
		scds_syslog -p error -t "${pre}.udlmstart_cmd" -m \
			"Unix DLM already running"
		exit 1
	fi

	# start the Unix DLM.  We can't do it before this point because
	# clustd has to determine the nodeid.
	log_info "${pre}.udlmstart_cmd" "Starting the Unix DLM."
	cpus=$(/usr/sbin/psrinfo | /bin/grep on-line | /bin/wc -l)
	if (( cpus == 1 )); then
		/usr/bin/priocntl -c TS -p 59 -m 59 \
			-e ${udlmbin}/lkmgr -n ${num_dlmds} \
			-c ${udlm_cfgfile} \
			-i ${LOCALNODEID} ${udlm_dbg_flag} > /dev/console 2>&1
	else
		/usr/bin/priocntl -c `udlm_cfg_match udlm.schedclass` \
			-p `udlm_cfg_match udlm.schedpriority` \
			-e ${udlmbin}/lkmgr -n ${num_dlmds} \
			-c ${udlm_cfgfile} \
			-i ${LOCALNODEID} ${udlm_dbg_flag} > /dev/console 2>&1
	fi

}


udlmabort_cmd() {

	udlminit;
	set +e
	/bin/pgrep -u 0 lkmgr >/dev/null 2>&1
	if [ $? -eq 0 ]; then
		${udlmctl} ${udlmctl_dbg_flag} abort $CLUSTNAME \
			`udlm_cfg_match udlm.abort_timeout` 
		if [ $? -ne 0 ]; then
			scds_syslog -p error -t "${pre}.udlmabort_cmd" -m \
				"Unix DLM abort failed"
			exit 200
		fi
	fi
	set -e

}

udlmstep1_cmd() {

	udlminit;
	# The Unix DLM is "lkmgr" before initialization, "dlmmon" after
        #    initialization, and multiple dlmmon processes during fork.
	/bin/pgrep -u 0 lkmgr >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		scds_syslog -p error -t "${pre}.udlmstep1_cmd" -m \
			"Unix DLM no longer running"
		exit 1
	fi
	${udlmctl} ${udlmctl_dbg_flag} step1 $CLUSTNAME `udlm_cfg_match udlm.step1_timeout` 

}

udlminit() {

	if [ -d "${udlmbin}/lkmgr" -o ! -x "${udlmbin}/lkmgr" ]; then
		scds_syslog -p error -t "${pre}.udlminit" -m \
			"Oracle UDLM package is not properly installed. %s not found." \
			"${udlmbin}/lkmgr"
		exit 1
	fi

	lkmgr_arch="`/bin/file ${udlmbin}/lkmgr | \
		/bin/awk '/(32|64)-/ {print substr($3,0,2);}'`"
	udlmctl="${udlmctlbin}/udlmctl_${lkmgr_arch}"
	if [ -d "${udlmctl}" -o ! -x "${udlmctl}" ]; then
		scds_syslog -p error -t "${pre}.udlminit" -m \
			"%s not found." "${udlmctl}"
		exit 1
	fi

}

check_lkmgr_arch()
{

	udlminit;

	if [ -d /bin/isainfo -o ! -x /bin/isainfo ]; then
		if [ "$lkmgr_arch" -ne "32" ]; then
			scds_syslog -p error -t "${pre}.check_lkmgr_arch" -m \
				"Oracle UDLM package wrong instruction set architecture."
			exit 1
		fi
	else
		if [ "$lkmgr_arch" -ne "32" ]; then
			if [ "$lkmgr_arch" -ne `/bin/isainfo -b` ]; then
				scds_syslog -p error \
				-t "${pre}.check_lkmgr_arch" \
				-m "Oracle UDLM package wrong instruction set architecture."
				exit 1
			fi
		fi
	fi

}

#
# turns on tracing for all functions
#
# typeset -tf $(typeset +f)


# Set default step version to B if step version is empty

[ -z "${STEP_VERSION:-""}" ] &&  STEP_VERSION="B"


log_info "${pre}.udlmreconfig"  "Running step version \"${STEP_VERSION}\" step ${CURRSTEP}";

if [ "${STEP_VERSION}" = "A" ]; then
# Step order for version A
case ${CURRSTEP} in
	cmmstart)
		udlmstart_cmd;;

	cmmabort|cmmstop)
		udlmabort_cmd;;

	cmmstep2)
		#
		# In step version A, work will be done only
		# when called in 'cmmstep2' and with step order 10.
		#
		progname=`/bin/basename $0`
		if [ "${progname}" = "10_udlm" ]; then
			
			udlmstep1_cmd
		fi
		;;

	cmmstep3)
		udlminit;
       		${udlmctl} ${udlmctl_dbg_flag} step2 \
			$CLUSTNAME `udlm_cfg_match udlm.step2_timeout`;;

	cmmstep4)
		udlminit;
       		${udlmctl} ${udlmctl_dbg_flag} step3 \
			$CLUSTNAME `udlm_cfg_match udlm.step3_timeout`;;

	cmmstep5)
		udlminit;
       		${udlmctl} ${udlmctl_dbg_flag} step4 \
			$CLUSTNAME `udlm_cfg_match udlm.step4_timeout`;;

	cmmstep6)
		udlminit;
       		${udlmctl} ${udlmctl_dbg_flag} step5 \
			$CLUSTNAME `udlm_cfg_match udlm.step5_timeout`;;

	cmmstep1 | cmmstep7 | cmmstep8 | cmmstep9 | cmmstep10)
		# No work in this step
		;;

	*)
		scds_syslog -p error -t "${pre}" -m \
			"Unknown step: %s" "${CURRSTEP}";
		exit 200;;
esac
elif [ "${STEP_VERSION:-}" = "B" ]; then
# Step order for version B

#
#       main switch statement, execute appropriate reconfiguration step
#
case ${CURRSTEP} in

	cmmstart)
		udlmstart_cmd;;

	cmmabort|cmmstop)
		udlmabort_cmd;;

	cmmstep2)
		#
		# In step version B, work will be done only
		# when called in 'cmmstep2' and with step order 05.
		#
		progname=`/bin/basename $0`
		if [ "${progname}" = "05_udlm" ]; then
			
			udlmstep1_cmd
		fi
		;;

	cmmstep4)
		udlminit;
       		${udlmctl} ${udlmctl_dbg_flag} step2 \
			$CLUSTNAME `udlm_cfg_match udlm.step2_timeout`;;

	cmmstep5)
		udlminit;
       		${udlmctl} ${udlmctl_dbg_flag} step3 \
			$CLUSTNAME `udlm_cfg_match udlm.step3_timeout`;;

	cmmstep6)
		udlminit;
       		${udlmctl} ${udlmctl_dbg_flag} step4 \
			$CLUSTNAME `udlm_cfg_match udlm.step4_timeout`;;

	cmmstep7)
		udlminit;
       		${udlmctl} ${udlmctl_dbg_flag} step5 \
			$CLUSTNAME `udlm_cfg_match udlm.step5_timeout`;;

	cmmstep1 | cmmstep3 | cmmstep8 | cmmstep9 | cmmstep10)
		# No work in this step
		;;

	*)
		scds_syslog -p error -t "${pre}" -m \
			"Unknown step: %s" "${CURRSTEP}";
		exit 200;;
esac
else
	scds_syslog -p error -t "${pre}" -m \
			"Unknown step version: %s" "${STEP_VERSION}";
	exit 200;
	
fi
exit 0

