debian: Provide an init script to run the daemon on boot.
[fwd] / debian / fw.init
1 #! /bin/sh
2 ### BEGIN INIT INFO
3 # Provides: fw
4 # Required-Start: $local_fs $remote_fs
5 # Required-Stop: $local_fs $remote_fs
6 # Default-Start: 2 3 4 5
7 # Default-Stop: 0 1 6
8 # Short-Description: Start port forwarding server
9 # Description: This file should be used to construct scripts to be
10 # placed in /etc/init.d.
11 ### END INIT INFO
12
13 # Author: Mark Wooding <mdw@distorted.org.uk>
14
15 # Do NOT "set -e"
16
17 # PATH should only include /usr/* if it runs after the mountnfs.sh script
18 PATH=/sbin:/usr/sbin:/bin:/usr/bin
19 DESC="port forwarding daemon"
20 NAME=fw
21 DAEMON=/usr/bin/fw
22 PIDFILE=/var/run/$NAME.pid
23 SCRIPTNAME=/etc/init.d/$NAME
24 FWUSER=fw FWGROUP=fw
25 DAEMON_ARGS="--syslog"
26 CONFIG=/etc/fw.conf
27
28 # Exit if the package is not installed
29 [ -x "$DAEMON" ] || exit 0
30
31 # Read configuration variable file if it is present
32 [ -r /etc/default/$NAME ] && . /etc/default/$NAME
33
34 # Exit if no configuration file
35 [ -r "$CONFIG" ] || exit 0
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.0-6) to ensure that this file is present.
42 . /lib/lsb/init-functions
43
44 #
45 # Function that starts the daemon/service
46 #
47 do_start()
48 {
49 # Return
50 # 0 if daemon has been started
51 # 1 if daemon was already running
52 # 2 if daemon could not be started
53 start-stop-daemon --start --quiet --pidfile $PIDFILE \
54 --exec $DAEMON --test > /dev/null \
55 || return 1
56 start-stop-daemon --start --quiet --pidfile $PIDFILE \
57 --exec $DAEMON -- \
58 --pidfile="$PIDFILE" \
59 --daemon \
60 $DAEMON_ARGS \
61 --setuid="$FWUSER" --setgid="$FWGROUP" \
62 --file="$CONFIG" \
63 || return 2
64 }
65
66 #
67 # Function that stops the daemon/service
68 #
69 do_stop()
70 {
71 # Return
72 # 0 if daemon has been stopped
73 # 1 if daemon was already stopped
74 # 2 if daemon could not be stopped
75 # other if a failure occurred
76 start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 \
77 --pidfile "$PIDFILE" --name $NAME
78 RETVAL="$?"
79 [ "$RETVAL" = 2 ] && return 2
80 rm -f "$PIDFILE"
81 return "$RETVAL"
82 }
83
84 #
85 # Function that sends a SIGHUP to the daemon/service
86 #
87 do_reload() {
88 #
89 # If the daemon can reload its configuration without
90 # restarting (for example, when it is sent a SIGHUP),
91 # then implement that here.
92 #
93 start-stop-daemon --stop --signal 1 --quiet --pidfile "$PIDFILE" \
94 --name $NAME
95 return 0
96 }
97
98 case "$1" in
99 start)
100 [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
101 do_start
102 case "$?" in
103 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
104 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
105 esac
106 ;;
107 stop)
108 [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
109 do_stop
110 case "$?" in
111 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
112 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
113 esac
114 ;;
115 reload|force-reload)
116 log_daemon_msg "Reloading $DESC" "$NAME"
117 do_reload
118 log_end_msg $?
119 ;;
120 restart)
121 log_daemon_msg "Restarting $DESC" "$NAME"
122 do_stop
123 case "$?" in
124 0|1)
125 do_start
126 case "$?" in
127 0) log_end_msg 0 ;;
128 1) log_end_msg 1 ;; # Old process is still running
129 *) log_end_msg 1 ;; # Failed to start
130 esac
131 ;;
132 *)
133 # Failed to stop
134 log_end_msg 1
135 ;;
136 esac
137 ;;
138 *)
139 echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
140 exit 3
141 ;;
142 esac
143
144 :