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
21 [ -r /etc/sysconfig/bootsplash ] && . /etc/sysconfig/bootsplash
23 [ -z "$COLUMNS" ] && COLUMNS=80
29 msg_usage " is_yes {value}"
35 yes|Yes|YES|true|True|TRUE|on|On|ON|Y|y|1)
50 msg_usage " is_no {value}"
55 no|No|NO|false|False|FALSE|off|Off|OFF|N|n|0)
66 if is_yes "$FASTRC"; then
67 INIT_DOTS=$(awk "BEGIN{for(\$i=0;\$i<$INIT_COL;\$i++)printf(\".\");}")
71 while [ "$1" != "${1##-}" ] || [ "$1" != "${1##+}" ]; do
90 awk '{split($3,v,"."); printf("%03d%03d%03d\n", v[1],v[2],v[3]);}' /proc/version
95 awk '{split($3,v,"."); printf("%03d%03d\n", v[1],v[2]);}' /proc/version
100 awk '{split($3,v,"."); printf("%03d\n", v[1]);}' /proc/version
106 if is_yes "$FASTRC" || [ ! -d /usr/share/terminfo ] || \
107 [ ! -x /usr/bin/tput -a ! -x /bin/tput ]; then
110 echo -ne "\033[$(($2+1))G"
113 echo -ne "\033[${2}A"
119 is_yes "$COLOR_INIT" && echo -ne "\033[0;3${2}m"
126 # check if we are on proper terminal
127 tput longname > /dev/null 2>&1 || return
134 is_yes "$COLOR_INIT" && tput "$@"
149 if [ $# -gt 0 ]; then
152 while [ $# -gt 0 ]; do
157 awk "BEGIN {printf \"$text\", \"$m\"; }"
160 # National language support function
163 typeset msg_echo old_nls_domain text message
165 old_nls_domain="$NLS_DOMAIN"
167 # don't use -o instead || here - this will break ksh --misiek
168 while [ "$1" != "${1##-}" ] || [ "$1" != "${1##+}" ]; do
183 # empty message, so we return --misiek
184 if [ -z "$message" ]; then
185 NLS_DOMAIN="$old_nls_domain"
190 if is_yes "$FASTRC"; then
191 printf "$message" "$@"
192 elif [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
193 text=$(TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="${NLS_DOMAIN:-rc-scripts}" "$message")
196 printf_ "$message" "$@"
200 NLS_DOMAIN="$old_nls_domain"
209 if ! is_no "$BOOT_SPLASH"; then
210 [ -x /bin/splash ] && /bin/splash "$action"
218 nls "ERROR: Networking is down. %s can't be run." "$1" >&2
223 show "Starting %s service" "$1"
226 msg_already_running()
228 nls "%s service is already running." "$1"
233 show "Stopping %s service" "$1"
238 nls "%s service is not running." "$1"
243 show "Reloading %s service" "$1"
251 # Some functions to handle PLD-style messages
256 if is_yes "$FASTRC"; then
260 termput hpa $INIT_COL
264 awk "BEGIN { for (j=length(\"$text\"); j<$INIT_COL; j++) printf \".\" }"
268 # Displays message in square brackests ("[ DONE ]"). Takes two arguments.
269 # First is the text to display, second is color number to use (argument to
270 # tput setaf). If second argument is not given, default (2, green) will be
275 if [ -n "$2" ]; then COLOR="$2"; else COLOR="2"; fi
277 echo -n "$(termput setaf 6)[$(termput setaf "$COLOR") $(nls --nls-domain rc-scripts "$1") $(termput setaf 6)]$(termput op)"
313 termput hpa $INIT_COL
316 # Check if $pid (could be plural) are running
320 [ -d "/proc/$1" ] && return 0
326 # outside chroot get only thsese processes which are outside chroot.
327 # inside chroot get only these processes which are inside of chroot.
328 # (note: some processes like named are chrooted but run outside chroot)
331 if [ $# -lt 1 -o ! -d /proc/1 ] ; then
336 for root_pid in $@; do
337 root_dir=$(resolvesymlink /proc/${root_pid}/root)
338 if [ -n "$root_dir" ]; then
340 if [ -n "${SYSTEM_CHROOTS}" ]; then
341 for r_dir in ${SYSTEM_CHROOTS}; do
342 echo "$root_dir" | grep -q "^${r_dir}" && good_add_pid=0
345 [ "$good_add_pid" -eq 1 ] && good_pids="$good_pids $root_pid"
351 # Usage run_cmd Message command_to_run
354 typeset exit_code errors message force_err
355 typeset -i force_err=0
356 typeset -i exit_code=0
364 show "$message"; busy
366 if errors=$(HOME=/tmp TMPDIR=/tmp initlog -c "$*" 2>&1); then
368 log_success "$1 $message"
371 log_failed "$1 $message"
374 [ -n "$errors" ] && [ $exit_code -eq 1 -o $force_err -eq 1 ] && echo "$errors"
378 # A function to start a program (now it's useful on read-only filesystem too)
381 typeset errors="" prog="" limits=""
382 typeset -i exit_code=0
383 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
384 # "-u unlimited" (-p for ksh) to make sure daemons will be able to fork.
385 # "-c 0" to make sure it doesn't core dump anywhere; while this could mask
386 # problems with the daemon, it also closes some security problems.
387 # Users' limits are set via pam_limits.
388 [ -z "$DEFAULT_SERVICE_LIMITS" ] && DEFAULT_SERVICE_LIMITS_HARD="-u unlimited -c 0"
389 # Test syntax. Don't use -o instead || here - this will break ksh --misiek
390 while [ "$1" != "${1##-}" ] || [ "$1" != "${1##+}" ]; do
393 msg_usage " daemon [--user user] [--fork] [+/-nicelevel] {program}"
397 # for compatibility with redhat/mandrake
398 nls "warning: --check option is ignored!"
404 [ "$1" != "root" ] && prog="/bin/su $1 -s /bin/sh -c \""
408 prog="/usr/bin/setsid sh -c \""
412 -*|+*) SERVICE_RUN_NICE_LEVEL=$1
416 # If command to execute ends with quotation mark, add remaining
417 # arguments and close quotation.
418 if [ "$prog" != "${prog%\"}" ]; then
419 prog="$prog $*$end\""
424 if [ -n "$KSH_VERSION" ]; then
425 limits="`echo "${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}" | awk '/-Su/ {sub(/-Su/,"-Sp");} /-Hu/ {sub(/-Hu/,"-Hp");} /-u/ {sub(/-u/,"-p");} {print;}'`"
426 elif [ -n "$ZSH_VERSION" ]; then
427 limits="${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}"
428 elif [ -n "$BASH_VERSION" ]; then
429 limits="${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}"
430 # elif [ -n "`$SH -c 'echo ${.sh.version}' 2>/dev/null`" ]; then
433 [ -n "$limits" ] && eval `echo "$limits" | awk 'BEGIN {RS="[\n-]";} !/^ *$/ { printf("ulimit -%s ;", $0); }'`
437 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
438 log_success "$1 startup"
443 log_failed "$1 startup"
444 [ -n "$errors" ] && echo "$errors"
449 # A function to stop a program.
452 typeset notset killlevel base pid result
455 msg_usage " killproc {program} [signal]"
462 # check for second arg to be kill level
463 if [ -n "$2" ] ; then
471 base=$(basename "$1")
474 pid=$(pidofproc "$1")
475 [ -z "$pid" ] && pid=$(pidofproc "$base")
478 if [ -n "$pid" -a "$pid" != "$$" ] && checkpid $pid 2>&1 ; then
479 if [ "$notset" = "1" ] ; then
480 if checkpid $pid 2>&1; then
481 # TERM first, then KILL if not dead
484 if checkpid $pid && sleep 1 &&
485 checkpid $pid && sleep 3 &&
493 if [ "$result" -eq 0 ]; then
495 log_failed "$1 shutdown"
498 log_success "$1 shutdown"
500 result=$(( ! $result ))
502 # use specified level only
503 if checkpid $pid > /dev/null 2>&1; then
506 if [ "$result" -eq 0 ]; then
508 log_success "$1 got $killlevel"
512 log_failed "$1 didn't get $killlevel"
517 log_failed "$1 shutdown"
522 log_failed "$1 shutdown"
526 # Remove pid file if any.
527 if [ "$notset" = "1" ]; then
528 rm -f /var/run/${base}.pid
534 # A function to find the pid of a program.
538 base=$(basename "$1")
542 msg_usage " pidofproc {program}"
546 # First try "/var/run/*.pid" files
547 if [ -f /var/run/${base}.pid ] ; then
550 read line < /var/run/${base}.pid
552 [ -z "$(echo "$p" | awk '{gsub(/[0-9]/,"");print;}')" ] && pid="$pid $p"
557 [ -z "$pid" ] && pidof -o $$ -o $PPID -o %PPID -x "$1"
558 pid=$(filter_chroot "$pid")
564 typeset base pid subsys daemon
567 base=$(basename $daemon)
571 msg_usage " status {subsys} [{daemon}]"
576 pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
577 pid=$(filter_chroot "$pid")
579 if [ "$pid" != "" ]; then
580 nls "%s (pid %s) is running..." "$daemon" "$pid"
583 # pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
584 # { if ((prog == $5) || (("(" prog ")") == $5) ||
585 # (("[" prog "]") == $5) ||
586 # ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
587 # if [ "$pid" != "" ]; then
588 # nls "%s (pid %s) is running..." "$daemon" "$pid"
593 # Next try "/var/run/*.pid" files
594 if [ -f /var/run/${base}.pid ]; then
595 read pid < /var/run/${base}.pid
596 pid=$(filter_chroot "$pid")
597 if [ "$pid" != "" ]; then
598 nls "%s dead but pid file exists" "$subsys"
603 # See if /var/lock/subsys/$subsys exists
604 if [ -f /var/lock/subsys/$subsys ]; then
605 nls "%s dead but subsys locked" "$subsys"
608 nls "%s is stopped" "$subsys"
612 # Confirm whether we really want to run this service
615 nls -n "Start service %s (Y)es/(N)o/(C)ontinue? [Y] " "$1"
634 # module is needed (ie. is requested, is available and isn't loaded already)
637 # module name without .o at end
638 if ! (lsmod | grep -q "$1"); then
639 if (ls -R /lib/modules/$(uname -r)/ 2> /dev/null | grep -q "${1}.\(\|k\)o\(\|.gz\)"); then
650 typeset parsed single die args foo result
652 while is_no "$parsed" ; do
671 if is_yes "${single}" ; then
673 show "Loading %s kernel module(s)" "$foo"
676 if [ -x /sbin/modprobe ] ; then
677 /sbin/modprobe -s $args "$@"
683 if is_yes "${single}" ; then
685 if [ $result == "0" ] ; then
686 is_yes "$single" && ok
689 if is_yes "$die" ; then
690 nls "Could not load %s kernel module(s)" "$@"
699 initlog -n $0 -s "$1 $2" -e 1
704 initlog -n $0 -s "$1 $2" -e 2
707 # RedHat/Mandrake specific functions
708 action () { STRING=$1; shift; run_cmd "$STRING" "$*"; }
709 success () { return 0; }
710 failure () { return 1; }
712 # TO BE REMOVED IN AC. --ankry
713 msg_Network_Down () { msg_network_down "$*"; }
714 msg_Already_Running () { msg_already_running "$*"; }
715 msg_Not_Running () { msg_not_running "$*"; }
716 msg_Usage () { msg_usage "$*"; }
721 # * indent-tabs-mode: notnil
725 # vi: syntax=sh:shiftwidth=8: