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