#!/bin/sh

# Start/stop/restart a drive_d 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.

# Set timeout duration for stopping
TIMEOUT=30

PIDFILE=/var/run/isi_drive_d.pid

. /etc/rc.subr

name="isi_drive_d"

load_rc_config $name

case $1 in
start)
	if [ -e ${PIDFILE} ] && kill -0 `cat ${PIDFILE}` 2>/dev/null; then
		# We could just let drive_d see the other process and exit, 
		# but it's good to be told
		echo "isi_drive_d [`cat ${PIDFILE}`] is already running"
		false
	else
		#
		# Refer to Bugzilla #57084 for more information, but
		# don't start the drive daemon if we're in manufacturing.
		# The reason being that a rebalance operation might be
		# started which could cause some imdd volumes to be 
		# mounted (i.e. /usr/update/root) and be written to 
		# while isi_mfg_check is running. This causes all sorts
		# of nasty things to happen, not the least of which is
		# when the imdd volume is found to be mounted, it is 
		# automatically unmounted by isi_mfg_check, but the 
		# mount point "/usr/update/root" on the MFG flash is then
		# exposed and the writes to to the flash rather than the
		# imdd volume and the flash ends up filling up.
		#
		if ! isi_checkyesno manufacturing; then
			/usr/sbin/daemon -f \
		    		/usr/libexec/isilon/isi_drive_d $2 $3 $4 \
								$5 $6 $7 $8 $9
			echo "isi_drive_d successfully started"
		else
			echo "Not starting isi_drive_d due to manufacturing"
		fi
	fi
	;;
stop)
	if [ -e ${PIDFILE} ]; then
		killall isi_drive_d
		COUNT=0
		while killall -0 isi_drive_d 2>/dev/null && test $COUNT -ne $TIMEOUT
		do 
			sleep 1
			COUNT=`expr $COUNT + 1` 
		done
		if test $COUNT -eq $TIMEOUT; then
			echo "$TIMEOUT second timeout waiting for process $PID to terminate"
			false
		else
			echo "isi_drive_d 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 $2 $3 $4 $5 $6 $7 $8 $9
	fi
	;;
*)
	echo "$0 {start|stop|restart}"
	;;
esac
