1219820Sjeff#!/bin/bash
2219820Sjeff#
3219820Sjeff# Bring up/down opensm
4219820Sjeff#
5219820Sjeff# chkconfig: - 15 85
6219820Sjeff# description: Activates/Deactivates InfiniBand Subnet Manager
7219820Sjeff#
8219820Sjeff### BEGIN INIT INFO
9219820Sjeff# Provides:       opensm
10219820Sjeff### END INIT INFO
11219820Sjeff#
12219820Sjeff# Copyright (c) 2008 Voltaire, Inc. All rights reserved.
13219820Sjeff# Copyright (c) 2006 Mellanox Technologies. All rights reserved.
14219820Sjeff#
15219820Sjeff# This Software is licensed under one of the following licenses:
16219820Sjeff#
17219820Sjeff# 1) under the terms of the "Common Public License 1.0" a copy of which is
18219820Sjeff#    available from the Open Source Initiative, see
19219820Sjeff#    http://www.opensource.org/licenses/cpl.php.
20219820Sjeff#
21219820Sjeff# 2) under the terms of the "The BSD License" a copy of which is
22219820Sjeff#    available from the Open Source Initiative, see
23219820Sjeff#    http://www.opensource.org/licenses/bsd-license.php.
24219820Sjeff#
25219820Sjeff# 3) under the terms of the "GNU General Public License (GPL) Version 2" a
26219820Sjeff#    copy of which is available from the Open Source Initiative, see
27219820Sjeff#    http://www.opensource.org/licenses/gpl-license.php.
28219820Sjeff#
29219820Sjeff# Licensee has the right to choose one of the above licenses.
30219820Sjeff#
31219820Sjeff# Redistributions of source code must retain the above copyright
32219820Sjeff# notice and one of the license notices.
33219820Sjeff#
34219820Sjeff# Redistributions in binary form must reproduce both the above copyright
35219820Sjeff# notice, one of the license notices in the documentation
36219820Sjeff# and/or other materials provided with the distribution.
37219820Sjeff#
38219820Sjeff#
39219820Sjeff#  $Id: openib-1.0-opensm.init,v 1.5 2006/08/02 18:18:23 dledford Exp $
40219820Sjeff#
41219820Sjeff# processname: @sbindir@/opensm
42219820Sjeff# config: @sysconfdir@/sysconfig/opensm
43219820Sjeff# pidfile: /var/run/opensm.pid
44219820Sjeff
45219820Sjeffprefix=@prefix@
46219820Sjeffexec_prefix=@exec_prefix@
47219820Sjeff
48219820Sjeff. /etc/rc.d/init.d/functions
49219820Sjeff
50219820SjeffCONFIG=@sysconfdir@/sysconfig/opensm
51219820Sjeffif [ -f $CONFIG ]; then
52219820Sjeff    . $CONFIG
53219820Sjefffi
54219820Sjeff
55219820Sjeffprog=@sbindir@/opensm
56219820Sjeffbin=${prog##*/}
57219820Sjeff
58219820Sjeff# Handover daemon for updating guid2lid cache file
59219820Sjeffsldd_prog=@sbindir@/sldd.sh
60219820Sjeffsldd_bin=${sldd_prog##*/}
61219820Sjeffsldd_pid_file=/var/run/sldd.pid
62219820Sjeff
63219820SjeffACTION=$1
64219820Sjeff
65219820Sjeff# Setting OpenSM start parameters
66219820SjeffPID_FILE=/var/run/${bin}.pid
67219820Sjefftouch $PID_FILE
68219820Sjeff
69219820Sjeffif [[ -n "${OSM_HOSTS}" && $(echo -n ${OSM_HOSTS} | wc -w | tr -d '[:space:]') -gt 1  ]]; then
70219820Sjeff    HONORE_GUID2LID="--honor_guid2lid"
71219820Sjefffi
72219820Sjeff
73219820Sjeff#########################################################################
74219820Sjeff
75219820Sjeffstart_sldd()
76219820Sjeff{
77219820Sjeff    if [ -f $sldd_pid_file ]; then
78219820Sjeff            local line p
79219820Sjeff            read line < $sldd_pid_file
80219820Sjeff            for p in $line ; do
81219820Sjeff                    [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && sldd_pid="$sldd_pid $p"
82219820Sjeff            done
83219820Sjeff    fi
84219820Sjeff
85219820Sjeff    if [ -z "$sldd_pid" ]; then
86219820Sjeff        sldd_pid=`pidof -x $sldd_bin`
87219820Sjeff    fi
88219820Sjeff
89219820Sjeff    if [ -n "${sldd_pid:-}" ] ; then
90219820Sjeff	kill -9 ${sldd_pid} > /dev/null 2>&1
91219820Sjeff    fi
92219820Sjeff
93219820Sjeff    $sldd_prog > /dev/null 2>&1 &
94219820Sjeff    sldd_pid=$!
95219820Sjeff
96219820Sjeff    echo ${sldd_pid} > $sldd_pid_file
97219820Sjeff    # Sleep is needed in order to update local gid2lid cache file before running opensm
98219820Sjeff    sleep 3
99219820Sjeff}
100219820Sjeff
101219820Sjeffstop_sldd()
102219820Sjeff{
103219820Sjeff    if [ -f $sldd_pid_file ]; then
104219820Sjeff            local line p
105219820Sjeff            read line < $sldd_pid_file
106219820Sjeff            for p in $line ; do
107219820Sjeff                    [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && sldd_pid="$sldd_pid $p"
108219820Sjeff            done
109219820Sjeff    fi
110219820Sjeff
111219820Sjeff    if [ -z "$sldd_pid" ]; then
112219820Sjeff        sldd_pid=`pidof -x $sldd_bin`
113219820Sjeff    fi
114219820Sjeff
115219820Sjeff    if [ -n "${sldd_pid:-}" ] ; then
116219820Sjeff        kill -15 ${sldd_pid} > /dev/null 2>&1
117219820Sjeff    fi
118219820Sjeff
119219820Sjeff}
120219820Sjeff
121219820Sjeffstart()
122219820Sjeff{
123219820Sjeff    local OSM_PID=
124219820Sjeff
125219820Sjeff    pid=""
126219820Sjeff
127219820Sjeff    if [ -f $PID_FILE ]; then
128219820Sjeff            local line p
129219820Sjeff            read line < $PID_FILE
130219820Sjeff            for p in $line ; do
131219820Sjeff                    [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p"
132219820Sjeff            done
133219820Sjeff    fi
134219820Sjeff
135219820Sjeff    if [ -z "$pid" ]; then
136219820Sjeff        pid=`pidof -o $$ -o $PPID -o %PPID -x $bin`
137219820Sjeff    fi
138219820Sjeff
139219820Sjeff    if [ -n "${pid:-}" ] ; then
140219820Sjeff        echo $"${bin} (pid $pid) is already running..."
141219820Sjeff    else
142219820Sjeff
143219820Sjeff	if [ -n "${HONORE_GUID2LID}" ]; then
144219820Sjeff		# Run sldd daemod
145219820Sjeff		start_sldd
146219820Sjeff	fi
147219820Sjeff
148219820Sjeff        # Start opensm
149219820Sjeff	echo -n "Starting IB Subnet Manager"
150219820Sjeff        $prog --daemon ${HONORE_GUID2LID} ${OPTIONS} > /dev/null
151219820Sjeff        cnt=0; alive=0
152219820Sjeff        while [ $cnt -lt 6 -a $alive -ne 1 ]; do
153219820Sjeff		echo -n ".";
154219820Sjeff		sleep 1
155219820Sjeff		alive=0
156219820Sjeff                OSM_PID=`pidof $prog`
157219820Sjeff                if [ "$OSM_PID" != "" ]; then
158219820Sjeff                        alive=1
159219820Sjeff                fi
160219820Sjeff		let cnt++;
161219820Sjeff	done
162219820Sjeff
163219820Sjeff        echo $OSM_PID > $PID_FILE
164219820Sjeff        checkpid $OSM_PID
165219820Sjeff        RC=$?
166219820Sjeff        [ $RC -eq 0 ] && echo_success || echo_failure
167219820Sjeff        [ $RC -eq 0 ] && touch /var/lock/subsys/opensm
168219820Sjeff	echo
169219820Sjeff
170219820Sjeff    fi
171219820Sjeffreturn $RC
172219820Sjeff}
173219820Sjeff
174219820Sjeffstop()
175219820Sjeff{
176219820Sjeff    local pid=
177219820Sjeff    local pid1=
178219820Sjeff    local pid2=
179219820Sjeff
180219820Sjeff    # Stop sldd daemon
181219820Sjeff    stop_sldd
182219820Sjeff
183219820Sjeff    if [ -f $PID_FILE ]; then
184219820Sjeff            local line p
185219820Sjeff            read line < $PID_FILE
186219820Sjeff            for p in $line ; do
187219820Sjeff                    [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid1="$pid1 $p"
188219820Sjeff            done
189219820Sjeff    fi
190219820Sjeff
191219820Sjeff    pid2=`pidof -o $$ -o $PPID -o %PPID -x $bin`
192219820Sjeff
193219820Sjeff    pid=`echo "$pid1 $pid2" | sed -e 's/\ /\n/g' | sort -n | uniq | sed -e 's/\n/\ /g'`
194219820Sjeff
195219820Sjeff    if [ -n "${pid:-}" ] ; then
196219820Sjeff        # Kill opensm
197219820Sjeff	echo -n "Stopping IB Subnet Manager."
198219820Sjeff        kill -15 $pid > /dev/null 2>&1
199219820Sjeff		cnt=0; alive=1
200219820Sjeff        while [ $cnt -lt 6 -a $alive -ne 0 ]; do
201219820Sjeff		echo -n ".";
202219820Sjeff		alive=0
203219820Sjeff		for p in $pid; do
204219820Sjeff			if checkpid $p ; then alive=1; echo -n "-"; fi
205219820Sjeff		done
206219820Sjeff		let cnt++;
207219820Sjeff		sleep $alive
208219820Sjeff	done
209219820Sjeff
210219820Sjeff        for p in $pid
211219820Sjeff        do
212219820Sjeff            while checkpid $p ; do
213219820Sjeff                kill -KILL $p > /dev/null 2>&1
214219820Sjeff                echo -n "+"
215219820Sjeff                sleep 1
216219820Sjeff            done
217219820Sjeff        done
218219820Sjeff        checkpid $pid
219219820Sjeff        RC=$?
220219820Sjeff        [ $RC -eq 0 ] && echo_failure || echo_success
221219820Sjeff	echo
222219820Sjeff        RC=$((! $RC))
223219820Sjeff    else
224219820Sjeff	echo -n "Stopping IB Subnet Manager."
225219820Sjeff        echo_failure
226219820Sjeff	echo
227219820Sjeff        RC=1
228219820Sjeff    fi
229219820Sjeff
230219820Sjeff    # Remove pid file if any.
231219820Sjeff    rm -f $PID_FILE
232219820Sjeff    rm -f /var/lock/subsys/opensm
233219820Sjeff    return $RC
234219820Sjeff}
235219820Sjeff
236219820Sjeffstatus()
237219820Sjeff{
238219820Sjeff    local pid
239219820Sjeff
240219820Sjeff    # First try "pidof"
241219820Sjeff    pid=`pidof -o $$ -o $PPID -o %PPID -x ${bin}`
242219820Sjeff    if [ -n "$pid" ]; then
243219820Sjeff            echo $"${bin} (pid $pid) is running..."
244219820Sjeff            return 0
245219820Sjeff    fi
246219820Sjeff
247219820Sjeff     # Next try "/var/run/opensm.pid" files
248219820Sjeff     if [ -f $PID_FILE ] ; then
249219820Sjeff             read pid < $PID_FILE
250219820Sjeff             if [ -n "$pid" ]; then
251219820Sjeff                     echo $"${bin} dead but pid file $PID_FILE exists"
252219820Sjeff                     return 1
253219820Sjeff             fi
254219820Sjeff     fi
255219820Sjeff     echo $"${bin} is stopped"
256219820Sjeff     return 3
257219820Sjeff}
258219820Sjeff
259219820Sjeff
260219820Sjeff
261219820Sjeffcase $ACTION in
262219820Sjeff	start)
263219820Sjeff                start
264219820Sjeff		;;
265219820Sjeff	stop)
266219820Sjeff		stop
267219820Sjeff		;;
268219820Sjeff	restart)
269219820Sjeff		stop
270219820Sjeff                start
271219820Sjeff		;;
272219820Sjeff	status)
273219820Sjeff		status
274219820Sjeff		;;
275219820Sjeff	condrestart)
276219820Sjeff		pid=`pidof -o $$ -o $PPID -o %PPID -x $bin`
277219820Sjeff		if [ -n "$pid" ]; then
278219820Sjeff			stop
279219820Sjeff			sleep 1
280219820Sjeff			start
281219820Sjeff		fi
282219820Sjeff		;;
283219820Sjeff	*)
284219820Sjeff		echo
285219820Sjeff		echo "Usage: `basename $0` {start|stop|restart|status}"
286219820Sjeff		echo
287219820Sjeff		exit 1
288219820Sjeff		;;
289219820Sjeffesac
290219820Sjeff
291219820SjeffRC=$?
292219820Sjeffexit $RC
293