1 # lsb init-functions {{{
2 # /lib/lsb/init-functions for Debian -*- shell-script -*-
4 # Copyright (c) 2002-03 Chris Lawrence
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
15 # 3. Neither the name of the author nor the names of other contributors
16 # may be used to endorse or promote products derived from this software
17 # without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 if [ -x $TPUT ] && $TPUT hpa 60 >/dev/null 2>&1; then
40 echo " ${RED}*${NORMAL} $@"
48 if [ -x $TPUT ] && $TPUT hpa 60 >/dev/null 2>&1; then
49 YELLOW=`$TPUT setaf 3`
51 # echo " *${NORMAL} $@"
52 echo " ${BLUE}*${NORMAL} $@"
58 log_warning_msg_nn () {
60 if [ -x $TPUT ] && $TPUT hpa 60 >/dev/null 2>&1; then
61 YELLOW=`$TPUT setaf 3`
62 echo -n " ${BLUE}*${NORMAL} $@"
68 # int log_begin_message (char *message)
73 echo " ${GREEN}*${NORMAL} $@"
80 echo -n " ${GREEN}*${NORMAL} $@"
84 SUBMSG=" ${GREEN}-${NORMAL} "
86 # int log_end_message (int exitstatus)
89 # If no arguments were passed, return
90 [ -z "$1" ] && return 1
92 # Only do the fancy stuff if we have an appropriate terminal
93 # and if /usr is already mounted
96 if [ -x $TPUT ] && [ -x $EXPR ] && $TPUT hpa 60 >/dev/null 2>&1; then
98 if [ -n "$COLS" ]; then
108 if [ $1 -eq 0 ]; then
109 echo "$UP$END${BLUE}[ ${GREEN}ok ${BLUE}]${NORMAL}"
111 echo -e "${UP}${START} ${RED}*${NORMAL}${END}[${RED}fail${NORMAL}]"
114 if [ $1 -eq 0 ]; then
123 # void esyslog(char* priority, char* tag, char* message)
124 # use the system logger to log a message
129 # if [ -x /usr/bin/logger ]
135 # [[ -z "$*" ]] && return 0
137 # /usr/bin/logger -p "${pri}" -t "${tag}" -- "$*"
143 # Copyright 1999-2005 Gentoo Foundation
144 # Distributed under the terms of the GNU General Public License v2
145 # $Header: /var/cvsroot/gentoo-src/rc-scripts/sbin/functions.sh,v 1.81.2.6 2005/05/15 20:00:31 vapier Exp $
147 RC_GOT_FUNCTIONS="yes"
149 # Different types of dependencies
151 # Different types of order deps
152 ordtypes="before after"
158 # Dont output to stdout?
160 RC_VERBOSE="${RC_VERBOSE:-no}"
162 # Should we use color?
163 if [ -r /proc/cmdline ] ; then
164 grep -q ' nocolor' /proc/cmdline && RC_NOCOLOR='yes'
166 RC_NOCOLOR="${RC_NOCOLOR:-no}"
167 # Can the terminal handle endcols?
171 # Default values for rc system
174 RC_NET_STRICT_CHECKING="no"
175 RC_PARALLEL_STARTUP="no"
176 RC_USE_CONFIG_PROFILE="yes"
179 # Default values for e-message indentation and dots
188 # Notify bootsplash/splashutils/gensplash/whatever about
195 # void profiling(...)
197 # Notify bootsplash/whatever about important events.
203 # void get_bootconfig()
205 # Get the BOOTLEVEL and SOFTLEVEL by setting
206 # 'bootlevel' and 'softlevel' via kernel
214 for copt in $(</proc/cmdline) ; do
217 newbootlevel="${copt##*=}"
220 newsoftlevel="${copt##*=}"
225 if [ -n "${newbootlevel}" ] ; then
226 export BOOTLEVEL="${newbootlevel}"
228 export BOOTLEVEL="boot"
231 if [ -n "${newsoftlevel}" ] ; then
232 export DEFAULTLEVEL="${newsoftlevel}"
234 export DEFAULTLEVEL="default"
240 # void get_libdir(void)
242 # prints the current libdir {lib,lib32,lib64}
245 if [ -n "${CONF_LIBDIR_OVERRIDE}" ] ; then
246 CONF_LIBDIR="${CONF_LIBDIR_OVERRIDE}"
247 elif [ -x "/usr/bin/portageq" ] ; then
248 CONF_LIBDIR="$(/usr/bin/portageq envvar CONF_LIBDIR)"
250 echo ${CONF_LIBDIR:=lib}
253 # void esyslog(char* priority, char* tag, char* message)
255 # use the system logger to log a message
261 if [ -x /usr/bin/logger ]
267 [ -z "$*" ] && return 0
269 /usr/bin/logger -p "${pri}" -t "${tag}" -- "$*"
275 # void eindent(int num)
277 # increase the indent used for e-commands.
281 (( i > 0 )) || (( i = RC_DEFAULT_INDENT ))
282 esetdent $(( ${#RC_INDENTATION} + i ))
285 # void eoutdent(int num)
287 # decrease the indent used for e-commands.
291 (( i > 0 )) || (( i = RC_DEFAULT_INDENT ))
292 esetdent $(( ${#RC_INDENTATION} - i ))
295 # void esetdent(int num)
297 # hard set the indent used for e-commands.
302 (( i < 0 )) && (( i = 0 ))
303 RC_INDENTATION=$(printf "%${i}s" '')
306 # void einfo(char* message)
308 # show an informative message (with a newline)
316 # void einfon(char* message)
318 # show an informative message (without a newline)
321 [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
322 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
323 echo -ne " ${GOOD}*${NORMAL} ${RC_INDENTATION}$*"
328 # void ewarn(char* message)
330 # show a warning message + log it
333 if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
336 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
337 echo -e " ${WARN}*${NORMAL} ${RC_INDENTATION}$*"
340 # Log warnings to system log
341 esyslog "daemon.warning" "rc-scripts" "$*"
347 # void eerror(char* message)
349 # show an error message + log it
352 if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
353 echo " $*" >/dev/stderr
355 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
356 echo -e " ${BAD}*${NORMAL} ${RC_INDENTATION}$*"
359 # Log errors to system log
360 esyslog "daemon.err" "rc-scripts" "$*"
366 # void ebegin(char* message)
368 # show a message indicating the start of a process
371 local msg="$*" dots spaces
372 spaces="$(printf '%'"${#RC_DOT_PATTERN}"'s' '')"
373 [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
375 if [ -n "${RC_DOT_PATTERN}" ]; then
376 dots=$(printf "%$(( COLS - 3 - ${#RC_INDENTATION} - ${#msg} - 7 ))s" '')
377 while [ "${dots#${spaces}}" != "${dots}" ] ; do
378 dots="${dots#${spaces}}${RC_DOT_PATTERN}"
385 [ "${RC_ENDCOL}" = "yes" ] && echo
387 LAST_E_LEN=$(( 3 + ${#RC_INDENTATION} + ${#msg} ))
392 # void _eend(int error, char *efunc, char* errstr)
394 # indicate the completion of process, called from eend/ewend
395 # if error, show errstr via efunc
397 # This function is private to functions.sh. Do not call it from a
401 local retval=${1:-0} efunc=${2:-eerror} msg
404 if [ "${retval}" -eq 0 ]; then
405 [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
406 msg="${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
411 msg="${BRACKET}[ ${BAD}!!${BRACKET} ]${NORMAL}"
414 if [ "${RC_ENDCOL}" = "yes" ]; then
415 echo -e "${ENDCOL} ${msg}"
417 [ "${LAST_E_CMD}" = "ebegin" ] || LAST_E_LEN=0
418 printf "%$(( COLS - LAST_E_LEN - 6 ))s%b\n" '' "${msg}"
424 # void eend(int error, char* errstr)
426 # indicate the completion of process
427 # if error, show errstr via eerror
433 _eend ${retval} eerror "$*"
439 # void ewend(int error, char* errstr)
441 # indicate the completion of process
442 # if error, show errstr via ewarn
448 _eend ${retval} ewarn "$*"
454 # v-e-commands honor RC_VERBOSE which defaults to no.
455 # The condition is negated so the return value will be zero.
456 veinfo() { [ "${RC_VERBOSE}" != yes ] || einfo "$@"; }
457 veinfon() { [ "${RC_VERBOSE}" != yes ] || einfon "$@"; }
458 vewarn() { [ "${RC_VERBOSE}" != yes ] || ewarn "$@"; }
459 veerror() { [ "${RC_VERBOSE}" != yes ] || eerror "$@"; }
460 vebegin() { [ "${RC_VERBOSE}" != yes ] || ebegin "$@"; }
462 [ "${RC_VERBOSE}" = "yes" ] && { eend "$@"; return $?; }
466 [ "${RC_VERBOSE}" = "yes" ] && { ewend "$@"; return $?; }
470 # char *KV_major(string)
472 # Return the Major (X of X.Y.Z) kernel version
475 [ -z "$1" ] && return 1
481 # char *KV_minor(string)
483 # Return the Minor (Y of X.Y.Z) kernel version
486 [ -z "$1" ] && return 1
493 # char *KV_micro(string)
495 # Return the Micro (Z of X.Y.Z) kernel version.
498 [ -z "$1" ] && return 1
502 echo "${KV%%[^[:digit:]]*}"
505 # int KV_to_int(string)
507 # Convert a string type kernel version (2.4.0) to an int (132096)
508 # for easy compairing or versions ...
511 [ -z "$1" ] && return 1
513 local KV_MAJOR="$(KV_major "$1")"
514 local KV_MINOR="$(KV_minor "$1")"
515 local KV_MICRO="$(KV_micro "$1")"
516 local KV_int=$(( KV_MAJOR * 65536 + KV_MINOR * 256 + KV_MICRO ))
518 # We make version 2.2.0 the minimum version we will handle as
519 # a sanity check ... if its less, we fail ...
520 if [ "${KV_int}" -ge 131584 ] ; then
530 # Return the kernel version (major, minor and micro concated) as an integer.
531 # Assumes X and Y of X.Y.Z are numbers. Also assumes that some leading
532 # portion of Z is a number.
533 # e.g. 2.4.25, 2.6.10, 2.6.4-rc3, 2.2.40-poop, 2.0.15+foo
536 local KV="$(uname -r)"
538 echo "$(KV_to_int "${KV}")"
543 # bool get_bootparam(param)
545 # return 0 if gentoo=param was passed to the kernel
547 # EXAMPLE: if get_bootparam "nodevfs" ; then ....
550 local x copt params retval=1
552 [ ! -r "/proc/cmdline" ] && return 1
554 for copt in $(< /proc/cmdline)
556 if [ "${copt%=*}" = "gentoo" ]
558 params="$(gawk -v PARAMS="${copt##*=}" '
560 split(PARAMS, nodes, ",")
565 # Parse gentoo option
580 # Safer way to list the contents of a directory,
581 # as it do not have the "empty dir bug".
583 # char *dolisting(param)
585 # print a list of the directory contents
587 # NOTE: quote the params if they contain globs.
588 # also, error checking is not that extensive ...
597 if [ "${mypath%/\*}" != "${mypath}" ]
599 mypath="${mypath%/\*}"
604 [ ! -e "${x}" ] && continue
606 if [ ! -d "${x}" ] && ( [ -L "${x}" -o -f "${x}" ] )
608 mylist="${mylist} $(ls "${x}" 2> /dev/null)"
610 [ "${x%/}" != "${x}" ] && x="${x%/}"
612 cd "${x}"; tmpstr="$(ls)"
616 mylist="${mylist} ${x}/${y}"
624 # char *add_suffix(char * configfile)
626 # Returns a config file name with the softlevel suffix
627 # appended to it. For use with multi-config services.
629 if [ "${RC_USE_CONFIG_PROFILE}" = "yes" -a -e "$1.${DEFAULTLEVEL}" ]
631 echo "$1.${DEFAULTLEVEL}"
639 # Network filesystems list for common use in rc-scripts.
640 # This variable is used in is_net_fs and other places such as
642 NET_FS_LIST="afs cifs coda gfs ncpfs nfs nfs4 shfs smbfs"
644 # bool is_net_fs(path)
646 # return 0 if path is the mountpoint of a networked filesystem
648 # EXAMPLE: if is_net_fs / ; then ...
652 # /proc/mounts is always accurate but may not always be available
653 if [ -e /proc/mounts ]; then
654 fstype="$( sed -n -e '/^rootfs/!s:.* '"$1"' \([^ ]*\).*:\1:p' /proc/mounts )"
656 fstype="$( mount | sed -n -e 's:.* on '"$1"' type \([^ ]*\).*:\1:p' )"
658 case " ${NET_FS_LIST} " in
670 # return 0 if the currently running system is User Mode Linux
672 # EXAMPLE: if is_uml_sys ; then ...
675 grep -qs 'UML' /proc/cpuinfo
679 # bool is_vserver_sys()
681 # return 0 if the currently running system is a Linux VServer
683 # EXAMPLE: if is_vserver_sys ; then ...
686 grep -qs '^s_context:[[:space:]]*[1-9]' /proc/self/status
690 # bool get_mount_fstab(path)
692 # return the parameters to pass to the mount command generated from fstab
694 # EXAMPLE: cmd=$( get_mount_fstab /proc )
695 # cmd=${cmd:--t proc none /proc}
699 awk '$1 ~ "^#" { next }
700 $2 == "'$*'" { if (found++ == 0) { print "-t "$3,"-o "$4,$1,$2 } }
701 END { if (found > 1) { print "More than one entry for '$*' found in /etc/fstab!" > "/dev/stderr" } }
705 # char *reverse_list(list)
707 # Returns the reversed order of list
712 while [ "$#" -gt 0 ] ; do
713 if [ -z "${ret}" ] ; then
724 # bool is_older_than(reference, files/dirs to check)
726 # return 0 if any of the files/dirs are newer than
729 # EXAMPLE: if is_older_than a.out *.o ; then ...
736 [ "${x}" -nt "${ref}" ] && return 0
738 if [ -d "${x}" ] ; then
739 is_older_than "${ref}" "${x}"/* && return 0
746 if [ -z "${EBUILD}" ] ; then
747 # Setup a basic $PATH. Just add system default to existing.
748 # This should solve both /sbin and /usr/sbin not present when
749 # doing 'su -c foo', or for something like: PATH= rcscript start
750 PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:${PATH}"
752 if [ "$(/sbin/consoletype 2> /dev/null)" = "serial" ] ; then
753 # We do not want colors/endcols on serial terminals
760 # Lastly check if the user disabled it with --nocolor argument
768 # Should we use colors ?
771 # Check user pref in portage
772 RC_NOCOLOR="$(portageq envvar NOCOLOR 2>/dev/null)"
773 [ "${RC_NOCOLOR}" = "true" ] && RC_NOCOLOR="yes"
776 # We do not want colors during emerge depend
778 # No output is seen during emerge depend, so this is not needed.
788 # We do not want stty to run during emerge depend
789 [ -n "${EBUILD}" ] && COLS=80
792 if [ "${COLS}" -eq 0 ] ; then
793 # Setup COLS and ENDCOL so eend can line up the [ ok ]
794 COLS="$(stty size 2>/dev/null | cut -d' ' -f2)"
795 [ "${COLS}" -gt 0 ] || COLS=80 # width of [ ok ] == 7
798 if [ "${RC_ENDCOL}" = "yes" ]; then
799 ENDCOL="
\e[A
\e[$(( ${COLS} - 8 ))G"
804 # Setup the colors so our messages all look pretty
805 if [ "${RC_NOCOLOR}" = "yes" ]; then
806 unset GOOD WARN BAD NORMAL HILITE BRACKET