#!/bin/bash
### BEGIN INIT INFO
# Provides:          srptools
# 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

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

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

[ -x $DAEMON ] || exit 0

. /lib/lsb/init-functions

[ -f /etc/default/srptools ] &&  . /etc/default/srptools

max() {
    echo $(($1 > $2 ? $1 : $2))
}

run_daemon() {
    # srp does not background itself; using the start-stop-daemon background
    # function causes us to lose stdout, which is where it logs to
    nohup start-stop-daemon --start --quiet -m \
	  --pidfile "/var/run/srp_daemon.${HCA_ID}.${PORT}" \
          --exec $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_daemon() {
    local RETVAL=0

    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_daemon() {
    local RETVAL=0 PORTS=ALL

    for_all_ports 'start-stop-daemon --stop --quiet --oknodo -m --pidfile "/var/run/srp_daemon.${HCA_ID}.${PORT}"; 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
}

case "$1" in
    start)
	start_daemon
	;;
    stop)
	stop_daemon
	;;
    status)
	daemon_status
	;;
    restart | reload | force-reload )
	stop_daemon
	start_daemon
	;;
esac
