#!/bin/sh
#
# srpd		Manage the SRP client daemon (srp_daemon)
#
# chkconfig: - 25 75
# description: Starts/Stops InfiniBand SRP client service
# config: /etc/srp_daemon.conf
# processname: srp_daemon
# pidfile: /var/run/srp_daemon.pid
### BEGIN INIT INFO
# Provides:          srptools srpd srp_daemon
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Discovers SRP scsi targets.
# Description:       Discovers SRP scsi over infiniband targets.
### END INIT INFO

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

DAEMON=/usr/sbin/srp_daemon
IBDIR=/sys/class/infiniband

PORTS=""
RETRIES=""
RETRIES_DEFAULT=60
LOG=""
LOG_DEFAULT=/var/log/srp_daemon.log

RETVAL=0


run_daemon() {

	start_daemon --background  --pidfile "/var/run/srp_daemon.${HCA_ID}.${PORT}" --lockfile "/var/lock/subsys/srp_daemon.${HCA_ID}.${PORT}" --expect-user root -- $DAEMON \
		-e -c -n -i "${HCA_ID}" -p "${PORT}" -R "${RETRIES:-${RETRIES_DEFAULT}}" >> "${LOG:-${LOG_DEFAULT}}" 2>&1
	RETVAL=$(max "$RETVAL" $?)
}

# Evaluate shell command $1 for every port in $PORTS
for_all_ports() {
    local cmd=$1 p

    if [ "$PORTS" = "ALL" ]; then
	for p in ${IBDIR}/*/ports/*; do
	    [ -e "$p" ] || continue
	    PORT=$(basename "$p")
	    HCA_ID=$(basename "$(dirname "$(dirname "$p")")")
	    eval "$cmd"
	done
    else
	for ADAPTER in $PORTS; do
	    HCA_ID=${ADAPTER%%:*}
	    PORT=${ADAPTER#${HCA_ID}:}
	    [ -n "$HCA_ID" ] && [ -n "$PORT" ] && eval "$cmd"
	done
    fi
}

start()
{
	if [ "$PORTS" = "NONE" ] ; then
		echo "srptools disabled."
		exit 0
	fi

	for_all_ports run_daemon
	case $RETVAL in
		0) log_success_msg "started $DAEMON";;
		*) log_failure_msg "failed to start $DAEMON";;
	esac
	return $RETVAL
}

stop()
{
	local RETVAL=0 PORTS=ALL

	for_all_ports 'stop_daemon --expect-user root --pidfile "/var/run/srp_daemon.${HCA_ID}.${PORT} -- $DAEMON"; RETVAL=$(max $RETVAL $?)'
	case $RETVAL in
		0) log_success_msg "stopped $DAEMON";;
		*) log_failure_msg "failed to stop $DAEMON";;
	esac
	return $RETVAL
}

check_status() {
    local pidfile=$1 pid

    [ -e "$pidfile" ] || return 3 # not running
    pid=$(<"$pidfile")
    [ -n "$pid" ] || return 3 # not running
    [ -d "/proc/$pid" ] || return 1 # not running and pid file exists
    return 0 # running
}

daemon_status() {
    local RETVAL=0

    for_all_ports 'check_status /var/run/srp_daemon.${HCA_ID}.${PORT} $DAEMON; RETVAL=$(max $RETVAL $?)'
    case $RETVAL in
	0) log_success_msg "$DAEMON is running";;
	*) log_failure_msg "$DAEMON is not running";;
    esac
    return $RETVAL
}

restart()
{
	stop
	start
}

reload()
{
	check || exit
	msg_reloading $NAME
	$BINARY -p "$PIDFILE" -f $CONFIG -sf `cat $PIDFILE`
	RETVAL=$?
	return $RETVAL
} 

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload)
		reload
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	condreload)
		if [ -e "$LOCKFILE" ]; then
			reload
		fi
		;;
	status)
		daemon_status
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
