#!/bin/ksh
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident   "@(#)scm 1.76     04/04/06 SMI"
#
#######################################################################
#
#   This file contains system setup requirements for scm.
#   It should be located in /etc/init.d directory with the following links:
#
#       ln /etc/init.d/scm /etc/rc0.d/K84scm
#       ln /etc/init.d/scm /etc/rc2.d/S002scm
#
#USAGE="Usage: $0 { start | stop }
#
#######################################################################

set_system_type()
{
	if [ "`/usr/bin/isalist | grep -cw 'sparcv9'`" -ne 0 ]
	then
		PKG=/usr/kernel/drv/sparcv9
		ISS=2
	else
		PKG=/usr/kernel/drv
		ISS=1
	fi

	if [ -x /usr/opt/SUNWscm/sbin/dsclinfo ]
	then
		CLINFO=/usr/opt/SUNWscm/sbin/dsclinfo
	else
		CLINFO=/bin/false
	fi

	FLAGFILE=/etc/opt/SUNWesm/donotrun
	DMCLIB=/usr/opt/SUNWdmc/lib
	ESMSBIN=/usr/opt/SUNWesm/sbin
	SCMBIN=/usr/opt/SUNWesm/SUNWscm/sbin
	ESMSCMLIB=/usr/opt/SUNWesm/SUNWscm/lib
	SCMLIB=/usr/opt/SUNWscm/lib
	SYS=$ISS
}


do_stopsdbc ()
{
    if [ ! -r /dev/sdbc ]
    then
	return
    fi

    ${SCMBIN}/scmadm -d
    if [ $? -ne 0 ] ; then
	# If the disable failed that means we have pinned data.
	echo Cache Not Deconfigured
    fi
}

do_stopnskernd ()
{
  if [ -r /dev/hm ] && [ -x $ESMSCMLIB/hmadm ]
  then
    # See if the secondary health monitor is running, and kill it.
    if [ -x $ESMSBIN/hmipd ]
    then
      pid=`ps -e | grep -w hmipd | sed -e 's/^  *//' -e 's/ .*//'`
      if [ -n "$pid" ]
      then
	kill -15 $pid
      fi
    fi
    # see if slhm is running, and kill it before
    # we attempt to disable the health monitor
    pid=`ps -e | grep -w slhm | sed -e 's/^  *//' -e 's/ .*//'`
    if [ -n "$pid" ] ; then
      kill -15 $pid
    fi
    $ESMSCMLIB/hmadm -d
  fi

  ps -e | grep -w nskernd > /dev/null 2>&1
  if [ $? -eq 0 ] ; then
    # make sure that all data services are unloaded before stopping
    # nskernd - cannot stop nskernd when its threads could be in use
    # Note: sv is unloadable, but its threadset is shutdown in the
    # final close(9e) call.
    stop=1
    for m in ste rdc ii sdbc ; do
      mid=`/usr/sbin/modinfo | grep -w $m | awk '{print $1}' -`
      if [ -z "$mid" ] ; then
	continue	# not loaded
      fi
      /usr/sbin/modunload -i $mid > /dev/null 2>&1
      if [ $? -ne 0 ] ; then
	stop=0
	break
      fi
    done

    # kill nskernd if we can
    pid=`ps -e | grep -w nskernd | sed -e 's/^  *//' -e 's/ .*//'`
    if [ $stop -eq 1 ] ; then
      if [ -n "$pid" ] ; then
        kill -15 $pid
      fi
    fi
  fi

  if [ -r /dev/ncall -a -x $ESMSCMLIB/ncalladm ]
  then
    $ESMSCMLIB/ncalladm -d
  fi

  ps -e | grep ncalld > /dev/null 2>&1
  if [ $? -eq 0 ] ; then
    # only stop ncalld if sdbc is stopped
    pid=`ps -e | grep -w ncalld | sed -e 's/^  *//' -e 's/ .*//'`
    if [ -n "$pid" ] ; then
      kill -15 $pid
    fi
  fi
}

do_stopdscfglockd ()
{
  pid=`ps -e | grep -w dscfgloc | sed -e 's/^  *//' -e 's/ .*//'`
  if [ -n "$pid" ] ; then
    kill -15 $pid
  fi
}

do_stop ()
{
  do_stopsdbc
  do_stopnskernd
  # do_stopdscfglockd (turned off for 3.2)
}

do_nskernd ()
{
  if [ -x /usr/opt/SUNWdmc/lib/ncalld ]
  then
    /usr/opt/SUNWdmc/lib/ncalld
  fi

  if [ -x $ESMSCMLIB/ncalladm ]
  then
    $ESMSCMLIB/ncalladm -e
  fi

  if [ -x $ESMSBIN/ncallipadm ]
  then
    $ESMSBIN/ncallipadm -e
  fi

  ps -e | grep -w nskernd > /dev/null 2>&1
  if [ $? -ne 0 ] ; then
    ${SCMLIB}/nskernd
    if [ $? -ne 0 ] ; then
      echo "Error: Unable to start nskernd"
      exit 1
    fi
  fi

  # Note: scmadm -e assumes that hm is enabled
  if [ -x $ESMSCMLIB/hmadm ]
  then
    $ESMSCMLIB/hmadm -e
  fi
  # Start the secondary health monitor.
  if [ -x $ESMSBIN/hmip ]
  then
    $ESMSBIN/hmip -e
  fi 
}

do_dscfglockd ()
{
  if [ -x $DMCLIB/ncallcfg.sh ]
  then
    # configure ncall, ncallip and dscfglockd for multinode systems
    $DMCLIB/ncallcfg.sh
  fi

  ps -e | grep -w dscfgloc > /dev/null 2>&1
  if [ $? -ne 0 ]
  then
    rm -f /var/tmp/.cfglockd.pid
  else
    # dscfglockd already running
    return
  fi

  if $CLINFO
  then
    # clustered start of dscfglockd
    if [ -f /var/opt/SUNWesm/dscfglockd.cf ]
    then
      echo "Starting dscfglockd"
      ${SCMLIB}/dscfglockd -f /var/opt/SUNWesm/dscfglockd.cf
    else
      echo "WARNING: Mis-Configuration of StorEdge Dataservices for clusters"
      echo "WARNING: Can't find configuration file for dscfglockd"
    fi
  else
    # non clustered start of dscfglockd here, if required
    if [ -f /var/opt/SUNWesm/dscfglockd.cf ]
    then
      echo "Starting dscfglockd"
      ${SCMLIB}/dscfglockd -f /var/opt/SUNWesm/dscfglockd.cf
    fi
  fi
}

do_sdbc ()
{
      ${SCMBIN}/scmadm  -e
      if [ $? -ne 0 ] ; then
        echo "Error: Cache initialization failed"
        exit 1
      fi
}


#
# do_bitmapfs()
# - mount any bitmap filesystems that the StorEdge Data Services
#   software is going to need before S01MOUNTFSYS is run.
#
do_bitmapfs()
{
	TMPFSTAB=/var/tmp/scm$$
	VFSTAB=/etc/vfstab

	if $CLINFO
	then
		# in cluster
		if [ "$CLUSTERTAG" != "" ]
		then
			# cluster node start, bitmapfs not supported, return
			return
		else
			# physical host start, nothing to do, return
			return
		fi
	else
		:	# non-clustered start
	fi

	/usr/bin/rm -f $TMPFSTAB
	/usr/bin/touch $TMPFSTAB >/dev/null 2>&1
	if [ ! -f $TMPFSTAB ]
	then
		echo "Could not create $TMPFSTAB"
		echo "StorEdge Data Service bitmap filesystems not mounted"
		return
	fi

	$SCMBIN/scmadm -L 2>/dev/null |
	while read bitmap
	do
		case "$bitmap" in
		'#'*|'')
			continue	# ignore comment or blank line
			;;
		esac

		/usr/bin/awk '
			BEGIN {
				found = 0
			}

			("'$bitmap'" == $1 || "'$bitmap'" == $3) {
				# matches block device or mount point
				found = 1
				# print, converting "mount at boot" to "yes"
				printf("%s %s %s %s %s yes %s\n", \
					$1, $2, $3, $4, $5, $7)
				exit
			}

			END {
				if (!found) {
					print \
"Error: '$bitmap' found in configuration but not found in '$VFSTAB'" |"cat >&2"
				}
			}' $VFSTAB
	done >$TMPFSTAB

	if [ -s $TMPFSTAB ]
	then
		echo "Mounting StorEdge Data Service bitmap filesystems"
		/usr/sbin/mountall $TMPFSTAB
	fi

	/usr/bin/rm -f $TMPFSTAB
}


do_start ()
{
  #
  # Ordering:
  #	dscfglockd	-- locking must be present before any dscfg access
  #	nskernd		-- starts infrastructure (nskernd, ncall and hm).
  #	bitmapfs	-- find and mount bitmap filesystems
  #	sdbc		-- start the cache itself
  #
  # do_dscfglockd (turned off for 3.2)
  do_nskernd
  do_bitmapfs
  do_sdbc
}


do_usage ()
{
  echo "Usage: $0"
  echo "   start"
  echo "   stop"
  exit 1
}

set_system_type

USED=0
ACTION=
CLUSTERTAG=

case $# in 
'0')
     do_usage
     ;;
'1') 
     ACTION=$1
     USED=1
     ;;
'2')
     ACTION=$1
     CLUSTERTAG="$2"
     USED=1
     exit 0
     ;;
'*')
     do_usage
     ;;
esac

if [ $USED = 0 ] ; then
     do_usage
fi

if [ $ACTION = "start" ] ; then
  # presence of $FLAGFILE prevents SVE software fom starting
  if [ -f $FLAGFILE ] ; then
    echo "$0: $FLAGFILE is set: startup aborted"
    exit 0
  fi
  do_start
elif [ $ACTION = "stop" ] ; then
  do_stop
else 
  do_usage
fi

exit 0
