#!/bin/sh
#
# tgtd<---->Linux target framework (tgt) aims to simplify various SCSI target driver
#
# chkconfig: - 90 10
# description:<>Linux target framework (tgt) aims to simplify various SCSI target \
#		driver (iSCSI, Fibre Channel, SRP, etc) creation and maintenance.
# processname: tgtd
# config: /etc/tgt/targets.conf
# pidfile: /var/run/tgtd.pid

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

PIDFILE=/var/run/tgtd.pid
LOCKFILE=/var/lock/subsys/tgtd
RETVAL=0

TGTD_CONFIG=/etc/tgt/targets.conf

TASK=$1

start()
{
	echo -n "Starting iSCSI target framework daemon"
	# Start tgtd first.
	tgtd &>/dev/null
	RETVAL=$?
	PID=$(pidof -s tgtd)
	if [ "$RETVAL" -ne 0 ] ; then
	    echo -n " ...  Could not start tgtd (is tgtd already running?)"
	    echo_failure
	    echo
	    exit 1
	else
	    echo $PID > $PIDFILE
	    touch $LOCKFILE
	    echo_success
	    echo
	fi
	# Put tgtd into "offline" state until all the targets are configured.
	# We don't want initiators to (re)connect and fail the connection
	# if it's not ready.
	tgtadm --op update --mode sys --name State -v offline
	# Configure the targets.
	tgt-admin -e -c $TGTD_CONFIG
	# Put tgtd into "ready" state.
	tgtadm --op update --mode sys --name State -v ready
}

stop()
{
	if [ "$RUNLEVEL" == 0 -o "$RUNLEVEL" == 6 ] ; then
	    forcedstop
	fi
	echo -n "Stopping iSCSI target framework daemon"
	# Remove all targets. It only removes targets which are not in use.
	tgt-admin --update ALL -c /dev/null &>/dev/null
	# tgtd will exit if all targets were removed
	tgtadm --op delete --mode system &>/dev/null
	RETVAL=$?
	if [ "$RETVAL" -eq 107 ] ; then
	    echo -n "  ...  tgtd is NOT running"
	    rm -f $LOCKFILE
	    rm -f $PIDFILE
	    echo_passed
	    echo
	    [ "$TASK" != "restart" ] && exit 1
	elif [ "$RETVAL" -ne 0 ] ; then
	    echo -n "  ...  Some initiators are still connected - could not stop tgtd"
	    echo_failure
	    echo
	    exit 2
	else
	    rm -f $LOCKFILE
	    rm -f $PIDFILE
	    echo_success
	    echo
	fi
	echo -n
}

forcedstop()
{
	# NOTE: Forced shutdown of the iscsi target may cause data corruption
	# for initiators that are connected.
	echo -n "Force-stopping iSCSI target framework daemon"
	# Offline everything first. May be needed if we're rebooting, but
	# expect the initiators to reconnect cleanly when we boot again
	# (i.e. we don't want them to reconnect to a tgtd which is still
	# working, but the target is gone).
	tgtadm --op update --mode sys --name State -v offline &>/dev/null
	RETVAL=$?
	if [ "$RETVAL" -eq 107 ] ; then
	    echo -n "  ...  tgtd is not running"
	    rm -f $LOCKFILE
	    rm -f $PIDFILE
	    echo_passed
	    echo
	    [ "$TASK" != "restart" ] && exit 1
	else
	    tgt-admin --offline ALL
	    # Remove all targets, even if they are still in use.
	    tgt-admin --update ALL -c /dev/null -f
	    # It will shut down tgtd only after all targets were removed.
	    tgtadm --op delete --mode system
	    RETVAL=$?
	    if [ "$RETVAL" -ne 0 ] ; then
		echo -n "  ...  Failed to shutdown tgtd"
		echo_failure
		echo
		exit 1
	    else
		rm -f $LOCKFILE
	        rm -f $PIDFILE
		echo_success
		echo
	    fi
	fi
	echo -n
}

reload()
{
	echo -n "Updating iSCSI target framework daemon configuration"
	# Update configuration for targets. Only targets which
	# are not in use will be updated.
	tgt-admin --update ALL -c $TGTD_CONFIG &>/dev/null
	RETVAL=$?
	if [ "$RETVAL" -eq 107 ] ; then
	    echo -n "  ...  tgtd is not running"
	    echo_passed
	    echo
	    exit 1
	else
	    echo_success
	    echo
	fi
}

forcedreload()
{
	echo -n "Force-updating iSCSI target framework daemon configuration"
	# Update configuration for targets, even those in use.
	tgt-admin --update ALL -f -c $TGTD_CONFIG &>/dev/null
	RETVAL=$?
	if [ "$RETVAL" -eq 107 ] ; then
	    echo -n "  ...  tgtd is not running"
	    echo_passed
	    echo
	    exit 1
	else
	    echo_success
	    echo
	fi
}

status()
{
	# Don't name this script "tgtd"...
	if [ -e "$PIDFILE" ] ; then
	    name=$(ps -p $(cat $PIDFILE) -o comm= >/dev/null 2>&1)
	    RETVAL=$?
	    if [[ "$RETVAL" -eq 0 || "$name" -eq "tgtd" ]] ; then
		echo "tgtd is running. Run 'tgt-admin -s' to see detailed target info."
	    else
		echo "tgtd is NOT running."
	    fi
	else
	    echo "tgtd is NOT running."
	fi
}

case $1 in
	start)
		start
		;;
	stop)
		stop
		;;
	forcedstop)
		forcedstop
		;;
	restart)
		TASK=restart
		stop && start
		;;
	forcedrestart)
		TASK=restart
		forcedstop && start
		;;
	reload)
		reload
		;;
	forcedreload)
		forcedreload
		;;
	condrestart)
	    if [ -e "$LOCKFILE" ]; then
		TASK=restart
		stop && start
	    fi
		;;
	condreload)
	    if [ -e "$LOCKFILE" ]; then
		reload
	    fi
	    ;;
	condstop)
	    if [ -e "$LOCKFILE" ]; then
		stop
	    fi
	    ;;
	status)
		status
		;;
	*)
		echo "Usage: $0 {start|stop|forcedstop|restart|forcedrestart|reload|forcedreload|condrestart|condreload|condstop|status}"
		exit 2
		;;
esac

