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.64 2001/05/15 16:03:38 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 [ -r /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 errors="" prog=""
232 typeset -i exit_code=0
233 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
234 # Test syntax. Don't use -o instead || here - this will broke ksh --misiek
235 while [ "$1" != "${1##-}" ] || [ "$1" != "${1##+}" ]; do
237 '') msg_usage " daemon [--user user] [+/-nicelevel] {program}"
241 # for compatibility with redhat/mandrake
242 nls "warning: --check option is ignored!"
251 -*|+*) SERVICE_RUN_NICE_LEVEL=$1
257 # make sure it doesn't core dump anywhere; while this could mask
258 # problems with the daemon, it also closes some security problems
263 if errors=$(HOME=/tmp TMPDIR=/tmp nice -n ${SERVICE_RUN_NICE_LEVEL:-$DEFAULT_SERVICE_RUN_NICE_LEVEL} initlog -c "$prog" 2>&1); then
264 log_success "$1 startup"
269 log_failed "$1 startup"
270 [ -n "$errors" ] && echo "$errors"
275 # A function to stop a program.
278 typeset notset killlevel base pid
281 msg_usage " killproc {program} [signal]"
288 # check for second arg to be kill level
289 if [ -n "$2" ] ; then
300 pid=$(pidofproc $base)
303 if [ -n "$pid" ] ; then
304 if [ "$notset" = "1" ] ; then
305 if ps h $pid>/dev/null 2>&1; then
306 # TERM first, then KILL if not dead
307 kill -TERM $pid >/dev/null 2>&1
309 if ps h $pid >/dev/null 2>&1 ; then
311 if ps h $pid >/dev/null 2>&1 ; then
313 if ps h $pid >/dev/null 2>&1 ; then
314 kill -KILL $pid >/dev/null 2>&1
318 if (ps h $pid >/dev/null 2>&1); then
320 log_failed "$1 shutdown"
323 log_success "$1 shutdown"
327 log_failed "$1 shutdown"
329 # use specified level only
331 if ps h $pid >/dev/null 2>&1; then
332 if (kill $killlevel $pid); then
334 log_success "$1 shutdown"
337 log_failed "$1 shutdown"
341 log_failed "$1 shutdown"
346 log_failed "$1 shutdown"
349 # Remove pid file if any.
350 if [ "$notset" = "1" ]; then
351 rm -f /var/run/$base.pid
355 # A function to find the pid of a program.
361 msg_usage " pidofproc {program}"
365 # First try "/var/run/*.pid" files
366 if [ -f /var/run/$1.pid ] ; then
367 pid=$(head -1 /var/run/$1.pid)
368 if [ -n "$pid" ] ; then
375 pid=$(pidof -o $$ -o $PPID -o %PPID -x $1)
376 if [ -n "$pid" ] ; then
381 # Finally try to extract it from ps
382 ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
383 { if ((prog == $5) || (("(" prog ")") == $5) ||
384 (("[" prog "]") == $5) ||
385 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1
392 msg_usage " status {subsys} [{daemon}]"
400 pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
401 if [ -n "$pid" ] ; then
402 nls "%s (pid %s) is running..." "$daemon" "$pid"
405 pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
406 { if ((prog == $5) || (("(" prog ")") == $5) ||
407 (("[" prog "]") == $5) ||
408 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
409 if [ "$pid" != "" ] ; then
410 nls "%s (pid %s) is running..." "$daemon" "$pid"
415 # Next try "/var/run/*.pid" files
416 if [ -f /var/run/$daemon.pid ] ; then
417 pid=$(head -1 /var/run/$1.pid)
418 if [ -n "$pid" ] ; then
419 nls "%s dead but pid file exists" "$1"
423 # See if /var/lock/subsys/$subsys exists
424 if [ -f /var/lock/subsys/$subsys ]; then
425 nls "%s dead but subsys locked" "$daemon"
428 nls "%s is stopped" "$subsys"
432 # Confirm whether we really want to run this service
435 nls -n "Start service %s (Y)es/(N)o/(C)ontinue? [Y] " "$1"
458 msg_usage " is_yes {value}"
464 yes|Yes|YES|true|True|TRUE|on|On|ON|Y|y|1)
479 msg_usage " is_no {value}"
483 # It's important that is_no() should always return logical
484 # negation of is_yes(). Really ALWAYS. --misiek
485 # NOTE: This means that passing value which mean neither "yes" nor
486 # "no" will return "true", beacause it does not mean "yes". Weird,
487 # isn't it? :) --mkochano
488 if is_yes "$1" ; then
495 # module is needed (ie. is requested, is available and isn't loaded already)
498 # module name without .o at end
499 if ! (lsmod | grep -q "$1"); then
500 if (ls -R /lib/modules/$(uname -r)/ 2> /dev/null | grep -q "${1}.o"); then
511 typeset parsed single die args foo result
513 while is_no "$parsed" ; do
532 if is_yes "${single}" ; then
534 show "Loading %s kernel module(s)" "$foo"
537 if [ -x /sbin/modprobe ] ; then
538 /sbin/modprobe -s $args "$@"
544 if is_yes "${single}" ; then
546 if [ $result == "0" ] ; then
547 is_yes "$single" && ok
550 if is_yes "$die" ; then
551 nls "Could not load %s kernel module(s)" "$@"
560 initlog -n $0 -s "$1 $2" -e 1
565 initlog -n $0 -s "$1 $2" -e 2
568 # RedHat/Mandrake specific functions
569 action () { STRING=$1; shift; run_cmd "$STRING" "$*"; }
570 success () { return 0; }
571 failure () { return 1; }
573 # TO BE REMOVED SOON. --misiek
574 msg_Network_Down () { msg_network_down "$*"; }
575 msg_Already_Running () { msg_already_running "$*"; }
576 msg_Not_Running () { msg_not_running "$*"; }
577 msg_Usage () { msg_usage "$*"; }
579 # This must be last line !
580 # vi:syntax=sh:tw=78:ts=8:sw=4