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 [ $# -lt 1 ] && return
333 for root_pid in $@; do
334 root_dir=$(resolvesymlink /proc/${root_pid}/root)
335 if [ -n "$root_dir" ]; then
336 for r_dir in ${SYSTEM_CHROOTS}; do
337 [ "$rewt" = "$CHROOT_TO_DIR" ] && good_pids="$good_pids $root_pid"
339 [ "$root_dir" = "/" -o "$root_dir" = "/usr/share/empty" ] && good_pids="$good_pids $root_pid"
345 # Usage run_cmd Message command_to_run
348 typeset exit_code errors message force_err
349 typeset -i force_err=0
350 typeset -i exit_code=0
358 show "$message"; busy
360 if errors=$(HOME=/tmp TMPDIR=/tmp initlog -c "$*" 2>&1); then
362 log_success "$1 $message"
365 log_failed "$1 $message"
368 [ -n "$errors" ] && [ $exit_code -eq 1 -o $force_err -eq 1 ] && echo "$errors"
372 # A function to start a program (now it's useful on read-only filesystem too)
375 typeset errors="" prog="" limits=""
376 typeset -i exit_code=0
377 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
378 # "-u unlimited" (-p for ksh) to make sure daemons will be able to fork.
379 # "-c 0" to make sure it doesn't core dump anywhere; while this could mask
380 # problems with the daemon, it also closes some security problems.
381 # Users' limits are set via pam_limits.
382 [ -z "$DEFAULT_SERVICE_LIMITS" ] && DEFAULT_SERVICE_LIMITS_HARD="-u unlimited -c 0"
383 # Test syntax. Don't use -o instead || here - this will break ksh --misiek
384 while [ "$1" != "${1##-}" ] || [ "$1" != "${1##+}" ]; do
387 msg_usage " daemon [--user user] [+/-nicelevel] {program}"
391 # for compatibility with redhat/mandrake
392 nls "warning: --check option is ignored!"
398 [ "$1" != "root" ] && prog="/bin/su $1 -c \""
401 -*|+*) SERVICE_RUN_NICE_LEVEL=$1
405 # If command to execute ends with quotation mark, add remaining
406 # arguments and close quotation.
407 if [ "$prog" != "${prog%\"}" ]; then
413 if [ -n "$KSH_VERSION" ]; then
414 limits="`echo "${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}" | awk '/-Su/ {sub(/-Su/,"-Sp");} /-Hu/ {sub(/-Hu/,"-Hp");} /-u/ {sub(/-u/,"-p");} {print;}'`"
415 elif [ -n "$ZSH_VERSION" ]; then
416 limits="${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}"
417 elif [ -n "$BASH_VERSION" ]; then
418 limits="${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}"
419 # elif [ -n "`$SH -c 'echo ${.sh.version}' 2>/dev/null`" ]; then
422 [ -n "$limits" ] && eval `echo "$limits" | awk 'BEGIN {RS="[-\n]";} !/^ *$/ { print "ulimit -" $0; }'`
426 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
427 log_success "$1 startup"
432 log_failed "$1 startup"
433 [ -n "$errors" ] && echo "$errors"
438 # A function to stop a program.
441 typeset notset killlevel base pid result
444 msg_usage " killproc {program} [signal]"
451 # check for second arg to be kill level
452 if [ -n "$2" ] ; then
460 base=$(basename "$1")
463 pid=$(pidofproc "$1")
464 [ -z "$pid" ] && pid=$(pidofproc "$base")
467 if [ -n "$pid" -a "$pid" != "$$" ] && checkpid $pid 2>&1 ; then
468 if [ "$notset" = "1" ] ; then
469 if checkpid $pid 2>&1; then
470 # TERM first, then KILL if not dead
473 if checkpid $pid && sleep 1 &&
474 checkpid $pid && sleep 3 &&
482 if [ "$result" -eq 0 ]; then
484 log_failed "$1 shutdown"
487 log_success "$1 shutdown"
489 result=$(( ! $result ))
491 # use specified level only
492 if checkpid $pid > /dev/null 2>&1; then
495 if [ "$result" -eq 0 ]; then
497 log_success "$1 got $killlevel"
501 log_failed "$1 didn't get $killlevel"
506 log_failed "$1 shutdown"
511 log_failed "$1 shutdown"
515 # Remove pid file if any.
516 if [ "$notset" = "1" ]; then
517 rm -f /var/run/${base}.pid
523 # A function to find the pid of a program.
527 base=$(basename "$1")
531 msg_usage " pidofproc {program}"
535 # First try "/var/run/*.pid" files
536 if [ -f /var/run/${base}.pid ] ; then
539 read line < /var/run/${base}.pid
541 [ -z "$(echo "$p" | awk '{gsub(/[0-9]/,"");print;}')" ] && pid="$pid $p"
546 [ -z "$pid" ] && pidof -o $$ -o $PPID -o %PPID -x "$1"
547 pid=$(filter_chroot "$pid")
553 typeset base pid subsys daemon
556 base=$(basename $daemon)
560 msg_usage " status {subsys} [{daemon}]"
565 pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
566 pid=$(filter_chroot "$pid")
568 if [ "$pid" != "" ]; then
569 nls "%s (pid %s) is running..." "$daemon" "$pid"
572 # pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
573 # { if ((prog == $5) || (("(" prog ")") == $5) ||
574 # (("[" prog "]") == $5) ||
575 # ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
576 # if [ "$pid" != "" ]; then
577 # nls "%s (pid %s) is running..." "$daemon" "$pid"
582 # Next try "/var/run/*.pid" files
583 if [ -f /var/run/${base}.pid ]; then
584 read pid < /var/run/${base}.pid
585 pid=$(filter_chroot "$pid")
586 if [ "$pid" != "" ]; then
587 nls "%s dead but pid file exists" "$subsys"
592 # See if /var/lock/subsys/$subsys exists
593 if [ -f /var/lock/subsys/$subsys ]; then
594 nls "%s dead but subsys locked" "$subsys"
597 nls "%s is stopped" "$subsys"
601 # Confirm whether we really want to run this service
604 nls -n "Start service %s (Y)es/(N)o/(C)ontinue? [Y] " "$1"
623 # module is needed (ie. is requested, is available and isn't loaded already)
626 # module name without .o at end
627 if ! (lsmod | grep -q "$1"); then
628 if (ls -R /lib/modules/$(uname -r)/ 2> /dev/null | grep -q "${1}.\(\|k\)o\(\|.gz\)"); then
639 typeset parsed single die args foo result
641 while is_no "$parsed" ; do
660 if is_yes "${single}" ; then
662 show "Loading %s kernel module(s)" "$foo"
665 if [ -x /sbin/modprobe ] ; then
666 /sbin/modprobe -s $args "$@"
672 if is_yes "${single}" ; then
674 if [ $result == "0" ] ; then
675 is_yes "$single" && ok
678 if is_yes "$die" ; then
679 nls "Could not load %s kernel module(s)" "$@"
688 initlog -n $0 -s "$1 $2" -e 1
693 initlog -n $0 -s "$1 $2" -e 2
696 # RedHat/Mandrake specific functions
697 action () { STRING=$1; shift; run_cmd "$STRING" "$*"; }
698 success () { return 0; }
699 failure () { return 1; }
701 # TO BE REMOVED IN AC. --ankry
702 msg_Network_Down () { msg_network_down "$*"; }
703 msg_Already_Running () { msg_already_running "$*"; }
704 msg_Not_Running () { msg_not_running "$*"; }
705 msg_Usage () { msg_usage "$*"; }
710 # * indent-tabs-mode: notnil
714 # vi: syntax=sh:shiftwidth=8: