#!/bin/sh

# Start/stop/restart a km_d session
#

# Copyright C 2002-2013 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_km_d.pid

. /etc/rc.subr

name="isi_km_d"

load_rc_config $name

case $1 in
start)
	if [ -e ${PIDFILE} ] && kill -0 `cat ${PIDFILE}` 2>/dev/null; then
		# We could just let km_d see the other process and exit, 
		# but it's good to be told
		echo "isi_km_d [`cat ${PIDFILE}`] is already running"
		false
	else
		/usr/libexec/isilon/isi_km_d $2 $3 $4 $5 $6 $7 $8 $9
		echo "isi_km_d successfully started"
	fi
	;;
stop)
	if [ -e ${PIDFILE} ]; then
		killall isi_km_d
		COUNT=0
		while killall -0 isi_km_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_km_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
