#!/bin/sh -p
# 
# @(#)vmsa_server	1.23 02/08/05 09:05:10
#
# Copyright(C) 2000 VERITAS Software Corporation. ALL RIGHTS RESERVED.
# UNPUBLISHED -- RIGHTS RESERVED UNDER THE COPYRIGHT
# LAWS OF THE UNITED STATES.  USE OF A COPYRIGHT NOTICE
# IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
# OR DISCLOSURE.
#
# THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND
# TRADE SECRETS OF VERITAS SOFTWARE.  USE, DISCLOSURE,
# OR REPRODUCTION IS PROHIBITED WITHOUT THE PRIOR
# EXPRESS WRITTEN PERMISSION OF VERITAS SOFTWARE.
#
#               RESTRICTED RIGHTS LEGEND
# USE, DUPLICATION, OR DISCLOSURE BY THE GOVERNMENT IS
# SUBJECT TO RESTRICTIONS AS SET FORTH IN SUBPARAGRAPH
# (C) (1) (ii) OF THE RIGHTS IN TECHNICAL DATA AND
# COMPUTER SOFTWARE CLAUSE AT DFARS 252.227-7013.
#               VERITAS SOFTWARE
# 1600 PLYMOUTH STREET, MOUNTAIN VIEW, CA 94043
#
#
stop_server()
{
	if [ -f $PID_FILE ]
	then
		echo "Stopping $PROD_NAME Server"
		while read pid
		do
			kill $pid >/dev/null 2>&1
		done < $PID_FILE
		rm -f $PID_FILE
	else
		echo "Unable to stop $PROD_NAME Server"
	fi
}

check_vold()
{
	if [ -x /usr/sbin/vxdctl -o -x /sbin/voldctl ]
	then
		if [ -x /sbin/voldctl ]
		then
			output=`voldctl mode`
		else
			output=`vxdctl mode`
		fi
		mode="`expr "\$output" : '.*: \(.*\)'`"
                if [ "$mode" != "enabled" ]
                then
                        echo "$VMNAME daemon not enabled, $PROD_NAME VM server cannot be started."
			exit 2
                fi
	else
		echo "$VMNAME daemon not found, $PROD_NAME VM server cannot be started."
		exit 2
	fi
}

check_permission()
{
	_UID=`id | cut -c5- | sed 's/(.*//'`
	if [ $_UID != "0" ]
	then 
		echo "Permission denied"; 
		exit 1; 
	fi
}

sig_rebind()
{
	rebind_rcvd=1 
	if [ "$signal_rebind" = 1 ]
	then
		# there is a need to inform vmsa client that rebind is over.
		# one way out would be to inform the parent daemon process that
		# could then inform the client. The other one is to use a 
		# special file descriptor for talking to the daemon.
		# using 3, 4, 5 as stdin, stdout, stderr for the socket
		echo "rebind ok">&4
	fi
	echo "rebind ok"
}

# sets CMD_PID to the process id of the process in the background
run_cmd() {
	if [ "$1" = "-bg" ]
	then 
		bg=1 #background
		shift
	else
		bg=0
	fi

	if [ "$1" = "-se" ]
	then 
		std_echo=1 
		shift
	else
		std_echo=0
	fi

	if [ "$LOG_OUTPUT" = 1 ]
	then
		if [ $bg = 1 ]
		then
			$* >> $LOGDIR/server.log 2>&1 &
			CMD_PID=$!
		else
			$* >> $LOGDIR/server.log 2>&1 
		fi
	else
		std_echo=1
	fi

	if [ $std_echo = 1 ] 
	then
		if [ $bg = 1 ]
		then
			$* & 
			CMD_PID=$!
		else
			$* 
		fi
	fi
	if [ $bg = 1 ]
	then
		echo "$CMD_PID">>$PID_FILE
	fi
}

start_server() {
	# check if root
	check_permission
	#
	# see if vxconfigd enabled
	if [ "$force_start" != 1 ]
	then
		check_vold
	fi
	#
	dgmax=`vxdctl support|grep dg_max|awk '{print $2}'` 
	if [ $dgmax = 70 ]
	then
		LD_LIBRARY_PATH="$VMSALIBDIR/3.1:$LD_LIBRARY_PATH"
		SHLIB_PATH="$VMSALIBDIR/3.1:$SHLIB_PATH"
	fi
	#
	# check to see if rmiregistry, cmdserver, and/or VMServer are already running
	REGPIDS=`ps -ef | grep VRTSReg | grep -v grep | sed -e 's/^  *root  *//' -e 's/ .*//'`
	CMDPIDS=`ps -ef | grep cmdserver | grep -v grep | sed -e 's/^  *root  *//' -e 's/ .*//'`
	SRVPIDS=`ps -ef | grep VMServerImpl | grep -v grep | sed -e 's/^  *root  *//' -e 's/ .*//'`
	if [ -f $PID_FILE -a "$signal_rebind" = 1 ] 
	then
		# grep against ps -ef can fail if the path is too long because
		# ps preserves only about 80 characters of the executable path. 
		# our only hope is to assume that the server is up if the pid
		# file is present. 
		sig_rebind 
		exit 0
	fi


	if [ "$REGPIDS" = "" -a "$CMDPIDS" = "" ]
	then
		rm -f $PID_FILE
	fi

	rm -f $STOP_FILE

	if [ "$REGPIDS" ]
	then
		# The following message is sent to the log because
		# all registry, cmd server, and server messages are logged 
		# when LOG_OUTPUT is set. Only when we exit with such
		# messages do we want to let the users know on standard out. 
	  	run_cmd echo "RMI Registry is already running"
	else
 	    	run_cmd -se echo "Starting RMI Registry" 
		if [ "$LOG_OUTPUT" = 1 ]
		then
                	run_cmd echo `date`  
		fi
                run_cmd -bg $JRE $TH_TYPE $JIT $GC -cp $CP $PROPS $POLICY -Dvrts.codebase=$CODEBASE vrts.vxfw.services.misc.VxVRTSRegistryImpl
	fi

 	CODEBASE_URL="file:$CODEBASE/"

	if [ "$CMDPIDS" ]
	then
  		run_cmd echo "$PROD_NAME Command Server already running" 
	else
  		run_cmd -se echo "Starting $PROD_NAME Command Server" 
		if [ "$LOG_OUTPUT" = 1 ]
		then
                	run_cmd echo `date`  
		fi
  		run_cmd -bg $CMDSERVER
		sleep 1
	fi

	if [ "$SRVPIDS" ]
	then
		if [ "$signal_rebind" = 1 ]
		then
			# this is the best we can do
			# the server is up and we really do not know
			# if it is bound
			sig_rebind
			# 0 should be the right exit status in this case 
			# because the parent is merely waiting for the
			# server to rebind
			exit 0
		else
			echo "$PROD_NAME Server is already running"
			exit 1
		fi
	else
		if [ "$signal_rebind" = 1 ]
		then
			ARGS=-s
		fi
		if [ "$force_start" = 1 ]
		then
			ARGS="$ARGS -f"
		fi
                run_cmd -se echo "Starting $PROD_NAME Server" 
		if [ "$LOG_OUTPUT" = 1 ]
		then
                	run_cmd echo `date`  
		fi
                run_cmd -bg $JRE $TH_TYPE $JIT $GC -cp $CP $PROPS $POLICY -Dvrts.codebase=$CODEBASE -Djava.rmi.server.codebase="$CODEBASE_URL"  -Dvrts.localHost=$LOCALHOST -Djava.rmi.server.hostname=$LOCALHOST $READONLY vrts.remote.VMServerImpl $ARGS 

		# Note that a signal will interrupt this wait.
		# Currently we do not go into a while loop because
		# a rebind signal should be received only once.
	
		# Also note that, after the trap, control is returned to the 
 		# statement following the statement being currently executed 
 	 	# (even if it is a function call). Therefore, this trap
		# will not work if it is placed outside of this function. 
 		trap "sig_rebind" USR1
		wait $CMD_PID >/dev/null 2>&1
		rc=$?
		if [ "$rebind_rcvd" = 1 ]  
		then
			rebind_rcvd=0 
			wait $CMD_PID >/dev/null 2>&1
			rc=$?
		fi
		if [ $rc != "0" ]
		then
			echo "$PROD_NAME Server terminated."
		fi
		if [ ! -f $STOP_FILE ]
		then
			stop_server
		fi
	fi
}



usage()
{
        cat >& $1 <<-EOF
           vmsa_server - $PROD_NAME Server
	   usage: vmsa_server [-V|-q|-k|-r|-h]
           Options:
              -V      Prints the version
              -q      Query to check if server is running
              -k      Kill the server processes
              -r      Start server in Read-Only mode
              -u      Print client users connected to the server
              -h      Prints out this help message

EOF
}

export PATH
unset ENV # prevent .kshrc from being executed by java/jre
umask 022
cd `dirname $0`
. ./pkg_params
. ./server_params

if [ ! "$LOGDIR" ]
then
	LOGDIR=/var/opt/vmsa/logs
fi
if [ ! -d $LOGDIR ]
then
	mkdir -p $LOGDIR
        chmod 755 $LOGDIR
fi

PID_FILE=$LOGDIR/.server_pids
STOP_FILE=$LOGDIR/.vmsa_stop

if [ ! "$JRE" ]
then
	JRE=jre
fi


LD_LIBRARY_PATH="$VMSALIBDIR:$LD_LIBRARY_PATH"; export LD_LIBRARY_PATH
SHLIB_PATH="$VMSALIBDIR:$SHLIB_PATH"; export SHLIB_PATH
LIBPATH="$VMSALIBDIR:$LIBPATH"; export LIBPATH
if [ ! -f /usr/sbin/vxrelayout ]
then
	LD_LIBRARY_PATH="$VMSALIBDIR/2.x:$LD_LIBRARY_PATH"
	SHLIB_PATH="$VMSALIBDIR/2.x:$SHLIB_PATH"
elif [ ! -f /etc/vx/bin/vxunreloc ]
then
	LD_LIBRARY_PATH="$VMSALIBDIR/3.0.x:$LD_LIBRARY_PATH"
	SHLIB_PATH="$VMSALIBDIR/3.0.x:$SHLIB_PATH"
fi

unset JAVA_HOME	# if set, may cause `java' to get confused
unset CLASSPATH # if classpath is set, on some versions of java, the class.zip
 		# file does not get included in the class path.
signal_rebind=0
force_start=0
bail=0
while getopts kVqrusfh  c
do
	case $c in
	k)   	# kill server
		check_permission;
		touch $STOP_FILE;
		stop_server;
		rm -f $STOP_FILE;
		echo; bail=1;;
	V)   	# show version 
		$JRE $TH_TYPE -cp $CP $PROPS -Dvrts.codebase=$CODEBASE vrts.remote.VMServerImpl -V; 
		echo; bail=1;;
	q)   	#  query the server to find out if it is running
		$JRE $TH_TYPE -cp $CP $PROPS -Dvrts.codebase=$CODEBASE vrts.remote.VMServerImpl -q; 
		echo; bail=1;;
	r)   	# set server read-only 
		READONLY="-Dvrts.server.readonly=true" ;;
	      
	u) 	# find the users connected to the specified server
		$JRE $TH_TYPE -cp $CP $PROPS -Dvrts.codebase=$CODEBASE vrts.remote.VMServerMgr -users; 
		echo; bail=1;;
	s)   	# request the server to signal successful rebind 
		signal_rebind=1;;
	f)	# force the server to start without vxconfigd
		force_start=1;;
	h|*) 	# print help/usage message 
		usage;
		echo; bail=1;;
	esac
done
shift `expr $OPTIND - 1`
if [ $# -gt 0 ]
then
	echo "Extra arguments (ignored): "$*
 	echo
fi
 
if [ $bail -eq 1 ]
then
	exit 0
fi

trap "stop_server; exit 0" 1 2
# Note that trap for signal_rebind is not placed here because, after serving
# the trap, control will return to the statement after start_server.
start_server
exit $rc
