Debian Codegraph @

scripts/diod.init.in

raw
1 #!/bin/sh
2 ###############################################################################
3 # $Id: diod.init.in 808 2008-01-09 19:20:23Z dun $
4 ###############################################################################
5 # Originally written by Chris Dunlap <[email protected]> for ConMan:
6 # Copyright (C) 2007-2008 Lawrence Livermore National Security, LLC.
7 # Copyright (C) 2001-2007 The Regents of the University of California.
8 # UCRL-CODE-2002-009.
9 ###############################################################################
10 # chkconfig: 2345 95 5
11 # Description: Start/Stop diod (distributed I/O daemon)
12 ###############################################################################
13 ### BEGIN INIT INFO
14 # Provides: diod
15 # Required-Start: $local_fs $named $network
16 # Required-Stop: $local_fs $named $network
17 # Should-Start: $remote_fs $syslog $time
18 # Should-Stop: $remote_fs $syslog $time
19 # Default-Start: 2 3 4 5
20 # Default-Stop: 0 1 6
21 # Short-Description: Start/Stop diod (distributed I/O daemon).
22 ### END INIT INFO
23 ###############################################################################
24
25 unset DESC DAEMON DAEMON_ARGS CONFIG PIDFILE NICE USER SIGHUP_RELOAD
26
27 prefix="@prefix@"
28 exec_prefix="@exec_prefix@"
29 sbindir="@sbindir@"
30 sysconfdir="@sysconfdir@"
31 localstatedir="@localstatedir@"
32
33 DESC="diod"
34 DAEMON="$sbindir/diod"
35 #DAEMON_ARGS=
36 CONFIG=$sysconfdir/diod.conf
37 PIDFILE="$localstatedir/run/diod/diod.pid"
38 #NICE=
39 #USER=@RUN_AS_USER@
40 SIGHUP_RELOAD=1
41
42 ###############################################################################
43
44 service_init ()
45 {
46 # Determine the system type and initialize the environment.
47 #
48 # Note that the shell positional parameters must be preserved when calling
49 # this function in order for SuSE to initialize its environment properly.
50 ##
51 PATH=/sbin:/usr/sbin:/bin:/usr/bin
52 DAEMON_NAME="`basename \"$DAEMON\"`"
53 SCRIPT_NAME="`basename \"$0\" .init | sed 's/^[SK][0-9][0-9]*//'`"
54 SIGTERM_TIMEOUT="3"
55 STATUS=0
56
57 # Read configuration defaults to override variables:
58 # $DAEMON_ARGS, $CONFIG, $PIDFILE, $USER, $NICE, $SIGHUP_RELOAD
59 ##
60 for dir in "$sysconfdir/default" "$sysconfdir/sysconfig"; do
61 [ -r "$dir/$SCRIPT_NAME" ] && . "$dir/$SCRIPT_NAME"
62 done
63 [ -z "$DAEMON_ARGS" -a -n "$OPTIONS" ] && DAEMON_ARGS="$OPTIONS"
64 [ "`id | sed 's/^uid=\([0-9]*\).*/\1/'`" -ne 0 ] && unset USER
65 expr -- "$NICE" : '[0-9]*$' >/dev/null 2>&1 && NICE="+$NICE"
66 [ -n "$SIGHUP_RELOAD" -a "$SIGHUP_RELOAD" != 0 ] \
67 && RELOAD=1 || unset RELOAD
68
69 if [ -f /etc/debian_version -a -x /sbin/start-stop-daemon ]; then
70 SYSTEM="DEBIAN"
71 [ -x "$DAEMON" ] || exit 0 # pkg removed but not purged
72 [ -r /etc/default/rcS ] && . /etc/default/rcS
73 [ -r /lib/init/vars.sh ] && . /lib/init/vars.sh
74 [ -r /lib/lsb/init-functions ] && . /lib/lsb/init-functions
75 elif [ -f /etc/redhat-release -a -r /etc/init.d/functions ]; then
76 SYSTEM="REDHAT"
77 . /etc/init.d/functions
78 RH_SUBSYS="/var/lock/subsys/$DAEMON_NAME"
79 elif [ -f /etc/SuSE-release -a -r /etc/rc.status ]; then
80 SYSTEM="SUSE"
81 . /etc/rc.status
82 rc_reset
83 elif [ -r /lib/lsb/init-functions ]; then
84 SYSTEM="LSB"
85 . /lib/lsb/init-functions
86 else
87 SYSTEM="OTHER"
88 fi
89
90 # Exit if the package has been removed.
91 ##
92 [ -x "$DAEMON" ] || exit 5 # LSB: program not installed
93
94 # Exit if the configuration has been removed.
95 ##
96 [ -z "$CONFIG" -o -r "$CONFIG" ] || exit 6 # LSB: program not configured
97 }
98
99 service_fini ()
100 {
101 # Return the exit status.
102 ##
103 case $SYSTEM in
104 SUSE)
105 rc_exit
106 ;;
107 DEBIAN|REDHAT|LSB|*)
108 exit $STATUS
109 ;;
110 esac
111 }
112
113 service_start ()
114 {
115 # Start the service.
116 #
117 # Required by LSB, where running "start" on a service already running should be
118 # considered successful.
119 ##
120 log_init "Starting $DESC" "$DAEMON_NAME"
121 case $SYSTEM in
122 DEBIAN)
123 if $0 status >/dev/null 2>&1; then
124 STATUS=0
125 else
126 ERRMSG=`start-stop-daemon --start --quiet \
127 ${NICE:+"--nicelevel"} ${NICE:+"$NICE"} \
128 ${USER:+"--chuid"} ${USER:+"$USER"} \
129 ${PIDFILE:+"--pidfile"} ${PIDFILE:+"$PIDFILE"} \
130 --exec "$DAEMON" -- $DAEMON_ARGS 2>&1`
131 STATUS=$?
132 fi
133 ;;
134 REDHAT)
135 if $0 status >/dev/null 2>&1; then
136 STATUS=0
137 else
138 daemon ${NICE:+"$NICE"} ${USER:+"--user"} ${USER:+"$USER"} \
139 "$DAEMON" $DAEMON_ARGS
140 STATUS=$?
141 fi
142 [ $STATUS -eq 0 ] && touch "$RH_SUBSYS" >/dev/null 2>&1
143 ;;
144 SUSE)
145 ERRMSG=`startproc ${NICE:+"-n"} ${NICE:+"$NICE"} \
146 ${USER:+"-u"} ${USER:+"$USER"} \
147 ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} \
148 "$DAEMON" $DAEMON_ARGS 2>&1`
149 rc_status -v
150 STATUS=$?
151 ;;
152 LSB)
153 if [ -n "$USER" ]; then
154 ERRMSG=`su "$USER" -c "/sbin/start_daemon \
155 ${NICE:+\"-n\"} ${NICE:+\"$NICE\"} \
156 ${PIDFILE:+\"-p\"} ${PIDFILE:+\"$PIDFILE\"} \
157 \"$DAEMON\" $DAEMON_ARGS" 2>&1`
158 else
159 ERRMSG=`start_daemon ${NICE:+"-n"} ${NICE:+"$NICE"} \
160 ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} "$DAEMON" $DAEMON_ARGS 2>&1`
161 fi
162 STATUS=$?
163 ;;
164 *)
165 if $0 status >/dev/null 2>&1; then
166 STATUS=0
167 else
168 [ -n "$NICE" ] && nice="nice -n $NICE"
169 if [ -n "$USER" ]; then
170 ERRMSG=`su "$USER" -c "$nice \"$DAEMON\" $DAEMON_ARGS" 2>&1`
171 else
172 ERRMSG=`$nice "$DAEMON" $DAEMON_ARGS 2>&1`
173 fi
174 STATUS=$?
175 fi
176 ;;
177 esac
178 log_fini "$STATUS" "$ERRMSG"
179 }
180
181 service_stop ()
182 {
183 # Stop the service.
184 #
185 # Required by LSB, where running "stop" on a service already stopped or not
186 # running should be considered successful.
187 ##
188 log_init "Stopping $DESC" "$DAEMON_NAME"
189 case $SYSTEM in
190 DEBIAN)
191 if ! $0 status >/dev/null 2>&1; then
192 STATUS=0
193 else
194 start-stop-daemon --stop --quiet \
195 ${PIDFILE:+"--pidfile"} ${PIDFILE:+"$PIDFILE"} \
196 --name "$DAEMON_NAME" ${SIGTERM_TIMEOUT:+"--retry"} \
197 ${SIGTERM_TIMEOUT:+"$SIGTERM_TIMEOUT"} >/dev/null 2>&1
198 STATUS=$?
199 fi
200 ;;
201 REDHAT)
202 if ! $0 status >/dev/null 2>&1; then
203 STATUS=0
204 else
205 killproc "$DAEMON"
206 STATUS=$?
207 fi
208 [ $STATUS -eq 0 ] && rm -f "$RH_SUBSYS" >/dev/null 2>&1
209 ;;
210 SUSE)
211 killproc ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} \
212 ${SIGTERM_TIMEOUT:+"-t"} ${SIGTERM_TIMEOUT:+"$SIGTERM_TIMEOUT"} \
213 "$DAEMON"
214 rc_status -v
215 ;;
216 LSB)
217 killproc ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} "$DAEMON"
218 STATUS=$?
219 ;;
220 *)
221 signal_process "$DAEMON"
222 rc=$?
223 [ $rc -eq 0 -o $rc -eq 2 ] && STATUS=0 || STATUS=1
224 ;;
225 esac
226 log_fini "$STATUS"
227 [ -f "$PIDFILE" ] && rm -f "$PIDFILE"
228 }
229
230 service_restart ()
231 {
232 # Stop and restart the service if it is already running;
233 # otherwise, start the service.
234 #
235 # Required by LSB, where running "restart" on a service already stopped or not
236 # running should be considered successful.
237 ##
238 if $0 status >/dev/null 2>&1; then
239 service_stop
240 service_start
241 else
242 service_start
243 fi
244
245 case $SYSTEM in
246 SUSE)
247 rc_status
248 ;;
249 DEBIAN|REDHAT|LSB|*)
250 STATUS=$?
251 ;;
252 esac
253 }
254
255 service_try_restart ()
256 {
257 # Restart the service if it is already running.
258 #
259 # Optional for LSB, where running "try-restart" on a service already stopped or
260 # not running should be considered successful.
261 # Also known as "condrestart" by RedHat.
262 ##
263 case $SYSTEM in
264 REDHAT)
265 [ -f "$RH_SUBSYS" ] && $0 restart || :
266 STATUS=$?
267 ;;
268 SUSE)
269 $0 status >/dev/null 2>&1 && $0 restart || rc_reset
270 rc_status
271 ;;
272 DEBIAN|LSB|*)
273 $0 status >/dev/null 2>&1 && $0 restart || :
274 STATUS=$?
275 ;;
276 esac
277 }
278
279 service_reload ()
280 {
281 # Reload the configuration without stopping and restarting the service.
282 #
283 # Optional for LSB.
284 ##
285 [ -z "$RELOAD" ] && STATUS=3 # LSB: unimplemented feature
286
287 log_init "Reloading $DESC" "$DAEMON_NAME"
288 case $SYSTEM in
289 DEBIAN)
290 if [ -n "$RELOAD" ]; then
291 start-stop-daemon --stop --quiet --signal HUP \
292 ${PIDFILE:+"--pidfile"} ${PIDFILE:+"$PIDFILE"} \
293 --name "$DAEMON_NAME" >/dev/null 2>&1
294 STATUS=$?
295 fi
296 ;;
297 REDHAT)
298 if [ -n "$RELOAD" ]; then
299 killproc "$DAEMON" -HUP
300 STATUS=$?
301 else
302 echo_failure
303 fi
304 ;;
305 SUSE)
306 if [ -n "$RELOAD" ]; then
307 killproc -HUP ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} "$DAEMON"
308 else
309 rc_failed $STATUS
310 fi
311 rc_status -v
312 ;;
313 LSB)
314 if [ -n "$RELOAD" ]; then
315 killproc ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} "$DAEMON" -HUP
316 STATUS=$?
317 fi
318 ;;
319 *)
320 if [ -n "$RELOAD" ]; then
321 signal_process "$DAEMON" "HUP"
322 STATUS=$?
323 fi
324 ;;
325 esac
326 log_fini "$STATUS"
327 }
328
329 service_force_reload ()
330 {
331 # Reload the configuration if the service supports this;
332 # otherwise, restart the service if it is already running.
333 #
334 # Required by LSB, where running "force-reload" on a service already stopped or
335 # not running should be considered successful.
336 ##
337 if [ -n "$RELOAD" ]; then
338 $0 reload
339 else
340 $0 try-restart
341 fi
342
343 case $SYSTEM in
344 SUSE)
345 rc_status
346 ;;
347 DEBIAN|REDHAT|LSB|*)
348 STATUS=$?
349 ;;
350 esac
351 }
352
353 service_status ()
354 {
355 # Print the current status of the service.
356 #
357 # Required by LSB.
358 ##
359 case $SYSTEM in
360 REDHAT)
361 status "$DAEMON"
362 STATUS=$?
363 ;;
364 SUSE)
365 printf "Checking for service $DESC: "
366 checkproc ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} "$DAEMON"
367 rc_status -v
368 ;;
369 LSB)
370 printf "Checking status of $DESC: "
371 pids=`pidofproc ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} \
372 "$DAEMON" 2>/dev/null`
373 STATUS=$?
374 if [ $STATUS -eq 0 -a -n "$pids" ]; then
375 echo "running."
376 elif [ $STATUS -ne 0 -a -s "$PIDFILE" ]; then
377 echo "dead."
378 else
379 echo "stopped."
380 fi
381 ;;
382 DEBIAN|*)
383 printf "Checking status of $DESC: "
384 pids=`query_pids "$DAEMON" "$PIDFILE"`
385 rc=$?
386 if [ $rc -eq 0 -a -n "$pids" ]; then
387 echo "running."
388 STATUS=0 # LSB: program is running
389 elif [ $rc -ne 0 -a -s "$PIDFILE" ]; then
390 echo "dead."
391 STATUS=1 # LSB: program is dead & pidfile exists
392 elif [ $rc -ne 0 ]; then
393 echo "stopped."
394 STATUS=3 # LSB: program is not running
395 else
396 echo "unknown."
397 STATUS=4 # LSB: program status unknown
398 fi
399 ;;
400 esac
401 }
402
403 query_pids ()
404 {
405 # Writes the matching PIDs to stdout.
406 # Returns 0 on success (ie, pids found).
407 ##
408 PROCNAME="$1"
409 PIDFILE="$2"
410
411 if type pgrep >/dev/null 2>&1; then
412 pids=`pgrep -d ' ' -x "\`basename \"$PROCNAME\"\`" 2>/dev/null`
413 rc=$?
414 elif type pidof >/dev/null 2>&1; then
415 pids=`pidof -o $$ -x "$PROCNAME" 2>/dev/null`
416 rc=$?
417 else
418 pids=`(ps awx -o pid -o command || ps -e -f -o pid -o args) 2>/dev/null \
419 | tail +2 | egrep "( |/)$PROCNAME( |$)" | grep -v egrep \
420 | sed 's/ *\([0-9]*\).*/\1/' | sort -n | tr '\012' ' '`
421 [ -n "$pids" ] && rc=0 || rc=1
422 fi
423
424 unset pids_running
425 if [ -n "$pids" -a -r "$PIDFILE" ]; then
426 read pid_line < "$PIDFILE"
427 for pid in $pid_line; do
428 expr -- "$pid" : '[0-9]*$' >/dev/null 2>&1 \
429 && expr -- " $pids " : ".* $pid .*" >/dev/null 2>&1 \
430 && pids_running="$pids_running $pid"
431 done
432 [ -n "$pids_running" ] && pids=$pids_running
433 fi
434
435 echo $pids
436 return $rc
437 }
438
439 signal_process ()
440 {
441 # Returns 0 on success, 1 if kill failed, 2 if PROCNAME is not running.
442 ##
443 PROCNAME="$1"
444 SIGNUM="$2"
445
446 pids=`query_pids "$DAEMON" "$PIDFILE"`
447 [ $? -ne 0 -o -z "$pids" ] && return 2
448
449 kill ${SIGNUM:+"-$SIGNUM"} $pids >/dev/null 2>&1
450 [ $? -ne 0 ] && return 1
451 [ -n "$SIGNUM" ] && return 0
452
453 pids=`query_pids "$DAEMON" "$PIDFILE"`
454 [ $? -ne 0 -o -z "$pids" ] && return 0
455 [ -z "$SIGTERM_TIMEOUT" ] && return 1
456
457 sleep "$SIGTERM_TIMEOUT"
458 kill -KILL $pids >/dev/null 2>&1
459 pids=`query_pids "$DAEMON" "$PIDFILE"`
460 [ $? -ne 0 -o -z "$pids" ] && return 0
461 return 1
462 }
463
464 log_init ()
465 {
466 # Output informational message at beginning of action.
467 ##
468 MESSAGE="$1"
469 PROCNAME="$2"
470
471 case $SYSTEM in
472 DEBIAN)
473 if [ "$VERBOSE" != no ]; then
474 if type log_daemon_msg >/dev/null 2>&1; then
475 log_daemon_msg "$MESSAGE" "$PROCNAME"
476 else
477 printf "$MESSAGE: $PROCNAME"
478 fi
479 fi
480 ;;
481 REDHAT|SUSE|LSB|*)
482 printf "$MESSAGE: "
483 ;;
484 esac
485 }
486
487 log_fini ()
488 {
489 # Output informational/error message at end of action.
490 ##
491 STATUS="$1"
492 ERRMSG="$2"
493
494 case $SYSTEM in
495 DEBIAN)
496 if [ "$VERBOSE" != no ]; then
497 if ( type log_end_msg && type log_failure_msg ) >/dev/null 2>&1; then
498 log_end_msg "$STATUS"
499 [ $STATUS -eq 0 -o -z "$ERRMSG" ] || log_failure_msg "$ERRMSG"
500 else
501 [ $STATUS -eq 0 ] && echo "." || echo " (failed)."
502 [ $STATUS -eq 0 -o -z "$ERRMSG" ] || echo "$ERRMSG" >&2
503 fi
504 fi
505 ;;
506 REDHAT)
507 echo
508 ;;
509 SUSE)
510 [ $STATUS -eq 0 -o -z "$ERRMSG" ] || echo "$ERRMSG" >&2
511 ;;
512 LSB|*)
513 [ $STATUS -eq 0 ] && echo "." || echo " (failed)."
514 [ $STATUS -eq 0 -o -z "$ERRMSG" ] || echo "$ERRMSG" >&2
515 ;;
516 esac
517 }
518
519 ###############################################################################
520
521 service_init "$@"
522
523 case "$1" in
524 start)
525 service_start
526 ;;
527 stop)
528 service_stop
529 ;;
530 restart)
531 service_restart
532 ;;
533 try-restart|condrestart)
534 service_try_restart
535 ;;
536 reload)
537 service_reload
538 ;;
539 force-reload)
540 service_force_reload
541 ;;
542 status)
543 service_status
544 ;;
545 *)
546 echo "Usage: `basename \"$0\"`" \
547 "(start|stop|restart|try-restart|reload|force-reload|status)" >&2
548 exit 2 # LSB: invalid or excess argument(s)
549 ;;
550 esac
551
552 service_fini