3 # /lib/lsb/init-functions for Debian -*- shell-script -*-
5 # Copyright (c) 2002-03 Chris Lawrence
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
11 # 1. Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
16 # 3. Neither the name of the author nor the names of other contributors
17 # may be used to endorse or promote products derived from this software
18 # without specific prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 TPUT="${TPUT:-"/usr/bin/tput"}"
35 [ -x "$TPUT" ] && "$TPUT" hpa 60 >/dev/null 2>&1 \
46 RED="$("$TPUT" setaf 1)"
47 #NORMAL="$("$TPUT" op)"
48 printf " ${RED}*${NORMAL} $@\n"
56 YELLOW="$("$TPUT" setaf 3)"
57 #NORMAL="$("$TPUT" op")"
58 # printf " *${NORMAL} $@\n"
59 printf " ${BLUE}*${NORMAL} $@\n"
65 log_warning_msg_nn () {
67 YELLOW="$("$TPUT" setaf 3)"
68 printf " ${BLUE}*${NORMAL} $@"
74 # int log_begin_message (char *message)
76 if [ "$#" -eq 0 ]; then
79 printf " ${GREEN}*${NORMAL} $@\n"
83 if [ "$#" -eq 0 ]; then
86 printf " ${GREEN}*${NORMAL} $@"
90 SUBMSG=" ${GREEN}-${NORMAL} "
92 # int log_end_message (int exitstatus)
95 # If no arguments were passed, return
96 [ "$#" -eq 0 ] && return 1
98 # Only do the fancy stuff if we have an appropriate terminal
99 # and if /usr is already mounted
101 COLS="$("$TPUT" cols)"
102 if [ -n "$COLS" ]; then
103 COL=$(( "$COLS" - 7 ))
108 END="$("$TPUT" hpa $COL)"
109 START="$("$TPUT" hpa 0)"
110 #RED="$("$TPUT" setaf 1)"
111 #NORMAL="$("$TPUT" op)"
112 if [ "$1" -eq 0 ]; then
113 printf "${UP}${END}${BLUE}[ ${GREEN}ok ${BLUE}]${NORMAL}\n"
115 printf "${UP}${START} ${RED}*${NORMAL}${END}[${RED}fail${NORMAL}]\n"
118 if [ "$1" -eq 0 ]; then
127 # Copyright 1999-2005 Gentoo Foundation
128 # Distributed under the terms of the GNU General Public License v2
129 # $Header: /var/cvsroot/gentoo-src/rc-scripts/sbin/functions.sh,v 1.81.2.6 2005/05/15 20:00:31 vapier Exp $
131 RC_GOT_FUNCTIONS="yes"
133 # Different types of dependencies
135 # Different types of order deps
136 ordtypes="before after"
142 # Dont output to stdout?
144 RC_VERBOSE="${RC_VERBOSE:-no}"
146 # Should we use color?
147 if [ -r /proc/cmdline ] ; then
148 grep -q ' nocolor' /proc/cmdline && RC_NOCOLOR='yes'
150 RC_NOCOLOR="${RC_NOCOLOR:-no}"
151 # Can the terminal handle endcols?
155 # Default values for rc system
158 RC_NET_STRICT_CHECKING="no"
159 RC_PARALLEL_STARTUP="no"
160 RC_USE_CONFIG_PROFILE="yes"
163 # Default values for e-message indentation and dots
172 # Notify bootsplash/splashutils/gensplash/whatever about
179 # void profiling(...)
181 # Notify bootsplash/whatever about important events.
187 # void get_bootconfig()
189 # Get the BOOTLEVEL and SOFTLEVEL by setting
190 # 'bootlevel' and 'softlevel' via kernel
198 for copt in $(</proc/cmdline) ; do
201 newbootlevel="${copt##*=}"
204 newsoftlevel="${copt##*=}"
209 if [ -n "${newbootlevel}" ] ; then
210 export BOOTLEVEL="${newbootlevel}"
212 export BOOTLEVEL="boot"
215 if [ -n "${newsoftlevel}" ] ; then
216 export DEFAULTLEVEL="${newsoftlevel}"
218 export DEFAULTLEVEL="default"
224 # void get_libdir(void)
226 # prints the current libdir {lib,lib32,lib64}
229 if [ -n "${CONF_LIBDIR_OVERRIDE}" ] ; then
230 CONF_LIBDIR="${CONF_LIBDIR_OVERRIDE}"
231 elif [ -x "/usr/bin/portageq" ] ; then
232 CONF_LIBDIR="$(/usr/bin/portageq envvar CONF_LIBDIR)"
234 printf '%s\n' ${CONF_LIBDIR:=lib}
237 # void esyslog(char* priority, char* tag, char* message)
239 # use the system logger to log a message
245 if [ -x /usr/bin/logger ]
251 [ -z "$*" ] && return 0
253 /usr/bin/logger -p "${pri}" -t "${tag}" -- "$*"
259 # void eindent(int num)
261 # increase the indent used for e-commands.
265 (( i > 0 )) || (( i = RC_DEFAULT_INDENT ))
266 esetdent $(( ${#RC_INDENTATION} + i ))
269 # void eoutdent(int num)
271 # decrease the indent used for e-commands.
275 (( i > 0 )) || (( i = RC_DEFAULT_INDENT ))
276 esetdent $(( ${#RC_INDENTATION} - i ))
279 # void esetdent(int num)
281 # hard set the indent used for e-commands.
286 (( i < 0 )) && (( i = 0 ))
287 RC_INDENTATION=$(printf "%${i}s" '')
290 # void einfo(char* message)
292 # show an informative message (with a newline)
300 # void einfon(char* message)
302 # show an informative message (without a newline)
305 [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
306 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
307 printf " ${GOOD}*${NORMAL} ${RC_INDENTATION}$*"
312 # void ewarn(char* message)
314 # show a warning message + log it
317 if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
320 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
321 printf " ${WARN}*${NORMAL} ${RC_INDENTATION}$*\n"
324 # Log warnings to system log
325 esyslog "daemon.warning" "rc-scripts" "$*"
331 # void eerror(char* message)
333 # show an error message + log it
336 if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
337 printf " $*\n" >/dev/stderr
339 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
340 printf " ${BAD}*${NORMAL} ${RC_INDENTATION}$*\n"
343 # Log errors to system log
344 esyslog "daemon.err" "rc-scripts" "$*"
350 # void ebegin(char* message)
352 # show a message indicating the start of a process
355 local msg="$*" dots spaces
356 spaces="$(printf '%'"${#RC_DOT_PATTERN}"'s' '')"
357 [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
359 if [ -n "${RC_DOT_PATTERN}" ]; then
360 dots=$(printf "%$(( COLS - 3 - ${#RC_INDENTATION} - ${#msg} - 7 ))s" '')
361 while [ "${dots#${spaces}}" != "${dots}" ] ; do
362 dots="${dots#${spaces}}${RC_DOT_PATTERN}"
369 [ "${RC_ENDCOL}" = "yes" ] && echo
371 LAST_E_LEN=$(( 3 + ${#RC_INDENTATION} + ${#msg} ))
376 # void _eend(int error, char *efunc, char* errstr)
378 # indicate the completion of process, called from eend/ewend
379 # if error, show errstr via efunc
381 # This function is private to functions.sh. Do not call it from a
385 local retval=${1:-0} efunc=${2:-eerror} msg
388 if [ "${retval}" -eq 0 ]; then
389 [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
390 msg="${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
395 msg="${BRACKET}[ ${BAD}!!${BRACKET} ]${NORMAL}"
398 if [ "${RC_ENDCOL}" = "yes" ]; then
399 printf "${ENDCOL} ${msg}\n"
401 [ "${LAST_E_CMD}" = "ebegin" ] || LAST_E_LEN=0
402 printf "%$(( "${COLS}" - "${LAST_E_LEN}" - 6 ))s%b\n" '' "${msg}"
408 # void eend(int error, char* errstr)
410 # indicate the completion of process
411 # if error, show errstr via eerror
417 _eend ${retval} eerror "$*"
423 # void ewend(int error, char* errstr)
425 # indicate the completion of process
426 # if error, show errstr via ewarn
432 _eend ${retval} ewarn "$*"
438 # v-e-commands honor RC_VERBOSE which defaults to no.
439 # The condition is negated so the return value will be zero.
440 veinfo() { [ "${RC_VERBOSE}" != "yes" ] || einfo "$@"; }
441 veinfon() { [ "${RC_VERBOSE}" != "yes" ] || einfon "$@"; }
442 vewarn() { [ "${RC_VERBOSE}" != "yes" ] || ewarn "$@"; }
443 veerror() { [ "${RC_VERBOSE}" != "yes" ] || eerror "$@"; }
444 vebegin() { [ "${RC_VERBOSE}" != "yes" ] || ebegin "$@"; }
446 [ "${RC_VERBOSE}" = "yes" ] && { eend "$@"; return $?; }
450 [ "${RC_VERBOSE}" = "yes" ] && { ewend "$@"; return $?; }
456 # Return the kernel version (major, minor and micro concated) as an integer.
457 # Assumes X and Y of X.Y.Z are numbers. Also assumes that some leading
458 # portion of Z is a number.
459 # e.g. 2.4.25, 2.6.10, 2.6.4-rc3, 2.2.40-poop, 2.0.15+foo
462 local KV="$(uname -r)"
464 printf "$(KV_to_int "${KV}")\n"
469 # bool get_bootparam(param)
471 # return 0 if gentoo=param was passed to the kernel
473 # EXAMPLE: if get_bootparam "nodevfs" ; then ....
476 local x copt params retval=1
478 [ ! -r "/proc/cmdline" ] && return 1
480 for copt in $(< /proc/cmdline)
482 if [ "${copt%=*}" = "gentoo" ]
484 params="$(gawk -v PARAMS="${copt##*=}" '
486 split(PARAMS, nodes, ",")
491 # Parse gentoo option
506 # Safer way to list the contents of a directory,
507 # as it do not have the "empty dir bug".
509 # char *dolisting(param)
511 # print a list of the directory contents
513 # NOTE: quote the params if they contain globs.
514 # also, error checking is not that extensive ...
523 if [ "${mypath%/\*}" != "${mypath}" ]
525 mypath="${mypath%/\*}"
530 [ ! -e "${x}" ] && continue
532 if [ ! -d "${x}" ] && ( [ -L "${x}" ] || [ -f "${x}" ] )
534 mylist="${mylist} $(ls "${x}" 2> /dev/null)"
536 [ "${x%/}" != "${x}" ] && x="${x%/}"
538 cd "${x}"; tmpstr="$(ls)"
542 mylist="${mylist} ${x}/${y}"
550 # char *add_suffix(char * configfile)
552 # Returns a config file name with the softlevel suffix
553 # appended to it. For use with multi-config services.
555 if [ "${RC_USE_CONFIG_PROFILE}" = "yes" -a -e "$1.${DEFAULTLEVEL}" ]
557 printf "$1.${DEFAULTLEVEL}\n"
565 # Network filesystems list for common use in rc-scripts.
566 # This variable is used in is_net_fs and other places such as
568 NET_FS_LIST="afs cifs coda gfs ncpfs nfs nfs4 shfs smbfs"
570 # bool is_net_fs(path)
572 # return 0 if path is the mountpoint of a networked filesystem
574 # EXAMPLE: if is_net_fs / ; then ...
578 # /proc/mounts is always accurate but may not always be available
579 if [ -e /proc/mounts ]; then
580 fstype="$( sed -n -e '/^rootfs/!s:.* '"$1"' \([^ ]*\).*:\1:p' /proc/mounts )"
582 fstype="$( mount | sed -n -e 's:.* on '"$1"' type \([^ ]*\).*:\1:p' )"
584 case " ${NET_FS_LIST} " in
596 # return 0 if the currently running system is User Mode Linux
598 # EXAMPLE: if is_uml_sys ; then ...
601 grep -qs 'UML' /proc/cpuinfo
605 # bool is_vserver_sys()
607 # return 0 if the currently running system is a Linux VServer
609 # EXAMPLE: if is_vserver_sys ; then ...
612 grep -qs '^s_context:[[:space:]]*[1-9]' /proc/self/status
616 # bool get_mount_fstab(path)
618 # return the parameters to pass to the mount command generated from fstab
620 # EXAMPLE: cmd=$( get_mount_fstab /proc )
621 # cmd=${cmd:--t proc none /proc}
625 awk '$1 ~ "^#" { next }
626 $2 == "'$*'" { if (found++ == 0) { print "-t "$3,"-o "$4,$1,$2 } }
627 END { if (found > 1) { print "More than one entry for '$*' found in /etc/fstab!" > "/dev/stderr" } }
631 # char *reverse_list(list)
633 # Returns the reversed order of list
638 while [ "$#" -gt 0 ] ; do
639 if [ -z "${ret}" ] ; then
650 # bool is_older_than(reference, files/dirs to check)
652 # return 0 if any of the files/dirs are newer than
655 # EXAMPLE: if is_older_than a.out *.o ; then ...
662 [ "${x}" -nt "${ref}" ] && return 0
664 if [ -d "${x}" ] ; then
665 is_older_than "${ref}" "${x}"/* && return 0
672 # Setup a basic $PATH. Just add system default to existing.
673 # This should solve both /sbin and /usr/sbin not present when
674 # doing 'su -c foo', or for something like: PATH= rcscript start
675 PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:${PATH}"
677 if [ "$(/sbin/consoletype 2> /dev/null)" = "serial" ] ; then
678 # We do not want colors/endcols on serial terminals
685 # Lastly check if the user disabled it with --nocolor argument
692 # Setup COLS and ENDCOL so eend can line up the [ ok ]
693 # width of [ ok ] == 7
694 COLS="$(stty size 2>/dev/null | cut -d' ' -f2)"
695 if [ -z "${COLS}" ] || [ "${COLS}" -le 0 ] ; then
699 if [ "${RC_ENDCOL}" = "yes" ]; then
700 ENDCOL="
\e[A
\e[$(( ${COLS} - 8 ))G"
705 # Setup the colors so our messages all look pretty
706 if [ "${RC_NOCOLOR}" = "yes" ]; then
707 unset GOOD WARN BAD NORMAL HILITE BRACKET