Merge branch 'master' into debian-wheezy
[snf-nfdhcpd] / debian / nfdhcpd.init
1 #!/bin/sh
2 #
3 # This is free software; you may redistribute it and/or modify
4 # it under the terms of the GNU General Public License as
5 # published by the Free Software Foundation; either version 2,
6 # or (at your option) any later version.
7 #
8 # This is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License with
14 # the Debian operating system, in /usr/share/common-licenses/GPL;  if
15 # not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA 02111-1307 USA
17 #
18 ### BEGIN INIT INFO
19 # Provides:          nfdhcpd
20 # Required-Start:    $network $local_fs $remote_fs
21 # Required-Stop:     $remote_fs
22 # Should-Start:
23 # Should-Stop:
24 # Default-Start:     2 3 4 5
25 # Default-Stop:      0 1 6
26 # Short-Description: NFQueue DHCP/RA server
27 ### END INIT INFO
28
29 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
30
31 DAEMON=/usr/sbin/nfdhcpd
32 NAME=nfdhcpd
33 DESC="NFQUEUE-based DHCP/RA server" 
34 LOGDIR=/var/log/nfdhcpd
35
36 PIDFILE=/var/run/$NAME/$NAME.pid
37
38 test -x $DAEMON || exit 0
39
40 . /lib/lsb/init-functions
41
42 # Default options, these can be overriden by the information
43 # at /etc/default/$NAME
44 DAEMON_OPTS=""          # Additional options given to the server
45
46 DIETIME=2              # Time to wait for the server to die, in seconds
47                         # If this value is set too low you might not
48                         # let some servers to die gracefully and
49                         # 'restart' will not work
50
51 STARTTIME=1             # Time to wait for the server to start, in seconds
52                         # If this value is set each time the server is
53                         # started (on start or restart) the script will
54                         # stall to try to determine if it is running
55                         # If it is not set and the server takes time
56                         # to setup a pid file the log message might 
57                         # be a false positive (says it did not start
58                         # when it actually did)
59                         
60 LOGFILE=$LOGDIR/$NAME.log  # Server logfile
61 #DAEMON_USER=nfdhcp     # Users to run the daemons as. If this value
62                         # is set start-stop-daemon will chuid the server
63
64 # Include defaults if available
65 if [ -f /etc/default/$NAME ] ; then
66         . /etc/default/$NAME
67 fi
68
69 # Use this if you want the user to explicitly set 'RUN' in
70 # /etc/default/
71 if [ "x$RUN" != "xyes" ] ; then
72   log_failure_msg "$NAME disabled, please adjust the configuration to your needs "
73   log_failure_msg "and then set RUN to 'yes' in /etc/default/$NAME to enable it."
74   exit 1
75 fi
76
77 # Check that the user exists (if we set a user)
78 # Does the user exist?
79 set -e
80
81 running_pid() {
82   # Check if a given process pid's cmdline matches a given name
83   pid=$1
84   name=$2
85   [ -z "$pid" ] && return 1
86   [ ! -d /proc/$pid ] &&  return 1
87   cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
88   # Is this the expected server
89   [ "$cmd" != "$name" ] &&  return 1
90   return 0
91 }
92
93 running() {
94   # Check if the process is running looking at /proc
95   # (works for all users)
96   # No pidfile, probably no daemon present
97   [ ! -f "$PIDFILE" ] && return 1
98   pid=`cat $PIDFILE`
99   running_pid $pid $DAEMON || return 1
100   return 0
101 }
102
103 start_server() {
104   # /var/run may be volatile, so we need to ensure that
105   # /var/run/$NAME exists here as well as in postinst
106   if [ ! -d /var/run/$NAME ]; then
107      mkdir /var/run/$NAME || return 1
108      chown nobody:nogroup /var/run/$NAME || return 1
109   fi
110   start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE \
111                     --exec $DAEMON -- $DAEMON_OPTS
112         errcode=$?
113         return $errcode
114 }
115
116 stop_server() {
117   start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
118   errcode=$?
119   return $errcode
120 }
121
122 reload_server() {
123   [ ! -f "$PIDFILE" ] && return 1
124   pid=pidofproc $PIDFILE # This is the daemon's pid
125   # Send a SIGHUP
126   kill -1 $pid
127   return $?
128 }
129
130 force_stop() {
131 # Force the process to die killing it manually
132   [ ! -e "$PIDFILE" ] && return
133   if running ; then
134     kill -15 $pid
135     # Is it really dead?
136     sleep "$DIETIME"s
137     if running ; then
138       kill -9 $pid
139       sleep "$DIETIME"s
140       if running ; then
141         echo "Cannot kill $NAME (pid=$pid)!"
142         exit 1
143       fi
144     fi
145   fi
146 }
147
148
149 case "$1" in
150   start)
151         log_daemon_msg "Starting $DESC " "$NAME"
152         # Check if it's running first
153         if running ;  then
154             log_progress_msg "apparently already running"
155             log_end_msg 0
156             exit 0
157         fi
158         if start_server ; then
159             # NOTE: Some servers might die some time after they start,
160             # this code will detect this issue if STARTTIME is set
161             # to a reasonable value
162             [ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time 
163             if  running ;  then
164                 # It's ok, the server started and is running
165                 log_end_msg 0
166             else
167                 # It is not running after we did start
168                 log_end_msg 1
169             fi
170         else
171             # Either we could not start it
172             log_end_msg 1
173         fi
174         ;;
175   stop)
176         log_daemon_msg "Stopping $DESC" "$NAME"
177         if running ; then
178             # Only stop the server if we see it running
179                               errcode=0
180             stop_server || errcode=$?
181             log_end_msg $errcode
182         else
183             # If it's not running don't do anything
184             log_progress_msg "apparently not running"
185             log_end_msg 0
186             exit 0
187         fi
188         ;;
189   force-stop)
190         # First try to stop gracefully the program
191         $0 stop
192         if running; then
193             # If it's still running try to kill it more forcefully
194             log_daemon_msg "Stopping (force) $DESC" "$NAME"
195                               errcode=0
196             force_stop || errcode=$?
197             log_end_msg $errcode
198         fi
199         ;;
200   restart|force-reload)
201         log_daemon_msg "Restarting $DESC" "$NAME"
202                 errcode=0
203         stop_server || errcode=$?
204         # Wait some sensible amount, some server need this
205         [ -n "$DIETIME" ] && sleep $DIETIME
206         start_server || errcode=$?
207         [ -n "$STARTTIME" ] && sleep $STARTTIME
208         running || errcode=$?
209         log_end_msg $errcode
210         ;;
211   status)
212
213         log_daemon_msg "Checking status of $DESC" "$NAME"
214         if running ;  then
215             log_progress_msg "running"
216             log_end_msg 0
217         else
218             log_progress_msg "apparently not running"
219             log_end_msg 1
220             exit 1
221         fi
222         ;;
223   reload)
224         log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon"
225         log_warning_msg "cannot re-read the config file (use restart)."
226         ;;
227   *)
228         N=/etc/init.d/$NAME
229         echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
230         exit 1
231         ;;
232 esac
233
234 exit 0