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=${RC_DOT_PATTERN//?/ }
372 [[ ${RC_QUIET_STDOUT} == yes ]] && return 0
374 if [[ -n ${RC_DOT_PATTERN} ]]; then
375 dots=$(printf "%$(( COLS - 3 - ${#RC_INDENTATION} - ${#msg} - 7 ))s" '')
376 dots=${dots//${spaces}/${RC_DOT_PATTERN}}
382 [[ ${RC_ENDCOL} == yes ]] && echo
384 LAST_E_LEN=$(( 3 + ${#RC_INDENTATION} + ${#msg} ))
389 # void _eend(int error, char *efunc, char* errstr)
391 # indicate the completion of process, called from eend/ewend
392 # if error, show errstr via efunc
394 # This function is private to functions.sh. Do not call it from a
398 local retval=${1:-0} efunc=${2:-eerror} msg
401 if [[ ${retval} == 0 ]]; then
402 [[ ${RC_QUIET_STDOUT} == yes ]] && return 0
403 msg="${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
405 if [[ -n "$*" ]]; then
408 msg="${BRACKET}[ ${BAD}!!${BRACKET} ]${NORMAL}"
411 if [[ ${RC_ENDCOL} == yes ]]; then
412 echo -e "${ENDCOL} ${msg}"
414 [[ ${LAST_E_CMD} == ebegin ]] || LAST_E_LEN=0
415 printf "%$(( COLS - LAST_E_LEN - 6 ))s%b\n" '' "${msg}"
421 # void eend(int error, char* errstr)
423 # indicate the completion of process
424 # if error, show errstr via eerror
430 _eend ${retval} eerror "$*"
436 # void ewend(int error, char* errstr)
438 # indicate the completion of process
439 # if error, show errstr via ewarn
445 _eend ${retval} ewarn "$*"
451 # v-e-commands honor RC_VERBOSE which defaults to no.
452 # The condition is negated so the return value will be zero.
453 veinfo() { [[ "${RC_VERBOSE}" != yes ]] || einfo "$@"; }
454 veinfon() { [[ "${RC_VERBOSE}" != yes ]] || einfon "$@"; }
455 vewarn() { [[ "${RC_VERBOSE}" != yes ]] || ewarn "$@"; }
456 veerror() { [[ "${RC_VERBOSE}" != yes ]] || eerror "$@"; }
457 vebegin() { [[ "${RC_VERBOSE}" != yes ]] || ebegin "$@"; }
459 [[ "${RC_VERBOSE}" == yes ]] && { eend "$@"; return $?; }
463 [[ "${RC_VERBOSE}" == yes ]] && { ewend "$@"; return $?; }
467 # char *KV_major(string)
469 # Return the Major (X of X.Y.Z) kernel version
472 [[ -z $1 ]] && return 1
478 # char *KV_minor(string)
480 # Return the Minor (Y of X.Y.Z) kernel version
483 [[ -z $1 ]] && return 1
490 # char *KV_micro(string)
492 # Return the Micro (Z of X.Y.Z) kernel version.
495 [[ -z $1 ]] && return 1
499 echo ${KV%%[^[:digit:]]*}
502 # int KV_to_int(string)
504 # Convert a string type kernel version (2.4.0) to an int (132096)
505 # for easy compairing or versions ...
508 [[ -z $1 ]] && return 1
510 local KV_MAJOR=$(KV_major "$1")
511 local KV_MINOR=$(KV_minor "$1")
512 local KV_MICRO=$(KV_micro "$1")
513 local KV_int=$(( KV_MAJOR * 65536 + KV_MINOR * 256 + KV_MICRO ))
515 # We make version 2.2.0 the minimum version we will handle as
516 # a sanity check ... if its less, we fail ...
517 if [[ ${KV_int} -ge 131584 ]] ; then
527 # Return the kernel version (major, minor and micro concated) as an integer.
528 # Assumes X and Y of X.Y.Z are numbers. Also assumes that some leading
529 # portion of Z is a number.
530 # e.g. 2.4.25, 2.6.10, 2.6.4-rc3, 2.2.40-poop, 2.0.15+foo
535 echo $(KV_to_int "${KV}")
540 # bool get_bootparam(param)
542 # return 0 if gentoo=param was passed to the kernel
544 # EXAMPLE: if get_bootparam "nodevfs" ; then ....
547 local x copt params retval=1
549 [ ! -r "/proc/cmdline" ] && return 1
551 for copt in $(< /proc/cmdline)
553 if [ "${copt%=*}" = "gentoo" ]
555 params="$(gawk -v PARAMS="${copt##*=}" '
557 split(PARAMS, nodes, ",")
562 # Parse gentoo option
577 # Safer way to list the contents of a directory,
578 # as it do not have the "empty dir bug".
580 # char *dolisting(param)
582 # print a list of the directory contents
584 # NOTE: quote the params if they contain globs.
585 # also, error checking is not that extensive ...
594 if [ "${mypath%/\*}" != "${mypath}" ]
596 mypath="${mypath%/\*}"
601 [ ! -e "${x}" ] && continue
603 if [ ! -d "${x}" ] && ( [ -L "${x}" -o -f "${x}" ] )
605 mylist="${mylist} $(ls "${x}" 2> /dev/null)"
607 [ "${x%/}" != "${x}" ] && x="${x%/}"
609 cd "${x}"; tmpstr="$(ls)"
613 mylist="${mylist} ${x}/${y}"
621 # char *add_suffix(char * configfile)
623 # Returns a config file name with the softlevel suffix
624 # appended to it. For use with multi-config services.
626 if [ "${RC_USE_CONFIG_PROFILE}" = "yes" -a -e "$1.${DEFAULTLEVEL}" ]
628 echo "$1.${DEFAULTLEVEL}"
636 # Network filesystems list for common use in rc-scripts.
637 # This variable is used in is_net_fs and other places such as
639 NET_FS_LIST="afs cifs coda gfs ncpfs nfs nfs4 shfs smbfs"
641 # bool is_net_fs(path)
643 # return 0 if path is the mountpoint of a networked filesystem
645 # EXAMPLE: if is_net_fs / ; then ...
649 # /proc/mounts is always accurate but may not always be available
650 if [[ -e /proc/mounts ]]; then
651 fstype=$( sed -n -e '/^rootfs/!s:.* '"$1"' \([^ ]*\).*:\1:p' /proc/mounts )
653 fstype=$( mount | sed -n -e 's:.* on '"$1"' type \([^ ]*\).*:\1:p' )
655 [[ " ${NET_FS_LIST} " == *" ${fstype} "* ]]
661 # return 0 if the currently running system is User Mode Linux
663 # EXAMPLE: if is_uml_sys ; then ...
666 grep -qs 'UML' /proc/cpuinfo
670 # bool is_vserver_sys()
672 # return 0 if the currently running system is a Linux VServer
674 # EXAMPLE: if is_vserver_sys ; then ...
677 grep -qs '^s_context:[[:space:]]*[1-9]' /proc/self/status
681 # bool get_mount_fstab(path)
683 # return the parameters to pass to the mount command generated from fstab
685 # EXAMPLE: cmd=$( get_mount_fstab /proc )
686 # cmd=${cmd:--t proc none /proc}
690 awk '$1 ~ "^#" { next }
691 $2 == "'$*'" { if (found++ == 0) { print "-t "$3,"-o "$4,$1,$2 } }
692 END { if (found > 1) { print "More than one entry for '$*' found in /etc/fstab!" > "/dev/stderr" } }
696 # char *reverse_list(list)
698 # Returns the reversed order of list
701 for (( i = $# ; i > 0 ; --i )); do
706 # bool is_older_than(reference, files/dirs to check)
708 # return 0 if any of the files/dirs are newer than
711 # EXAMPLE: if is_older_than a.out *.o ; then ...
718 [[ ${x} -nt ${ref} ]] && return 0
720 if [[ -d ${x} ]] ; then
721 is_older_than "${ref}" "${x}"/* && return 0
728 if [ -z "${EBUILD}" ] ; then
729 # Setup a basic $PATH. Just add system default to existing.
730 # This should solve both /sbin and /usr/sbin not present when
731 # doing 'su -c foo', or for something like: PATH= rcscript start
732 PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:${PATH}"
734 if [ "$(/sbin/consoletype 2> /dev/null)" = "serial" ] ; then
735 # We do not want colors/endcols on serial terminals
742 # Lastly check if the user disabled it with --nocolor argument
750 # Should we use colors ?
751 if [[ $* != *depend* ]]; then
752 # Check user pref in portage
753 RC_NOCOLOR="$(portageq envvar NOCOLOR 2>/dev/null)"
754 [ "${RC_NOCOLOR}" = "true" ] && RC_NOCOLOR="yes"
756 # We do not want colors during emerge depend
758 # No output is seen during emerge depend, so this is not needed.
763 if [[ -n ${EBUILD} && $* == *depend* ]]; then
764 # We do not want stty to run during emerge depend
767 # Setup COLS and ENDCOL so eend can line up the [ ok ]
768 COLS=${COLUMNS:-0} # bash's internal COLUMNS variable
769 (( COLS == 0 )) && COLS=$(stty size 2>/dev/null | cut -d' ' -f2)
770 (( COLS > 0 )) || (( COLS = 80 )) # width of [ ok ] == 7
773 if [[ ${RC_ENDCOL} == yes ]]; then
774 ENDCOL=$'\e[A\e['$(( COLS - 7 ))'G'
779 # Setup the colors so our messages all look pretty
780 if [[ ${RC_NOCOLOR} == yes ]]; then
781 unset GOOD WARN BAD NORMAL HILITE BRACKET