#!/bin/sh
# @(#)ms_srv.sh	1.43 07/09/03
#
# stops, starts, or restarts the server 
#

unset PCNL_INSTANCE
LMPATHS="/opt/lanman/lib/lmpaths"

LMXCTRL_PATH=/opt/lanman/lib/lmx.ctrl

#if [ -f ${LMPATHS} ]
#then
#    . ${LMPATHS}
#else
#    . lmpaths
#fi

# Choose variable initialization based on whether SC2.2 or SC3.0 :
if [ -x /usr/cluster/bin/scstat ]
then
  CLlist=/opt/lanman/lib/sc3/ha_list
else
  CLlist=/opt/lanman/lib/ha/ha_list
fi

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

DEV_NULL_PATH=/dev/null
TEMPFILE=/tmp/.lmxpsfile$$
OnCluster="no"
TargetLogicalHost=""

#
# First check if ha is installed: if so, if ha is configured;
# and if so, make sure the current machine is mastering the
# logical host supporting the PC Netlink service
#

if [ -x $CLlist ]
then
     ${CLlist} -l > $DEV_NULL_PATH 2>&1
     if [ $? -eq 0 ]
     then
	OnCluster="yes"
     fi
fi

#
# Stop the server
#
xstop () {
	DoAll="no"

	# Next check to see that the user is root
	lname=`id`
	case "$lname" in
	uid=0*)
	:
	;;
	*)
	echo "You must be logged in as root in order to stop the Solaris (TM) PC NetLink processes."
	exit 0;;
	esac

	if [ ${PCNL_INSTANCE:-"0"} != "0" ]
	then 
	    Ilist=`/opt/lanman/lib/instlist ${PCNL_INSTANCE}| nawk '{print $1}'`
	else
	    Ilist=`/opt/lanman/lib/instlist | nawk '{print $1}'`
	    DoAll="yes"
	fi

	for i in $Ilist
	do 

	PCNL_INSTANCE=$i

	if [ -f ${LMPATHS} ]
	then
	    . ${LMPATHS}
	else
	    . lmpaths
	fi

	if [ $OnCluster = "yes" ]
	then 
	  lhost="`/opt/lanman/lib/instlist ${PCNL_INSTANCE} | nawk '{print $3}'`"
	  if [ "$lhost" != ""  -a "$lhost" != "$TargetLogicalHost" ]
	  then
	      master=`${CLlist} -m $lhost 2> $DEV_NULL_PATH`
	      if [ "${master}" != "`uname -n`" ]
	      then 
		continue
	      fi
	  fi
       	fi

	# From here on there are several points where we might
	# decide that netlink is stopped and continue
	#
	# However, we can be sure that if we get paseed here we will stop PC Netlink,
	# so mark it stopped.

	cp /dev/null /tmp/.pcnl_instance_${PCNL_INSTANCE}_stopped

        # First check to see that there are processes running

	rm -rf $TEMPFILE
	ps -ef -o pid,args | fgrep 'lmx.' > $TEMPFILE
	psline=`cat $TEMPFILE | egrep "lmx.* -I ${PCNL_INSTANCE}$"`
	if [ "$psline" = "" ]
	then
		continue
#		echo "Error when trying to stop Solaris (TM) PC NetLink"
#		echo "Solaris (TM) PC NetLink processes were not running."
#		exit 0
	fi

	#
	# Stop $LMXCTRL_NAME which will kill any possibly running $LMXSRV_NAMEs
	# Find the pid and kill it.
	#
	ctrlpid=`while read pid cmd dashi inst rest
	do 
		if [ "$cmd" = "lmx.ctrl" -a "${inst:-0}" = "$PCNL_INSTANCE" ]
		then 	
			echo $pid
			kill $pid
			break
		fi
	done < $TEMPFILE`

#	psline=`cat $TEMPFILE | fgrep "$LMXCTRL_NAME -I $PCNL_INSTANCE"`
#	if [ "$psline" ]
#	then
#		set -- $psline
#		ctrlpid=$2
#		kill $ctrlpid

#		Wait for awhile, checking to see if $LMXCTRL_NAME died

	if [ ${ctrlpid:-"0"} != "0" ]
	then
		counter=0
		while kill -0 $ctrlpid && [ "$counter" -lt 15 ]
		do
			if [ "$counter" -ge 10 ]
			then
				kill -9 $ctrlpid
				if [ $? -ne 0 ]
				then 
					break
				fi
			fi
			counter=`expr ${counter} "+" "1"`
			sleep 2
		done
	fi > $DEV_NULL_PATH 2>&1
#	fi

	# Now, kill anything left with 'lmx' in its name!

	rm $TEMPFILE
	ps -ef -o pid,args | fgrep 'lmx.' >$TEMPFILE
	pidlist=`while read pid cmd dashi inst rest
	do 
#RJK add lmx check
		if [ ${dashi:-"0"} = "-I" -a ${inst:-0} = $PCNL_INSTANCE ]
		then 	
			echo $pid
		fi
	done < $TEMPFILE`

	for pid in $pidlist
	do
		kill -9 $pid
#		if [ $? -ne 0 ]
#		then
#			echo didn't kill $pid
#		fi
	done > $DEV_NULL_PATH 2>&1



	rm $TEMPFILE
	ps -ef -o pid,args | fgrep 'lmx.' > $TEMPFILE
	pidlist=`while read pid cmd dashi inst rest
	do 
		if [ ${dashi:-"0"} = "-I" -a ${inst:-0} = $PCNL_INSTANCE ]
		then 	
			echo $pid
		fi
	done < $TEMPFILE`

	if [ "$pidlist" != "" ]
	then
		echo "Error when trying to stop Solaris (TM) PC NetLink"
		echo "All of the processes did not terminate:"
		echo "$psline"
	else
		echo "Solaris (TM) PC NetLink daemons have been stopped."
	fi

	rm $TEMPFILE

	$DELSHMEM_PATH -I $PCNL_INSTANCE -f > $DEV_NULL_PATH 2>&1

	done

	if [ "${DoAll}" = "no" ]
	then
	  DoAll="yes"
	  #Stop lmx.ntpw and lmx.solpw, if there is no PCNL instance running.
	  Ilist=`/opt/lanman/lib/instlist | nawk '{print $1}'`
	  for Inumber in ${Ilist}
	  do
	    Idir=`/opt/lanman/lib/instlist ${Inumber} | awk '{print $2}'`
	    if [ -p ${Idir}/var/opt/lanman/${Inumber}/${CTRLPIPE_NAME} ]
	    then
	      DoAll="no"
	      break
	    fi
	  done
	fi

	if [ "${DoAll}" = "yes" ]
	then
	    rm -f $TEMPFILE
	    ps -ef -o pid,args | egrep "lmx.ntpw|lmx.solpw" > $TEMPFILE
	    pidlist=`while read pid cmd rest
	    do 
		echo $pid
	    done < $TEMPFILE`

	    for pid in $pidlist
	    do
		kill -9 $pid
	    done > $DEV_NULL_PATH 2>&1

	    rm -f $TEMPFILE

	fi


}

fixup_blobs()
{
    for blob in ${LSA_PATH} ${ACL_PATH} ${REGISTRY_PATH} ${CHANGELOG_PATH} ${SHAREFILE_PATH} ${SAMDIR_PATH}/*
    do
	if expr "${blob}" : '.*\..*' >/dev/null 2>&1
	then
	    continue

	elif [ -f "${blob}.pregrow" ]
	then
	    # blob file may be corrupt; replace it with pregrown file
	    mv -f "${blob}.pregrow" "${blob}"
	fi
    done

    # now tidy up any partly-written grow backup files

    for dir in `dirname ${LSA_PATH}` ${SAMDIR_PATH}
    do
	rm -f ${dir}/.lmtmp??????
    done
}

#
# Start the server
#
xstart ()
{
	DoAll="no"

	# Next check to see that the user is root
	lname=`id`
	case "$lname" in
	uid=0*)
	:
	;;
	*)
	echo "You must be logged in as root in order to start the Solaris (TM) PC NetLink processes."
	exit 0;;
	esac

	if [ ${PCNL_INSTANCE:-"0"} != "0" ]
	then 
	    Ilist=`/opt/lanman/lib/instlist ${PCNL_INSTANCE}| nawk '{print $1}'`
	else
	    Ilist=`/opt/lanman/lib/instlist | nawk '{print $1}'`
	    DoAll="yes"
	fi


	SolPwStarted="no"
	NtPwStarted="no"

	pwcomms="`ps -ef -o comm | egrep 'lmx.ntpw|lmx.solpw'`"

	for comm in $pwcomms
	do
	    case $comm in
	    *lmx.solpw)
		SolPwStarted="yes"
		;;
	    *lmx.ntpw)
		NtPwStarted="yes"
		;;
	     esac
	done

	if [ ${SolPwStarted} != "yes" ]
	then
	    cd `dirname $LMXCTRL_PATH` && lmx.solpw <$DEV_NULL_PATH
	fi

	if [ ${NtPwStarted} != "yes" ]
	then
	    cd `dirname $LMXCTRL_PATH` && lmx.ntpw <$DEV_NULL_PATH
	fi

	for i in $Ilist
	do 

	PCNL_INSTANCE=$i

	if [ -f ${LMPATHS} ]
	then
	    . ${LMPATHS}
	else
	    . lmpaths
	fi

	if [ $OnCluster = "yes" ]
	then 
	  lhost="`/opt/lanman/lib/instlist ${PCNL_INSTANCE} | nawk '{print $3}'`"
	  if [ "$lhost" != ""  -a "$lhost" != "$TargetLogicalHost" ]
	  then
	      master=`${CLlist} -m $lhost 2> $DEV_NULL_PATH`
	      if [ "${master}" != "`uname -n`" ]
	      then 
		continue
	      fi
	  fi
       	fi


        
    # first check to see if server already running
	ps -ef -o pid,args  | fgrep 'lmx.' > $TEMPFILE
	pidlist=`while read pid cmd dashi inst rest
	do 
		if [ ${cmd} = "lmx.passwd" -a ${inst:-0} = $PCNL_INSTANCE ]
		then
			continue
		fi
		if [ ${dashi:-"0"} = "-I" -a ${inst:-0} = $PCNL_INSTANCE ]
		then 	
			echo $pid
		fi
	done < $TEMPFILE`
	if [ "$pidlist" != "" ]
	then
	    echo "Solaris (TM) PC NetLink daemons are already running."
	    continue
#	    exit 0
	fi
	while read pid cmd dashi inst rest
	do
		if [ ${cmd} = "lmx.passwd" -a ${inst:-0} = $PCNL_INSTANCE ]
		then
			kill $pid
		fi
	done < $TEMPFILE
	rm -rf $TEMPFILE
	
	fixup_blobs

	# Tell the kernel where to put core files.
	# The setting applies to this process and
	# all its children (all lmx.*)
	if [ -x /usr/bin/coreadm ] ; then
	  /usr/bin/coreadm -p ${DEBUGDIR_PATH}/core.%p $$
	fi


        #
        # remove unused shared memory segments left
        # by lmx processes
        #
        $DELSHMEM_PATH -I $PCNL_INSTANCE -f > $DEV_NULL_PATH 2>&1


	# delete 'service stopped' file

	rm -f /tmp/.pcnl_instance_${PCNL_INSTANCE}_stopped

	#
	# if 'timex' is active, then cleanup old timex(1) files and
	# timex(1) lmx.ctrl; else, just start lmx.ctrl
	#
	echo "Starting Solaris (TM) PC NetLink daemons...."
	timex=`${SRVCONFIG_PATH} -I $PCNL_INSTANCE -g "lmxserver,timex"`
	if [ ${timex:-0} -ne 0 ]
	then
		#
		# clean up the TIMEXDIR_PATH
		#
		if [ -d "${TIMEXDIR_PATH}" ]
		then
			rm -fr ${TIMEXDIR_PATH}/lmx*
		else
			mkdir -p ${TIMEXDIR_PATH}
		fi

		#
		# and run things
		#
		cd `dirname $LMXCTRL_PATH` && \
			$TIMEX_PATH -s $LMXCTRL_NAME -I $PCNL_INSTANCE < $DEV_NULL_PATH \
				2> ${TIMEXDIR_PATH}/${LMXCTRL_NAME}
	else
		cd `dirname $LMXCTRL_PATH` && `$LMXCTRL_NAME -I $PCNL_INSTANCE` < $DEV_NULL_PATH
	fi

	done


}

#
# Check to see nbdaemon is running, if not warn user
#
check_netbios()
{
    if [ -z "`ps -e | grep nbdaemon`" ]
    then
	echo "WARNING: Sun NetBIOS software is not running. If you are not using a third party NetBIOS software, start Sun NetBIOS software by [ /etc/init.d/netbios start ] first."
    fi
}

signal_netbios()
{
    ps -ef -o pid,args | grep nbdaemon | grep -v grep | `read pid rest
    if [ "$pid" != "" ]
    then
	kill -USR1 $pid
    fi`
}

case $1 in
'stop' | '-stop')	
	if [ $# -gt 1 ]
	then
		PCNL_INSTANCE=$2
	fi
	if [ $# -gt 2 ]
	then
		TargetLogicalHost=$3
	fi
	xstop
	signal_netbios
	exit 0
	;;

'start' | '-start')
	if [ $# -gt 1 ]
	then
		PCNL_INSTANCE=$2
	fi
	if [ $# -gt 2 ]
	then
		TargetLogicalHost=$3
	fi
        check_netbios
	signal_netbios
	xstart
	exit 0
	;;

'restart' | '-restart')
	if [ $# -gt 1 ]
	then
		ORG_PCNL_INSTANCE=$PCNL_INSTANCE
		PCNL_INSTANCE=$2
	fi
	xstop
	if [ $# -le 1 ]
	then
		PCNL_INSTANCE=$ORG_PCNL_INSTANCE
	fi
	xstart
	exit 0
	;;

'powerfail' | '-powerfail')
	#
	# Find the pid of $LMXCTRL_NAME and send SIGPWR to it
	#
	if [ $# -gt 1 ]
	then
		PCNL_INSTANCE=$2
	fi
	psline=`ps -er | fgrep "$LMXCTRL_NAME -I $PCNL_INSTANCE -s"`
	if [ "$psline" != "" ]
	then
		set -- $psline
		pid="$1"
		kill -19 "$pid"
	fi
	exit 0
	;;

esac

echo "Usage: $0 start|stop|restart|powerfail [instance]"
exit 1
