1#!/sbin/sh
2#
3# Copyright (c) 2000, 2017 Oracle and/or its affiliates. All rights reserved.
4#
5# This program and the accompanying materials are made available under the
6# terms of the Eclipse Public License v. 2.0, which is available at
7# http://www.eclipse.org/legal/epl-2.0.
8#
9# This Source Code may also be made available under the following Secondary
10# Licenses when the conditions for such availability set forth in the
11# Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
12# version 2 with the GNU Classpath Exception, which is available at
13# https://www.gnu.org/software/classpath/license.html.
14#
15# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16#
17
18#
19# Message Queue Broker init.d script
20#
21# This script starts/stops the Message Queue broker process as part of
22# normal UNIX system operation based on the value of the
23# AUTOSTART in the /etc/imq/imqbrokerd.conf file.
24#
25# To debug this script, set the environmental variable
26# DEBUG to 1 (true)
27#
28# e.g.
29# env DEBUG=1 /etc/init.d/imq start
30#
31# This file is installed as:
32#     /etc/init.d/imq
33#
34# It is linked into the rc*.d directories as:
35#	# ln imq /etc/rc3.d/S52imq
36#	# ln imq /etc/rc0.d/K15imq
37#	# ln imq /etc/rc1.d/K15imq
38#	# ln imq /etc/rc2.d/K15imq
39#
40# The S52imq link in /etc/rc3.d will start the broker when
41# the system enters multiuser mode. The K15imq links in
42# rc0.d, rc1.d and rc2.d will bring down the broker when the
43# system is brought down to single user, or administrative modes.
44#
45# The script looks at the file:  /etc/imq/imqbrokerd.conf to
46# determine whether or not the broker should be started.
47
48###### START LOCAL CONFIGURATION
49# You may wish to modify these variables to suit your local configuration
50
51# IMQ_HOME   Location of the Message Queue installation
52IMQ_HOME=/
53export IMQ_HOME
54
55# IMQ_VARHOME
56# Set IMQ_VARHOME if you wish to designate an alternate
57# location for storing broker specific data like:
58#	- persistent storage
59#	- SSL certificates
60#	- user passwd database
61#
62# IMQ_VARHOME=/var/opt/SUNWjmq
63# export IMQ_VARHOME
64
65# BROKER_OPTIONS	Default Options to pass to the broker executable
66# additional arguments can be defined in the ARGS property
67#
68BROKER_OPTIONS="-silent"
69
70#
71# ETC HOME
72IMQ_ETCHOME="/etc/imq"
73#
74# imqbrokerd.conf
75CONF_FILE=$IMQ_ETCHOME/imqbrokerd.conf
76#
77#
78###### END LOCAL CONFIGURATION
79
80EXECUTABLE=imqbrokerd
81
82
83# error "description"
84error () {
85  echo $0: $* 2>&1
86  exit 1
87}
88
89# find the named process(es)
90findproc() {
91  pid=`/usr/bin/ps -ef |
92  /usr/bin/grep -w $1 |
93  /usr/bin/grep -v grep |
94  /usr/bin/awk '{print $2}'`
95  echo $pid
96}
97
98# kill the named process(es) (borrowed from S15nfs.server)
99killproc() {
100   pid=`findproc $1`
101   [ "$pid" != "" ] && kill $pid
102}
103
104#
105# require /usr/bin and Message Queue
106#
107if [ ! -d /usr/bin ]; then
108   error "Cannot find /usr/bin."
109
110elif [ ! -d "$IMQ_HOME" ]; then
111   error "Cannot find Message Queue in IMQ_HOME ($IMQ_HOME)."
112fi
113
114
115
116
117#
118# Start/stop Message Queue Broker
119#
120case "$1" in
121'start')
122         if [ -f $CONF_FILE ] ; then
123            AUTOSTART=`grep -v "^#" $CONF_FILE | grep AUTOSTART | cut -c11-`
124            RESTART=`grep -v "^#" $CONF_FILE | grep RESTART | cut -c9-`
125            ARGS=`grep -v "^#" $CONF_FILE | grep ARGS | cut -c6-`
126            if [ $AUTOSTART ] ; then
127                if [ $AUTOSTART = "YES" -o $AUTOSTART = "Yes" -o $AUTOSTART = "yes" -o $AUTOSTART = "Y" -o $AUTOSTART = "y" ] ; then
128                    START=1
129                    REASON="The AUTOSTART property is turned on (set to $AUTOSTART) in  $CONF_FILE. The ARGS property is set to \"$ARGS\""
130
131                elif [ $AUTOSTART = "NO" -o $AUTOSTART = "No" -o $AUTOSTART = "no" -o $AUTOSTART = "N" -o $AUTOSTART = "n" ] ; then
132                    START=0
133                    REASON="The AUTOSTART property is turned off (set to $AUTOSTART) in $CONF_FILE"
134                else
135                    START=0
136                    REASON="The AUTOSTART property is not set to an expected value (value is $AUTOSTART)"
137                fi
138            else
139                START=0
140                REASON="AUTOSTART was not set in $CONF_FILE"
141            fi
142         else
143            START=0
144            REASON="$CONF_FILE, the configuration file, was not found"
145         fi
146
147         # Handle restart option
148         if [ ! -z "$RESTART" ]; then
149             if [ $RESTART = "YES" ]; then
150                BROKER_OPTIONS="-autorestart $BROKER_OPTIONS"
151             fi
152         fi
153
154         if [ $START -eq 0 ] ; then
155             if [ $DEBUG ] ; then
156                 echo $IMQ_HOME/bin/$EXECUTABLE is not being started because [ $REASON ]
157             fi
158             exit 0
159         else
160             if [ $DEBUG ] ; then
161                 echo $IMQ_HOME/bin/$EXECUTABLE is starting because [ $REASON ]
162             fi
163         fi
164
165         if [ $DEBUG ] ; then
166	     echo "Command \c"
167	     echo "$IMQ_HOME/bin/$EXECUTABLE -bgnd $BROKER_OPTIONS  $ARGS... \c"
168         fi
169	 cd $IMQ_HOME
170
171	 # Check if the server is already running.
172	 if [ -n "`findproc $EXECUTABLE`" ]; then
173             if [ $DEBUG ] ; then
174	        echo "$EXECUTABLE is already running."
175             fi
176	     exit 0
177	 fi
178
179	 bin/$EXECUTABLE -bgnd $BROKER_OPTIONS $ARGS &
180         if [ $DEBUG ] ; then
181  	     echo "done"
182         fi
183
184	;;
185
186'stop')
187        if [ $DEBUG ] ; then
188	    echo "Stopping $EXECUTABLE ... \c"
189        fi
190	if [ -z "`findproc $EXECUTABLE`" ]; then
191            if [ $DEBUG ] ; then
192	        echo "$EXECUTABLE is not running."
193            fi
194	   exit 0
195	fi
196	killproc $EXECUTABLE
197        if [ $DEBUG ] ; then
198	    echo "done"
199        fi
200	;;
201
202*)
203	echo "Usage: $0 { start | stop }"
204	;;
205esac
206