lsb-functions: removed is_net_fs()
[grml-etc-core.git] / etc / grml / lsb-functions
1 # lsb init-functions
2 # vim:ft=sh:tw=80
3 # /lib/lsb/init-functions for Debian -*- shell-script -*-
4 #
5 # Copyright (c) 2002-03 Chris Lawrence
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
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.
19 #
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
30 # SUCH DAMAGE.
31
32 TPUT="${TPUT:-"/usr/bin/tput"}"
33
34 _have_tput() {
35     [ -x "$TPUT" ] && "$TPUT" hpa 60 >/dev/null 2>&1 \
36         && return 0 \
37         || return 1
38 }
39
40 log_success_msg() {
41     printf " * $@\n"
42 }
43
44 log_failure_msg() {
45     if _have_tput ; then
46         RED="$("$TPUT" setaf 1)"
47         #NORMAL="$("$TPUT" op)"
48         printf " ${RED}*${NORMAL} $@\n"
49     else
50         printf " * $@\n"
51     fi
52 }
53
54 log_warning_msg() {
55     if _have_tput ; then
56         YELLOW="$("$TPUT" setaf 3)"
57         #NORMAL="$("$TPUT" op")"
58         # printf " *${NORMAL} $@\n"
59         printf " ${BLUE}*${NORMAL} $@\n"
60     else
61         printf " * $@\n"
62     fi
63 }
64
65 log_warning_msg_nn() {
66     if _have_tput ; then
67         YELLOW="$("$TPUT" setaf 3)"
68         printf " ${BLUE}*${NORMAL} $@"
69     else
70         printf " * $@"
71     fi
72 }
73
74 # int log_begin_message (char *message)
75 log_begin_msg() {
76         if [ "$#" -eq 0 ]; then
77                 return 1
78         fi
79         printf " ${GREEN}*${NORMAL} $@\n"
80 }
81
82 log_begin_msg_nn() {
83         if [ "$#" -eq 0 ]; then
84                 return 1
85         fi
86         printf " ${GREEN}*${NORMAL} $@"
87 }
88
89
90 SUBMSG="   ${GREEN}-${NORMAL} "
91
92 # int log_end_message (int exitstatus)
93 log_end_msg() {
94
95     # If no arguments were passed, return
96     [ "$#" -eq 0 ] && return 1
97
98     # Only do the fancy stuff if we have an appropriate terminal
99     # and if /usr is already mounted
100     if _have_tput ; then
101         COLS="$("$TPUT" cols)"
102         if [ -n "$COLS" ]; then
103             COL=$(( "$COLS" - 7 ))
104         else
105             COL=73
106         fi
107         UP="$("$TPUT" cuu1)"
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"
114         else
115             printf "${UP}${START} ${RED}*${NORMAL}${END}[${RED}fail${NORMAL}]\n"
116         fi
117     else
118         if [ "$1" -eq 0 ]; then
119             printf "   ...done.\n"
120         else
121             printf "   ...fail!\n"
122         fi
123     fi
124     return "$1"
125 }
126
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 $
130
131 RC_GOT_FUNCTIONS="yes"
132
133 # Different types of dependencies
134 deptypes="need use"
135 # Different types of order deps
136 ordtypes="before after"
137
138 #
139 # Internal variables
140 #
141
142 # Dont output to stdout?
143 RC_QUIET_STDOUT="no"
144 RC_VERBOSE="${RC_VERBOSE:-no}"
145
146 # Should we use color?
147 if [ -r /proc/cmdline ] ; then
148     grep -q ' nocolor' /proc/cmdline && RC_NOCOLOR='yes'
149 fi
150 RC_NOCOLOR="${RC_NOCOLOR:-no}"
151 # Can the terminal handle endcols?
152 RC_ENDCOL="yes"
153
154 #
155 # Default values for rc system
156 #
157 RC_TTY_NUMBER=11
158 RC_NET_STRICT_CHECKING="no"
159 RC_PARALLEL_STARTUP="no"
160 RC_USE_CONFIG_PROFILE="yes"
161
162 #
163 # Default values for e-message indentation and dots
164 #
165 RC_INDENTATION=''
166 RC_DEFAULT_INDENT=2
167 #RC_DOT_PATTERN=' .'
168 RC_DOT_PATTERN=''
169
170 # void get_bootconfig()
171 #
172 #    Get the BOOTLEVEL and SOFTLEVEL by setting
173 #    'bootlevel' and 'softlevel' via kernel
174 #    parameters.
175 #
176 get_bootconfig() {
177     local copt
178     local newbootlevel
179     local newsoftlevel
180
181     for copt in $(</proc/cmdline) ; do
182         case "${copt%=*}" in
183             "bootlevel")
184                 newbootlevel="${copt##*=}"
185                 ;;
186             "softlevel")
187                 newsoftlevel="${copt##*=}"
188                 ;;
189         esac
190     done
191
192     if [ -n "${newbootlevel}" ] ; then
193         export BOOTLEVEL="${newbootlevel}"
194     else
195         export BOOTLEVEL="boot"
196     fi
197
198     if [ -n "${newsoftlevel}" ] ; then
199         export DEFAULTLEVEL="${newsoftlevel}"
200     else
201         export DEFAULTLEVEL="default"
202     fi
203
204     return 0
205 }
206
207 # void get_libdir(void)
208 #
209 #    prints the current libdir {lib,lib32,lib64}
210 #
211 get_libdir() {
212     if [ -n "${CONF_LIBDIR_OVERRIDE}" ] ; then
213         CONF_LIBDIR="${CONF_LIBDIR_OVERRIDE}"
214     elif [ -x "/usr/bin/portageq" ] ; then
215         CONF_LIBDIR="$(/usr/bin/portageq envvar CONF_LIBDIR)"
216     fi
217     printf '%s\n' ${CONF_LIBDIR:=lib}
218 }
219
220 # void esyslog(char* priority, char* tag, char* message)
221 #
222 #    use the system logger to log a message
223 #
224 esyslog() {
225     local pri
226     local tag
227
228     if [ -x /usr/bin/logger ]
229     then
230         pri="$1"
231         tag="$2"
232
233         shift 2
234         [ -z "$*" ] && return 0
235
236         /usr/bin/logger -p "${pri}" -t "${tag}" -- "$*"
237     fi
238
239     return 0
240 }
241
242 # void eindent(int num)
243 #
244 #    increase the indent used for e-commands.
245 #
246 eindent() {
247     local i=$1
248     (( i > 0 )) || (( i = RC_DEFAULT_INDENT ))
249     esetdent $(( ${#RC_INDENTATION} + i ))
250 }
251
252 # void eoutdent(int num)
253 #
254 #    decrease the indent used for e-commands.
255 #
256 eoutdent() {
257     local i=$1
258     (( i > 0 )) || (( i = RC_DEFAULT_INDENT ))
259     esetdent $(( ${#RC_INDENTATION} - i ))
260 }
261
262 # void esetdent(int num)
263 #
264 #    hard set the indent used for e-commands.
265 #    num defaults to 0
266 #
267 esetdent() {
268     local i=$1
269     (( i < 0 )) && (( i = 0 ))
270     RC_INDENTATION=$(printf "%${i}s" '')
271 }
272
273 # void einfo(char* message)
274 #
275 #    show an informative message (with a newline)
276 #
277 einfo() {
278     einfon "$*\n"
279     LAST_E_CMD=einfo
280     return 0
281 }
282
283 # void einfon(char* message)
284 #
285 #    show an informative message (without a newline)
286 #
287 einfon() {
288     [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
289     [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
290     printf " ${GOOD}*${NORMAL} ${RC_INDENTATION}$*"
291     LAST_E_CMD=einfon
292     return 0
293 }
294
295 # void ewarn(char* message)
296 #
297 #    show a warning message + log it
298 #
299 ewarn() {
300     if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
301         printf " $*\n"
302     else
303         [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
304         printf " ${WARN}*${NORMAL} ${RC_INDENTATION}$*\n"
305     fi
306
307     # Log warnings to system log
308     esyslog "daemon.warning" "rc-scripts" "$*"
309
310     LAST_E_CMD=ewarn
311     return 0
312 }
313
314 # void eerror(char* message)
315 #
316 #    show an error message + log it
317 #
318 eerror() {
319     if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
320         printf " $*\n" >/dev/stderr
321     else
322         [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
323         printf " ${BAD}*${NORMAL} ${RC_INDENTATION}$*\n"
324     fi
325
326     # Log errors to system log
327     esyslog "daemon.err" "rc-scripts" "$*"
328
329     LAST_E_CMD=eerror
330     return 0
331 }
332
333 # void ebegin(char* message)
334 #
335 #    show a message indicating the start of a process
336 #
337 ebegin() {
338     local msg="$*" dots spaces
339     spaces="$(printf '%'"${#RC_DOT_PATTERN}"'s' '')"
340     [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
341
342     if [ -n "${RC_DOT_PATTERN}" ]; then
343         dots=$(printf "%$(( COLS - 3 - ${#RC_INDENTATION} - ${#msg} - 7 ))s" '')
344         while [ "${dots#${spaces}}" != "${dots}" ] ; do
345             dots="${dots#${spaces}}${RC_DOT_PATTERN}"
346         done
347         msg="${msg}${dots}"
348     else
349         msg="${msg} ..."
350     fi
351     einfon "${msg}"
352     [ "${RC_ENDCOL}" = "yes" ] && echo
353
354     LAST_E_LEN=$(( 3 + ${#RC_INDENTATION} + ${#msg} ))
355     LAST_E_CMD=ebegin
356     return 0
357 }
358
359 # void _eend(int error, char *efunc, char* errstr)
360 #
361 #    indicate the completion of process, called from eend/ewend
362 #    if error, show errstr via efunc
363 #
364 #    This function is private to functions.sh.  Do not call it from a
365 #    script.
366 #
367 _eend() {
368     local retval=${1:-0} efunc=${2:-eerror} msg
369     shift 2
370
371     if [ "${retval}" -eq 0 ]; then
372         [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
373         msg="${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
374     else
375         if [ -n "$*" ]; then
376             "${efunc}" "$*"
377         fi
378         msg="${BRACKET}[ ${BAD}!!${BRACKET} ]${NORMAL}"
379     fi
380
381     if [ "${RC_ENDCOL}" = "yes" ]; then
382         printf "${ENDCOL}  ${msg}\n"
383     else
384         [ "${LAST_E_CMD}" = "ebegin" ] || LAST_E_LEN=0
385         printf "%$(( "${COLS}" - "${LAST_E_LEN}" - 6 ))s%b\n" '' "${msg}"
386     fi
387
388     return ${retval}
389 }
390
391 # void eend(int error, char* errstr)
392 #
393 #    indicate the completion of process
394 #    if error, show errstr via eerror
395 #
396 eend() {
397     local retval=${1:-0}
398     shift
399
400     _eend ${retval} eerror "$*"
401
402     LAST_E_CMD=eend
403     return $retval
404 }
405
406 # void ewend(int error, char* errstr)
407 #
408 #    indicate the completion of process
409 #    if error, show errstr via ewarn
410 #
411 ewend() {
412     local retval=${1:-0}
413     shift
414
415     _eend ${retval} ewarn "$*"
416
417     LAST_E_CMD=ewend
418     return $retval
419 }
420
421 # v-e-commands honor RC_VERBOSE which defaults to no.
422 # The condition is negated so the return value will be zero.
423 veinfo()  { [ "${RC_VERBOSE}" != "yes" ] || einfo  "$@"; }
424 veinfon() { [ "${RC_VERBOSE}" != "yes" ] || einfon "$@"; }
425 vewarn()  { [ "${RC_VERBOSE}" != "yes" ] || ewarn  "$@"; }
426 veerror() { [ "${RC_VERBOSE}" != "yes" ] || eerror "$@"; }
427 vebegin() { [ "${RC_VERBOSE}" != "yes" ] || ebegin "$@"; }
428 veend() {
429     [ "${RC_VERBOSE}" = "yes" ] && { eend "$@"; return $?; }
430     return ${1:-0}
431 }
432 veend() {
433     [ "${RC_VERBOSE}" = "yes" ] && { ewend "$@"; return $?; }
434     return ${1:-0}
435 }
436
437 # bool get_bootparam(param)
438 #
439 #   return 0 if gentoo=param was passed to the kernel
440 #
441 #   EXAMPLE:  if get_bootparam "nodevfs" ; then ....
442 #
443 get_bootparam() {
444     local x copt params retval=1
445
446     [ ! -r "/proc/cmdline" ] && return 1
447
448     for copt in $(< /proc/cmdline)
449     do
450         if [ "${copt%=*}" = "gentoo" ]
451         then
452             params="$(gawk -v PARAMS="${copt##*=}" '
453                 BEGIN {
454                     split(PARAMS, nodes, ",")
455                     for (x in nodes)
456                         print nodes[x]
457                 }')"
458
459             # Parse gentoo option
460             for x in ${params}
461             do
462                 if [ "${x}" = "$1" ]
463                 then
464 #                    printf "YES\n"
465                     retval=0
466                 fi
467             done
468         fi
469     done
470
471     return ${retval}
472 }
473
474 # char *add_suffix(char * configfile)
475 #
476 #    Returns a config file name with the softlevel suffix
477 #    appended to it.  For use with multi-config services.
478 add_suffix() {
479     if [ "${RC_USE_CONFIG_PROFILE}" = "yes" -a -e "$1.${DEFAULTLEVEL}" ]
480     then
481         printf "$1.${DEFAULTLEVEL}\n"
482     else
483         printf "$1\n"
484     fi
485
486     return 0
487 }
488
489 # bool is_uml_sys()
490 #
491 #   return 0 if the currently running system is User Mode Linux
492 #
493 #   EXAMPLE:  if is_uml_sys ; then ...
494 #
495 is_uml_sys() {
496     grep -qs 'UML' /proc/cpuinfo
497     return $?
498 }
499
500 # bool is_vserver_sys()
501 #
502 #   return 0 if the currently running system is a Linux VServer
503 #
504 #   EXAMPLE:  if is_vserver_sys ; then ...
505 #
506 is_vserver_sys() {
507     grep -qs '^s_context:[[:space:]]*[1-9]' /proc/self/status
508     return $?
509 }
510
511 # bool get_mount_fstab(path)
512 #
513 #   return the parameters to pass to the mount command generated from fstab
514 #
515 #   EXAMPLE: cmd=$( get_mount_fstab /proc )
516 #            cmd=${cmd:--t proc none /proc}
517 #            mount -n ${cmd}
518 #
519 get_mount_fstab() {
520     awk '$1 ~ "^#" { next }
521          $2 == "'$*'" { if (found++ == 0) { print "-t "$3,"-o "$4,$1,$2 } }
522          END { if (found > 1) { print "More than one entry for '$*' found in /etc/fstab!" > "/dev/stderr" } }
523     ' /etc/fstab
524 }
525
526 # char *reverse_list(list)
527 #
528 #   Returns the reversed order of list
529 #
530 reverse_list() {
531     local ret
532     ret=''
533     while [ "$#" -gt 0 ] ; do
534         if [ -z "${ret}" ] ; then
535             ret="$1"
536         else
537             ret="$1 ${ret}"
538         fi
539         shift
540     done
541     printf '%s' "${ret}"
542 }
543
544
545 # bool is_older_than(reference, files/dirs to check)
546 #
547 #   return 0 if any of the files/dirs are newer than
548 #   the reference file
549 #
550 #   EXAMPLE: if is_older_than a.out *.o ; then ...
551 is_older_than() {
552     local x
553     local ref="$1"
554     shift
555
556     for x in "$@" ; do
557         [ "${x}" -nt "${ref}" ] && return 0
558
559         if [ -d "${x}" ] ; then
560             is_older_than "${ref}" "${x}"/* && return 0
561         fi
562     done
563
564     return 1
565 }
566
567 # Setup a basic $PATH.  Just add system default to existing.
568 # This should solve both /sbin and /usr/sbin not present when
569 # doing 'su -c foo', or for something like:  PATH= rcscript start
570 PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:${PATH}"
571
572 if [ "$(/sbin/consoletype 2> /dev/null)" = "serial" ] ; then
573     # We do not want colors/endcols on serial terminals
574     RC_NOCOLOR="yes"
575     RC_ENDCOL="no"
576 fi
577
578 for arg in "$@" ; do
579     case "${arg}" in
580         # Lastly check if the user disabled it with --nocolor argument
581         --nocolor|-nc)
582             RC_NOCOLOR="yes"
583             ;;
584     esac
585 done
586
587 # Setup COLS and ENDCOL so eend can line up the [ ok ]
588 # width of [ ok ] == 7
589 COLS="$(stty size 2>/dev/null | cut -d' ' -f2)"
590 if [ -z "${COLS}" ] || [ "${COLS}" -le 0 ] ; then
591     COLS=80
592 fi
593
594 if [ "${RC_ENDCOL}" = "yes" ]; then
595     ENDCOL="\e[A\e[$(( ${COLS} - 8 ))G"
596 else
597     ENDCOL=''
598 fi
599
600 # Setup the colors so our messages all look pretty
601 if [ "${RC_NOCOLOR}" = "yes" ]; then
602     unset GOOD WARN BAD NORMAL HILITE BRACKET
603 else
604     GOOD='\e[32;01m'
605     WARN='\e[33;01m'
606     BAD='\e[31;01m'
607     NORMAL='\e[0m'
608     HILITE='\e[36;01m'
609     BRACKET='\e[34;01m'
610 fi
611
612 # vim:ts=4