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"
19 if [ -z "$COLUMNS" -o -z "$LINES" ]; then
21 set -- $(stty size 2>/dev/null)
23 COLUMNS=${COLUMNS:-$2}
28 [ -z "$COLUMNS" ] && COLUMNS=80
30 INIT_COL=$((COLUMNS - 13))
41 # Bold definition (second parameter to termput setaf)
45 CBRACKETS="$CYAN" # brackets [ ] color
46 CDONE="$GREEN" # DONE and WORK color
47 CBUSY="$MAGENTA" # BUSY color
48 CFAIL="$RED" # FAIL and DIED color
49 CPOWEREDBY="$CYAN" # "Powered by" color
50 CPLD="$GREEN" # "PLD Linux Distribution" color
51 CI="$RED" # Capital I color (press I to enter interactive startup)
52 CRESMAN="$GREEN" # "Resource Manager" color
53 CHARS="" # Characters displayed on the beginning of show line
54 CCHARS="$NORMAL" # Color of these characters (look at /etc/sysconfig/init-colors.gentoo example)
56 # Source configuration if available - may override default values
57 [ -r /etc/sysconfig/init-colors ] && . /etc/sysconfig/init-colors
58 [ -r /etc/sysconfig/system ] && . /etc/sysconfig/system
59 [ -r /etc/sysconfig/bootsplash ] && . /etc/sysconfig/bootsplash
61 if [ -z "$VSERVER" -o "$VSERVER" = "detect" ]; then
63 while read _f _ctx; do
64 [ "$_f" = "VxID:" -o "$_f" = "s_context:" ] && break
65 done </proc/self/status
67 if [ -z "$_ctx" -o "$_ctx" = "0" ]; then
75 # we need to know in functions if we were called from a terminal
76 if [ -z "$ISATTY" ]; then
77 [ -t ] && ISATTY=yes || ISATTY=no
84 msg_usage " is_yes {value}"
90 yes|Yes|YES|true|True|TRUE|on|On|ON|Y|y|1)
104 msg_usage " is_no {value}"
109 no|No|NO|false|False|FALSE|off|Off|OFF|N|n|0)
120 # checks if file is empty
121 # empty lines and lines beginning with hash are ignored
123 [ -s "$1" ] || return 0
124 egrep -vq "^(#|[[:blank:]]*$)" "$1" && return 1 || return 0
127 # returns OK if $1 contains $2
130 [ "${1#*$a*}" = "$1" ] && return 1
134 if is_yes "$FASTRC" || is_yes "$IN_SHUTDOWN"; then
138 if is_no "$RC_LOGGING"; then
141 while [ "$1" != "${1##-}" ]; do
159 typeset _x _y _z v old_IFS ver
167 # strip _* or -* from versions like: "2.6.25_vanilla-1", "2.6.25-1"
170 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
172 while [ ${#ver} -lt 6 ]; do ver="0$ver"; done
174 while [ ${#ver} -lt 9 ]; do ver="0$ver"; done
180 typeset _x _y _z v old_IFS ver
188 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
190 while [ ${#ver} -lt 6 ]; do ver="0$ver"; done
196 typeset _x _y _z v old_IFS ver
204 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
211 is_yes "$ISATTY" || return
213 if is_yes "$FASTRC" || is_no "$TPUT"; then
216 echo -ne "\033[$(($2+1))G"
219 echo -ne "\033[${2}A"
231 is_yes "$COLOR_INIT" && echo -ne "\033[${ISBOLD};3${2}m"
234 termput setaf $NORMAL
243 if [ "$3" = "1" ]; then tput bold; else tput sgr0; fi
244 is_yes "$COLOR_INIT" && tput setaf "$2"
247 termput setaf $NORMAL
253 if [ ! -x /bin/printf ]; then
255 # FIXME: buggy when single or double quotes in message!
260 if [ $# -gt 0 ]; then
263 while [ $# -gt 0 ]; do
268 awk "BEGIN {printf \"$text\", \"$m\"; }"
272 # National language support function
274 typeset msg_echo nls_domain text message
276 nls_domain="$NLS_DOMAIN"
277 while [ "$1" != "${1##-}" ]; do
293 # empty message, so we return --misiek
294 if [ -z "$message" ]; then
299 if is_yes "$GETTEXT"; then
300 message=$(TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="${nls_domain:-rc-scripts}" "$message")
303 printf "$message" "$@"
312 if ! is_no "$BOOT_SPLASH" && ! is_yes "$VSERVER"; then
313 [ -x /bin/splash ] && /bin/splash "$action"
320 nls "ERROR: Networking is down. %s can't be run." "$1" >&2
324 show "Starting %s service" "$1"
327 msg_already_running() {
328 nls "%s service is already running." "$1"
332 show "Stopping %s service" "$1"
336 nls "%s service is not running." "$1"
340 show "Reloading %s service" "$1"
347 # Some functions to handle PLD Linux-style messages
352 if is_no "$FASTRC" && is_yes "$GETTEXT"; then
358 while [ $((len++)) -lt $INIT_COL ]; do
361 if [ -n "$CHARS" ]; then
362 termput setaf $CCHARS
370 termput hpa $INIT_COL
373 # Displays message in square brackests ("[ DONE ]"). Takes two arguments.
374 # First is the text to display, second is color number to use (argument to
375 # tput setaf). If second argument is not given, default (2, green) will be
379 if [ -n "$2" ]; then COLOR="$2"; else COLOR="$CDONE"; fi
381 echo -n "$(termput setaf $CBRACKETS)[$(termput setaf $COLOR) $(nls --nls-domain rc-scripts "$1") $(termput setaf $CBRACKETS)]$(termput op)"
406 # Check if $pid (could be plural) are running
409 [ -d "/proc/$1" ] && return 0
415 # - outside chroot get only those processes, which are outside chroot.
416 # - inside chroot get only those processes, which are inside chroot.
417 # - don't filter out pids which do not have corresponding running processes (process died etc)
418 # (note: some processes like named are chrooted but run outside chroot)
419 # - do nothing inside vserver
421 if is_yes "$VSERVER"; then
425 if [ $# -lt 1 -o ! -d /proc/1 ]; then
429 local root_dir good_pids="" good_add_pid
430 for root_pid in $@; do
431 root_dir=$(resolvesymlink /proc/${root_pid}/root)
432 if [ -n "$root_dir" ]; then
434 if [ -n "${SYSTEM_CHROOTS}" ]; then
435 for r_dir in ${SYSTEM_CHROOTS}; do
436 echo "$root_dir" | grep -q "^${r_dir}" && good_add_pid=0
439 [ "$good_add_pid" -eq 1 ] && good_pids="$good_pids $root_pid"
440 elif [ ! -d "/proc/$root_pid" ]; then
441 good_pids="$good_pids $root_pid"
447 # Usage run_cmd Message command_to_run
449 typeset exit_code errors message force_err
450 typeset -i force_err=0
451 typeset -i exit_code=0
459 show "$message"; busy
463 export HOME=/tmp TMPDIR=/tmp
464 if is_no "$RC_LOGGING"; then
471 log_success "$1 $message"
474 log_failed "$1 $message"
477 [ -n "$errors" ] && [ $exit_code -eq 1 -o $force_err -eq 1 ] && echo "$errors"
481 _daemon_set_ulimits() {
482 local opt val ksh=${KSH_VERSION:+1}
483 set -- ${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}
484 while [ $# -gt 0 ]; do
505 # A function to start a program (now it's useful on read-only filesystem too)
507 typeset errors="" prog="" end="" waitname="" waittime=""
508 typeset -i exit_code=0
509 local nice=$SERVICE_RUN_NICE_LEVEL
510 local fork user closefds pidfile makepid chdir=/
512 while [ $# -gt 0 ]; do
515 msg_usage " daemon [--check] [--user user] [--fork] [--chdir directory] [--closefds] [--waitforname procname] [--waitfortime seconds] [--pidfile file] [--makepid] [+/-nicelevel] {program} <program args>"
519 # for compatibility with redhat/mandrake
520 nls "warning: --check option is ignored!"
525 [ "$1" != "root" ] && prog="/bin/su $1 -s /bin/sh -c \""
530 prog="/usr/bin/setsid sh -c \""
551 [[ "$pidfile" != /* ]] && pidfile="/var/run/$pidfile"
567 # If command to execute ends with quotation mark, add remaining
568 # arguments and close quotation.
569 if [ "$prog" != "${prog%\"}" ]; then
570 prog="$prog $*$end\""
577 [ -z "$DEFAULT_SERVICE_UMASK" ] && DEFAULT_SERVICE_UMASK=022
578 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
583 [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS" && echo $$ > "/dev/cpuset/${SERVICE_CPUSET}/tasks"
585 umask ${SERVICE_UMASK:-$DEFAULT_SERVICE_UMASK};
586 export USER=root HOME=/tmp TMPDIR=/tmp
587 nice=${nice:-$DEFAULT_SERVICE_RUN_NICE_LEVEL}
590 if [ "$closefds" = 1 ]; then
598 if is_no "$RC_LOGGING"; then
600 if [ ! -x $prog ]; then
601 logger -t rc-scripts -p daemon.debug "daemon: Searching PATH for $prog, consider using full path in initscript"
605 if [ -x $a/$prog ]; then
612 /sbin/start-stop-daemon -q --start \
614 ${pidfile:+--pidfile $pidfile} \
615 ${makepid:+--make-pidfile} \
616 ${user:+--chuid $user} \
617 ${chdir:+--chdir "$chdir"} \
618 ${fork:+--background} \
619 ${waitname:+--name $waitname} \
623 nice -n $nice initlog -c "$prog" 2>&1
627 if [ -n "$waitname" -a -n "$waittime" ]; then
631 pid=$(pidofproc "$waitname" "$pidfile")
632 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
634 while [ "$i" -lt "$waittime" ]; do
636 checkpid $pid && sleep 1 || break
639 log_success "$1 startup"
644 log_failed "$1 startup"
645 [ -n "$errors" ] && echo >&2 "$errors"
650 # A function to stop a program.
652 typeset notset killlevel base pid pidfile result
655 msg_usage " killproc [--pidfile PIDFILE] {program} [-SIGNAME]"
659 while [ "$1" != "${1##-}" ]; do
663 [[ "$pidfile" != /* ]] && pidfile="/var/run/$pidfile"
680 # check for second arg to be kill level
688 # experimental start-stop-daemon based killing.
689 # works only with pidfile
690 if is_no "$RC_LOGGING" && [ "$pidfile" ]; then
691 local sig=${killlevel:--TERM}
692 /sbin/start-stop-daemon --stop \
693 --retry ${sig#-}/10/${sig#-}/60/KILL/10 \
695 ${pidfile:+--pidfile $pidfile}
697 if [ "$result" -eq 0 ]; then
710 pid=$(pidofproc "$1" "$pidfile")
711 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
714 if [ -n "$pid" -a "$pid" != "$$" ] && checkpid $pid 2>&1; then
715 if [ "$notset" = "1" ]; then
716 if checkpid $pid 2>&1; then
717 # TERM first, then KILL if not dead
720 if checkpid $pid && sleep 1 &&
721 checkpid $pid && sleep 3 &&
723 # XXX: SIGKILL is sent already on 4th second!
724 # HARMFUL for example to mysqld (which is already workarounded)
731 if [ "$result" -eq 0 ]; then
733 log_failed "$1 shutdown"
736 log_success "$1 shutdown"
738 result=$(( ! $result ))
740 # use specified level only
741 if checkpid $pid > /dev/null 2>&1; then
744 if [ "$result" -eq 0 ]; then
746 log_success "$1 got $killlevel"
750 log_failed "$1 didn't get $killlevel"
755 log_failed "$1 shutdown"
760 log_failed "$1 shutdown"
764 if [ -n "$waitname" -a -n "$waittime" ]; then
768 pid=$(pidofproc "$waitname" "$pidfile")
769 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
771 while [ "$i" -lt "$waittime" ]; do
773 checkpid $pid && sleep 1 || break
777 # Remove pid file if any.
778 if [ "$notset" = "1" ]; then
779 rm -f /var/run/${base}.pid
785 # A function to find the pid of a program.
787 typeset pid pidfile base=${1##*/}
789 [ -n "$2" ] && pidfile="$2"
793 msg_usage " pidofproc {program}"
797 # First try pidfile or "/var/run/*.pid"
798 if [[ "$pidfile" = /* ]]; then
801 pidfile="/var/run/${pidfile}";
803 if [ -f "${pidfile}" ]; then
806 for p in $(< "${pidfile}"); do
807 [ -z "$(echo "$p" | awk '{gsub(/[0-9]/,"");print;}')" ] && pid="$pid $p"
812 [ -z "$pid" ] && pidof -o $$ -o $PPID -o %PPID -x "$1"
813 pid=$(filter_chroot "$pid")
818 typeset pid subsys daemon cpuset_msg pidfile
819 if [ "$1" = "--pidfile" ]; then
821 [[ "$pidfile" != /* ]] && pidfile="/var/run/$pidfile"
830 msg_usage " status [--pidfile PIDFILE] {subsys} [{daemon}]"
834 # if pidfile specified, pid must be there
835 if [ "$pidfile" -a -f "$pidfile" ]; then
838 pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
840 pid=$(filter_chroot "$pid")
844 if [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS" ]; then
845 if $(grep -q "$pid" "/dev/cpuset/${SERVICE_CPUSET}/tasks"); then
846 cpuset_msg=$(nls " in cpuset %s..." "$SERVICE_CPUSET")
848 cpuset_msg=$(nls " outside of configured cpuset %s..." "$SERVICE_CPUSET")
851 nls "%s (pid %s) is running%s" "$daemon" "$pid" "$cpuset_msg"
855 # Next try "/var/run/*.pid" files
856 local base=${daemon##*/}
857 if [ -f /var/run/${base}.pid ]; then
858 read pid < /var/run/${base}.pid
859 pid=$(filter_chroot "$pid")
860 if [ "$pid" != "" ]; then
861 nls "%s dead but pid file exists" "$subsys"
866 # See if /var/lock/subsys/$subsys exists
867 if [ -f /var/lock/subsys/$subsys ]; then
868 nls "%s dead but subsys locked" "$subsys"
871 nls "%s is stopped" "$subsys"
875 # Confirm whether we really want to run this service
878 nls -n "Start service %s (Y)es/(N)o/(C)ontinue? [Y] " "$1"
897 # module is needed (ie. is requested, is available and isn't loaded already)
899 # module name without .o at end
900 if ! lsmod | grep -q "$1"; then
901 if ls -R /lib/modules/$(uname -r)/ 2> /dev/null | grep -q "${1}.\(\|k\)o\(\|.gz\)"; then
911 typeset parsed single die args foo result
913 while is_no "$parsed"; do
932 if is_yes "${single}"; then
934 show "Loading %s kernel module(s)" "$foo"
937 if [ -x /sbin/modprobe ]; then
938 /sbin/modprobe -s $args "$@"
944 if is_yes "${single}"; then
946 if [ $result = "0" ]; then
947 is_yes "$single" && ok
950 if is_yes "$die"; then
951 nls "Could not load %s kernel module(s)" "$@"
958 if is_no "$RC_LOGGING"; then
968 initlog -n $0 -s "$1 $2" -e 1
972 initlog -n $0 -s "$1 $2" -e 2
976 # Check if any flavor of portmapper is running
980 if [ -x /usr/sbin/rpcinfo ]; then
981 if /usr/sbin/rpcinfo -p localhost >/dev/null 2>/dev/null; then
986 elif [ -x /sbin/pidof ]; then
987 [ -z "$(/sbin/pidof portmap)" -a \
988 -z "$(/sbin/pidof rpcbind)" ] && return 1
994 # If we have cachefile, use it.
995 # If we don't, create memory variables and try to save silently,
996 local cachefile='/var/cache/rc-scripts/msg.cache'
999 if is_yes "$ISATTY"; then
1005 # We create $check variable which is used to invalidate the cache.
1006 # The $check contains user locale and terminal.
1007 local check="$term.$LC_MESSAGES.$INIT_COL"
1009 if [ -f "$cachefile" -a "$cachefile" -nt /etc/sysconfig/system -a "$cachefile" -nt /etc/sysconfig/init-colors ]; then
1010 if . "$cachefile" 2>/dev/null; then
1011 if [ "$check" = "$_check" ]; then
1018 _busy=$(progress "BUSY" "$CBUSY")
1019 _ok=$(progress "DONE")
1020 _started=$(progress "WORK")
1021 _fail=$(progress "FAIL" "$CFAIL")
1022 _died=$(progress "DIED" "$CFAIL")
1024 # we don't use heredoc, as ksh attempts to create tempfile then
1025 (> "$cachefile" ) 2>/dev/null || return
1026 echo "_busy='$_busy';" >> "$cachefile"
1027 echo "_ok='$_ok';" >> "$cachefile"
1028 echo "_started='$_started';" >> "$cachefile"
1029 echo "_fail='$_fail';" >> "$cachefile"
1030 echo "_died='$_died';" >> "$cachefile"
1031 echo "_check='$check';" >> "$cachefile"
1035 if [ -z "$GETTEXT" ]; then
1036 if [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
1043 if [ -z "$TPUT" ]; then
1044 if [ -d /usr/share/terminfo ] && [ -x /usr/bin/tput -o -x /bin/tput ]; then
1046 # check if we are on proper terminal
1047 tput longname >/dev/null 2>&1 || TPUT=no
1058 # * Local variables:
1060 # * indent-tabs-mode: notnil