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"
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 \""
566 # If command to execute ends with quotation mark, add remaining
567 # arguments and close quotation.
568 if [ "$prog" != "${prog%\"}" ]; then
569 prog="$prog $*$end\""
576 [ -z "$DEFAULT_SERVICE_UMASK" ] && DEFAULT_SERVICE_UMASK=022
577 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
582 [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS" && echo $$ > "/dev/cpuset/${SERVICE_CPUSET}/tasks"
584 umask ${SERVICE_UMASK:-$DEFAULT_SERVICE_UMASK};
585 export USER=root HOME=/tmp TMPDIR=/tmp
586 nice=${nice:-$DEFAULT_SERVICE_RUN_NICE_LEVEL}
589 if [ "$closefds" = 1 ]; then
597 if is_no "$RC_LOGGING" ; then
599 if [ ! -x $prog ]; then
600 logger -t rc-scripts -p daemon.debug "daemon: Searching PATH for $prog, consider using full path in initscript"
604 if [ -x $a/$prog ]; then
611 /sbin/start-stop-daemon -q --start \
613 ${pidfile:+--pidfile $pidfile} \
615 ${chdir:+--chdir "$chdir"} \
621 nice -n $nice initlog -c "$prog" 2>&1
625 if [ -n "$waitname" -a -n "$waittime" ]; then
629 pid=$(pidofproc "$waitname" "$pidfile")
630 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
632 while [ "$i" -lt "$waittime" ]; do
634 checkpid $pid && sleep 1 || break
637 log_success "$1 startup"
642 log_failed "$1 startup"
643 [ -n "$errors" ] && echo "$errors"
648 # A function to stop a program.
650 typeset notset killlevel base pid pidfile result
653 msg_usage " killproc [--pidfile PIDFILE] {program} [-SIGNAME]"
657 while [ "$1" != "${1##-}" ]; do
677 # check for second arg to be kill level
678 if [ -n "$2" ] ; then
685 # experimental start-stop-daemon based killing.
686 # works only with pidfile
687 if is_no "$RC_LOGGING" && [ "$pidfile" ]; then
688 local sig=${killlevel:--TERM}
689 /sbin/start-stop-daemon --stop \
690 --retry ${sig#-}/10/${sig#-}/60/KILL/10 \
692 ${pidfile:+--pidfile $pidfile}
694 if [ "$result" -eq 0 ]; then
707 pid=$(pidofproc "$1" "$pidfile")
708 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
711 if [ -n "$pid" -a "$pid" != "$$" ] && checkpid $pid 2>&1 ; then
712 if [ "$notset" = "1" ] ; then
713 if checkpid $pid 2>&1; then
714 # TERM first, then KILL if not dead
717 if checkpid $pid && sleep 1 &&
718 checkpid $pid && sleep 3 &&
720 # XXX: SIGKILL is sent already on 4th second!
721 # HARMFUL for example to mysqld (which is already workarounded)
728 if [ "$result" -eq 0 ]; then
730 log_failed "$1 shutdown"
733 log_success "$1 shutdown"
735 result=$(( ! $result ))
737 # use specified level only
738 if checkpid $pid > /dev/null 2>&1; then
741 if [ "$result" -eq 0 ]; then
743 log_success "$1 got $killlevel"
747 log_failed "$1 didn't get $killlevel"
752 log_failed "$1 shutdown"
757 log_failed "$1 shutdown"
761 if [ -n "$waitname" -a -n "$waittime" ]; then
765 pid=$(pidofproc "$waitname" "$pidfile")
766 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
768 while [ "$i" -lt "$waittime" ]; do
770 checkpid $pid && sleep 1 || break
774 # Remove pid file if any.
775 if [ "$notset" = "1" ]; then
776 rm -f /var/run/${base}.pid
782 # A function to find the pid of a program.
784 typeset pid pidfile base=${1##*/}
786 [ -n "$2" ] && pidfile="$2"
790 msg_usage " pidofproc {program}"
794 # First try pidfile or "/var/run/*.pid"
795 if [[ "$pidfile" = /* ]]; then
798 pidfile="/var/run/${pidfile}";
800 if [ -f "${pidfile}" ] ; then
803 for p in $(< "${pidfile}"); do
804 [ -z "$(echo "$p" | awk '{gsub(/[0-9]/,"");print;}')" ] && pid="$pid $p"
809 [ -z "$pid" ] && pidof -o $$ -o $PPID -o %PPID -x "$1"
810 pid=$(filter_chroot "$pid")
815 typeset pid subsys daemon cpuset_msg pidfile
816 if [ "$1" = "--pidfile" ]; then
826 msg_usage " status [--pidfile PIDFILE] {subsys} [{daemon}]"
830 # if pidfile specified, pid must be there
831 if [ "$pidfile" -a -f "$pidfile" ]; then
834 pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
836 pid=$(filter_chroot "$pid")
840 if [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS" ]; then
841 if $(grep -q "$pid" "/dev/cpuset/${SERVICE_CPUSET}/tasks"); then
842 cpuset_msg=$(nls " in cpuset %s..." "$SERVICE_CPUSET")
844 cpuset_msg=$(nls " outside of configured cpuset %s..." "$SERVICE_CPUSET")
847 nls "%s (pid %s) is running%s" "$daemon" "$pid" "$cpuset_msg"
851 # Next try "/var/run/*.pid" files
852 local base=${daemon##*/}
853 if [ -f /var/run/${base}.pid ]; then
854 read pid < /var/run/${base}.pid
855 pid=$(filter_chroot "$pid")
856 if [ "$pid" != "" ]; then
857 nls "%s dead but pid file exists" "$subsys"
862 # See if /var/lock/subsys/$subsys exists
863 if [ -f /var/lock/subsys/$subsys ]; then
864 nls "%s dead but subsys locked" "$subsys"
867 nls "%s is stopped" "$subsys"
871 # Confirm whether we really want to run this service
874 nls -n "Start service %s (Y)es/(N)o/(C)ontinue? [Y] " "$1"
893 # module is needed (ie. is requested, is available and isn't loaded already)
895 # module name without .o at end
896 if ! lsmod | grep -q "$1"; then
897 if ls -R /lib/modules/$(uname -r)/ 2> /dev/null | grep -q "${1}.\(\|k\)o\(\|.gz\)"; then
907 typeset parsed single die args foo result
909 while is_no "$parsed" ; do
928 if is_yes "${single}" ; then
930 show "Loading %s kernel module(s)" "$foo"
933 if [ -x /sbin/modprobe ] ; then
934 /sbin/modprobe -s $args "$@"
940 if is_yes "${single}" ; then
942 if [ $result == "0" ] ; then
943 is_yes "$single" && ok
946 if is_yes "$die" ; then
947 nls "Could not load %s kernel module(s)" "$@"
954 if is_no "$RC_LOGGING"; then
964 initlog -n $0 -s "$1 $2" -e 1
968 initlog -n $0 -s "$1 $2" -e 2
972 # Check if any flavor of portmapper is running
976 if [ -x /usr/sbin/rpcinfo ]; then
977 if /usr/sbin/rpcinfo -p localhost >/dev/null 2>/dev/null; then
982 elif [ -x /sbin/pidof ]; then
983 [ -z "$(/sbin/pidof portmap)" -a \
984 -z "$(/sbin/pidof rpcbind)" ] && return 1
990 # If we have cachefile, use it.
991 # If we don't, create memory variables and try to save silently,
992 local cachefile='/var/cache/rc-scripts/msg.cache'
995 if is_yes "$ISATTY"; then
1001 # We create $check variable which is used to invalidate the cache.
1002 # The $check contains user locale and terminal.
1003 local check="$term.$LC_MESSAGES.$INIT_COL"
1005 if [ -f "$cachefile" -a "$cachefile" -nt /etc/sysconfig/system -a "$cachefile" -nt /etc/sysconfig/init-colors ]; then
1006 if . "$cachefile" 2>/dev/null; then
1007 if [ "$check" = "$_check" ]; then
1014 _busy=$(progress "BUSY" "$CBUSY")
1015 _ok=$(progress "DONE")
1016 _started=$(progress "WORK")
1017 _fail=$(progress "FAIL" "$CFAIL")
1018 _died=$(progress "DIED" "$CFAIL")
1020 # we don't use heredoc, as ksh attempts to create tempfile then
1021 (> "$cachefile" ) 2>/dev/null || return
1022 echo "_busy='$_busy';" >> "$cachefile"
1023 echo "_ok='$_ok';" >> "$cachefile"
1024 echo "_started='$_started';" >> "$cachefile"
1025 echo "_fail='$_fail';" >> "$cachefile"
1026 echo "_died='$_died';" >> "$cachefile"
1027 echo "_check='$check';" >> "$cachefile"
1031 if [ -z "$GETTEXT" ]; then
1032 if [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
1039 if [ -z "$TPUT" ]; then
1040 if [ -d /usr/share/terminfo ] && [ -x /usr/bin/tput -o -x /bin/tput ] ; then
1042 # check if we are on proper terminal
1043 tput longname >/dev/null 2>&1 || TPUT=no
1054 # * Local variables:
1056 # * indent-tabs-mode: notnil