root / debian / nfdhcpd.init @ 8f433e9f
History | View | Annotate | Download (6.8 kB)
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 |
#DAEMONUSER=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 python || 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_daemon -p $PIDFILE $DAEMON $DAEMON_OPTS |
111 |
errcode=$? |
112 |
return $errcode |
113 |
} |
114 |
|
115 |
stop_server() { |
116 |
killproc -p $PIDFILE $DAEMON |
117 |
errcode=$? |
118 |
return $errcode |
119 |
} |
120 |
|
121 |
reload_server() { |
122 |
[ ! -f "$PIDFILE" ] && return 1 |
123 |
pid=pidofproc $PIDFILE # This is the daemon's pid |
124 |
# Send a SIGHUP |
125 |
kill -1 $pid |
126 |
return $? |
127 |
} |
128 |
|
129 |
force_stop() { |
130 |
# Force the process to die killing it manually |
131 |
[ ! -e "$PIDFILE" ] && return |
132 |
if running ; then |
133 |
kill -15 $pid |
134 |
# Is it really dead? |
135 |
sleep "$DIETIME"s |
136 |
if running ; then |
137 |
kill -9 $pid |
138 |
sleep "$DIETIME"s |
139 |
if running ; then |
140 |
echo "Cannot kill $NAME (pid=$pid)!" |
141 |
exit 1 |
142 |
fi |
143 |
fi |
144 |
fi |
145 |
} |
146 |
|
147 |
|
148 |
case "$1" in |
149 |
start) |
150 |
log_daemon_msg "Starting $DESC " "$NAME" |
151 |
# Check if it's running first |
152 |
if running ; then |
153 |
log_progress_msg "apparently already running" |
154 |
log_end_msg 0 |
155 |
exit 0 |
156 |
fi |
157 |
if start_server ; then |
158 |
# NOTE: Some servers might die some time after they start, |
159 |
# this code will detect this issue if STARTTIME is set |
160 |
# to a reasonable value |
161 |
[ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time |
162 |
if running ; then |
163 |
# It's ok, the server started and is running |
164 |
log_end_msg 0 |
165 |
else |
166 |
# It is not running after we did start |
167 |
log_end_msg 1 |
168 |
fi |
169 |
else |
170 |
# Either we could not start it |
171 |
log_end_msg 1 |
172 |
fi |
173 |
;; |
174 |
stop) |
175 |
log_daemon_msg "Stopping $DESC" "$NAME" |
176 |
if running ; then |
177 |
# Only stop the server if we see it running |
178 |
errcode=0 |
179 |
stop_server || errcode=$? |
180 |
log_end_msg $errcode |
181 |
else |
182 |
# If it's not running don't do anything |
183 |
log_progress_msg "apparently not running" |
184 |
log_end_msg 0 |
185 |
exit 0 |
186 |
fi |
187 |
;; |
188 |
force-stop) |
189 |
# First try to stop gracefully the program |
190 |
$0 stop |
191 |
if running; then |
192 |
# If it's still running try to kill it more forcefully |
193 |
log_daemon_msg "Stopping (force) $DESC" "$NAME" |
194 |
errcode=0 |
195 |
force_stop || errcode=$? |
196 |
log_end_msg $errcode |
197 |
fi |
198 |
;; |
199 |
restart|force-reload) |
200 |
log_daemon_msg "Restarting $DESC" "$NAME" |
201 |
errcode=0 |
202 |
stop_server || errcode=$? |
203 |
# Wait some sensible amount, some server need this |
204 |
[ -n "$DIETIME" ] && sleep $DIETIME |
205 |
start_server || errcode=$? |
206 |
[ -n "$STARTTIME" ] && sleep $STARTTIME |
207 |
running || errcode=$? |
208 |
log_end_msg $errcode |
209 |
;; |
210 |
status) |
211 |
|
212 |
log_daemon_msg "Checking status of $DESC" "$NAME" |
213 |
if running ; then |
214 |
log_progress_msg "running" |
215 |
log_end_msg 0 |
216 |
else |
217 |
log_progress_msg "apparently not running" |
218 |
log_end_msg 1 |
219 |
exit 1 |
220 |
fi |
221 |
;; |
222 |
reload) |
223 |
log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon" |
224 |
log_warning_msg "cannot re-read the config file (use restart)." |
225 |
;; |
226 |
*) |
227 |
N=/etc/init.d/$NAME |
228 |
echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2 |
229 |
exit 1 |
230 |
;; |
231 |
esac |
232 |
|
233 |
exit 0 |