1 # functions This file contains functions to be used by most or all
2 # shell scripts in the /etc/init.d directory.
4 # $Id: functions,v 1.63 2001/05/13 17:10:17 baggins Exp $
6 # Author: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
7 # Hacked by: Greg Galloway and Marc Ewing
9 # Marek Obuchowicz <elephant@pld.org.pl>
10 # Arkadiusz Mi¶kiewicz <misiek@pld.org.pl>
11 # Micha³ Kochanowicz <mkochano@pld.org.pl>
13 # First set up a default search path.
14 export PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin"
19 # Source configuration if available - may override default values
20 [ -f /etc/sysconfig/system ] && . /etc/sysconfig/system
22 [ -z "$COLUMNS" ] && COLUMNS=80
27 if [ ! -d /usr/share/terminfo ] || \
28 [ ! -x /usr/bin/tput -a ! -x /bin/tput ]; then
31 echo -ne "\033[$(($2+1))G"
40 is_yes "$COLOR_INIT" && echo -ne "\033[0;3${2}m"
60 while [ $# -gt 0 ]; do
65 awk "BEGIN {printf \"$text\", \"$m\"; }"
68 # National language support function
71 typeset msg_echo old_nls_domain text message
73 old_nls_domain="$NLS_DOMAIN"
75 # don't use -o instead || here - this will broke ksh --misiek
76 while [ "$1" != "${1##-}" ] || [ "$1" != "${1##+}" ]; do
91 # empty message, so we return --misiek
92 if [ -z "$message" ]; then
93 NLS_DOMAIN="$old_nls_domain"
98 if [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
99 text=$(TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="${NLS_DOMAIN:-rc-scripts}" "$message")
102 printf_ "$message" "$@"
106 NLS_DOMAIN="$old_nls_domain"
111 nls "ERROR: Networking is down. %s can't be run." "$1"
116 show "Starting %s service" "$1"
119 msg_already_running()
121 nls "%s service is already running." "$1"
126 show "Stopping %s service" "$1"
131 nls "%s service is not running." "$1"
136 show "Reloading %s service" "$1"
144 # Some functions to handle PLD-style messages
150 awk "BEGIN { for (j=length(\"$text\"); j<$INIT_COL; j++) printf \".\" }"
153 # Displays message in square brackests ("[ DONE ]"). Takes two arguments.
154 # First is the text to display, second is color number to use (argument to
155 # tput setaf). If second argument is not given, default (2, green) will be
160 if [ -n "$2" ]; then COLOR="$2"; else COLOR="2"; fi
162 echo -n "$(termput setaf 6)[$(termput setaf "$COLOR") $(nls --nls-domain rc-scripts "$1") $(termput setaf 6)]$(termput op)"
198 termput hpa $INIT_COL
201 # Usage run_cmd Message command_to_run
204 typeset exit_code errors message force_err
205 typeset -i force_err=0
206 typeset -i exit_code=0
214 show "$message"; busy
216 if errors=$(HOME=/tmp TMPDIR=/tmp initlog -c "$*" 2>&1); then
218 log_success "$1 $message"
221 log_failed "$1 $message"
224 [ -n "$errors" ] && [ $exit_code -eq 1 -o $force_err -eq 1 ] && echo "$errors"
228 # A function to start a program (now it's usefull on read-only filesystem too)
231 typeset nicelevel exit_code errors prog
232 typeset -i nicelevel=0
233 typeset -i exit_code=0
234 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
235 # Test syntax. Don't use -o instead || here - this will broke ksh --misiek
236 while [ "$1" != "${1##-}" ] || [ "$1" != "${1##+}" ]; do
238 '') msg_usage " daemon [--user user] [+/-nicelevel] {program}"
242 # for compatibility with redhat/mandrake
243 nls "warning: --check option is ignored!"
252 -*|+*) SERVICE_RUN_NICE_LEVEL=$1
258 # make sure it doesn't core dump anywhere; while this could mask
259 # problems with the daemon, it also closes some security problems
264 if errors=$(HOME=/tmp TMPDIR=/tmp nice -n ${SERVICE_RUN_NICE_LEVEL:-$DEFAULT_SERVICE_RUN_NICE_LEVEL} initlog -c "$prog" 2>&1); then
265 log_success "$1 startup"
270 log_failed "$1 startup"
271 [ -n "$errors" ] && echo "$errors"
276 # A function to stop a program.
279 typeset notset killlevel base pid
282 msg_usage " killproc {program} [signal]"
289 # check for second arg to be kill level
290 if [ -n "$2" ] ; then
301 pid=$(pidofproc $base)
304 if [ -n "$pid" ] ; then
305 if [ "$notset" = "1" ] ; then
306 if ps h $pid>/dev/null 2>&1; then
307 # TERM first, then KILL if not dead
308 kill -TERM $pid >/dev/null 2>&1
310 if ps h $pid >/dev/null 2>&1 ; then
312 if ps h $pid >/dev/null 2>&1 ; then
314 if ps h $pid >/dev/null 2>&1 ; then
315 kill -KILL $pid >/dev/null 2>&1
319 if (ps h $pid >/dev/null 2>&1); then
321 log_failed "$1 shutdown"
324 log_success "$1 shutdown"
328 log_failed "$1 shutdown"
330 # use specified level only
332 if ps h $pid >/dev/null 2>&1; then
333 if (kill $killlevel $pid); then
335 log_success "$1 shutdown"
338 log_failed "$1 shutdown"
342 log_failed "$1 shutdown"
347 log_failed "$1 shutdown"
350 # Remove pid file if any.
351 if [ "$notset" = "1" ]; then
352 rm -f /var/run/$base.pid
356 # A function to find the pid of a program.
362 msg_usage " pidofproc {program}"
366 # First try "/var/run/*.pid" files
367 if [ -f /var/run/$1.pid ] ; then
368 pid=$(head -1 /var/run/$1.pid)
369 if [ -n "$pid" ] ; then
376 pid=$(pidof -o $$ -o $PPID -o %PPID -x $1)
377 if [ -n "$pid" ] ; then
382 # Finally try to extract it from ps
383 ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
384 { if ((prog == $5) || (("(" prog ")") == $5) ||
385 (("[" prog "]") == $5) ||
386 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1
393 msg_usage " status {subsys} [{daemon}]"
401 pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
402 if [ -n "$pid" ] ; then
403 nls "%s (pid %s) is running..." "$daemon" "$pid"
406 pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
407 { if ((prog == $5) || (("(" prog ")") == $5) ||
408 (("[" prog "]") == $5) ||
409 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
410 if [ "$pid" != "" ] ; then
411 nls "%s (pid %s) is running..." "$daemon" "$pid"
416 # Next try "/var/run/*.pid" files
417 if [ -f /var/run/$daemon.pid ] ; then
418 pid=$(head -1 /var/run/$1.pid)
419 if [ -n "$pid" ] ; then
420 nls "%s dead but pid file exists" "$1"
424 # See if /var/lock/subsys/$subsys exists
425 if [ -f /var/lock/subsys/$subsys ]; then
426 nls "%s dead but subsys locked" "$daemon"
429 nls "%s is stopped" "$subsys"
433 # Confirm whether we really want to run this service
436 nls -n "Start service %s (Y)es/(N)o/(C)ontinue? [Y] " "$1"
459 msg_usage " is_yes {value}"
465 yes|Yes|YES|true|True|TRUE|on|On|ON|Y|y|1)
480 msg_usage " is_no {value}"
484 # It's important that is_no() should always return logical
485 # negation of is_yes(). Really ALWAYS. --misiek
486 # NOTE: This means that passing value which mean neither "yes" nor
487 # "no" will return "true", beacause it does not mean "yes". Weird,
488 # isn't it? :) --mkochano
489 if is_yes "$1" ; then
496 # module is needed (ie. is requested, is available and isn't loaded already)
499 # module name without .o at end
500 if ! (lsmod | grep -q "$1"); then
501 if (ls -R /lib/modules/$(uname -r)/ 2> /dev/null | grep -q "${1}.o"); then
512 typeset parsed single die args foo result
514 while is_no "$parsed" ; do
533 if is_yes "${single}" ; then
535 show "Loading %s kernel module(s)" "$foo"
538 if [ -x /sbin/modprobe ] ; then
539 /sbin/modprobe -s $args "$@"
545 if is_yes "${single}" ; then
547 if [ $result == "0" ] ; then
548 is_yes "$single" && ok
551 if is_yes "$die" ; then
552 nls "Could not load %s kernel module(s)" "$@"
561 initlog -n $0 -s "$1 $2" -e 1
566 initlog -n $0 -s "$1 $2" -e 2
569 # RedHat/Mandrake specific functions
570 action () { STRING=$1; shift; run_cmd "$STRING" "$*"; }
571 success () { return 0; }
572 failure () { return 1; }
574 # Will be removed in the future
575 msg_Network_Down () { msg_network_down "$*"; }
576 msg_Already_Running () { msg_already_running "$*"; }
577 msg_Not_Running () { msg_not_running "$*"; }
578 msg_Usage () { msg_usage "$*"; }
580 # This must be last line !
581 # vi:syntax=sh:tw=78:ts=8:sw=4