1 #!/bin/sh - keep it for file(1) to get bourne shell script result
2 # functions This file contains functions to be used by most or all
3 # shell scripts in the /etc/init.d directory.
7 # Author: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
8 # Hacked by: Greg Galloway and Marc Ewing
10 # Marek Obuchowicz <elephant@pld-linux.org>
11 # Arkadiusz Mi¶kiewicz <misiek@pld-linux.org>
12 # Micha³ Kochanowicz <mkochano@pld-linux.org>
13 # £ukasz Pawelczyk <havner@pld-linux.org>
15 # First set up a default search path.
16 export PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin"
21 [ -z "${CONSOLETYPE}" ] && CONSOLETYPE="$(/sbin/consoletype)"
32 # Bold definition (second parameter to termput setaf)
36 CBRACKETS="$CYAN" # brackets [ ] color
37 CDONE="$GREEN" # DONE and WORK color
38 CBUSY="$MAGENTA" # BUSY color
39 CFAIL="$RED" # FAIL and DIED color
40 CPOWEREDBY="$CYAN" # "Powered by" color
41 CPLD="$GREEN" # "PLD Linux Distribution" color
42 CI="$RED" # Capital I color (press I to enter interactive startup)
43 CRESMAN="$GREEN" # "Resource Manager" color
44 CHARS="" # Characters displayed on the begining of show line
45 CCHARS="$NORMAL" # Color of these characters (look at /etc/sysconfig/init-colors.gentoo example)
47 # Source configuration if available - may override default values
48 [ -r /etc/sysconfig/init-colors ] && . /etc/sysconfig/init-colors
49 [ -r /etc/sysconfig/system ] && . /etc/sysconfig/system
50 [ -r /etc/sysconfig/bootsplash ] && . /etc/sysconfig/bootsplash
52 [ -z "$COLUMNS" ] && COLUMNS=80
58 msg_usage " is_yes {value}"
64 yes|Yes|YES|true|True|TRUE|on|On|ON|Y|y|1)
79 msg_usage " is_no {value}"
84 no|No|NO|false|False|FALSE|off|Off|OFF|N|n|0)
95 if is_yes "$FASTRC"; then
96 INIT_DOTS=$(awk "BEGIN{for(\$i=0;\$i<$INIT_COL;\$i++)printf(\".\");}")
100 while [ "$1" != "${1##-}" ] || [ "$1" != "${1##+}" ]; do
119 awk '{split($3,v,"."); printf("%03d%03d%03d\n", v[1],v[2],v[3]);}' /proc/version
124 awk '{split($3,v,"."); printf("%03d%03d\n", v[1],v[2]);}' /proc/version
129 awk '{split($3,v,"."); printf("%03d\n", v[1]);}' /proc/version
135 if is_yes "$FASTRC" || [ ! -d /usr/share/terminfo ] || \
136 [ ! -x /usr/bin/tput -a ! -x /bin/tput ]; then
139 echo -ne "\033[$(($2+1))G"
142 echo -ne "\033[${2}A"
154 is_yes "$COLOR_INIT" && echo -ne "\033[${ISBOLD};3${2}m"
157 termput setaf $NORMAL
161 # check if we are on proper terminal
162 tput longname > /dev/null 2>&1 || return
169 if [ "$3" == "1" ]; then tput bold; else tput sgr0; fi
170 is_yes "$COLOR_INIT" && tput setaf "$2"
173 termput setaf $NORMAL
185 if [ $# -gt 0 ]; then
188 while [ $# -gt 0 ]; do
193 awk "BEGIN {printf \"$text\", \"$m\"; }"
196 # National language support function
199 typeset msg_echo old_nls_domain text message
201 old_nls_domain="$NLS_DOMAIN"
203 # don't use -o instead || here - this will break ksh --misiek
204 while [ "$1" != "${1##-}" ] || [ "$1" != "${1##+}" ]; do
219 # empty message, so we return --misiek
220 if [ -z "$message" ]; then
221 NLS_DOMAIN="$old_nls_domain"
226 if is_yes "$FASTRC"; then
227 printf "$message" "$@"
228 elif [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
229 text=$(TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="${NLS_DOMAIN:-rc-scripts}" "$message")
232 printf_ "$message" "$@"
236 NLS_DOMAIN="$old_nls_domain"
245 if ! is_no "$BOOT_SPLASH"; then
246 [ -x /bin/splash ] && /bin/splash "$action"
254 nls "ERROR: Networking is down. %s can't be run." "$1" >&2
259 show "Starting %s service" "$1"
262 msg_already_running()
264 nls "%s service is already running." "$1"
269 show "Stopping %s service" "$1"
274 nls "%s service is not running." "$1"
279 show "Reloading %s service" "$1"
287 # Some functions to handle PLD-style messages
292 if is_yes "$FASTRC"; then
295 if [ -n "$CHARS" ]; then
296 termput setaf $CCHARS
301 termput hpa $INIT_COL
304 if [ -n "$CHARS" ]; then
305 termput setaf $CCHARS
310 awk "BEGIN { for (j=length(\"$text\"); j<$INIT_COL; j++) printf \".\" }"
314 # Displays message in square brackests ("[ DONE ]"). Takes two arguments.
315 # First is the text to display, second is color number to use (argument to
316 # tput setaf). If second argument is not given, default (2, green) will be
321 if [ -n "$2" ]; then COLOR="$2"; else COLOR="$CDONE"; fi
323 echo -n "$(termput setaf $CBRACKETS)[$(termput setaf $COLOR) $(nls --nls-domain rc-scripts "$1") $(termput setaf $CBRACKETS)]$(termput op)"
328 progress "BUSY" "$CBUSY"
345 progress "FAIL" "$CFAIL"
352 progress "DIED" "$CFAIL"
359 termput hpa $INIT_COL
362 # Check if $pid (could be plural) are running
366 [ -d "/proc/$1" ] && return 0
372 # outside chroot get only thsese processes which are outside chroot.
373 # inside chroot get only these processes which are inside of chroot.
374 # (note: some processes like named are chrooted but run outside chroot)
377 if [ $# -lt 1 -o ! -d /proc/1 ] ; then
382 for root_pid in $@; do
383 root_dir=$(resolvesymlink /proc/${root_pid}/root)
384 if [ -n "$root_dir" ]; then
386 if [ -n "${SYSTEM_CHROOTS}" ]; then
387 for r_dir in ${SYSTEM_CHROOTS}; do
388 echo "$root_dir" | grep -q "^${r_dir}" && good_add_pid=0
391 [ "$good_add_pid" -eq 1 ] && good_pids="$good_pids $root_pid"
397 # Usage run_cmd Message command_to_run
400 typeset exit_code errors message force_err
401 typeset -i force_err=0
402 typeset -i exit_code=0
410 show "$message"; busy
413 if errors=$(HOME=/tmp TMPDIR=/tmp initlog -c "$*" 2>&1); then
415 log_success "$1 $message"
418 log_failed "$1 $message"
421 [ -n "$errors" ] && [ $exit_code -eq 1 -o $force_err -eq 1 ] && echo "$errors"
425 # A function to start a program (now it's useful on read-only filesystem too)
428 typeset errors="" prog="" limits="" waitname="" waittime=""
429 typeset -i exit_code=0
430 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
431 # "-u unlimited" (-p for ksh) to make sure daemons will be able to fork.
432 # "-c 0" to make sure it doesn't core dump anywhere; while this could mask
433 # problems with the daemon, it also closes some security problems.
434 # Users' limits are set via pam_limits.
435 [ -z "$DEFAULT_SERVICE_LIMITS" ] && DEFAULT_SERVICE_LIMITS_HARD="-u unlimited -c 0"
436 # Test syntax. Don't use -o instead || here - this will break ksh --misiek
437 while [ "$1" != "${1##-}" ] || [ "$1" != "${1##+}" ]; do
440 msg_usage " daemon [--user user] [--fork] [+/-nicelevel] {program}"
444 # for compatibility with redhat/mandrake
445 nls "warning: --check option is ignored!"
451 [ "$1" != "root" ] && prog="/bin/su $1 -s /bin/sh -c \""
455 prog="/usr/bin/setsid sh -c \""
469 -*|+*) SERVICE_RUN_NICE_LEVEL=$1
474 # If command to execute ends with quotation mark, add remaining
475 # arguments and close quotation.
476 if [ "$prog" != "${prog%\"}" ]; then
477 prog="$prog $*$end\""
482 if [ -n "$KSH_VERSION" ]; then
483 limits="`echo "${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}" | awk '/-Su/ {sub(/-Su/,"-Sp");} /-Hu/ {sub(/-Hu/,"-Hp");} /-u/ {sub(/-u/,"-p");} {print;}'`"
484 elif [ -n "$ZSH_VERSION" ]; then
485 limits="${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}"
486 elif [ -n "$BASH_VERSION" ]; then
487 limits="${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}"
488 # elif [ -n "`$SH -c 'echo ${.sh.version}' 2>/dev/null`" ]; then
491 [ -n "$limits" ] && eval `echo "$limits" | awk 'BEGIN {RS="[\n-]";} !/^ *$/ { printf("ulimit -%s ;", $0); }'`
493 [ -z "$DEFAULT_SERVICE_UMASK" ] && DEFAULT_SERVICE_UMASK=022
498 if errors=$(umask ${SERVICE_UMASK:-$DEFAULT_SERVICE_UMASK}; USER=root HOME=/tmp TMPDIR=/tmp nice -n ${SERVICE_RUN_NICE_LEVEL:-$DEFAULT_SERVICE_RUN_NICE_LEVEL} initlog -c "$prog" 2>&1); then
499 if [ -n "$waitname" -a -n "$waittime" ]; then
501 base=$(basename "$waitname")
503 pid=$(pidofproc "$waitname" "$pidfile")
504 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
506 while [ "$i" -lt "$waittime" ]; do
507 checkpid $pid && sleep 1 || break
510 log_success "$1 startup"
515 log_failed "$1 startup"
516 [ -n "$errors" ] && echo "$errors"
521 # A function to stop a program.
524 typeset notset killlevel base pid pidfile result
527 msg_usage " killproc {program} [signal]"
531 while [ "$1" != "${1##-}" ] || [ "$1" != "${1##+}" ]; do
554 # check for second arg to be kill level
555 if [ -n "$2" ] ; then
563 base=$(basename "$1")
566 pid=$(pidofproc "$1" "$pidfile")
567 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
570 if [ -n "$pid" -a "$pid" != "$$" ] && checkpid $pid 2>&1 ; then
571 if [ "$notset" = "1" ] ; then
572 if checkpid $pid 2>&1; then
573 # TERM first, then KILL if not dead
576 if checkpid $pid && sleep 1 &&
577 checkpid $pid && sleep 3 &&
585 if [ "$result" -eq 0 ]; then
587 log_failed "$1 shutdown"
590 log_success "$1 shutdown"
592 result=$(( ! $result ))
594 # use specified level only
595 if checkpid $pid > /dev/null 2>&1; then
598 if [ "$result" -eq 0 ]; then
600 log_success "$1 got $killlevel"
604 log_failed "$1 didn't get $killlevel"
609 log_failed "$1 shutdown"
614 log_failed "$1 shutdown"
618 if [ -n "$waitname" -a -n "$waittime" ]; then
620 while [ "$i" -lt "$waittime" ]; do
621 checkpid $pid && sleep 1 || break
625 # Remove pid file if any.
626 if [ "$notset" = "1" ]; then
627 rm -f /var/run/${base}.pid
633 # A function to find the pid of a program.
636 typeset pid pidfile base
637 base=$(basename "$1")
639 [ -n "$2" ] && pidfile="$2"
643 msg_usage " pidofproc {program}"
647 # First try pidfile or "/var/run/*.pid"
648 if [ -f /var/run/${pidfile} ] ; then
651 read line < /var/run/${pidfile}
653 [ -z "$(echo "$p" | awk '{gsub(/[0-9]/,"");print;}')" ] && pid="$pid $p"
658 [ -z "$pid" ] && pidof -o $$ -o $PPID -o %PPID -x "$1"
659 pid=$(filter_chroot "$pid")
665 typeset base pid subsys daemon
668 base=$(basename $daemon)
672 msg_usage " status {subsys} [{daemon}]"
677 pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
678 pid=$(filter_chroot "$pid")
680 if [ "$pid" != "" ]; then
681 nls "%s (pid %s) is running..." "$daemon" "$pid"
684 # pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
685 # { if ((prog == $5) || (("(" prog ")") == $5) ||
686 # (("[" prog "]") == $5) ||
687 # ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
688 # if [ "$pid" != "" ]; then
689 # nls "%s (pid %s) is running..." "$daemon" "$pid"
694 # Next try "/var/run/*.pid" files
695 if [ -f /var/run/${base}.pid ]; then
696 read pid < /var/run/${base}.pid
697 pid=$(filter_chroot "$pid")
698 if [ "$pid" != "" ]; then
699 nls "%s dead but pid file exists" "$subsys"
704 # See if /var/lock/subsys/$subsys exists
705 if [ -f /var/lock/subsys/$subsys ]; then
706 nls "%s dead but subsys locked" "$subsys"
709 nls "%s is stopped" "$subsys"
713 # Confirm whether we really want to run this service
716 nls -n "Start service %s (Y)es/(N)o/(C)ontinue? [Y] " "$1"
735 # module is needed (ie. is requested, is available and isn't loaded already)
738 # module name without .o at end
739 if ! (lsmod | grep -q "$1"); then
740 if (ls -R /lib/modules/$(uname -r)/ 2> /dev/null | grep -q "${1}.\(\|k\)o\(\|.gz\)"); then
751 typeset parsed single die args foo result
753 while is_no "$parsed" ; do
772 if is_yes "${single}" ; then
774 show "Loading %s kernel module(s)" "$foo"
777 if [ -x /sbin/modprobe ] ; then
778 /sbin/modprobe -s $args "$@"
784 if is_yes "${single}" ; then
786 if [ $result == "0" ] ; then
787 is_yes "$single" && ok
790 if is_yes "$die" ; then
791 nls "Could not load %s kernel module(s)" "$@"
800 initlog -n $0 -s "$1 $2" -e 1
805 initlog -n $0 -s "$1 $2" -e 2
808 # RedHat/Mandrake specific functions
809 action () { STRING=$1; shift; run_cmd "$STRING" "$*"; }
810 success () { return 0; }
811 failure () { return 1; }
814 selinuxfs=`awk '/ selinuxfs / { print $2 }' /proc/mounts`
815 echo "*** Warning -- SELinux is active"
816 echo "*** Disabling security enforcement for system recovery."
817 echo "*** Run 'setenforce 1' to reenable."
818 echo "0" > $selinuxfs/enforce
822 selinuxfs=`awk '/ selinuxfs / { print $2 }' /proc/mounts`
824 *** Warning -- SELinux relabel is required. ***
825 *** Disabling security enforcement. ***
826 *** Relabeling could take a very long time, ***
827 *** depending on file system size. ***
829 echo "0" > $selinuxfs/enforce
830 /sbin/fixfiles -F relabel > /dev/null 2>&1
832 echo "*** Enabling security enforcement. ***"
833 echo $SELINUX > $selinuxfs/enforce
839 # * indent-tabs-mode: notnil
843 # vi: syntax=sh:shiftwidth=8: