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