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