1#! /bin/sh
2#
3#   Bacula(R) - The Network Backup Solution
4#
5#   Copyright (C) 2000-2020 Kern Sibbald
6#
7#   The original author of Bacula is Kern Sibbald, with contributions
8#   from many others, a complete list can be found in the file AUTHORS.
9#
10#   You may use this file and others of this release according to the
11#   license defined in the LICENSE file, which includes the Affero General
12#   Public License, v3.0 ("AGPLv3") and some additional permissions and
13#   terms pursuant to its AGPLv3 Section 7.
14#
15#   This notice must be preserved when any source code is
16#   conveyed and/or propagated.
17#
18#   Bacula(R) is a registered trademark of Kern Sibbald.
19#
20# bacula-ctl-dir This shell script takes care of starting and stopping
21#		 the bacula Director daemon
22#
23#   This is pretty much watered down version of the RedHat script
24#   that works on Solaris as well as Linux, but it won't work everywhere.
25#
26# description: The Leading Open Source Backup Solution.
27#
28# Copyright (C) 2000-2020 Kern Sibbald
29# License: BSD 2-Clause; see file LICENSE-FOSS
30#
31
32PSCMD="@PSCMD@"
33PS="ps"
34
35#
36# On Solaris, you may need to use nawk, or alternatively,
37#  add the GNU binaries to your path, such as /usr/xpg4/bin
38#
39AWK=@AWK@
40
41# All these are not *really* needed but it makes it
42#  easier to "steal" this code for the development
43#  environment where they are different.
44#
45BACDIRBIN=@sbindir@
46BACDIRCFG=@sysconfdir@
47PIDDIR=@piddir@
48SUBSYSDIR=@subsysdir@
49
50DIR_PORT=@dir_port@
51
52DIR_USER=@dir_user@
53DIR_GROUP=@dir_group@
54Bacula="@BACULA@"
55PIDOF=@PIDOF@
56
57# A function to stop a program.
58killproc() {
59   RC=0
60   # Test syntax.
61   if [ $# = 0 ]; then
62      echo "Usage: killproc {program} {port} [signal]"
63      return 1
64   fi
65
66   notset=0
67   # check for third arg to be kill level
68   if [ "$3" != "" ] ; then
69      killlevel=$3
70   else
71      notset=1
72      killlevel="-9"
73   fi
74
75   # Get base program name
76   base=`basename $1`
77
78   # Find pid.
79   pid=`pidofproc $base $2`
80
81   # Kill it.
82   if [ "$pid" != "" ] ; then
83      if [ "$notset" = "1" ] ; then
84	 if ${PS} -p "$pid">/dev/null 2>&1; then
85	     # TERM first, then KILL if not dead
86	     kill -TERM $pid 2>/dev/null
87	     sleep 1
88	     if ${PS} -p "$pid" >/dev/null 2>&1 ; then
89		 sleep 1
90		 if ${PS} -p "$pid" >/dev/null 2>&1 ; then
91		     sleep 3
92		     if ${PS} -p "$pid" >/dev/null 2>&1 ; then
93			 kill -KILL $pid 2>/dev/null
94		     fi
95		 fi
96	     fi
97	  fi
98	  ${PS} -p "$pid" >/dev/null 2>&1
99	  RC=$?
100	  [ $RC -eq 0 ] && failure "$base shutdown" || success "$base shutdown"
101      #    RC=$((! $RC))
102      # use specified level only
103      else
104	 if ${PS} -p "$pid" >/dev/null 2>&1; then
105	    kill $killlevel $pid 2>/dev/null
106	    RC=$?
107	    [ $RC -eq 0 ] && success "$base $killlevel" || failure "$base $killlevel"
108	 fi
109      fi
110   else
111      failure "$base shutdown"
112   fi
113   # Remove pid file if any.
114   if [ "$notset" = "1" ]; then
115      rm -f ${PIDDIR}/$base.$2.pid
116   fi
117   return $RC
118}
119
120# A function to find the pid of a program.
121pidofproc() {
122   pid=""
123   # Test syntax.
124   if [ $# = 0 ] ; then
125      echo "Usage: pidofproc {program}"
126      return 1
127   fi
128
129   # Get base program name
130   base=`basename $1`
131
132   # First try PID file
133   if [ -f ${PIDDIR}/$base.$2.pid ] ; then
134      pid=`head -n 1 ${PIDDIR}/$base.$2.pid`
135      if [ "$pid" != "" ] ; then
136	 echo $pid
137	 return 0
138      fi
139   fi
140
141   # Next try "pidof"
142   if [ -x ${PIDOF} ] ; then
143      pid=`${PIDOF} $1`
144   fi
145   if [ "$pid" != "" ] ; then
146      echo $pid
147      return 0
148   fi
149
150   # Finally try to extract it from ps
151   pid=`${PSCMD} | grep $1 | ${AWK} '{ print $1 }' | tr '\n' ' '`
152   echo $pid
153   return 0
154}
155
156status() {
157   pid=""
158   # Test syntax.
159   if [ $# = 0 ] ; then
160       echo "Usage: status {program} {port}"
161       return 1
162   fi
163
164   # Get base program name
165   base=`basename $1`
166
167   # First try "pidof"
168   if [ -x ${PIDOF} ] ; then
169      pid=`${PIDOF} $1`
170   fi
171   if [ "$pid" != "" ] ; then
172      echo "$base (pid $pid) is running..."
173      return 0
174   else
175      pid=`${PSCMD} | ${AWK} 'BEGIN { prog=ARGV[1]; ARGC=1 }
176	    { if ((prog == $2) || (("(" prog ")") == $2) ||
177		 (("[" prog "]") == $2) ||
178		 ((prog ":") == $2)) { print $1 ; exit 0 } }' $1`
179      if [ "$pid" != "" ] ; then
180	 echo "$base (pid $pid) is running..."
181	 return 0
182      fi
183   fi
184
185   # Next try the PID files
186   if [ -f ${PIDDIR}/$base.$2.pid ] ; then
187      pid=`head -n 1 ${PIDDIR}/$base.$2.pid`
188      if [ "$pid" != "" ] ; then
189	 echo "$base dead but pid file exists"
190	 return 1
191      fi
192   fi
193   # See if the subsys lock exists
194   if [ -f ${SUBSYSDIR}/$base ] ; then
195      echo "$base dead but subsys locked"
196      return 2
197   fi
198   echo "$base is stopped"
199   return 3
200}
201
202success() {
203   return 0
204}
205
206failure() {
207   rc=$?
208   return $rc
209}
210
211OS=`uname -s`
212
213# if /lib/tls exists, force Bacula to use the glibc pthreads instead
214if [ -d "/lib/tls" -a $OS = "Linux" -a `uname -r | cut -c1-3` = "2.4" ] ; then
215   export LD_ASSUME_KERNEL=2.4.19
216fi
217
218case "$1" in
219   start)
220      [ -x ${BACDIRBIN}/bacula-dir ] && {
221	  echo "Starting the $Bacula Director daemon"
222	 OPTIONS=''
223	 if [ "${DIR_USER}" != '' ]; then
224	    OPTIONS="${OPTIONS} -u ${DIR_USER}"
225	 fi
226
227	 if [ "${DIR_GROUP}" != '' ]; then
228	    OPTIONS="${OPTIONS} -g ${DIR_GROUP}"
229	 fi
230
231	 if [ "x${VALGRIND_DIR}" = "x1" ]; then
232	    valgrind --leak-check=full ${BACDIRBIN}/bacula-dir $2 $3 ${OPTIONS} -v -c ${BACDIRCFG}/bacula-dir.conf
233	 else
234	    ${BACDIRBIN}/bacula-dir $2 $3 ${OPTIONS} -v -c ${BACDIRCFG}/bacula-dir.conf
235	 fi
236	 sleep 1
237      }
238      ;;
239
240   stop)
241      [ -x ${BACDIRBIN}/bacula-dir ] && {
242	 echo "Stopping the $Bacula Director daemon"
243	 killproc ${BACDIRBIN}/bacula-dir ${DIR_PORT} $2
244      }
245      ;;
246
247   restart)
248      $0 stop
249      sleep 5
250      $0 start
251      ;;
252
253   status)
254      [ -x ${BACDIRBIN}/bacula-dir ] && status ${BACDIRBIN}/bacula-dir ${DIR_PORT}
255      ;;
256
257   *)
258      echo "Usage: $0 {start|stop|restart|status}"
259      exit 1
260      ;;
261esac
262exit 0
263