167754Smsmith#!/bin/bash
267754Smsmith#
367754Smsmith# Bring up/down opensm
4102550Siwasaki#
567754Smsmith# chkconfig: - 15 85
667754Smsmith# description: Activates/Deactivates InfiniBand Subnet Manager
767754Smsmith#
867754Smsmith### BEGIN INIT INFO
967754Smsmith# Provides:       opensm
1067754Smsmith### END INIT INFO
1167754Smsmith#
1291116Smsmith# Copyright (c) 2008 Voltaire, Inc. All rights reserved.
1370243Smsmith# Copyright (c) 2006 Mellanox Technologies. All rights reserved.
1467754Smsmith#
1567754Smsmith# This Software is licensed under one of the following licenses:
1667754Smsmith#
1767754Smsmith# 1) under the terms of the "Common Public License 1.0" a copy of which is
1867754Smsmith#    available from the Open Source Initiative, see
1967754Smsmith#    http://www.opensource.org/licenses/cpl.php.
2067754Smsmith#
2167754Smsmith# 2) under the terms of the "The BSD License" a copy of which is
2267754Smsmith#    available from the Open Source Initiative, see
2367754Smsmith#    http://www.opensource.org/licenses/bsd-license.php.
2467754Smsmith#
2567754Smsmith# 3) under the terms of the "GNU General Public License (GPL) Version 2" a
2667754Smsmith#    copy of which is available from the Open Source Initiative, see
2767754Smsmith#    http://www.opensource.org/licenses/gpl-license.php.
2867754Smsmith#
2967754Smsmith# Licensee has the right to choose one of the above licenses.
3067754Smsmith#
3167754Smsmith# Redistributions of source code must retain the above copyright
3267754Smsmith# notice and one of the license notices.
3367754Smsmith#
3467754Smsmith# Redistributions in binary form must reproduce both the above copyright
3567754Smsmith# notice, one of the license notices in the documentation
3667754Smsmith# and/or other materials provided with the distribution.
3767754Smsmith#
3867754Smsmith#
3967754Smsmith#  $Id: openib-1.0-opensm.init,v 1.5 2006/08/02 18:18:23 dledford Exp $
4067754Smsmith#
4167754Smsmith# processname: @sbindir@/opensm
4267754Smsmith# config: @sysconfdir@/sysconfig/opensm
4367754Smsmith# pidfile: /var/run/opensm.pid
4467754Smsmith
4567754Smsmithprefix=@prefix@
4667754Smsmithexec_prefix=@exec_prefix@
4767754Smsmith
4867754Smsmith. /etc/rc.d/init.d/functions
4967754Smsmith
5067754SmsmithCONFIG=@sysconfdir@/sysconfig/opensm
5167754Smsmithif [ -f $CONFIG ]; then
5267754Smsmith    . $CONFIG
5367754Smsmithfi
5467754Smsmith
5567754Smsmithprog=@sbindir@/opensm
5667754Smsmithbin=${prog##*/}
5767754Smsmith
5867754Smsmith# Handover daemon for updating guid2lid cache file
5967754Smsmithsldd_prog=@sbindir@/sldd.sh
6067754Smsmithsldd_bin=${sldd_prog##*/}
6167754Smsmithsldd_pid_file=/var/run/sldd.pid
6267754Smsmith
6367754SmsmithACTION=$1
6467754Smsmith
6567754Smsmith# Setting OpenSM start parameters
6667754SmsmithPID_FILE=/var/run/${bin}.pid
6767754Smsmithtouch $PID_FILE
6867754Smsmith
6967754Smsmithif [[ -n "${OSM_HOSTS}" && $(echo -n ${OSM_HOSTS} | wc -w | tr -d '[:space:]') -gt 1  ]]; then
7067754Smsmith    HONORE_GUID2LID="--honor_guid2lid"
7167754Smsmithfi
7267754Smsmith
7367754Smsmith#########################################################################
7467754Smsmith
7567754Smsmithstart_sldd()
7667754Smsmith{
7767754Smsmith    if [ -f $sldd_pid_file ]; then
7867754Smsmith            local line p
7967754Smsmith            read line < $sldd_pid_file
8067754Smsmith            for p in $line ; do
8167754Smsmith                    [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && sldd_pid="$sldd_pid $p"
8267754Smsmith            done
8367754Smsmith    fi
8467754Smsmith
8567754Smsmith    if [ -z "$sldd_pid" ]; then
8667754Smsmith        sldd_pid=`pidof -x $sldd_bin`
8767754Smsmith    fi
8867754Smsmith
8967754Smsmith    if [ -n "${sldd_pid:-}" ] ; then
9067754Smsmith	kill -9 ${sldd_pid} > /dev/null 2>&1
9167754Smsmith    fi
9267754Smsmith
9367754Smsmith    $sldd_prog > /dev/null 2>&1 &
9467754Smsmith    sldd_pid=$!
9567754Smsmith
9667754Smsmith    echo ${sldd_pid} > $sldd_pid_file
9767754Smsmith    # Sleep is needed in order to update local gid2lid cache file before running opensm
9867754Smsmith    sleep 3
9967754Smsmith}
10067754Smsmith
10167754Smsmithstop_sldd()
10267754Smsmith{
10367754Smsmith    if [ -f $sldd_pid_file ]; then
10467754Smsmith            local line p
10567754Smsmith            read line < $sldd_pid_file
10667754Smsmith            for p in $line ; do
10767754Smsmith                    [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && sldd_pid="$sldd_pid $p"
10867754Smsmith            done
10967754Smsmith    fi
11067754Smsmith
11167754Smsmith    if [ -z "$sldd_pid" ]; then
11267754Smsmith        sldd_pid=`pidof -x $sldd_bin`
11367754Smsmith    fi
11467754Smsmith
11567754Smsmith    if [ -n "${sldd_pid:-}" ] ; then
11667754Smsmith        kill -15 ${sldd_pid} > /dev/null 2>&1
11767754Smsmith    fi
11867754Smsmith
11967754Smsmith}
12067754Smsmith
12167754Smsmithstart()
12267754Smsmith{
12377424Smsmith    local OSM_PID=
12491116Smsmith
12567754Smsmith    pid=""
12667754Smsmith
12767754Smsmith    if [ -f $PID_FILE ]; then
12867754Smsmith            local line p
12967754Smsmith            read line < $PID_FILE
13067754Smsmith            for p in $line ; do
13167754Smsmith                    [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p"
13277424Smsmith            done
13367754Smsmith    fi
13477424Smsmith
13567754Smsmith    if [ -z "$pid" ]; then
13667754Smsmith        pid=`pidof -o $$ -o $PPID -o %PPID -x $bin`
13767754Smsmith    fi
13867754Smsmith
13967754Smsmith    if [ -n "${pid:-}" ] ; then
14067754Smsmith        echo $"${bin} (pid $pid) is already running..."
14167754Smsmith    else
14267754Smsmith
14367754Smsmith	if [ -n "${HONORE_GUID2LID}" ]; then
14467754Smsmith		# Run sldd daemod
14567754Smsmith		start_sldd
14667754Smsmith	fi
14767754Smsmith
14867754Smsmith        # Start opensm
14967754Smsmith	echo -n "Starting IB Subnet Manager"
15067754Smsmith        $prog --daemon ${HONORE_GUID2LID} ${OPTIONS} > /dev/null
15167754Smsmith        cnt=0; alive=0
15267754Smsmith        while [ $cnt -lt 6 -a $alive -ne 1 ]; do
15367754Smsmith		echo -n ".";
15467754Smsmith		sleep 1
15567754Smsmith		alive=0
15667754Smsmith                OSM_PID=`pidof $prog`
15791116Smsmith                if [ "$OSM_PID" != "" ]; then
15867754Smsmith                        alive=1
15967754Smsmith                fi
16067754Smsmith		let cnt++;
16167754Smsmith	done
16267754Smsmith
16391116Smsmith        echo $OSM_PID > $PID_FILE
16467754Smsmith        checkpid $OSM_PID
16571867Smsmith        RC=$?
166102550Siwasaki        [ $RC -eq 0 ] && echo_success || echo_failure
16782367Smsmith        [ $RC -eq 0 ] && touch /var/lock/subsys/opensm
16867754Smsmith	echo
16971867Smsmith
17077424Smsmith    fi
17191116Smsmithreturn $RC
17271867Smsmith}
17371867Smsmith
17491116Smsmithstop()
17591116Smsmith{
17671867Smsmith    local pid=
17780062Smsmith    local pid1=
17871867Smsmith    local pid2=
17967754Smsmith
18071867Smsmith    # Stop sldd daemon
18167754Smsmith    stop_sldd
18267754Smsmith
18367754Smsmith    if [ -f $PID_FILE ]; then
18467754Smsmith            local line p
18567754Smsmith            read line < $PID_FILE
18667754Smsmith            for p in $line ; do
18767754Smsmith                    [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid1="$pid1 $p"
18867754Smsmith            done
18967754Smsmith    fi
19067754Smsmith
19199146Siwasaki    pid2=`pidof -o $$ -o $PPID -o %PPID -x $bin`
19267754Smsmith
19367754Smsmith    pid=`echo "$pid1 $pid2" | sed -e 's/\ /\n/g' | sort -n | uniq | sed -e 's/\n/\ /g'`
19477424Smsmith
19577424Smsmith    if [ -n "${pid:-}" ] ; then
19677424Smsmith        # Kill opensm
19777424Smsmith	echo -n "Stopping IB Subnet Manager."
19877424Smsmith        kill -15 $pid > /dev/null 2>&1
19967754Smsmith		cnt=0; alive=1
20077424Smsmith        while [ $cnt -lt 6 -a $alive -ne 0 ]; do
20167754Smsmith		echo -n ".";
20267754Smsmith		alive=0
20367754Smsmith		for p in $pid; do
20467754Smsmith			if checkpid $p ; then alive=1; echo -n "-"; fi
20567754Smsmith		done
20667754Smsmith		let cnt++;
20777424Smsmith		sleep $alive
20877424Smsmith	done
20977424Smsmith
21077424Smsmith        for p in $pid
21167754Smsmith        do
21267754Smsmith            while checkpid $p ; do
21367754Smsmith                kill -KILL $p > /dev/null 2>&1
21467754Smsmith                echo -n "+"
21567754Smsmith                sleep 1
21667754Smsmith            done
21767754Smsmith        done
21867754Smsmith        checkpid $pid
21967754Smsmith        RC=$?
22083174Smsmith        [ $RC -eq 0 ] && echo_failure || echo_success
22192388Smsmith	echo
22292388Smsmith        RC=$((! $RC))
22367754Smsmith    else
22467754Smsmith	echo -n "Stopping IB Subnet Manager."
22567754Smsmith        echo_failure
22667754Smsmith	echo
22767754Smsmith        RC=1
22867754Smsmith    fi
22967754Smsmith
23067754Smsmith    # Remove pid file if any.
23167754Smsmith    rm -f $PID_FILE
23267754Smsmith    rm -f /var/lock/subsys/opensm
23367754Smsmith    return $RC
23467754Smsmith}
23567754Smsmith
23667754Smsmithstatus()
23767754Smsmith{
23867754Smsmith    local pid
23967754Smsmith
24067754Smsmith    # First try "pidof"
24167754Smsmith    pid=`pidof -o $$ -o $PPID -o %PPID -x ${bin}`
24267754Smsmith    if [ -n "$pid" ]; then
24367754Smsmith            echo $"${bin} (pid $pid) is running..."
24467754Smsmith            return 0
24567754Smsmith    fi
24699146Siwasaki
24799146Siwasaki     # Next try "/var/run/opensm.pid" files
24867754Smsmith     if [ -f $PID_FILE ] ; then
24967754Smsmith             read pid < $PID_FILE
25067754Smsmith             if [ -n "$pid" ]; then
25167754Smsmith                     echo $"${bin} dead but pid file $PID_FILE exists"
25267754Smsmith                     return 1
25367754Smsmith             fi
25467754Smsmith     fi
25567754Smsmith     echo $"${bin} is stopped"
25667754Smsmith     return 3
25767754Smsmith}
25877424Smsmith
25967754Smsmith
26077424Smsmith
26167754Smsmithcase $ACTION in
26267754Smsmith	start)
26367754Smsmith                start
26467754Smsmith		;;
26567754Smsmith	stop)
26667754Smsmith		stop
26767754Smsmith		;;
26867754Smsmith	restart)
26967754Smsmith		stop
27067754Smsmith                start
27167754Smsmith		;;
27267754Smsmith	status)
27367754Smsmith		status
27467754Smsmith		;;
27567754Smsmith	condrestart)
27667754Smsmith		pid=`pidof -o $$ -o $PPID -o %PPID -x $bin`
27767754Smsmith		if [ -n "$pid" ]; then
27869450Smsmith			stop
27967754Smsmith			sleep 1
28067754Smsmith			start
28167754Smsmith		fi
28291116Smsmith		;;
28367754Smsmith	*)
28467754Smsmith		echo
28567754Smsmith		echo "Usage: `basename $0` {start|stop|restart|status}"
28667754Smsmith		echo
28767754Smsmith		exit 1
28867754Smsmith		;;
28991116Smsmithesac
29067754Smsmith
29167754SmsmithRC=$?
29291116Smsmithexit $RC
29367754Smsmith