:
# Filename: S89rdsd
# 
# Description:
#
# 	Start Diagnostic daemon.
#
#        INTEL CORPORATION PROPRIETARY INFORMATION		  
#								 
#  This software is supplied under the terms of a license	
#  agreement or nondisclosure agreement with Intel Corporation
#  and may not be copied or disclosed except in accordance
#  with the terms of that agreement.			
#
# Note on Filename:
#	It is important that the name of this script be such that
#	it is alphabetically after the name of the NFS script.
#	This gurantees that this script is started after NFS
#	since the diagnostic daemon needs NFS support. 
#
# Modification History:
# $Log: S89rdsd,v $
# Revision 1.6  1995/03/28  01:15:55  chowell
# chowell - added version string
#
# Revision 1.5  1994/01/28  00:55:39  mouli
# Added a function to make ps work on secure systems.
#
# Revision 1.4  1994/01/17  23:12:06  mouli
# The daemon startup script checks that portmap daemon is running.
# This fixes bug #7651.
#
# Revision 1.3  1993/03/25  18:57:02  mouli
# Took off debug echo
#
# Revision 1.2  1993/03/25  18:16:49  mouli
# 1. Took care of killing dtx_serv when the daemon is stopped.
# 2. dsd should be invoked from the directory where the diagnostic s/w
#    resides so that the daemon knows about the location of dtx_serv.
#
# Revision 1.1  1993/03/25  00:31:34  mouli
# Initial revision
#
#
##############################################################################
#
# What string:
#
# @(#) S89rdsd DIAG_REL_2.0 Thu Jan  5 12:04:09 PST 1995
#
##############################################################################

USAGE="Usage: $0 {start | stop }"
# Set up process names
# The dsd_path is important for starting up diagnostic daemon
# for it to know whereabouts of dtx_serv. Any change to the
# diagnostic s/w directory structure should reflect here.
dsd_path=/u/paragon/diag
dsd_proc=dsd
dtx_proc=dtx_serv
dsd_exe=${dsd_path}/${dsd_proc}
PROCS="${dsd_proc} ${dtx_proc}"
stat=0
# PATH is set here and exported so that the daemon and its executed
# programs get a known environment.
# 
PATH=".:/bin:/usr/bin:/etc:/usr/lib:/usr/dbin:/usr/ldbin:${dsd_path}"
export PATH
PS=
# Ps() makes ps work on secure systems
#
Ps()
{
  if [ "$PS" = "" ] ; then
      if [ -x /bin/ps ] ; then
	 /bin/su root -c /bin/ps 2>/tmp/ps.err$$ 1>&2
         if grep "not booted" /tmp/ps.err$$ ; then
	    PS="/bin/ps -n /unix.old"
         else
            PS="/bin/ps"
         fi
         rm -f /tmp/ps.err$$
      else
         echo "$0: Can't find ps"
	 echo "$0: Diagnostic daemon not started"
         exit 1
      fi
  fi
  /bin/su root -c "$PS $*" 
} 

case $1 in
	'start')
		Ps -e  2>/dev/null | \
			/usr/bin/fgrep ${dsd_proc} > /dev/null 2>&1

		if [ $? -eq 0 ]
		then
			echo "DSD is already running.  DSD start aborted."
			exit 0
		fi
	
		if [ ! -f ${dsd_exe} ] ; then
			echo "$0: Diagnostic daemon ${dsd_exe} is missing!"
			stat=1
		elif [ ! -x ${dsd_exe} ] ; then
			echo "$0: Diagnostic daemon ${dsd_exe} is not executable!"
			stat=1
		fi
		pcount=`Ps -u root | grep -c portmap`
		if [ "$pcount" -ne 1 ] ; then
			echo "$0: Diagnostic daemon requires portmap daemon to be running"
			echo "$0: portmap daemon is started by NFS startup"
			echo "$0: start NFS via NFS startup script"
			exit 1
		fi
		if [ ${stat} -eq 0 ] ; then
			(cd ${dsd_path}; ${dsd_exe})
			echo "DSD started"
		else
			echo "DSD could not be started !"
		fi
		exit ${stat}
		;;
	'stop')

		echo "DSD shutdown: \c"

#
# Get our ps out of the way
#
		Ps -e  2>/dev/null 1>/tmp/ps$$

#
# Now, kill the procs
#
# There is a hole here because some procs
# may die without us knowing about them
# because we are working from an old image.
#
		PIDS=`(
			for proc in ${PROCS}
			do
				cat /tmp/ps$$ | /bin/grep $proc | \
				/bin/grep -v grep | /usr/bin/awk '{ print $1 }'
			done )`

		if [ "${PIDS}" != "" ]
		then
			/bin/kill ${PIDS} 2> /dev/null
			sleep 5
			echo "[DSD Shutdown Complete]"
		else
			echo "DSD is not running currently"
		fi

		rm -f /tmp/ps$$
		;;
	*)
		echo $USAGE
		exit 1
		;;
esac
