#!/bin/sh
#
# Start/stop/restart a isi_sasphymon session
#
#
# Copyright C 2002-2003 by Isilon Systems, Inc. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
# 
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
# 
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems. 
#  
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

#
# This script is simply a wrapper around the isi_sasphymon daemon (program).
# It's job is to provide a simple start/stop/restart capability for the
# SAS PHY Monitor service.
#

# Set timeout duration for stopping
TIMEOUT=30

# QA Mode scan settings -- These values allow for QA to more easily 
#			   use the failpoint logic to inject errors
#			   and observe that the errors get detected
#			   Note that these values can only be used in 
#			   combination with the "-f" (fault injection 
#			   mode) command line option.
QA_SHORT_SCAN=5
QA_LONG_SCAN=10

PIDFILE=/var/run/isi_sasphymon.pid

case $1 in
start)
	if [ -e ${PIDFILE} ] && kill -0 `cat ${PIDFILE}` 2>/dev/null; then
		# We could just let isi_sasphymon see the other process 
		# and exit, but it's good to be told
		echo "isi_sasphymon [`cat ${PIDFILE}`] is already running"
		false
	else
		/usr/bin/isi_hwtools/isi_sasphymon -d
		if [ $? -eq 0 ]
		then
			echo "isi_sasphymon successfully started"
		else
			echo "isi_sasphymon did not start"
		fi
	fi
	;;
stop)
	if [ -e ${PIDFILE} ]; then
		killall -QUIT isi_sasphymon
		COUNT=0
		NEW_TIMEOUT=`expr $TIMEOUT \* 10`
		while killall -0 isi_sasphymon 2>/dev/null && test $COUNT -ne $NEW_TIMEOUT
		do 
			sleep 0.1
			COUNT=`expr $COUNT + 1` 
		done
		if test $COUNT -eq $NEW_TIMEOUT; then
			echo "$TIMEOUT second timeout waiting for process $PID to terminate"
			false
		else
			echo "isi_sasphymon successfully terminated"
			true
		fi
	else
		echo Warning: ${PIDFILE} not found
		# Do not consider this an error--allow restart to continue
		true
	fi
	;;
restart)
	if $0 stop; then
		$0 start
	fi
	;;

qa_mode)
	# QA Mode uses verbosity and fault injection mode
	/usr/bin/isi_hwtools/isi_sasphymon -d -v -f -s $QA_SHORT_SCAN -l $QA_LONG_SCAN
	if [ $? -eq 0 ]
	then
		echo "isi_sasphymon successfully started in QA mode"
	else
		echo "isi_sasphymon did not start in QA mode"
	fi
	;;

qa_restart)
	if $0 stop; then
		$0 qa_mode
	fi
	;;

*)
	echo "$0 {start|stop|restart|qa_restart}"
	;;
esac
