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_
431 function netGetIfaces
433 local error_function_=${1:-"eprint"} # function to call on error
434 local message_="$2" # user supplied error message
438 #ip a|grep 'inet ' |awk '$NF !~ /lo/{print $NF}'
439 if_="`ip a|grep 'inet ' |awk '{print $NF}'`"
441 if [ -z "$if_" ]; then
442 if [ -z "$message_" ]; then
443 "$error_function_" "no interfaces found" $ret_
445 "$error_function_" "$message_" $ret_
449 dprint "interfaces found" $ret_
454 function netGetDefaultGateway
456 local error_function_=${1:-"eprint"} # function to call on error
457 local message_="$2" # user supplied error message
463 ip_=`route -n | awk '/^0\.0\.0\.0/{print $2; exit}'`
466 if [ -z "$ip_" ]; then
467 if [ -z "$message_" ]; then
468 "$error_function_" "no default gateway found" $ret_
470 "$error_function_" "$message_" $ret_
474 dprint "default gateway is \"$ip_\"" $ret_
480 function netGetNetmask
483 local error_function_=${2:-"eprint"} # function to call on error
484 local message_="$3" # user supplied error message
490 nm_=`ifconfig "$iface_" | awk '/[Mm]ask/{FS="[: ]*"; $0=$0; print $8; exit}'`
493 if [ -z "$nm_" ]; then
494 if [ -z "$message_" ]; then
495 "$error_function_" "could not find a netmask for \"$iface_\"" $ret_
497 "$error_function_" "$message_" $ret_
501 dprint "netmask on \"$iface_\" is \"$nm_\"" $ret_
510 local error_function_=${2:-"eprint"} # function to call on error
511 local message_="$3" # user supplied error message
517 #ip_=`ip addr list eth0 |mawk '/inet/{split($2,A,"/"); print A[1]}'`
518 ip_=`ifconfig "$iface_" | awk '/[Ii]net [Aa]ddr/{FS="[: ]*"; $0=$0; print $4; exit}'`
521 if [ -z "$ip_" ]; then
522 if [ -z "$message_" ]; then
523 "$error_function_" "no ip for \"$iface_\" found" $ret_
525 "$error_function_" "$message_" $ret_
529 dprint "addresse for \"$iface_\" is \"$ip_\"" $ret_
534 function netGetNameservers
536 local error_function_=${1:-"eprint"} # function to call on error
537 local message_="$2" # user supplied error message
539 local file_="/etc/resolv.conf"
542 if [ ! -e $file_ ]; then
543 warn "file \"$file_\" does not exist, could not get nameservers"
548 ns_=`awk '/^nameserver/{printf "%s ",$2}' $file_`
550 if [ -z "$ns_" ]; then
551 if [ -z "$message_" ]; then
552 "$error_function_" "no nameservers found" $ret_
554 "$error_function_" "$message_" $ret_
558 dprint "nameservers: \"$ns_\"" $ret_
566 # prints the next free /dev/loop* to stdout
567 function findNextFreeLoop
569 local error_function_=${1:-"eprint"} # function to call on error
570 local message_="$2" # user supplied error message
574 local ret_='' # saved return value
576 for i in 'losetup' 'losetup.orig'; do
577 tmp_=`$i -f 2>/dev/null`
578 if [ $? -eq 0 ]; then
585 dprint 'findNextFreeLoop(): losetup does not recognice option -f, searching next free loop device'
586 for i in `seq 0 100`; do
587 test -e /dev/loop$i || continue
588 losetup /dev/loop$i &>/dev/null
591 2) continue ;; # losetup could not get status of loopdevice (EPERM)
592 0) continue ;; # device exist
593 1) echo "/dev/loop$i"; return 0 ;; # device does not exist and no error
594 ?) continue ;; # return value not available in 'man losetup'
598 # hmm... could not find a loopdevice
599 if [ -z "$message_" ]; then
600 $error_function_ "could not find a free loop device"
602 $error_function_ "$message_"
610 function _initProgName
612 local name_="$1" # program name
614 local tmp_name_=`basename "$name_"` || \
615 logger -p user.alert -i "Init-initProgName: problems executing ( basename \"$name_\" ) ret($?)" >/dev/null
617 secureInput "$tmp_name_"
619 PROG_NAME__=`_initProgName "$0"`
622 function _checkExecutables
625 for i in tr dirname basename id logger kill cat grep route awk ifconfig; do
626 type -p $i &>/dev/null || tmp_="${tmp_}$i "
628 if [ -n "$tmp_" ]; then
629 eprint "Init-checkExecutables: following executables not found or not executable:\n$tmp_"
630 #syslog "Init-checkExecutables: following executables not found or not executable: $tmp_"
636 function _checkBootParam
638 local path_="/proc/cmdline"
639 if [ -e "$path_" ]; then
640 CMD_LINE__=`execute "cat $path_" warnLog`
643 warnLog "$path_ does not exist, thus sh-lib may not work reliable!"
650 ################################################################################
651 # vim:foldmethod=marker