1 # functions This file contains functions to be used by most or all
2 # shell scripts in the /etc/init.d directory.
6 # Author: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
7 # Hacked by: Greg Galloway and Marc Ewing
9 # Marek Obuchowicz <elephant@pld-linux.org>
10 # Arkadiusz Mi¶kiewicz <misiek@pld-linux.org>
11 # Micha³ Kochanowicz <mkochano@pld-linux.org>
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
28 msg_usage " is_yes {value}"
34 yes|Yes|YES|true|True|TRUE|on|On|ON|Y|y|1)
49 msg_usage " is_no {value}"
54 no|No|NO|false|False|FALSE|off|Off|OFF|N|n|0)
65 if is_yes "$FASTRC"; then
66 INIT_DOTS=$(awk "BEGIN{for(\$i=0;\$i<$INIT_COL;\$i++)printf(\".\");}")
70 while [ "$1" != "${1##-}" ] || [ "$1" != "${1##+}" ]; do
89 awk '{split($3,v,"."); printf("%03d%03d%03d\n", v[1],v[2],v[3]);}' /proc/version
94 awk '{split($3,v,"."); printf("%03d%03d\n", v[1],v[2]);}' /proc/version
99 awk '{split($3,v,"."); printf("%03d\n", v[1]);}' /proc/version
105 if is_yes "$FASTRC" || [ ! -d /usr/share/terminfo ] || \
106 [ ! -x /usr/bin/tput -a ! -x /bin/tput ]; then
109 echo -ne "\033[$(($2+1))G"
112 echo -ne "\033[${2}A"
118 is_yes "$COLOR_INIT" && echo -ne "\033[0;3${2}m"
125 # check if we are on proper terminal
126 tput longname > /dev/null 2>&1 || return
133 is_yes "$COLOR_INIT" && tput "$@"
148 if [ $# -gt 0 ]; then
151 while [ $# -gt 0 ]; do
156 awk "BEGIN {printf \"$text\", \"$m\"; }"
159 # National language support function
162 typeset msg_echo old_nls_domain text message
164 old_nls_domain="$NLS_DOMAIN"
166 # don't use -o instead || here - this will break ksh --misiek
167 while [ "$1" != "${1##-}" ] || [ "$1" != "${1##+}" ]; do
182 # empty message, so we return --misiek
183 if [ -z "$message" ]; then
184 NLS_DOMAIN="$old_nls_domain"
189 if is_yes "$FASTRC"; then
190 printf "$message" "$@"
191 elif [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
192 text=$(TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="${NLS_DOMAIN:-rc-scripts}" "$message")
195 printf_ "$message" "$@"
199 NLS_DOMAIN="$old_nls_domain"
208 if ! is_no "$BOOT_SPLASH"; then
209 [ -x /bin/splash ] && /bin/splash "$action"
217 nls "ERROR: Networking is down. %s can't be run." "$1" >&2
222 show "Starting %s service" "$1"
225 msg_already_running()
227 nls "%s service is already running." "$1"
232 show "Stopping %s service" "$1"
237 nls "%s service is not running." "$1"
242 show "Reloading %s service" "$1"
250 # Some functions to handle PLD-style messages
255 if is_yes "$FASTRC"; then
259 termput hpa $INIT_COL
263 awk "BEGIN { for (j=length(\"$text\"); j<$INIT_COL; j++) printf \".\" }"
267 # Displays message in square brackests ("[ DONE ]"). Takes two arguments.
268 # First is the text to display, second is color number to use (argument to
269 # tput setaf). If second argument is not given, default (2, green) will be
274 if [ -n "$2" ]; then COLOR="$2"; else COLOR="2"; fi
276 echo -n "$(termput setaf 6)[$(termput setaf "$COLOR") $(nls --nls-domain rc-scripts "$1") $(termput setaf 6)]$(termput op)"
312 termput hpa $INIT_COL
315 # Check if $pid (could be plural) are running
319 [ -d "/proc/$1" ] && return 0
325 # Usage run_cmd Message command_to_run
328 typeset exit_code errors message force_err
329 typeset -i force_err=0
330 typeset -i exit_code=0
338 show "$message"; busy
340 if errors=$(HOME=/tmp TMPDIR=/tmp initlog -c "$*" 2>&1); then
342 log_success "$1 $message"
345 log_failed "$1 $message"
348 [ -n "$errors" ] && [ $exit_code -eq 1 -o $force_err -eq 1 ] && echo "$errors"
352 # A function to start a program (now it's useful on read-only filesystem too)
355 typeset errors="" prog="" limits=""
356 typeset -i exit_code=0
357 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
358 # "-u unlimited" (-p for ksh) to make sure daemons will be able to fork.
359 # "-c 0" to make sure it doesn't core dump anywhere; while this could mask
360 # problems with the daemon, it also closes some security problems.
361 # Users' limits are set via pam_limits.
362 [ -z "$DEFAULT_SERVICE_LIMITS" ] && DEFAULT_SERVICE_LIMITS_HARD="-u unlimited -c 0"
363 # Test syntax. Don't use -o instead || here - this will break ksh --misiek
364 while [ "$1" != "${1##-}" ] || [ "$1" != "${1##+}" ]; do
367 msg_usage " daemon [--user user] [+/-nicelevel] {program}"
371 # for compatibility with redhat/mandrake
372 nls "warning: --check option is ignored!"
378 [ "$1" != "root" ] && prog="/bin/su $1 -c \""
381 -*|+*) SERVICE_RUN_NICE_LEVEL=$1
385 # If command to execute ends with quotation mark, add remaining
386 # arguments and close quotation.
387 if [ "$prog" != "${prog%\"}" ]; then
393 if [ -n "$KSH_VERSION" ]; then
394 limits="`echo ${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS} | awk '/-Su/ {sub(/-Su/,"-Sp");} /-Hu/ {sub(/-Hu/,"-Hp");} /-u/ {sub(/-u/,"-p");} {print;}'`"
395 elif [ -n "$ZSH_VERSION" ]; then
396 limits="${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}"
397 elif [ -n "$BASH_VERSION" ]; then
398 limits="${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}"
399 # elif [ -n "`$SH -c 'echo ${.sh.version}' 2>/dev/null`" ]; then
401 while echo "$limits" | grep -q '[^ ]'; do
402 ulimit `echo ${limits} | sed -e 's/^ *\(-[^-]*\)\(-.*\)\?$/\1/'`
403 limits="`echo ${limits} | sed -e 's/^ *\(-[^-]*\)\(-.*\)\?$/\2/'`"
408 if errors=$(USER=root HOME=/tmp TMPDIR=/tmp nice -n ${SERVICE_RUN_NICE_LEVEL:-$DEFAULT_SERVICE_RUN_NICE_LEVEL} initlog -c "$prog" 2>&1); then
409 log_success "$1 startup"
414 log_failed "$1 startup"
415 [ -n "$errors" ] && echo "$errors"
420 # A function to stop a program.
423 typeset notset killlevel base pid result
426 msg_usage " killproc {program} [signal]"
433 # check for second arg to be kill level
434 if [ -n "$2" ] ; then
442 base=$(basename "$1")
445 pid=$(pidofproc "$1")
446 [ -z "$pid" ] && pid=$(pidofproc "$base")
449 if [ -n "$pid" -a "$pid" != "$$" ] && checkpid $pid 2>&1 ; then
450 if [ "$notset" = "1" ] ; then
451 if checkpid $pid 2>&1; then
452 # TERM first, then KILL if not dead
455 if checkpid $pid && sleep 1 &&
456 checkpid $pid && sleep 3 &&
464 if [ "$result" -eq 0 ]; then
466 log_failed "$1 shutdown"
469 log_success "$1 shutdown"
471 result=$(( ! $result ))
473 # use specified level only
474 if checkpid $pid > /dev/null 2>&1; then
477 if [ "$result" -eq 0 ]; then
479 log_success "$1 got $killlevel"
483 log_failed "$1 didn't get $killlevel"
488 log_failed "$1 shutdown"
493 log_failed "$1 shutdown"
497 # Remove pid file if any.
498 if [ "$notset" = "1" ]; then
499 rm -f /var/run/${base}.pid
505 # A function to find the pid of a program.
509 base=$(basename "$1")
513 msg_usage " pidofproc {program}"
517 # First try "/var/run/*.pid" files
518 if [ -f /var/run/${base}.pid ] ; then
521 read line < /var/run/${base}.pid
523 [ -z "$(echo "$p" | awk '{gsub(/[0-9]/,"");print;}')" ] && pid="$pid $p"
525 if [ -n "$pid" ]; then
532 pidof -o $$ -o $PPID -o %PPID -x "$1"
537 typeset base pid subsys daemon
540 base=$(basename $daemon)
544 msg_usage " status {subsys} [{daemon}]"
549 pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
551 if [ "$pid" != "" ]; then
552 nls "%s (pid %s) is running..." "$daemon" "$pid"
555 # pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
556 # { if ((prog == $5) || (("(" prog ")") == $5) ||
557 # (("[" prog "]") == $5) ||
558 # ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
559 # if [ "$pid" != "" ]; then
560 # nls "%s (pid %s) is running..." "$daemon" "$pid"
565 # Next try "/var/run/*.pid" files
566 if [ -f /var/run/${base}.pid ]; then
567 read pid < /var/run/${base}.pid
568 if [ "$pid" != "" ]; then
569 nls "%s dead but pid file exists" "$subsys"
574 # See if /var/lock/subsys/$subsys exists
575 if [ -f /var/lock/subsys/$subsys ]; then
576 nls "%s dead but subsys locked" "$subsys"
579 nls "%s is stopped" "$subsys"
583 # Confirm whether we really want to run this service
586 nls -n "Start service %s (Y)es/(N)o/(C)ontinue? [Y] " "$1"
605 # module is needed (ie. is requested, is available and isn't loaded already)
608 # module name without .o at end
609 if ! (lsmod | grep -q "$1"); then
610 if (ls -R /lib/modules/$(uname -r)/ 2> /dev/null | grep -q "${1}.\(\|k\)o\(\|.gz\)"); then
621 typeset parsed single die args foo result
623 while is_no "$parsed" ; do
642 if is_yes "${single}" ; then
644 show "Loading %s kernel module(s)" "$foo"
647 if [ -x /sbin/modprobe ] ; then
648 /sbin/modprobe -s $args "$@"
654 if is_yes "${single}" ; then
656 if [ $result == "0" ] ; then
657 is_yes "$single" && ok
660 if is_yes "$die" ; then
661 nls "Could not load %s kernel module(s)" "$@"
670 initlog -n $0 -s "$1 $2" -e 1
675 initlog -n $0 -s "$1 $2" -e 2
678 # RedHat/Mandrake specific functions
679 action () { STRING=$1; shift; run_cmd "$STRING" "$*"; }
680 success () { return 0; }
681 failure () { return 1; }
683 # TO BE REMOVED IN AC. --ankry
684 msg_Network_Down () { msg_network_down "$*"; }
685 msg_Already_Running () { msg_already_running "$*"; }
686 msg_Not_Running () { msg_not_running "$*"; }
687 msg_Usage () { msg_usage "$*"; }
692 # * indent-tabs-mode: notnil
696 # vi: syntax=sh:shiftwidth=8: