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
29 # CONFIG FUNCTIONS {{{
31 function setProgName { PROG_NAME__="$1"; }
33 function setExitFunction { EXIT_FUNCTION__="$1"; }
35 function disableSyslog { SYSLOG__="NO"; }
36 function enableSyslog { SYSLOG__="YES"; }
42 function setVerbose { VERBOSE__=${1:-1}; }
43 function unsetVerbose { VERBOSE_TMP__=$VERBOSE__; VERBOSE__=0; }
44 function restoreVerbose { VERBOSE__=$VERBOSE_TMP__; }
45 function getVerbose { echo "$VERBOSE__"; }
47 function setExitFunction { EXIT_FUNCTION__="$1"; }
48 function resetExitFunction { EXIT_FUNCTION__="_syslog"; }
52 # ERROR REPORTING FUNCTIONS {{{
54 # default print backend (there may be other functions)
61 if [ $VERBOSE__ -ge $level_ -a -n "$message_" ]; then
70 # FIXME vprint should be a var, because we want to call user-defined functions
71 # global var (there should be a syslog, and vprint + syslog function)
72 vprint $EPRINT__ "Error - " "$1"
75 # should be used for intern silentExecutes
78 vprint $EEPRINT__ " Error2 - " "$1"
81 # print debug output (function intern errors)
84 vprint $DPRINT__ "Debug - " "$1"
87 # for program notice messages
90 vprint $EPRINT__ "Notice - " "$1"
95 local error_message_="$1" # print this error message
96 local exit_code_="$2" # command exited with this exit code
98 echo -n "PANIC: $error_message_" >&2
100 echo "; ret($exit_code_)" >&2
105 if [ -n "$EXIT_FUNCTION__" ]; then
106 $EXIT_FUNCTION__ "$error_message_" "$exit_code_" >&2
113 local error_message_="$1" # print this error message
114 local exit_code_="$2" # command exits with this exit code
116 echo -n "WARN: $error_message_" >&2
117 if [ -n "$exit_code_" ]; then
118 echo "; ret($exit_code_)" >&2
126 local message_="$1" # error message
127 local exit_code_="$2"
129 if [ "$SYSLOG__" = "YES" ]; then
130 if [ -n "$exit_code_" ]; then
131 logger -p user.alert -t "$PROG_NAME__" -i "$message_ ret($exit_code_)" >&2
133 logger -p user.alert -t "$PROG_NAME__" -i "$message_" >&2
140 local message_="$1" # error message
141 local exit_code_="$2"
143 if [ -n "$exit_code_" ]; then
144 logger -p user.alert -t "$PROG_NAME__" -i "$message_ ret($exit_code_)" >&2
146 logger -p user.alert -t "$PROG_NAME__" -i "$message_" >&2
152 local error_message_="$1" # print this error message
153 local exit_code_="$2" # command exits with this exit code
155 warn "$error_message_" "$exit_code_"
156 syslog "$error_message_" "$exit_code_"
167 # i don't want to write exit status controle stuff every time
170 local to_exec_="$1" # command to execute
171 local error_function_=${2:-"eprint"} # function to call on error
172 local message_="$3" # user supplied error message
179 if [ $ret_ -eq 127 ]; then
180 syslog "problems executing ( $to_exec_ )" $ret_
182 if [ $ret_ -ne 0 ]; then
183 if [ -z "$message_" ]; then
184 $error_function_ "problems executing ( $to_exec_ )" "$ret_"
186 $error_function_ "$message_" "$ret_"
189 dprint "exec-$error_function_: ( $to_exec_ ) ret($ret_)"
193 function silentExecute
209 # if the file DOES exist, everything is fine
212 local file_to_test_="$1" # file to test
213 local error_function_=${2:-"eprint"} # function to call on error
214 local message_="$3" # user supplied error message
216 if [ ! -e "$file_to_test_" ]; then
217 if [ -z "$message_" ]; then
218 $error_function_ "file does not exist \"$file_to_test_\"" 66
220 $error_function_ "$message_"
224 dprint "isExistent(): file \"$1\" does exist => ready to go"
228 function isNotExistent
230 local file_to_test_="$1" # file to test
231 local error_function_=${2:-"eprint"} # function to call on error
232 local message_="$3" # user supplied error message
234 if [ -e "$file_to_test_" ]; then
235 if [ -z "$message_" ]; then
236 $error_function_ "file does allready exist \"$file_to_test_\"" 67
238 $error_function_ "$message_"
242 dprint "isNotExistent(): file \"$1\" does not exist => ready to go"
249 local to_check_="$1" # username to check against running process
250 local error_function_=${2:-"eprint"} # function to call on error
251 local message_="$3" # user supplied error message
256 if [ $user_ != "$to_check_" ]; then
257 if [ -z "$message_" ]; then
258 $error_function_ "username \"$user_\" is not \"$to_check_\"" 77 $exit_function_
260 $error_function_ "$message_"
264 dprint "checkUser(): accepted, username matches \"$to_check_\""
271 local to_check_="$1" # user-id to check against running process
272 local error_function_=${2:-"eprint"} # function to call on error
273 local message_="$3" # user supplied error message
278 if [ $user_id_ != "$to_check_" ]; then
279 if [ -z "$message_" ]; then
280 $error_function_ "UID \"$user_id_\" is not \"$to_check_\"" 77
282 $error_function_ "$message_"
286 dprint "checkId(): accepted, UID matches \"$to_check_\""
299 if [ -e "/etc/grml_cd" ]; then
300 dprint "runsFromHd(): grml is on CD"
303 dprint "runsFromHd(): grml is on HD"
310 if [ -e "/etc/grml_cd" ]; then
311 dprint "runsFromCd(): grml is on CD"
314 dprint "runsFromCd(): grml is on HD"
320 # secure input from console
323 local to_secure_="$1"
327 secured_=`echo -n "$to_secure_" |tr -c '[:alnum:]/.\-,\(\)' '_'`
328 dprint "secureInput(): \"$to_secure_\" => \"$secured_\""
333 # convert all possible path formats to absolute paths
343 D_=`dirname "$relpath_"`
344 B_=`basename "$relpath_"`
345 abspath_=`cd "$D_" 2>/dev/null && pwd || echo "$D_"`/$B_
346 end_path_=`echo "$abspath_" |tr --squeeze-repeats /`
347 dprint "relToAbs(): \"$relpath_\" => \"$end_path_\""
352 function stringInFile
354 local to_test_="$1" # matching pattern
355 local source_="$2" # source-file to grep
357 if [ ! -e "$source_" ]; then
358 eprint "stringInFile(): \"$source_\" does not exist"
362 case "$(cat $source_)" in *$to_test_*) return 0;; esac
367 function stringInString
369 local to_test_="$1" # matching pattern
370 local source_="$2" # string to search in
372 case "$source_" in *$to_test_*) return 0;; esac
376 # get value for bootparam given as first param
377 function getBootParam
379 local param_to_search_="$1"
382 stringInString " $param_to_search_=" "$CMD_LINE__" || return 1
383 result_="${CMD_LINE__##*$param_to_search_=}"
384 result_="${result_%%[ ]*}"
389 # Check boot commandline for specified option
390 function checkBootParam
392 stringInString " $1" "$CMD_LINE__"
399 # validates an IP FIXME
402 local ip_="$1" # ip addresse to validate
403 local error_function_=${2:-"eprint"} # function to call on error
404 local message_="$3" # user supplied error message
408 echo "$ip_" | grep -E -q -e '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}.[0-9]{1,3}' \
411 if [ $ret_ -ne 0 ]; then
412 if [ -z "$message_" ]; then
413 "$error_function_" "ip-addresse \"$ip_\" is NOT valied" $ret_
415 "$error_function_" "$message_" $ret_
420 dprint "ip-addresse \"$ip_\" is valied" $ret_
425 function netGetDefaultGateway
427 local error_function_=${1:-"eprint"} # function to call on error
428 local message_="$2" # user supplied error message
435 ip_=`route -n | awk '/^0\.0\.0\.0/{print $2; exit}'`
437 if [ -z "$ip_" ]; then
438 if [ -z "$message_" ]; then
439 "$error_function_" "no default gateway found" $ret_
441 "$error_function_" "$message_" $ret_
445 dprint "default gateway is \"$ip_\"" $ret_
451 function netGetNetmask
454 local error_function_=${2:-"eprint"} # function to call on error
455 local message_="$3" # user supplied error message
462 nm_=`ifconfig "$iface_" | awk '/[Mm]ask/{FS="[: ]*"; $0=$0; print $8; exit}'`
464 if [ -z "$nm_" ]; then
465 if [ -z "$message_" ]; then
466 "$error_function_" "could not find a netmask for \"$iface_\"" $ret_
468 "$error_function_" "$message_" $ret_
472 dprint "netmask on \"$iface_\" is \"$nm_\"" $ret_
481 local error_function_=${2:-"eprint"} # function to call on error
482 local message_="$3" # user supplied error message
489 #ip_=`ip addr list eth0 |mawk '/inet/{split($2,A,"/"); print A[1]}'`
490 ip_=`ifconfig "$iface_" | awk '/[Ii]net [Aa]ddr/{FS="[: ]*"; $0=$0; print $4; exit}'`
492 if [ -z "$ip_" ]; then
493 if [ -z "$message_" ]; then
494 "$error_function_" "no ip for \"$iface_\" found" $ret_
496 "$error_function_" "$message_" $ret_
500 dprint "addresse for \"$iface_\" is \"$ip_\"" $ret_
505 function netGetNameservers
507 local error_function_=${1:-"eprint"} # function to call on error
508 local message_="$2" # user supplied error message
510 local file_="/etc/resolv.conf"
513 if [ ! -e $file_ ]; then
514 warn "file \"$file_\" does not exist, could not get nameservers"
518 ns_=`awk '/^nameserver/{printf "%s ",$2}' $file_`
519 if [ -z "$ns_" ]; then
520 if [ -z "$message_" ]; then
521 "$error_function_" "no nameservers found" $ret_
523 "$error_function_" "$message_" $ret_
527 dprint "nameservers: \"$ns_\"" $ret_
535 # prints the next free /dev/loop* to stdout
536 function findNextFreeLoop
538 local error_function_=${1:-"eprint"} # function to call on error
539 local message_="$2" # user supplied error message
543 local ret_='' # saved return value
545 for i in 'losetup' 'losetup.orig'; do
546 tmp_=`$i -f 2>/dev/null`
547 if [ $? -eq 0 ]; then
554 dprint 'findNextFreeLoop(): losetup does not recognice option -f, searching next free loop device'
555 for i in `seq 0 100`; do
556 test -e /dev/loop$i || continue
557 losetup /dev/loop$i &>/dev/null
560 2) continue ;; # losetup could not get status of loopdevice (EPERM)
561 0) continue ;; # device exist
562 1) echo "/dev/loop$i"; return 0 ;; # device does not exist and no error
563 ?) continue ;; # return value not available in 'man losetup'
567 # hmm... could not find a loopdevice
568 if [ -z "$message_" ]; then
569 $error_function_ "could not find a free loop device"
571 $error_function_ "$message_"
579 function _initProgName
581 local name_="$1" # program name
583 local tmp_name_=`basename "$name_"` || \
584 logger -p user.alert -i "Init-initProgName: problems executing ( basename \"$name_\" ) ret($?)" >/dev/null
586 secureInput "$tmp_name_"
588 PROG_NAME__=`_initProgName "$0"`
591 function _checkExecutables
594 for i in tr dirname basename id logger kill cat grep route awk ifconfig; do
595 type -p $i &>/dev/null || tmp_="${tmp_}$i "
597 if [ -n "$tmp_" ]; then
598 eprint "Init-checkExecutables: following executables not found or not executable:\n$tmp_"
599 #syslog "Init-checkExecutables: following executables not found or not executable: $tmp_"
605 function _checkBootParam
607 local path_="/proc/cmdline"
608 if [ -e "$path_" ]; then
609 CMD_LINE__=`execute "cat $path_" warnLog`
612 warnLog "$path_ does not exist, thus sh-lib may not work reliable!"
619 ################################################################################
620 # vim:foldmethod=marker