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
9 # Modified for PLD Linux by:
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 beginning 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
54 if [ -z "$VSERVER" -o "$VSERVER" = "detect" ]; then
55 _ctx="$(awk '/(s_context|VxID):.*/ { print $2 }' /proc/self/status 2>/dev/null)"
56 if [ -z "$_ctx" -o "$_ctx" = "0" ]; then
68 msg_usage " is_yes {value}"
74 yes|Yes|YES|true|True|TRUE|on|On|ON|Y|y|1)
89 msg_usage " is_no {value}"
94 no|No|NO|false|False|FALSE|off|Off|OFF|N|n|0)
105 INIT_DOTS=$(awk -vcol=$INIT_COL 'BEGIN{ for(i = 0; i < col; i++) printf("."); }')
106 if is_yes "$FASTRC"; then
110 if is_yes "${IN_SHUTDOWN}" || is_no "${RC_LOGGING}" ; then
114 while [ "$1" != "${1##-}" ]; do
133 awk '{split($3,v,"."); printf("%03d%03d%03d\n", v[1],v[2],v[3]);}' /proc/version
138 awk '{split($3,v,"."); printf("%03d%03d\n", v[1],v[2]);}' /proc/version
143 awk '{split($3,v,"."); printf("%03d\n", v[1]);}' /proc/version
149 local ck="_tput_$1_$2_$3"
152 if [ -n "$cv" ]; then
163 if is_yes "$FASTRC" || is_no "$TPUT"; then
166 echo -ne "\033[$(($2+1))G"
169 echo -ne "\033[${2}A"
181 is_yes "$COLOR_INIT" && echo -ne "\033[${ISBOLD};3${2}m"
184 termput setaf $NORMAL
193 if [ "$3" == "1" ]; then tput bold; else tput sgr0; fi
194 is_yes "$COLOR_INIT" && tput setaf "$2"
197 termput setaf $NORMAL
203 if [ ! -x /bin/printf ]; then
205 # FIXME: buggy when single or double quotes in message!
211 if [ $# -gt 0 ]; then
214 while [ $# -gt 0 ]; do
219 awk "BEGIN {printf \"$text\", \"$m\"; }"
223 # National language support function
226 typeset msg_echo nls_domain text message
228 nls_domain="$NLS_DOMAIN"
229 while [ "$1" != "${1##-}" ]; do
245 # empty message, so we return --misiek
246 if [ -z "$message" ]; then
251 if is_yes "$GETTEXT"; then
252 message=$(TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="${nls_domain:-rc-scripts}" "$message")
255 printf "$message" "$@"
265 if ! is_no "$BOOT_SPLASH" && ! is_yes "$VSERVER"; then
266 [ -x /bin/splash ] && /bin/splash "$action"
274 nls "ERROR: Networking is down. %s can't be run." "$1" >&2
279 show "Starting %s service" "$1"
282 msg_already_running()
284 nls "%s service is already running." "$1"
289 show "Stopping %s service" "$1"
294 nls "%s service is not running." "$1"
299 show "Reloading %s service" "$1"
307 # Some functions to handle PLD Linux-style messages
315 if [ -n "$CHARS" ]; then
316 termput setaf $CCHARS
321 if is_no "$FASTRC" && is_yes "$GETTEXT"; then
327 termput hpa $INIT_COL
334 termput hpa $INIT_COL
337 # Displays message in square brackests ("[ DONE ]"). Takes two arguments.
338 # First is the text to display, second is color number to use (argument to
339 # tput setaf). If second argument is not given, default (2, green) will be
344 if [ -n "$2" ]; then COLOR="$2"; else COLOR="$CDONE"; fi
346 echo -n "$(termput setaf $CBRACKETS)[$(termput setaf $COLOR) $(nls --nls-domain rc-scripts "$1") $(termput setaf $CBRACKETS)]$(termput op)"
376 # Check if $pid (could be plural) are running
380 [ -d "/proc/$1" ] && return 0
386 # - outside chroot get only those processes, which are outside chroot.
387 # - inside chroot get only those processes, which are inside chroot.
388 # - don't filter out pids which do not have corresponding running processes (process died etc)
389 # (note: some processes like named are chrooted but run outside chroot)
390 # - do nothing inside vserver
393 if is_yes "$VSERVER"; then
397 if [ $# -lt 1 -o ! -d /proc/1 ] ; then
402 for root_pid in $@; do
403 root_dir=$(resolvesymlink /proc/${root_pid}/root)
404 if [ -n "$root_dir" ]; then
406 if [ -n "${SYSTEM_CHROOTS}" ]; then
407 for r_dir in ${SYSTEM_CHROOTS}; do
408 echo "$root_dir" | grep -q "^${r_dir}" && good_add_pid=0
411 [ "$good_add_pid" -eq 1 ] && good_pids="$good_pids $root_pid"
412 elif [ ! -d "/proc/$root_pid" ]; then
413 good_pids="$good_pids $root_pid"
419 # Usage run_cmd Message command_to_run
422 typeset exit_code errors message force_err
423 typeset -i force_err=0
424 typeset -i exit_code=0
432 show "$message"; busy
435 if errors=$(HOME=/tmp TMPDIR=/tmp initlog -c "$*" 2>&1); then
437 log_success "$1 $message"
440 log_failed "$1 $message"
443 [ -n "$errors" ] && [ $exit_code -eq 1 -o $force_err -eq 1 ] && echo "$errors"
447 # A function to start a program (now it's useful on read-only filesystem too)
450 typeset errors="" prog="" limits="" waitname="" waittime=""
451 typeset -i exit_code=0
452 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
453 # "-u unlimited" (-p for ksh) to make sure daemons will be able to fork.
454 # "-c 0" to make sure it doesn't core dump anywhere; while this could mask
455 # problems with the daemon, it also closes some security problems.
456 # Users' limits are set via pam_limits.
457 [ -z "$DEFAULT_SERVICE_LIMITS" ] && DEFAULT_SERVICE_LIMITS_HARD="-u unlimited -c 0"
458 # Test syntax. Don't use -o instead || here - this will break ksh --misiek
459 while [ "$1" != "${1##-}" ] || [ "$1" != "${1##+}" ]; do
462 msg_usage " daemon [--user user] [--fork] [+/-nicelevel] {program}"
466 # for compatibility with redhat/mandrake
467 nls "warning: --check option is ignored!"
473 [ "$1" != "root" ] && prog="/bin/su $1 -s /bin/sh -c \""
477 prog="/usr/bin/setsid sh -c \""
491 -*|+*) SERVICE_RUN_NICE_LEVEL=$1
496 # If command to execute ends with quotation mark, add remaining
497 # arguments and close quotation.
498 if [ "$prog" != "${prog%\"}" ]; then
499 prog="$prog $*$end\""
504 if [ -n "$KSH_VERSION" ]; then
505 limits="`echo "${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}" | awk '/-Su/ {sub(/-Su/,"-Sp");} /-Hu/ {sub(/-Hu/,"-Hp");} /-u/ {sub(/-u/,"-p");} {print;}'`"
506 elif [ -n "$ZSH_VERSION" ]; then
507 limits="${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}"
508 elif [ -n "$BASH_VERSION" ]; then
509 limits="${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}"
510 # elif [ -n "`$SH -c 'echo ${.sh.version}' 2>/dev/null`" ]; then
513 [ -n "$limits" ] && eval `echo "$limits" | awk 'BEGIN {RS="[\n-]";} !/^ *$/ { printf("ulimit -%s ;", $0); }'`
515 [ -z "$DEFAULT_SERVICE_UMASK" ] && DEFAULT_SERVICE_UMASK=022
520 is_yes "$CPUSETS" && [ -n "$SERVICE_CPUSET" ] && echo $$ > "/dev/cpuset/${SERVICE_CPUSET}/tasks"
521 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
522 if [ -n "$waitname" -a -n "$waittime" ]; then
524 base=$(basename "$waitname")
526 pid=$(pidofproc "$waitname" "$pidfile")
527 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
529 while [ "$i" -lt "$waittime" ]; do
531 checkpid $pid && sleep 1 || break
534 log_success "$1 startup"
539 log_failed "$1 startup"
540 [ -n "$errors" ] && echo "$errors"
545 # A function to stop a program.
548 typeset notset killlevel base pid pidfile result
551 msg_usage " killproc {program} [signal]"
555 while [ "$1" != "${1##-}" ]; do
578 # check for second arg to be kill level
579 if [ -n "$2" ] ; then
587 base=$(basename "$1")
590 pid=$(pidofproc "$1" "$pidfile")
591 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
594 if [ -n "$pid" -a "$pid" != "$$" ] && checkpid $pid 2>&1 ; then
595 if [ "$notset" = "1" ] ; then
596 if checkpid $pid 2>&1; then
597 # TERM first, then KILL if not dead
600 if checkpid $pid && sleep 1 &&
601 checkpid $pid && sleep 3 &&
609 if [ "$result" -eq 0 ]; then
611 log_failed "$1 shutdown"
614 log_success "$1 shutdown"
616 result=$(( ! $result ))
618 # use specified level only
619 if checkpid $pid > /dev/null 2>&1; then
622 if [ "$result" -eq 0 ]; then
624 log_success "$1 got $killlevel"
628 log_failed "$1 didn't get $killlevel"
633 log_failed "$1 shutdown"
638 log_failed "$1 shutdown"
642 if [ -n "$waitname" -a -n "$waittime" ]; then
644 base=$(basename "$waitname")
646 pid=$(pidofproc "$waitname" "$pidfile")
647 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
649 while [ "$i" -lt "$waittime" ]; do
651 checkpid $pid && sleep 1 || break
655 # Remove pid file if any.
656 if [ "$notset" = "1" ]; then
657 rm -f /var/run/${base}.pid
663 # A function to find the pid of a program.
666 typeset pid pidfile base
667 base=$(basename "$1")
669 [ -n "$2" ] && pidfile="$2"
673 msg_usage " pidofproc {program}"
677 # First try pidfile or "/var/run/*.pid"
678 if (echo "${pidfile}" | grep -Eq "^/"); then
681 pidfile="/var/run/${pidfile}";
683 if [ -f "${pidfile}" ] ; then
686 read line < "${pidfile}"
688 [ -z "$(echo "$p" | awk '{gsub(/[0-9]/,"");print;}')" ] && pid="$pid $p"
693 [ -z "$pid" ] && pidof -o $$ -o $PPID -o %PPID -x "$1"
694 pid=$(filter_chroot "$pid")
700 typeset base pid subsys daemon cpuset_msg
703 base=$(basename $daemon)
707 msg_usage " status {subsys} [{daemon}]"
712 pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
713 pid=$(filter_chroot "$pid")
715 if [ "$pid" != "" ]; then
717 if is_yes "$CPUSETS" && [ -n "$SERVICE_CPUSET" ]; then
718 if $(grep -q "$pid" "/dev/cpuset/${SERVICE_CPUSET}/tasks"); then
719 cpuset_msg=$(nls " in cpuset %s..." "$SERVICE_CPUSET")
721 cpuset_msg=$(nls " outside of configured cpuset %s..." "$SERVICE_CPUSET")
724 nls "%s (pid %s) is running%s" "$daemon" "$pid" "$cpuset_msg"
727 # pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
728 # { if ((prog == $5) || (("(" prog ")") == $5) ||
729 # (("[" prog "]") == $5) ||
730 # ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
731 # if [ "$pid" != "" ]; then
733 # if is_yes "$CPUSETS" && [ -n "$SERVICE_CPUSET" ]; then
734 # if $(grep -q "$pid" "/dev/cpuset/${SERVICE_CPUSET}/tasks"); then
735 # cpuset_msg=$(nls " in cpuset %s..." "$SERVICE_CPUSET")
737 # cpuset_msg=$(nls " outside of configured cpuset %s..." "$SERVICE_CPUSET")
740 # nls "%s (pid %s) is running%s" "$daemon" "$pid" "$cpuset_msg"
745 # Next try "/var/run/*.pid" files
746 if [ -f /var/run/${base}.pid ]; then
747 read pid < /var/run/${base}.pid
748 pid=$(filter_chroot "$pid")
749 if [ "$pid" != "" ]; then
750 nls "%s dead but pid file exists" "$subsys"
755 # See if /var/lock/subsys/$subsys exists
756 if [ -f /var/lock/subsys/$subsys ]; then
757 nls "%s dead but subsys locked" "$subsys"
760 nls "%s is stopped" "$subsys"
764 # Confirm whether we really want to run this service
767 nls -n "Start service %s (Y)es/(N)o/(C)ontinue? [Y] " "$1"
786 # module is needed (ie. is requested, is available and isn't loaded already)
789 # module name without .o at end
790 if ! (lsmod | grep -q "$1"); then
791 if (ls -R /lib/modules/$(uname -r)/ 2> /dev/null | grep -q "${1}.\(\|k\)o\(\|.gz\)"); then
802 typeset parsed single die args foo result
804 while is_no "$parsed" ; do
823 if is_yes "${single}" ; then
825 show "Loading %s kernel module(s)" "$foo"
828 if [ -x /sbin/modprobe ] ; then
829 /sbin/modprobe -s $args "$@"
835 if is_yes "${single}" ; then
837 if [ $result == "0" ] ; then
838 is_yes "$single" && ok
841 if is_yes "$die" ; then
842 nls "Could not load %s kernel module(s)" "$@"
851 initlog -n $0 -s "$1 $2" -e 1
856 initlog -n $0 -s "$1 $2" -e 2
859 # RedHat/Mandrake specific functions
860 action () { STRING=$1; shift; run_cmd "$STRING" "$*"; }
861 success () { return 0; }
862 failure () { return 1; }
865 selinuxfs=`awk '/ selinuxfs / { print $2 }' /proc/mounts`
866 echo "*** Warning -- SELinux is active"
867 echo "*** Disabling security enforcement for system recovery."
868 echo "*** Run 'setenforce 1' to reenable."
869 echo "0" > $selinuxfs/enforce
873 selinuxfs=`awk '/ selinuxfs / { print $2 }' /proc/mounts`
875 *** Warning -- SELinux relabel is required. ***
876 *** Disabling security enforcement. ***
877 *** Relabeling could take a very long time, ***
878 *** depending on file system size. ***
880 echo "0" > $selinuxfs/enforce
881 /sbin/fixfiles -F relabel > /dev/null 2>&1
883 echo "*** Enabling security enforcement. ***"
884 echo $SELINUX > $selinuxfs/enforce
887 # Remove duplicate entries from mtab (for vserver guest use only)
888 clean_vserver_mtab() {
890 cat /etc/mtab | while read device mountpoint line ; do
891 grep -qs "$mountpoint" /etc/mtab.clean || \
892 echo "$device $mountpoint $line" >> /etc/mtab.clean
894 mv -f /etc/mtab.clean /etc/mtab
898 # If we have cachefile, use it.
899 # If we don't, create memory variables and try to save silently,
900 # We store cache in /etc, as /var isn't available at boot time.
901 local cachefile='/etc/rc.d/.rc-scripts.cache'
903 # We create $check variable which is used to invalidate the cache.
904 # The $check contains user locale and terminal.
905 local check="$TERM.$LC_MESSAGES"
907 if [ -f "$cachefile" -a "$cachefile" -nt /etc/sysconfig/system -a "$cachefile" -nt /etc/sysconfig/init-colors ]; then
908 if . "$cachefile" 2>/dev/null; then
909 if [ "$check" = "$_check" ]; then
916 _busy=$(progress "BUSY" "$CBUSY")
917 _ok=$(progress "DONE")
918 _started=$(progress "WORK")
919 _fail=$(progress "FAIL" "$CFAIL")
920 _died=$(progress "DIED" "$CFAIL")
922 # we don't use heredoc, as ksh attempts to create tempfile then
923 (> "$cachefile" ) 2>/dev/null || return
924 echo "_busy='$_busy';" >> "$cachefile"
925 echo "_ok='$_ok';" >> "$cachefile"
926 echo "_started='$_started';" >> "$cachefile"
927 echo "_fail='$_fail';" >> "$cachefile"
928 echo "_died='$_died';" >> "$cachefile"
929 echo "_check='$check';" >> "$cachefile"
933 if [ -z "$GETTEXT" ]; then
934 if [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
941 if [ -z "$TPUT" ]; then
942 if [ -d /usr/share/terminfo ] && [ -x /usr/bin/tput -o -x /bin/tput ] ; then
944 # check if we are on proper terminal
945 tput longname >/dev/null 2>&1 || TPUT=no
958 # * indent-tabs-mode: notnil