3 # Purpose: Shellscript library
4 # Authors: grml-team (grml.org), (c) Michael Gebetsroither <gebi@grml.org>
5 # Bug-Reports: see http://grml.org/bugs/
6 # License: This file is licensed under the GPL v2.
7 # Latest change: Mon May 02 00:17:44 CEST 2005 [gebi]
8 ################################################################################
14 # FIXME maybe PROG_PATH__ for better error reporting?
15 PROG_NAME__="" # initialised within init section
17 # >= level and the function will print the message
18 EPRINT__=1 # eprint (error print)
19 EEPRINT__=2 # 2print (intern error print)
20 DPRINT__=3 # dprint (debug print)
22 EXIT_FUNCTION__="_syslog" # function to call upon die (can be set by user)
26 CMD_LINE__="" # /proc/cmdline
32 # CONFIG FUNCTIONS {{{
34 function setProgName { PROG_NAME__="$1"; }
36 function setExitFunction { EXIT_FUNCTION__="$1"; }
38 function disableSyslog { SYSLOG__="NO"; }
39 function enableSyslog { SYSLOG__="YES"; }
41 function saveLang { LANG__="$LANG"; LC_ALL__="$LC_ALL"; }
42 function restoreLang { LANG="$LANG__"; LC_ALL="$LC_ALL__"; }
43 function setCLang { saveLang; LANG="C"; LC_ALL="C"; }
49 function setVerbose { VERBOSE__=${1:-1}; }
50 function unsetVerbose { VERBOSE_TMP__=$VERBOSE__; VERBOSE__=0; }
51 function restoreVerbose { VERBOSE__=$VERBOSE_TMP__; }
52 function getVerbose { echo "$VERBOSE__"; }
54 function setDebug { setVerbose "$DPRINT__"; }
55 function unsetDebug { restoreVerbose; }
57 function setExitFunction { EXIT_FUNCTION__="$1"; }
58 function resetExitFunction { EXIT_FUNCTION__="_syslog"; }
62 # ERROR REPORTING FUNCTIONS {{{
64 # default print backend (there may be other functions)
71 if [ $VERBOSE__ -ge $level_ -a -n "$message_" ]; then
80 # FIXME vprint should be a var, because we want to call user-defined functions
81 # global var (there should be a syslog, and vprint + syslog function)
82 vprint $EPRINT__ "Error - " "$1"
85 # should be used for intern silentExecutes
88 vprint $EEPRINT__ " Error2 - " "$1"
91 # print debug output (function intern errors)
94 vprint $DPRINT__ "Debug - " "$1"
97 # for program notice messages
100 vprint $EPRINT__ "Notice - " "$1"
105 local error_message_="$1" # print this error message
106 local exit_code_="$2" # command exited with this exit code
108 echo -n "PANIC: $error_message_" >&2
110 echo "; ret($exit_code_)" >&2
115 if [ -n "$EXIT_FUNCTION__" ]; then
116 $EXIT_FUNCTION__ "$error_message_" "$exit_code_" >&2
123 local error_message_="$1" # print this error message
124 local exit_code_="$2" # command exits with this exit code
126 echo -n "WARN: $error_message_" >&2
127 if [ -n "$exit_code_" ]; then
128 echo "; ret($exit_code_)" >&2
136 local message_="$1" # error message
137 local exit_code_="$2"
139 if [ "$SYSLOG__" = "YES" ]; then
140 if [ -n "$exit_code_" ]; then
141 logger -p user.alert -t "$PROG_NAME__" -i "$message_ ret($exit_code_)" >&2
143 logger -p user.alert -t "$PROG_NAME__" -i "$message_" >&2
150 local message_="$1" # error message
151 local exit_code_="$2"
153 if [ -n "$exit_code_" ]; then
154 logger -p user.alert -t "$PROG_NAME__" -i "$message_ ret($exit_code_)" >&2
156 logger -p user.alert -t "$PROG_NAME__" -i "$message_" >&2
162 local error_message_="$1" # print this error message
163 local exit_code_="$2" # command exits with this exit code
165 warn "$error_message_" "$exit_code_"
166 syslog "$error_message_" "$exit_code_"
177 # i don't want to write exit status controle stuff every time
180 local to_exec_="$1" # command to execute
181 local error_function_=${2:-"eprint"} # function to call on error
182 local message_="$3" # user supplied error message
189 if [ $ret_ -eq 127 ]; then
190 syslog "problems executing ( $to_exec_ )" $ret_
192 if [ $ret_ -ne 0 ]; then
193 if [ -z "$message_" ]; then
194 $error_function_ "problems executing ( $to_exec_ )" "$ret_"
196 $error_function_ "$message_" "$ret_"
199 dprint "exec-$error_function_: ( $to_exec_ ) ret($ret_)"
203 function silentExecute
219 # if the file DOES exist, everything is fine
222 local file_to_test_="$1" # file to test
223 local error_function_=${2:-"eprint"} # function to call on error
224 local message_="$3" # user supplied error message
226 if [ ! -e "$file_to_test_" ]; then
227 if [ -z "$message_" ]; then
228 $error_function_ "file does not exist \"$file_to_test_\"" 66
230 $error_function_ "$message_"
234 dprint "isExistent(): file \"$1\" does exist => ready to go"
238 function isNotExistent
240 local file_to_test_="$1" # file to test
241 local error_function_=${2:-"eprint"} # function to call on error
242 local message_="$3" # user supplied error message
244 if [ -e "$file_to_test_" ]; then
245 if [ -z "$message_" ]; then
246 $error_function_ "file does allready exist \"$file_to_test_\"" 67
248 $error_function_ "$message_"
252 dprint "isNotExistent(): file \"$1\" does not exist => ready to go"
259 local to_check_="$1" # username to check against running process
260 local error_function_=${2:-"eprint"} # function to call on error
261 local message_="$3" # user supplied error message
266 if [ $user_ != "$to_check_" ]; then
267 if [ -z "$message_" ]; then
268 $error_function_ "username \"$user_\" is not \"$to_check_\"" 77 $exit_function_
270 $error_function_ "$message_"
274 dprint "checkUser(): accepted, username matches \"$to_check_\""
281 local to_check_="$1" # user-id to check against running process
282 local error_function_=${2:-"eprint"} # function to call on error
283 local message_="$3" # user supplied error message
288 if [ $user_id_ != "$to_check_" ]; then
289 if [ -z "$message_" ]; then
290 $error_function_ "UID \"$user_id_\" is not \"$to_check_\"" 77
292 $error_function_ "$message_"
296 dprint "checkId(): accepted, UID matches \"$to_check_\""
309 if [ -e "/etc/grml_cd" ]; then
310 dprint "runsFromHd(): grml is on CD"
313 dprint "runsFromHd(): grml is on HD"
320 if [ -e "/etc/grml_cd" ]; then
321 dprint "runsFromCd(): grml is on CD"
324 dprint "runsFromCd(): grml is on HD"
330 # secure input from console
333 local to_secure_="$1"
337 secured_=`echo -n "$to_secure_" |tr -c '[:alnum:]/.\-,\(\)' '_'`
338 dprint "secureInput(): \"$to_secure_\" => \"$secured_\""
343 # convert all possible path formats to absolute paths
353 D_=`dirname "$relpath_"`
354 B_=`basename "$relpath_"`
355 abspath_=`cd "$D_" 2>/dev/null && pwd || echo "$D_"`/$B_
356 end_path_=`echo "$abspath_" |tr --squeeze-repeats /`
357 dprint "relToAbs(): \"$relpath_\" => \"$end_path_\""
362 function stringInFile
364 local to_test_="$1" # matching pattern
365 local source_="$2" # source-file to grep
367 if [ ! -e "$source_" ]; then
368 eprint "stringInFile(): \"$source_\" does not exist"
372 case "$(cat $source_)" in *$to_test_*) return 0;; esac
377 function stringInString
379 local to_test_="$1" # matching pattern
380 local source_="$2" # string to search in
382 case "$source_" in *$to_test_*) return 0;; esac
386 # get value for bootparam given as first param
387 function getBootParam
389 local param_to_search_="$1"
392 stringInString " $param_to_search_=" "$CMD_LINE__" || return 1
393 result_="${CMD_LINE__##*$param_to_search_=}"
394 result_="${result_%%[ ]*}"
399 # Check boot commandline for specified option
400 function checkBootParam
402 stringInString " $1" "$CMD_LINE__"
409 # validates an IP FIXME
412 local ip_="$1" # ip addresse to validate
413 local error_function_=${2:-"eprint"} # function to call on error
414 local message_="$3" # user supplied error message
418 echo "$ip_" | grep -E -q -e '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}.[0-9]{1,3}' \
421 if [ $ret_ -ne 0 ]; then
422 if [ -z "$message_" ]; then
423 "$error_function_" "ip-addresse \"$ip_\" is NOT valied" $ret_
425 "$error_function_" "$message_" $ret_
430 dprint "ip-addresse \"$ip_\" is valied" $ret_
434 function netGetIfaces
436 local error_function_=${1:-"eprint"} # function to call on error
437 local message_="$2" # user supplied error message
441 #ip a|grep 'inet ' |awk '$NF !~ /lo/{print $NF}'
442 if_="`ip a|grep 'inet ' |awk '{print $NF}'`"
444 if [ -z "$if_" ]; then
445 if [ -z "$message_" ]; then
446 "$error_function_" "no interfaces found" $ret_
448 "$error_function_" "$message_" $ret_
452 dprint "interfaces found" $ret_
457 function netGetDefaultGateway
459 local error_function_=${1:-"eprint"} # function to call on error
460 local message_="$2" # user supplied error message
466 ip_=`route -n | awk '/^0\.0\.0\.0/{print $2; exit}'`
469 if [ -z "$ip_" ]; then
470 if [ -z "$message_" ]; then
471 "$error_function_" "no default gateway found" $ret_
473 "$error_function_" "$message_" $ret_
477 dprint "default gateway is \"$ip_\"" $ret_
483 function netGetNetmask
486 local error_function_=${2:-"eprint"} # function to call on error
487 local message_="$3" # user supplied error message
493 nm_=`ifconfig "$iface_" | awk '/[Mm]ask/{FS="[: ]*"; $0=$0; print $8; exit}'`
496 if [ -z "$nm_" ]; then
497 if [ -z "$message_" ]; then
498 "$error_function_" "could not find a netmask for \"$iface_\"" $ret_
500 "$error_function_" "$message_" $ret_
504 dprint "netmask on \"$iface_\" is \"$nm_\"" $ret_
513 local error_function_=${2:-"eprint"} # function to call on error
514 local message_="$3" # user supplied error message
520 #ip_=`ip addr list eth0 |mawk '/inet/{split($2,A,"/"); print A[1]}'`
521 ip_=`ifconfig "$iface_" | awk '/[Ii]net [Aa]ddr/{FS="[: ]*"; $0=$0; print $4; exit}'`
524 if [ -z "$ip_" ]; then
525 if [ -z "$message_" ]; then
526 "$error_function_" "no ip for \"$iface_\" found" $ret_
528 "$error_function_" "$message_" $ret_
532 dprint "addresse for \"$iface_\" is \"$ip_\"" $ret_
537 function netGetNameservers
539 local error_function_=${1:-"eprint"} # function to call on error
540 local message_="$2" # user supplied error message
542 local file_="/etc/resolv.conf"
545 if [ ! -e $file_ ]; then
546 warn "file \"$file_\" does not exist, could not get nameservers"
551 ns_=`awk '/^nameserver/{printf "%s ",$2}' $file_`
553 if [ -z "$ns_" ]; then
554 if [ -z "$message_" ]; then
555 "$error_function_" "no nameservers found" $ret_
557 "$error_function_" "$message_" $ret_
561 dprint "nameservers: \"$ns_\"" $ret_
569 # prints the next free /dev/loop* to stdout
570 function findNextFreeLoop
572 local error_function_=${1:-"eprint"} # function to call on error
573 local message_="$2" # user supplied error message
577 local ret_='' # saved return value
579 for i in 'losetup' 'losetup.orig'; do
580 tmp_=`$i -f 2>/dev/null`
581 if [ $? -eq 0 ]; then
588 dprint 'findNextFreeLoop(): losetup does not recognice option -f, searching next free loop device'
589 for i in `seq 0 100`; do
590 test -e /dev/loop$i || continue
591 losetup /dev/loop$i &>/dev/null
594 2) continue ;; # losetup could not get status of loopdevice (EPERM)
595 0) continue ;; # device exist
596 1) echo "/dev/loop$i"; return 0 ;; # device does not exist and no error
597 ?) continue ;; # return value not available in 'man losetup'
601 # hmm... could not find a loopdevice
602 if [ -z "$message_" ]; then
603 $error_function_ "could not find a free loop device"
605 $error_function_ "$message_"
613 function _initProgName
615 local name_="$1" # program name
617 local tmp_name_=`basename "$name_"` || \
618 logger -p user.alert -i "Init-initProgName: problems executing ( basename \"$name_\" ) ret($?)" >/dev/null
620 secureInput "$tmp_name_"
622 PROG_NAME__=`_initProgName "$0"`
625 function _checkExecutables
628 for i in tr dirname basename id logger kill cat grep route awk ifconfig; do
629 type -p $i &>/dev/null || tmp_="${tmp_}$i "
631 if [ -n "$tmp_" ]; then
632 eprint "Init-checkExecutables: following executables not found or not executable:\n$tmp_"
633 #syslog "Init-checkExecutables: following executables not found or not executable: $tmp_"
639 function _checkBootParam
641 local path_="/proc/cmdline"
642 if [ -e "$path_" ]; then
643 CMD_LINE__=`execute "cat $path_" warnLog`
646 warnLog "$path_ does not exist, thus sh-lib may not work reliable!"
652 function _setDebugLevel
654 local debug_="${DEBUG:-0}"
661 ################################################################################
662 # vim:foldmethod=marker expandtab shiftwidth=2 tabstop=2