#! /bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2007 
# 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 
#
# The rpc.statd daemon started by nfslock service uses glibc bindresvport()
# function to bind a reserve port. bindresvport() arbitrarily binds to a 
# port in the range 600-1023 without checking /etc/services. Sometimes,
# it happened to bind to 657, the RMC reserved port. The real fix would
# be made to bindresvport() to honor the reservations in /etc/services.
# The work around is to arrange the start up sequence so that ctrmc 
# starts before nfslock.
# In RedHat, the start up sequence of nfslock is 14 and that of syslog
# is 12. Move ctrmc start up sequence from 33 to 13.
# In SuSE, the sequence depends on the dependency between services. 
# ctrmc depends on $network, $syslog, and $remote_fs. However, $remote_fs
# depends on nfs. This dependency causes ctrmc to start after nfslock.
# We have to temporarily remove $remote_fs dependency.
#
# ===== RedHat/Turbo chkconfig =====
# chkconfig: 2345 13 87
# description: Resource Monitoring and Control services
#
#*===========================================================================*/
#*                                                                           */
#* Module Name:  rc_ctrmc                                                    */
#*                                                                           */
#* Description:                                                              */
#*      Script to start/stop the Resource Monitor & Control Daemon           */
#*      at bootup or when the runlevel changes.                              */
#*                                                                           */
#* Note: Due to changes in chkconfig processing in RedHat there are          */
#*       2 versions of this script, rc_ctrmc and rc_ctrmc_rh.  The only      */
#*       difference between the 2 versions is in the LSB comments section.   */
#*       Any changes required to the script must be made to both versions.   */
#*===========================================================================*/
#*   @(#)27   1.2   src/rsct/rmc/mcdaemon/rc_ctrmc_rh.sh, mcdaemon, rsct_relgh, relghs001a 5/14/07 10:40:17

# define function to wait for the ctrmc subsystem to start

wait_ctrmc() {
	typeset	rc

	let i=$RETRY_LIMIT

	while :
	do
		lsrsrcdef-api -c '*' > /dev/null 2>&1
		rc=$?
		if [[ $rc -eq 0 ]]
		then
			break
		fi

		i=$((i - 1))

		if (( i > 0 ))
		then
			sleep 1
		else
			break
		fi
	done

	return $rc
}

# define function to start the ctrmc subsystem. Use initlog to log messages

start_ctrmc() {
	if [[ -x /sbin/initlog ]]
	then
		initlog $I_ARGS -c "rmcctrl -s"
		rc=$?
		if [[ $rc -eq 0 ]]
		then
			wait_ctrmc
			initlog $I_ARGS -n rc_ctrmc -s "ctrmc startup" -e 1
			# touch lock file /var/lock/subsys/ctrmc in rmcctrl -s
			#touch /var/lock/subsys/ctrmc
		else
			initlog $I_ARGS -n rc_ctrmc -s "ctrmc startup" -e 2
		fi
	else
		rmcctrl -s
		rc=$?
		if [[ $rc -eq 0 ]]
		then
			wait_ctrmc
		fi
	fi
	return $rc
}

# define function to stop the ctrmc subsystem. Use initlog to log messages

stop_ctrmc() {
	if [[ -x /sbin/initlog ]]
	then
		initlog $I_ARGS -c "rmcctrl -k"
		rc=$?
		if [[ $rc -eq 0 ]]
		then
			initlog $I_ARGS -n rc_ctrmc -s "ctrmc shutdown" -e 1
			# remove lock file /var/lock/subsys/ctrmc in rmcctrl -k
			#rm -f /var/lock/subsys/ctrmc
		else
			initlog $I_ARGS -n rc_ctrmc -s "ctrmc shutdown" -e 2
		fi
	else
		rmcctrl -k
		rc=$?
	fi
	return $rc
}

# Set path to known value
PATH=/usr/sbin/rsct/bin:/usr/bin:/usr/sbin:/bin:/sbin
export PATH

# Set retry limit when waiting for ctrmc to come up
RETRY_LIMIT=20

if [[ "$BOOTUP" != "verbose" ]]
then
	I_ARGS="-q"
else
	I_ARGS=
fi

# Invoke the logic specified by the command argument

case "$1" in
	start)
		start_ctrmc
		rc=$?
		;;

	stop)
		stop_ctrmc
		rc=$?
		;;

	status)
		lssrc -s ctrmc
		rc=$?
		;;

	restart|reload)
		stop_ctrmc
		start_ctrmc
		rc=$?
		;;

	*)
		echo "Usage: rc_ctrmc {start|stop|status|restart}"
		exit 1
		;;
esac

exit $rc

