1#!/bin/bash
2#
3# Bring up/down opensm
4#
5# chkconfig: - 15 85
6# description: Activates/Deactivates InfiniBand Subnet Manager
7#
8### BEGIN INIT INFO
9# Provides:       opensm
10### END INIT INFO
11#
12# Copyright (c) 2008 Voltaire, Inc. All rights reserved.
13# Copyright (c) 2006 Mellanox Technologies. All rights reserved.
14#
15# This Software is licensed under one of the following licenses:
16#
17# 1) under the terms of the "Common Public License 1.0" a copy of which is
18#    available from the Open Source Initiative, see
19#    http://www.opensource.org/licenses/cpl.php.
20#
21# 2) under the terms of the "The BSD License" a copy of which is
22#    available from the Open Source Initiative, see
23#    http://www.opensource.org/licenses/bsd-license.php.
24#
25# 3) under the terms of the "GNU General Public License (GPL) Version 2" a
26#    copy of which is available from the Open Source Initiative, see
27#    http://www.opensource.org/licenses/gpl-license.php.
28#
29# Licensee has the right to choose one of the above licenses.
30#
31# Redistributions of source code must retain the above copyright
32# notice and one of the license notices.
33#
34# Redistributions in binary form must reproduce both the above copyright
35# notice, one of the license notices in the documentation
36# and/or other materials provided with the distribution.
37#
38#
39#  $Id: openib-1.0-opensm.init,v 1.5 2006/08/02 18:18:23 dledford Exp $
40#
41# processname: @sbindir@/opensm
42# config: @sysconfdir@/sysconfig/opensm
43# pidfile: /var/run/opensm.pid
44
45prefix=@prefix@
46exec_prefix=@exec_prefix@
47
48. /etc/rc.d/init.d/functions
49
50CONFIG=@sysconfdir@/sysconfig/opensm
51if [ -f $CONFIG ]; then
52    . $CONFIG
53fi
54
55prog=@sbindir@/opensm
56bin=${prog##*/}
57
58# Handover daemon for updating guid2lid cache file
59sldd_prog=@sbindir@/sldd.sh
60sldd_bin=${sldd_prog##*/}
61sldd_pid_file=/var/run/sldd.pid
62
63ACTION=$1
64
65# Setting OpenSM start parameters
66PID_FILE=/var/run/${bin}.pid
67touch $PID_FILE
68
69if [[ -n "${OSM_HOSTS}" && $(echo -n ${OSM_HOSTS} | wc -w | tr -d '[:space:]') -gt 1  ]]; then
70    HONORE_GUID2LID="--honor_guid2lid"
71fi
72
73#########################################################################
74
75start_sldd()
76{
77    if [ -f $sldd_pid_file ]; then
78            local line p
79            read line < $sldd_pid_file
80            for p in $line ; do
81                    [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && sldd_pid="$sldd_pid $p"
82            done
83    fi
84
85    if [ -z "$sldd_pid" ]; then
86        sldd_pid=`pidof -x $sldd_bin`
87    fi
88
89    if [ -n "${sldd_pid:-}" ] ; then
90	kill -9 ${sldd_pid} > /dev/null 2>&1
91    fi
92
93    $sldd_prog > /dev/null 2>&1 &
94    sldd_pid=$!
95
96    echo ${sldd_pid} > $sldd_pid_file
97    # Sleep is needed in order to update local gid2lid cache file before running opensm
98    sleep 3
99}
100
101stop_sldd()
102{
103    if [ -f $sldd_pid_file ]; then
104            local line p
105            read line < $sldd_pid_file
106            for p in $line ; do
107                    [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && sldd_pid="$sldd_pid $p"
108            done
109    fi
110
111    if [ -z "$sldd_pid" ]; then
112        sldd_pid=`pidof -x $sldd_bin`
113    fi
114
115    if [ -n "${sldd_pid:-}" ] ; then
116        kill -15 ${sldd_pid} > /dev/null 2>&1
117    fi
118
119}
120
121start()
122{
123    local OSM_PID=
124
125    pid=""
126
127    if [ -f $PID_FILE ]; then
128            local line p
129            read line < $PID_FILE
130            for p in $line ; do
131                    [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p"
132            done
133    fi
134
135    if [ -z "$pid" ]; then
136        pid=`pidof -o $$ -o $PPID -o %PPID -x $bin`
137    fi
138
139    if [ -n "${pid:-}" ] ; then
140        echo $"${bin} (pid $pid) is already running..."
141    else
142
143	if [ -n "${HONORE_GUID2LID}" ]; then
144		# Run sldd daemod
145		start_sldd
146	fi
147
148        # Start opensm
149	echo -n "Starting IB Subnet Manager"
150        $prog --daemon ${HONORE_GUID2LID} ${OPTIONS} > /dev/null
151        cnt=0; alive=0
152        while [ $cnt -lt 6 -a $alive -ne 1 ]; do
153		echo -n ".";
154		sleep 1
155		alive=0
156                OSM_PID=`pidof $prog`
157                if [ "$OSM_PID" != "" ]; then
158                        alive=1
159                fi
160		let cnt++;
161	done
162
163        echo $OSM_PID > $PID_FILE
164        checkpid $OSM_PID
165        RC=$?
166        [ $RC -eq 0 ] && echo_success || echo_failure
167        [ $RC -eq 0 ] && touch /var/lock/subsys/opensm
168	echo
169
170    fi
171return $RC
172}
173
174stop()
175{
176    local pid=
177    local pid1=
178    local pid2=
179
180    # Stop sldd daemon
181    stop_sldd
182
183    if [ -f $PID_FILE ]; then
184            local line p
185            read line < $PID_FILE
186            for p in $line ; do
187                    [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid1="$pid1 $p"
188            done
189    fi
190
191    pid2=`pidof -o $$ -o $PPID -o %PPID -x $bin`
192
193    pid=`echo "$pid1 $pid2" | sed -e 's/\ /\n/g' | sort -n | uniq | sed -e 's/\n/\ /g'`
194
195    if [ -n "${pid:-}" ] ; then
196        # Kill opensm
197	echo -n "Stopping IB Subnet Manager."
198        kill -15 $pid > /dev/null 2>&1
199		cnt=0; alive=1
200        while [ $cnt -lt 6 -a $alive -ne 0 ]; do
201		echo -n ".";
202		alive=0
203		for p in $pid; do
204			if checkpid $p ; then alive=1; echo -n "-"; fi
205		done
206		let cnt++;
207		sleep $alive
208	done
209
210        for p in $pid
211        do
212            while checkpid $p ; do
213                kill -KILL $p > /dev/null 2>&1
214                echo -n "+"
215                sleep 1
216            done
217        done
218        checkpid $pid
219        RC=$?
220        [ $RC -eq 0 ] && echo_failure || echo_success
221	echo
222        RC=$((! $RC))
223    else
224	echo -n "Stopping IB Subnet Manager."
225        echo_failure
226	echo
227        RC=1
228    fi
229
230    # Remove pid file if any.
231    rm -f $PID_FILE
232    rm -f /var/lock/subsys/opensm
233    return $RC
234}
235
236status()
237{
238    local pid
239
240    # First try "pidof"
241    pid=`pidof -o $$ -o $PPID -o %PPID -x ${bin}`
242    if [ -n "$pid" ]; then
243            echo $"${bin} (pid $pid) is running..."
244            return 0
245    fi
246
247     # Next try "/var/run/opensm.pid" files
248     if [ -f $PID_FILE ] ; then
249             read pid < $PID_FILE
250             if [ -n "$pid" ]; then
251                     echo $"${bin} dead but pid file $PID_FILE exists"
252                     return 1
253             fi
254     fi
255     echo $"${bin} is stopped"
256     return 3
257}
258
259
260
261case $ACTION in
262	start)
263                start
264		;;
265	stop)
266		stop
267		;;
268	restart)
269		stop
270                start
271		;;
272	status)
273		status
274		;;
275	condrestart)
276		pid=`pidof -o $$ -o $PPID -o %PPID -x $bin`
277		if [ -n "$pid" ]; then
278			stop
279			sleep 1
280			start
281		fi
282		;;
283	*)
284		echo
285		echo "Usage: `basename $0` {start|stop|restart|status}"
286		echo
287		exit 1
288		;;
289esac
290
291RC=$?
292exit $RC
293