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 setExitFunction { EXIT_FUNCTION__="$1"; }
55 function resetExitFunction { EXIT_FUNCTION__="_syslog"; }
59 # ERROR REPORTING FUNCTIONS {{{
61 # default print backend (there may be other functions)
68 if [ $VERBOSE__ -ge $level_ -a -n "$message_" ]; then
77 # FIXME vprint should be a var, because we want to call user-defined functions
78 # global var (there should be a syslog, and vprint + syslog function)
79 vprint $EPRINT__ "Error - " "$1"
82 # should be used for intern silentExecutes
85 vprint $EEPRINT__ " Error2 - " "$1"
88 # print debug output (function intern errors)
91 vprint $DPRINT__ "Debug - " "$1"
94 # for program notice messages
97 vprint $EPRINT__ "Notice - " "$1"
102 local error_message_="$1" # print this error message
103 local exit_code_="$2" # command exited with this exit code
105 echo -n "PANIC: $error_message_" >&2
107 echo "; ret($exit_code_)" >&2
112 if [ -n "$EXIT_FUNCTION__" ]; then
113 $EXIT_FUNCTION__ "$error_message_" "$exit_code_" >&2
120 local error_message_="$1" # print this error message
121 local exit_code_="$2" # command exits with this exit code
123 echo -n "WARN: $error_message_" >&2
124 if [ -n "$exit_code_" ]; then
125 echo "; ret($exit_code_)" >&2
133 local message_="$1" # error message
134 local exit_code_="$2"
136 if [ "$SYSLOG__" = "YES" ]; then
137 if [ -n "$exit_code_" ]; then
138 logger -p user.alert -t "$PROG_NAME__" -i "$message_ ret($exit_code_)" >&2
140 logger -p user.alert -t "$PROG_NAME__" -i "$message_" >&2
147 local message_="$1" # error message
148 local exit_code_="$2"
150 if [ -n "$exit_code_" ]; then
151 logger -p user.alert -t "$PROG_NAME__" -i "$message_ ret($exit_code_)" >&2
153 logger -p user.alert -t "$PROG_NAME__" -i "$message_" >&2
159 local error_message_="$1" # print this error message
160 local exit_code_="$2" # command exits with this exit code
162 warn "$error_message_" "$exit_code_"
163 syslog "$error_message_" "$exit_code_"
174 # i don't want to write exit status controle stuff every time
177 local to_exec_="$1" # command to execute
178 local error_function_=${2:-"eprint"} # function to call on error
179 local message_="$3" # user supplied error message
186 if [ $ret_ -eq 127 ]; then
187 syslog "problems executing ( $to_exec_ )" $ret_
189 if [ $ret_ -ne 0 ]; then
190 if [ -z "$message_" ]; then
191 $error_function_ "problems executing ( $to_exec_ )" "$ret_"
193 $error_function_ "$message_" "$ret_"
196 dprint "exec-$error_function_: ( $to_exec_ ) ret($ret_)"
200 function silentExecute
216 # if the file DOES exist, everything is fine
219 local file_to_test_="$1" # file to test
220 local error_function_=${2:-"eprint"} # function to call on error
221 local message_="$3" # user supplied error message
223 if [ ! -e "$file_to_test_" ]; then
224 if [ -z "$message_" ]; then
225 $error_function_ "file does not exist \"$file_to_test_\"" 66
227 $error_function_ "$message_"
231 dprint "isExistent(): file \"$1\" does exist => ready to go"
235 function isNotExistent
237 local file_to_test_="$1" # file to test
238 local error_function_=${2:-"eprint"} # function to call on error
239 local message_="$3" # user supplied error message
241 if [ -e "$file_to_test_" ]; then
242 if [ -z "$message_" ]; then
243 $error_function_ "file does allready exist \"$file_to_test_\"" 67
245 $error_function_ "$message_"
249 dprint "isNotExistent(): file \"$1\" does not exist => ready to go"
256 local to_check_="$1" # username to check against running process
257 local error_function_=${2:-"eprint"} # function to call on error
258 local message_="$3" # user supplied error message
263 if [ $user_ != "$to_check_" ]; then
264 if [ -z "$message_" ]; then
265 $error_function_ "username \"$user_\" is not \"$to_check_\"" 77 $exit_function_
267 $error_function_ "$message_"
271 dprint "checkUser(): accepted, username matches \"$to_check_\""
278 local to_check_="$1" # user-id to check against running process
279 local error_function_=${2:-"eprint"} # function to call on error
280 local message_="$3" # user supplied error message
285 if [ $user_id_ != "$to_check_" ]; then
286 if [ -z "$message_" ]; then
287 $error_function_ "UID \"$user_id_\" is not \"$to_check_\"" 77
289 $error_function_ "$message_"
293 dprint "checkId(): accepted, UID matches \"$to_check_\""
306 if [ -e "/etc/grml_cd" ]; then
307 dprint "runsFromHd(): grml is on CD"
310 dprint "runsFromHd(): grml is on HD"
317 if [ -e "/etc/grml_cd" ]; then
318 dprint "runsFromCd(): grml is on CD"
321 dprint "runsFromCd(): grml is on HD"
327 # secure input from console
330 local to_secure_="$1"
334 secured_=`echo -n "$to_secure_" |tr -c '[:alnum:]/.\-,\(\)' '_'`
335 dprint "secureInput(): \"$to_secure_\" => \"$secured_\""
340 # convert all possible path formats to absolute paths
350 D_=`dirname "$relpath_"`
351 B_=`basename "$relpath_"`
352 abspath_=`cd "$D_" 2>/dev/null && pwd || echo "$D_"`/$B_
353 end_path_=`echo "$abspath_" |tr --squeeze-repeats /`
354 dprint "relToAbs(): \"$relpath_\" => \"$end_path_\""
359 function stringInFile
361 local to_test_="$1" # matching pattern
362 local source_="$2" # source-file to grep
364 if [ ! -e "$source_" ]; then
365 eprint "stringInFile(): \"$source_\" does not exist"
369 case "$(cat $source_)" in *$to_test_*) return 0;; esac
374 function stringInString
376 local to_test_="$1" # matching pattern
377 local source_="$2" # string to search in
379 case "$source_" in *$to_test_*) return 0;; esac
383 # get value for bootparam given as first param
384 function getBootParam
386 local param_to_search_="$1"
389 stringInString " $param_to_search_=" "$CMD_LINE__" || return 1
390 result_="${CMD_LINE__##*$param_to_search_=}"
391 result_="${result_%%[ ]*}"
396 # Check boot commandline for specified option
397 function checkBootParam
399 stringInString " $1" "$CMD_LINE__"
406 # validates an IP FIXME
409 local ip_="$1" # ip addresse to validate
410 local error_function_=${2:-"eprint"} # function to call on error
411 local message_="$3" # user supplied error message
415 echo "$ip_" | grep -E -q -e '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}.[0-9]{1,3}' \
418 if [ $ret_ -ne 0 ]; then
419 if [ -z "$message_" ]; then
420 "$error_function_" "ip-addresse \"$ip_\" is NOT valied" $ret_
422 "$error_function_" "$message_" $ret_
427 dprint "ip-addresse \"$ip_\" is valied" $ret_
432 function netGetDefaultGateway
434 local error_function_=${1:-"eprint"} # function to call on error
435 local message_="$2" # user supplied error message
441 ip_=`route -n | awk '/^0\.0\.0\.0/{print $2; exit}'`
444 if [ -z "$ip_" ]; then
445 if [ -z "$message_" ]; then
446 "$error_function_" "no default gateway found" $ret_
448 "$error_function_" "$message_" $ret_
452 dprint "default gateway is \"$ip_\"" $ret_
458 function netGetNetmask
461 local error_function_=${2:-"eprint"} # function to call on error
462 local message_="$3" # user supplied error message
468 nm_=`ifconfig "$iface_" | awk '/[Mm]ask/{FS="[: ]*"; $0=$0; print $8; exit}'`
471 if [ -z "$nm_" ]; then
472 if [ -z "$message_" ]; then
473 "$error_function_" "could not find a netmask for \"$iface_\"" $ret_
475 "$error_function_" "$message_" $ret_
479 dprint "netmask on \"$iface_\" is \"$nm_\"" $ret_
488 local error_function_=${2:-"eprint"} # function to call on error
489 local message_="$3" # user supplied error message
495 #ip_=`ip addr list eth0 |mawk '/inet/{split($2,A,"/"); print A[1]}'`
496 ip_=`ifconfig "$iface_" | awk '/[Ii]net [Aa]ddr/{FS="[: ]*"; $0=$0; print $4; exit}'`
499 if [ -z "$ip_" ]; then
500 if [ -z "$message_" ]; then
501 "$error_function_" "no ip for \"$iface_\" found" $ret_
503 "$error_function_" "$message_" $ret_
507 dprint "addresse for \"$iface_\" is \"$ip_\"" $ret_
512 function netGetNameservers
514 local error_function_=${1:-"eprint"} # function to call on error
515 local message_="$2" # user supplied error message
517 local file_="/etc/resolv.conf"
520 if [ ! -e $file_ ]; then
521 warn "file \"$file_\" does not exist, could not get nameservers"
526 ns_=`awk '/^nameserver/{printf "%s ",$2}' $file_`
528 if [ -z "$ns_" ]; then
529 if [ -z "$message_" ]; then
530 "$error_function_" "no nameservers found" $ret_
532 "$error_function_" "$message_" $ret_
536 dprint "nameservers: \"$ns_\"" $ret_
544 # prints the next free /dev/loop* to stdout
545 function findNextFreeLoop
547 local error_function_=${1:-"eprint"} # function to call on error
548 local message_="$2" # user supplied error message
552 local ret_='' # saved return value
554 for i in 'losetup' 'losetup.orig'; do
555 tmp_=`$i -f 2>/dev/null`
556 if [ $? -eq 0 ]; then
563 dprint 'findNextFreeLoop(): losetup does not recognice option -f, searching next free loop device'
564 for i in `seq 0 100`; do
565 test -e /dev/loop$i || continue
566 losetup /dev/loop$i &>/dev/null
569 2) continue ;; # losetup could not get status of loopdevice (EPERM)
570 0) continue ;; # device exist
571 1) echo "/dev/loop$i"; return 0 ;; # device does not exist and no error
572 ?) continue ;; # return value not available in 'man losetup'
576 # hmm... could not find a loopdevice
577 if [ -z "$message_" ]; then
578 $error_function_ "could not find a free loop device"
580 $error_function_ "$message_"
588 function _initProgName
590 local name_="$1" # program name
592 local tmp_name_=`basename "$name_"` || \
593 logger -p user.alert -i "Init-initProgName: problems executing ( basename \"$name_\" ) ret($?)" >/dev/null
595 secureInput "$tmp_name_"
597 PROG_NAME__=`_initProgName "$0"`
600 function _checkExecutables
603 for i in tr dirname basename id logger kill cat grep route awk ifconfig; do
604 type -p $i &>/dev/null || tmp_="${tmp_}$i "
606 if [ -n "$tmp_" ]; then
607 eprint "Init-checkExecutables: following executables not found or not executable:\n$tmp_"
608 #syslog "Init-checkExecutables: following executables not found or not executable: $tmp_"
614 function _checkBootParam
616 local path_="/proc/cmdline"
617 if [ -e "$path_" ]; then
618 CMD_LINE__=`execute "cat $path_" warnLog`
621 warnLog "$path_ does not exist, thus sh-lib may not work reliable!"
628 ################################################################################
629 # vim:foldmethod=marker