1 |
#!/bin/sh |
2 |
#----------------------------------------------------------------------------- |
3 |
#- Runlevel control shell script |
4 |
C='rabit@netfrag.org, 04.11.2004' |
5 |
I='$Id$' |
6 |
#----------------------------------------------------------------------------- |
7 |
#- Setup: |
8 |
|
9 |
# Service/daemon short name: |
10 |
SVCNAME=anfud |
11 |
|
12 |
# Service/daemon long name: |
13 |
SVCLONGNAME="Adjusting Net Filter Updater Daemon" |
14 |
|
15 |
# The daemon executable: |
16 |
BINFILE=/usr/sbin/anfud |
17 |
|
18 |
# The PID file created on daemon start: |
19 |
PIDFILE=/var/run/anfud.pid |
20 |
|
21 |
# User and group to run the daemon as: |
22 |
USER=root |
23 |
GROUP=adm |
24 |
|
25 |
#----------------------------------------------------------------------------- |
26 |
#- Constants: |
27 |
|
28 |
N=$(basename $0) |
29 |
|
30 |
F="$N - Runlevel control for '$SVCNAME'." |
31 |
|
32 |
I=${I:4} |
33 |
I=${I:-No CVS ID yet} |
34 |
|
35 |
E='echo -e' |
36 |
|
37 |
RD="\033[1;31m" |
38 |
GN="\033[1;32m" |
39 |
YW="\033[1;33m" |
40 |
GY="\033[0;37m" |
41 |
B1="\033[1m" |
42 |
B0="\033[0m" |
43 |
|
44 |
# Return codes: |
45 |
# 0 - no error |
46 |
# 1 - start-stop-daemon: nothing done |
47 |
# 2 - start-stop-daemon: with --retry, processes wouldn't die |
48 |
# 3 - start-stop-daemon: trouble |
49 |
# 4 - daemon is not running |
50 |
# 5 - daemon is running |
51 |
# 6 - no PID file |
52 |
# 7 - PID file exists |
53 |
# 8 - Could not remove PID file |
54 |
|
55 |
BreakOnError=1 |
56 |
|
57 |
#----------------------------------------------------------------------------- |
58 |
#- Functions: |
59 |
|
60 |
do_start() { |
61 |
|
62 |
if [ "$BreakOnError" == 1 ]; then |
63 |
|
64 |
# Break if daemon is already running: |
65 |
do_status |
66 |
[ $? == 0 ] && return 5 |
67 |
|
68 |
# Break if PID file exists: |
69 |
[ -f $PIDFILE ] && return 7 |
70 |
fi |
71 |
|
72 |
# Start daemon: |
73 |
start-stop-daemon --start --quiet --background --make-pidfile \ |
74 |
--chuid $USER:$GROUP --pidfile $PIDFILE --exec $BINFILE >/dev/null |
75 |
|
76 |
return $? |
77 |
} |
78 |
|
79 |
do_stop() { |
80 |
|
81 |
local exit_rm exit_ssd |
82 |
|
83 |
if [ "$BreakOnError" == 1 ]; then |
84 |
|
85 |
# Break if PID file does not exist: |
86 |
[ ! -f $PIDFILE ] && return 6 |
87 |
|
88 |
# Break if daemon is not running: |
89 |
do_status |
90 |
[ $? != 0 ] && return 4 |
91 |
|
92 |
fi |
93 |
|
94 |
# Stop daemon: |
95 |
start-stop-daemon --stop --quiet --pidfile $PIDFILE >/dev/null |
96 |
exit_ssd=$? |
97 |
|
98 |
if [ $exit_ssd != 0 ] && [ $exit_ssd != 1 ]; then |
99 |
return $exit_ssd |
100 |
fi |
101 |
|
102 |
# Unlink stopped daemons PID file: |
103 |
rm $PIDFILE |
104 |
exit_rm=$? |
105 |
|
106 |
[ $exit_rm != 0 ] && return 8 |
107 |
|
108 |
# All went good: |
109 |
return 0 |
110 |
} |
111 |
|
112 |
do_status() { |
113 |
# Test to kill a running daemon: |
114 |
start-stop-daemon --stop --test --quiet \ |
115 |
--pidfile $PIDFILE >/dev/null |
116 |
|
117 |
return $? |
118 |
|
119 |
# $? is zero, when start-stop-daemon could kill a running |
120 |
# daemon, what means that it would be running. |
121 |
} |
122 |
|
123 |
exitinfo() { |
124 |
|
125 |
local ExitCode Info |
126 |
|
127 |
ExitCode=${1:-0} |
128 |
[ $ExitCode == 0 ] && return |
129 |
|
130 |
Info[0]="No error." |
131 |
Info[1]="Warning: 'start-stop-daemon: nothing done'." |
132 |
Info[2]="ERROR: 'start-stop-daemon: with --retry, processes wouldn't die'." |
133 |
Info[3]="ERROR: 'start-stop-daemon: trouble'." |
134 |
Info[4]="ERROR: The daemon is not running." |
135 |
Info[5]="ERROR: The daemon is already running." |
136 |
Info[6]="ERROR: No PID file found. The daemon may not run." |
137 |
Info[7]="ERROR: A PID file exists. This may result from an unclear termination." |
138 |
Info[8]="ERROR: Could not unlink PID file." |
139 |
|
140 |
$E " "${Info[$ExitCode]} |
141 |
|
142 |
return |
143 |
} |
144 |
|
145 |
restart() { |
146 |
local exit_start exit_stop |
147 |
|
148 |
ebegin "Restarting '$B1$SVCNAME$B0'..." |
149 |
|
150 |
do_stop |
151 |
exit_stop=$? |
152 |
|
153 |
if [ "$exit_stop" != 0 ]; then |
154 |
eend 1 |
155 |
return $exit_stop |
156 |
fi |
157 |
|
158 |
do_start |
159 |
exit_start=$? |
160 |
|
161 |
if [ "$exit_start" != 0 ]; then |
162 |
eend 1 |
163 |
return $exit_start |
164 |
fi |
165 |
|
166 |
eend 0 |
167 |
|
168 |
return 0 |
169 |
} |
170 |
|
171 |
start() { |
172 |
local exit_start |
173 |
|
174 |
ebegin "Starting '$B1$SVCNAME$B0'..." |
175 |
|
176 |
do_start |
177 |
exit_start=$? |
178 |
|
179 |
eend $exit_start |
180 |
|
181 |
return $exit_start |
182 |
} |
183 |
|
184 |
status() { |
185 |
local |
186 |
|
187 |
ebegin "Status of '$B1$SVCNAME$B0':" |
188 |
|
189 |
do_status |
190 |
exit_status=$? |
191 |
|
192 |
estatus $exit_status |
193 |
|
194 |
return $exit_status |
195 |
} |
196 |
|
197 |
stop() { |
198 |
local exit_stop |
199 |
|
200 |
ebegin "Stopping '$B1$SVCNAME$B0'..." |
201 |
|
202 |
do_stop |
203 |
exit_stop=$? |
204 |
|
205 |
eend $exit_stop |
206 |
|
207 |
return $exit_stop |
208 |
} |
209 |
|
210 |
#--------------------------------------------------------------------- |
211 |
|
212 |
ebegin() { |
213 |
$E -n " $B1"'*'"$B0 $1 " |
214 |
|
215 |
return |
216 |
} |
217 |
|
218 |
eend() { |
219 |
if [ "${1:-0}" == 0 ]; then |
220 |
$E "[ ${GN}done${GY} ]" |
221 |
else |
222 |
$E "[ ${RD}failed${GY} ]" |
223 |
fi |
224 |
|
225 |
return |
226 |
} |
227 |
|
228 |
estatus() { |
229 |
if [ "$1" == 0 ]; then |
230 |
$E "${GN}started${GY}" |
231 |
else |
232 |
$E "${RD}stopped${GY}" |
233 |
fi |
234 |
|
235 |
return |
236 |
} |
237 |
|
238 |
print_help() { |
239 |
print_title |
240 |
|
241 |
print_usage |
242 |
|
243 |
$E $B1'Parameters:'$B0 |
244 |
$E " ${B1}start$B0" |
245 |
$E " Start the daemon." |
246 |
$E " ${B1}stop$B0" |
247 |
$E " Stop the daemon." |
248 |
$E " ${B1}restart$B0" |
249 |
$E " Restart the daemon (stop and start)." |
250 |
$E " ${B1}status$B0" |
251 |
$E " Print out the daemons status." |
252 |
$E " ${B1}-h$B0|${B1}--help$B0" |
253 |
$E " Print out this help." |
254 |
$E " ${B1}-v$B0|${B1}--version$B0" |
255 |
$E " Print out title and version only." |
256 |
|
257 |
return |
258 |
} |
259 |
|
260 |
print_title() { |
261 |
$E "$YW$F$GY\n$C\n($I)" |
262 |
|
263 |
return |
264 |
} |
265 |
|
266 |
print_usage() { |
267 |
$E $B1'Usage:'$B0 |
268 |
$E " $B1$N [status|start|stop|restart|-h|--help|-v|--version]$B0" |
269 |
|
270 |
return |
271 |
} |
272 |
|
273 |
#--------------------------------------------------------------------- |
274 |
|
275 |
case "$1" in |
276 |
|
277 |
(restart) |
278 |
restart |
279 |
exit_code=$? |
280 |
exitinfo $exit_code |
281 |
exit $exit_code |
282 |
;; |
283 |
|
284 |
(start) |
285 |
start |
286 |
exit_code=$? |
287 |
exitinfo $exit_code |
288 |
exit $exit_code |
289 |
;; |
290 |
|
291 |
(status) |
292 |
status |
293 |
exit_code=$? |
294 |
exitinfo $exit_code |
295 |
exit $exit_code |
296 |
;; |
297 |
|
298 |
(stop) |
299 |
stop |
300 |
exit_code=$? |
301 |
exitinfo $exit_code |
302 |
exit $exit_code |
303 |
;; |
304 |
|
305 |
(-h|--help) |
306 |
print_help |
307 |
exit |
308 |
;; |
309 |
|
310 |
(-v|--version) |
311 |
print_title |
312 |
exit |
313 |
;; |
314 |
|
315 |
(*) |
316 |
# Any other parameter is unknown: |
317 |
[ -n "$1" ] && $E "Unknown parameter '$1'." |
318 |
;; |
319 |
|
320 |
esac |
321 |
|
322 |
print_usage |
323 |
|
324 |
$E "Try $B1$N --help$B0 for more." |
325 |
|
326 |
#--------------------------------------------------------------------- |