Blob Blame History Raw
#!/bin/bash
#
# Bring up/down opensm
#
# chkconfig: - 15 85
# description: Activates/Deactivates InfiniBand Subnet Manager
#
### BEGIN INIT INFO
# Provides:       opensm
# Required-Start: $syslog @RDMA_SERVICE@
# Required-Stop: $syslog @RDMA_SERVICE@
# Default-Start: @DEFAULT_START@
# Default-Stop: @DEFAULT_STOP@
# Description:  Manage OpenSM
### END INIT INFO
#
# Copyright (c) 2008 Voltaire, Inc. All rights reserved.
# Copyright (c) 2006 Mellanox Technologies. All rights reserved.
#
# This Software is licensed under one of the following licenses:
#
# 1) under the terms of the "Common Public License 1.0" a copy of which is
#    available from the Open Source Initiative, see
#    http://www.opensource.org/licenses/cpl.php.
#
# 2) under the terms of the "The BSD License" a copy of which is
#    available from the Open Source Initiative, see
#    http://www.opensource.org/licenses/bsd-license.php.
#
# 3) under the terms of the "GNU General Public License (GPL) Version 2" a
#    copy of which is available from the Open Source Initiative, see
#    http://www.opensource.org/licenses/gpl-license.php.
#
# Licensee has the right to choose one of the above licenses.
#
# Redistributions of source code must retain the above copyright
# notice and one of the license notices.
#
# Redistributions in binary form must reproduce both the above copyright
# notice, one of the license notices in the documentation
# and/or other materials provided with the distribution.
#
#
#  $Id: openib-1.0-opensm.init,v 1.5 2006/08/02 18:18:23 dledford Exp $
#
# processname: @sbindir@/opensm
# config: @sysconfdir@/sysconfig/opensm
# pidfile: /var/run/opensm.pid

prefix=@prefix@
exec_prefix=@exec_prefix@

. /etc/rc.d/init.d/functions

CONFIG=@sysconfdir@/sysconfig/opensm
if [ -f $CONFIG ]; then
    . $CONFIG
fi

prog=@sbindir@/opensm
bin=${prog##*/}

# Handover daemon for updating guid2lid cache file
sldd_prog=@sbindir@/sldd.sh
sldd_bin=${sldd_prog##*/}
sldd_pid_file=/var/run/sldd.pid

ACTION=$1

# Setting OpenSM start parameters
PID_FILE=/var/run/${bin}.pid
touch $PID_FILE

if [[ -n "${OSM_HOSTS}" && $(echo -n ${OSM_HOSTS} | wc -w | tr -d '[:space:]') -gt 1  ]]; then
    HONORE_GUID2LID="--honor_guid2lid"
fi

#########################################################################

start_sldd()
{
    if [ -f $sldd_pid_file ]; then
            local line p
            read line < $sldd_pid_file
            for p in $line ; do
                    [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && sldd_pid="$sldd_pid $p"
            done
    fi

    if [ -z "$sldd_pid" ]; then
        sldd_pid=`pidof -x $sldd_bin`
    fi

    if [ -n "${sldd_pid:-}" ] ; then
	kill -9 ${sldd_pid} > /dev/null 2>&1
    fi

    $sldd_prog > /dev/null 2>&1 &
    sldd_pid=$!

    echo ${sldd_pid} > $sldd_pid_file
    # Sleep is needed in order to update local gid2lid cache file before running opensm
    sleep 3
}

stop_sldd()
{
    if [ -f $sldd_pid_file ]; then
            local line p
            read line < $sldd_pid_file
            for p in $line ; do
                    [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && sldd_pid="$sldd_pid $p"
            done
    fi

    if [ -z "$sldd_pid" ]; then
        sldd_pid=`pidof -x $sldd_bin`
    fi

    if [ -n "${sldd_pid:-}" ] ; then
        kill -15 ${sldd_pid} > /dev/null 2>&1
    fi

}

start()
{
    local OSM_PID=

    pid=""

    if [ -f $PID_FILE ]; then
            local line p
            read line < $PID_FILE
            for p in $line ; do
                    [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p"
            done
    fi

    if [ -z "$pid" ]; then
        pid=`pidof -o $$ -o $PPID -o %PPID -x $bin`
    fi

    if [ -n "${pid:-}" ] ; then
        echo $"${bin} (pid $pid) is already running..."
    else

	if [ -n "${HONORE_GUID2LID}" ]; then
		# Run sldd daemod
		start_sldd
	fi

        # Start opensm
	echo -n "Starting IB Subnet Manager"
        $prog --daemon ${HONORE_GUID2LID} ${OPTIONS} > /dev/null
        cnt=0; alive=0
        while [ $cnt -lt 6 -a $alive -ne 1 ]; do
		echo -n ".";
		sleep 1
		alive=0
                OSM_PID=`pidof $prog`
                if [ "$OSM_PID" != "" ]; then
                        alive=1
                fi
		let cnt++;
	done

        echo $OSM_PID > $PID_FILE
        checkpid $OSM_PID
        RC=$?
        [ $RC -eq 0 ] && echo_success || echo_failure
        [ $RC -eq 0 ] && touch /var/lock/subsys/opensm
	echo

    fi
return $RC
}

stop()
{
    local pid=
    local pid1=
    local pid2=

    # Stop sldd daemon
    stop_sldd

    if [ -f $PID_FILE ]; then
            local line p
            read line < $PID_FILE
            for p in $line ; do
                    [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid1="$pid1 $p"
            done
    fi

    pid2=`pidof -o $$ -o $PPID -o %PPID -x $bin`

    pid=`echo "$pid1 $pid2" | sed -e 's/\ /\n/g' | sort -n | uniq | sed -e 's/\n/\ /g'`

    if [ -n "${pid:-}" ] ; then
        # Kill opensm
	echo -n "Stopping IB Subnet Manager."
        kill -15 $pid > /dev/null 2>&1
		cnt=0; alive=1
        while [ $cnt -lt 6 -a $alive -ne 0 ]; do
		echo -n ".";
		alive=0
		for p in $pid; do
			if checkpid $p ; then alive=1; echo -n "-"; fi
		done
		let cnt++;
		sleep $alive
	done

        for p in $pid
        do
            while checkpid $p ; do
                kill -KILL $p > /dev/null 2>&1
                echo -n "+"
                sleep 1
            done
        done
        checkpid $pid
        RC=$?
        [ $RC -eq 0 ] && echo_failure || echo_success
	echo
        RC=$((! $RC))
    else
	echo -n "Stopping IB Subnet Manager."
        echo_failure
	echo
        RC=1
    fi

    # Remove pid file if any.
    rm -f $PID_FILE
    rm -f /var/lock/subsys/opensm
    return $RC
}

status()
{
    local pid

    # First try "pidof"
    pid=`pidof -o $$ -o $PPID -o %PPID -x ${bin}`
    if [ -n "$pid" ]; then
            echo $"${bin} (pid $pid) is running..."
            return 0
    fi

     # Next try "/var/run/opensm.pid" files
     if [ -f $PID_FILE ] ; then
             read pid < $PID_FILE
             if [ -n "$pid" ]; then
                     echo $"${bin} dead but pid file $PID_FILE exists"
                     return 1
             fi
     fi
     echo $"${bin} is stopped"
     return 3
}



case $ACTION in
	start)
                start
		;;
	stop)
		stop
		;;
	restart)
		stop
                start
		;;
	status)
		status
		;;
	condrestart)
		pid=`pidof -o $$ -o $PPID -o %PPID -x $bin`
		if [ -n "$pid" ]; then
			stop
			sleep 1
			start
		fi
		;;
	*)
		echo
		echo "Usage: `basename $0` {start|stop|restart|status}"
		echo
		exit 1
		;;
esac

RC=$?
exit $RC