1#! /bin/sh
2### BEGIN INIT INFO
3# Provides:          bareos-dir
4# Required-Start:    $network $remote_fs $time $syslog
5# Should-Start:      postgresql mysql bareos-sd bareos-fd
6# Required-Stop:     $network $remote_fs $time $syslog
7# Should-Stop:       postgresql mysq bareos-sd bareos-fd
8# Default-Start:     2 3 4 5
9# Default-Stop:      0 1 6
10# Short-Description: Bareos Director
11# Description:       Bareos is a network backup and restore program.
12#                    The Bareos Director controls the Bareos Storage- and File-Daemons.
13### END INIT INFO
14
15DESC="Bareos Director"
16NAME=bareos-dir
17NETWORK_PORT=@dir_port@
18DAEMON_USER=@dir_user@
19DAEMON_GROUP=@dir_group@
20
21DAEMON=/usr/sbin/$NAME
22if [ "x${DAEMON_USER}" != "x" ]; then
23   DAEMON_USERGROUP="--chuid ${DAEMON_USER}"
24   if [ "x${DAEMON_GROUP}" != "x" ]; then
25      DAEMON_USERGROUP="${DAEMON_USERGROUP}:${DAEMON_GROUP}"
26   fi
27fi
28DAEMON_ARGS=
29PIDFILE=@piddir@/${NAME}.${NETWORK_PORT}.pid
30SCRIPTNAME=/etc/init.d/$NAME
31PATH=/sbin:/usr/sbin:/bin:/usr/bin
32
33# Exit if the package is not installed
34[ -x "$DAEMON" ] || exit 0
35
36# Read configuration variable file if it is present
37[ -r /etc/default/$NAME ] && . /etc/default/$NAME
38
39# Load the VERBOSE setting and other rcS variables
40. /lib/init/vars.sh
41
42# Define LSB log_* functions.
43# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
44# and status_of_proc is working.
45. /lib/lsb/init-functions
46
47# workaround, if status_of_proc is not defined (Ubuntu 8.04)
48if ! command -v status_of_proc >/dev/null; then
49status_of_proc ()
50{
51    local pidfile daemon name status OPTIND
52
53    pidfile=
54    OPTIND=1
55    while getopts p: opt ; do
56        case "$opt" in
57            p)  pidfile="$OPTARG";;
58        esac
59    done
60    shift $(($OPTIND - 1))
61
62    if [ -n "$pidfile" ]; then
63        pidfile="-p $pidfile"
64    fi
65    daemon="$1"
66    name="$2"
67
68    status="0"
69    pidofproc $pidfile $daemon >/dev/null || status="$?"
70    if [ "$status" = 0 ]; then
71        log_success_msg "$name is running"
72        return 0
73    elif [ "$status" = 4 ]; then
74        log_failure_msg "could not access PID file for $name"
75        return $status
76    else
77        log_failure_msg "$name is not running"
78        return $status
79    fi
80}
81fi
82
83#
84# Function that checks if the configuration is OK and
85# the Database can be connected
86#
87
88checkcfg() {
89   echo "Checking Configuration and Database connection ... "
90   su -s /bin/sh $DAEMON_USER -c "@sbindir@/bareos-dir -f -t"
91   if [ $? -eq 0 ]; then
92      return 0
93   else
94      return 1
95   fi
96}
97
98#
99# Function that starts the daemon/service
100#
101do_start()
102{
103    # Return
104    #   0 if daemon has been started
105    #   1 if daemon was already running
106    #   2 if daemon could not be started
107    checkcfg || return 2
108    start-stop-daemon --start --quiet --pidfile $PIDFILE $DAEMON_USERGROUP --exec $DAEMON --test > /dev/null \
109        || return 1
110    start-stop-daemon --start --quiet --pidfile $PIDFILE $DAEMON_USERGROUP --exec $DAEMON -- \
111        $DAEMON_ARGS \
112        || return 2
113    # Add code here, if necessary, that waits for the process to be ready
114    # to handle requests from services started subsequently which depend
115    # on this one.  As a last resort, sleep for some time.
116    TIMEOUT=5
117    while [ $TIMEOUT -gt 0 ] && ! do_check; do
118        sleep 1
119        TIMEOUT=$((TIMEOUT-1))
120    done
121    do_check
122    return $?
123}
124
125#
126# Function that stops the daemon/service
127#
128do_stop()
129{
130    # Return
131    #   0 if daemon has been stopped
132    #   1 if daemon was already stopped
133    #   2 if daemon could not be stopped
134    #   other if a failure occurred
135    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE $DAEMON_USERGROUP --name $NAME
136    RETVAL="$?"
137    [ "$RETVAL" = 2 ] && return 2
138    # Wait for children to finish too if this is a daemon that forks
139    # and if the daemon is only ever run from this initscript.
140    # If the above conditions are not satisfied then add some other code
141    # that waits for the process to drop all resources that could be
142    # needed by services started subsequently.  A last resort is to
143    # sleep for some time.
144    start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 $DAEMON_USERGROUP --exec $DAEMON
145    [ "$?" = 2 ] && return 2
146    # Many daemons don't delete their pidfiles when they exit.
147    rm -f $PIDFILE
148    return "$RETVAL"
149}
150
151#
152# Function that checks if daemon is running
153#
154do_check()
155{
156    STATUS=2
157    # Ubuntu 8.04 does not support "lsof -s "TCP:LISTEN", therefore "grep '(LISTEN)'" is used
158    pidofproc -p $PIDFILE $DAEMON >/dev/null && lsof -i "TCP:${NETWORK_PORT}" | grep --quiet '(LISTEN)' && STATUS=0
159    return $STATUS
160}
161
162#
163# Function that sends a SIGHUP to the daemon/service
164#
165do_reload() {
166    #
167    # If the daemon can reload its configuration without
168    # restarting (for example, when it is sent a SIGHUP),
169    # then implement that here.
170    #
171    start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
172    return 0
173}
174
175
176
177case "$1" in
178  start)
179    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
180    do_start
181    case "$?" in
182        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
183        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
184    esac
185    ;;
186  stop)
187    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
188    do_stop
189    case "$?" in
190        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
191        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
192    esac
193    ;;
194  status)
195    status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
196    ;;
197  reload|force-reload)
198    #
199    # If do_reload() is not implemented then leave this commented out
200    # and leave 'force-reload' as an alias for 'restart'.
201    #
202    log_daemon_msg "Reloading $DESC" "$NAME"
203    do_reload
204    log_end_msg $?
205    ;;
206  restart)
207    log_daemon_msg "Restarting $DESC" "$NAME"
208    do_stop
209    case "$?" in
210      0|1)
211        do_start
212        case "$?" in
213            0) log_end_msg 0 ;;
214            1) log_end_msg 1 ;; # Old process is still running
215            *) log_end_msg 1 ;; # Failed to start
216        esac
217        ;;
218      *)
219        # Failed to stop
220        log_end_msg 1
221        ;;
222    esac
223    ;;
224  *)
225    echo "Usage: $SCRIPTNAME {start|stop|status|restart|reload|force-reload}" >&2
226    exit 3
227    ;;
228esac
229
230:
231