#! /bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2000,2008 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
#*===========================================================================*/
#*                                                                           */
#* Module Name:  rmcd_start                                                  */
#*                                                                           */
#* Description:                                                              */
#*      Script to start the Resource Monitor & Control Daemon                */
#*                                                                           */
#* N.B. This command is intended to be executed by the RMC subsystem only !! */
#*                                                                           */
#*===========================================================================*/
#*   @(#)58   1.8.1.11   src/rsct/rmc/mcdaemon/rmcd_start.sh, mcdaemon, rsct_rfos, rfos0838a 8/25/08 22:49:28 

PATH=/usr/sbin/rsct/install/bin:/usr/sbin/rsct/bin:/usr/bin:/usr/sbin:/bin
export PATH

FIRST=/var/ct/first_start
FIRST_LOG=/var/ct/first.log
START_PORT_FILE=/var/ct/cfg/ctrmc.rpe
REVERSE_HB_FILE=/var/ct/cfg/ctrmc.rhb
MSG_POLICY_FILE=/var/ct/cfg/ctrmc.msgp
DMSG_POLICY_FILE=/var/ct/cfg/ctrmc.dmsgp
TIMEOUT_OPTS_FILE=/var/ct/cfg/ctrmc.topts
MIG_CKPT_FILE=/lpp/rsct.core/rsct.core.rmc.146721.marker
MIG_CKD_FILE=/var/ct/cfg/ctrmc.tblmig
NO_CLIENT_PKT_CHK_FILE=/var/ct/cfg/ctrmc.nopktchk

REDHAT_RELFILE="/etc/redhat-release"
REDHAT_VERSION="^Red Hat Enterprise Linux .* release 3 (.*)$"
SUSE_RELFILE="/etc/SuSE-release"
SUSE_VERSION="^SUSE LINUX Enterprise Server 9 (.*)$"
SLES9_PATCH="PATCHLEVEL"

# sed string to extract subsystem state from "lssrc -s <subsys>" output
PARSE_SRC_STATE="-e1d;s/.* \([a-zA-Z0-9]*\)$/\1/"

# if the first start file is present, start the ctfirst script in the
# background.

if [[ -a $FIRST ]]
then
	ctfirst > $FIRST_LOG 2>&1 &
fi

# if a migration of the ctrmc.mntbl and ctrmc.mcptbl has not yet been done,
# do so now. 
#
# N.B. this migration must be done just prior to starting the RMC daemon,
# and not at install time, since the configuration files may be backed up
# pre-install and restored post-install.

if [[ ! -a $MIG_CKD_FILE ]]
then
	MIGVRMF=$(migrate_ctrmc_tbls 2)
	rc=$?
	if [[ $rc -eq 0 ]]
	then
		print "$MIGVRMF" > $MIG_CKD_FILE	# may be NULL string
		chmod 644 $MIG_CKD_FILE

		OS=$(uname -s)		# get operating system name

		if [[ $OS = "AIX" && ! -a $MIG_CKPT_FILE && -n $MIGVRMF ]]
		then
			print "$MIGVRMF" > $MIG_CKPT_FILE
			chmod 644 $MIG_CKPT_FILE
		fi

		# In the same release as the table format changes, the ACL file
		# must now include an entry for IBM.PublicKeyExchange

		print "IBM.PublicKeyExchange\n\tUNAUTHENT C x" | chrmcacl -r
	fi
fi

# check if remote clients are to be accepted

if [[ -a $START_PORT_FILE ]]
then
	ROPT="-r"
else
	ROPT=
fi

# check if Reverse Heartbeat is to be enabled in the Management Domain

if [[ -a $REVERSE_HB_FILE ]]
then
	RHBOPT="-R"
else
	RHBOPT=
fi

# check if client message authentication policy is to be set

MOPT=
if [[ -s $MSG_POLICY_FILE ]]
then
	POLICY=$(< $MSG_POLICY_FILE)
	case $POLICY in
		R ) MOPT="-m" ;;
		D ) MOPT="-M" ;;
	esac
fi

# check if daemon-to-daemon message authentication policy is to be set

NOPT=
if [[ -s $DMSG_POLICY_FILE ]]
then
	POLICY=$(< $DMSG_POLICY_FILE)
	case $POLICY in
		R ) NOPT="-n" ;;
		D ) NOPT="-N" ;;
	esac
fi

# check if RMCAPI client packet check is to be disabled

if [[ -a $NO_CLIENT_PKT_CHK_FILE ]]
then
	POPT="-p"
else
	POPT=
fi


# check if running on RH EL release 3 or SLES 9. If so, special processing

if [[ -f $REDHAT_RELFILE ]]
then
	grep "$REDHAT_VERSION" $REDHAT_RELFILE > /dev/null 2>&1
	if (($? == 0))
	then
		export LD_ASSUME_KERNEL=2.4.19
	fi
elif [[ -f $SUSE_RELFILE ]]
then
	grep "$SUSE_VERSION" $SUSE_RELFILE > /dev/null 2>&1
	if (($? == 0))
	then
		PATCH_LEVEL=`grep "$SLES9_PATCH" $SUSE_RELFILE | awk '{print $3}' 2>/dev/null`
		if [[ -z "$PATCH_LEVEL" || $PATCH_LEVEL -lt 4 ]]
        
		then
			export LD_ASSUME_KERNEL=2.4.19
		fi
	fi
fi

# check for timeout options

TOPT=
UOPT=
VOPT=
WOPT=
XOPT=
if [[ -s $TIMEOUT_OPTS_FILE ]]
then
	while read Flag junk
	do
		case $Flag in
			MSG_TIMEOUT* )
					FlagValue=${Flag#MSG_TIMEOUT=}
					TOPT="-t $FlagValue"
					echo $TOPT
					;;
			START_TIMEOUT* )
					FlagValue=${Flag#START_TIMEOUT=}
					UOPT="-u $FlagValue"
					echo $UOPT
					;;
			CMD_TIMEOUT* )
					FlagValue=${Flag#CMD_TIMEOUT=}
					VOPT="-v $FlagValue"
					echo $VOPT
					;;
			CMD_THRESHOLD* )
					FlagValue=${Flag#CMD_THRESHOLD=}
					WOPT="-w $FlagValue"
					echo $WOPT
					;;
			CMD_AUTHENT_ENABLE  )
					XOPT=-x
					echo $XOPT
					;;
		esac
	done < $TIMEOUT_OPTS_FILE
fi

#define resource classes that support new ACLs

AOPT="-a IBM.LPCommands"

# now start the RMC daemon in the current process so it is the child of the
# SRC subsystem daemon

unset EXTSHM
exec /usr/sbin/rsct/bin/rmcd $AOPT $ROPT $RHBOPT $MOPT $NOPT $POPT \
		$TOPT $UOPT $VOPT $WOPT $XOPT  $@

