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 # directory for init scripts
18 INITD_DIR__="/etc/init.d/"
20 # >= level and the function will print the message
21 EPRINT__=1 # eprint (error print)
22 EEPRINT__=2 # 2print (intern error print)
23 DPRINT__=3 # dprint (debug print)
25 EXIT_FUNCTION__="_syslog" # function to call upon die (can be set by user)
29 CMD_LINE__="" # /proc/cmdline
35 # CONFIG FUNCTIONS {{{
37 function setProgName { PROG_NAME__="$1"; }
39 function setExitFunction { EXIT_FUNCTION__="$1"; }
41 function disableSyslog { SYSLOG__="NO"; }
42 function enableSyslog { SYSLOG__="YES"; }
44 function saveLang { LANG__="$LANG"; LC_ALL__="$LC_ALL"; }
45 function restoreLang { LANG="$LANG__"; LC_ALL="$LC_ALL__"; }
46 function setCLang { saveLang; LANG="C"; LC_ALL="C"; }
52 function setVerbose { VERBOSE__=${1:-1}; }
53 function unsetVerbose { VERBOSE_TMP__=$VERBOSE__; VERBOSE__=0; }
54 function restoreVerbose { VERBOSE__=$VERBOSE_TMP__; }
55 function getVerbose { echo "$VERBOSE__"; }
57 function setDebug { setVerbose "$DPRINT__"; }
58 function unsetDebug { restoreVerbose; }
60 function setExitFunction { EXIT_FUNCTION__="$1"; }
61 function resetExitFunction { EXIT_FUNCTION__="_syslog"; }
65 # ERROR REPORTING FUNCTIONS {{{
67 # default print backend (there may be other functions)
74 if [ $VERBOSE__ -ge $level_ -a -n "$message_" ]; then
83 # FIXME vprint should be a var, because we want to call user-defined functions
84 # global var (there should be a syslog, and vprint + syslog function)
85 vprint $EPRINT__ "Error - " "$1"
88 # should be used for intern silentExecutes
91 vprint $EEPRINT__ " Error2 - " "$1"
94 # print debug output (function intern errors)
97 vprint $DPRINT__ "Debug - " "$1"
100 # for program notice messages
103 vprint $EPRINT__ "Notice - " "$1"
108 local error_message_="$1" # print this error message
109 local exit_code_="$2" # command exited with this exit code
111 echo -n "PANIC: $error_message_" >&2
113 echo "; ret($exit_code_)" >&2
118 if [ -n "$EXIT_FUNCTION__" ]; then
119 $EXIT_FUNCTION__ "$error_message_" "$exit_code_" >&2
126 local error_message_="$1" # print this error message
127 local exit_code_="$2" # command exits with this exit code
129 echo -n "WARN: $error_message_" >&2
130 if [ -n "$exit_code_" ]; then
131 echo "; ret($exit_code_)" >&2
139 local message_="$1" # error message
140 local exit_code_="$2"
142 if [ "$SYSLOG__" = "YES" ]; then
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
153 local message_="$1" # error message
154 local exit_code_="$2"
156 if [ -n "$exit_code_" ]; then
157 logger -p user.alert -t "$PROG_NAME__" -i "$message_ ret($exit_code_)" >&2
159 logger -p user.alert -t "$PROG_NAME__" -i "$message_" >&2
165 local error_message_="$1" # print this error message
166 local exit_code_="$2" # command exits with this exit code
168 warn "$error_message_" "$exit_code_"
169 syslog "$error_message_" "$exit_code_"
180 # i don't want to write exit status controle stuff every time
183 local to_exec_="$1" # command to execute
184 local error_function_=${2:-"eprint"} # function to call on error
185 local message_="$3" # user supplied error message
192 if [ $ret_ -eq 127 ]; then
193 syslog "problems executing ( $to_exec_ )" $ret_
195 if [ $ret_ -ne 0 ]; then
196 if [ -z "$message_" ]; then
197 $error_function_ "problems executing ( $to_exec_ )" "$ret_"
199 $error_function_ "$message_" "$ret_"
202 dprint "exec-$error_function_: ( $to_exec_ ) ret($ret_)"
206 function silentExecute
222 # if the file DOES exist, everything is fine
225 local file_to_test_="$1" # file to test
226 local error_function_=${2:-"eprint"} # function to call on error
227 local message_="$3" # user supplied error message
229 if [ ! -e "$file_to_test_" ]; then
230 if [ -z "$message_" ]; then
231 $error_function_ "file does not exist \"$file_to_test_\"" 66
233 $error_function_ "$message_"
237 dprint "isExistent(): file \"$1\" does exist => ready to go"
241 function isNotExistent
243 local file_to_test_="$1" # file to test
244 local error_function_=${2:-"eprint"} # function to call on error
245 local message_="$3" # user supplied error message
247 if [ -e "$file_to_test_" ]; then
248 if [ -z "$message_" ]; then
249 $error_function_ "file does allready exist \"$file_to_test_\"" 67
251 $error_function_ "$message_"
255 dprint "isNotExistent(): file \"$1\" does not exist => ready to go"
262 local to_check_="$1" # username to check against running process
263 local error_function_=${2:-"eprint"} # function to call on error
264 local message_="$3" # user supplied error message
269 if [ $user_ != "$to_check_" ]; then
270 if [ -z "$message_" ]; then
271 $error_function_ "username \"$user_\" is not \"$to_check_\"" 77 $exit_function_
273 $error_function_ "$message_"
277 dprint "checkUser(): accepted, username matches \"$to_check_\""
284 local to_check_="$1" # user-id to check against running process
285 local error_function_=${2:-"eprint"} # function to call on error
286 local message_="$3" # user supplied error message
291 if [ $user_id_ != "$to_check_" ]; then
292 if [ -z "$message_" ]; then
293 $error_function_ "UID \"$user_id_\" is not \"$to_check_\"" 77
295 $error_function_ "$message_"
299 dprint "checkId(): accepted, UID matches \"$to_check_\""
311 if [ -f /etc/grml_version ] ; then
312 dprint "isGrml(): this seems to be a grml system"
315 dprint "isGrml(): this is not a grml system"
322 if [ -e "/etc/grml_cd" ]; then
323 dprint "runsFromHd(): grml is on CD"
326 dprint "runsFromHd(): grml is on HD"
333 if [ -e "/etc/grml_cd" ]; then
334 dprint "runsFromCd(): grml is on CD"
337 dprint "runsFromCd(): grml is on HD"
343 # secure input from console
346 local to_secure_="$1"
350 secured_=`echo -n "$to_secure_" |tr -c '[:alnum:]/.\-,\(\)' '_'`
351 dprint "secureInput(): \"$to_secure_\" => \"$secured_\""
356 # convert all possible path formats to absolute paths
362 abspath_="`readlink -f \"$relpath_\"`" || \
363 warn "relToAbs(): Problems getting absolute path" "$?" || return 1
364 dprint "relToAbs(): \"$relpath_\" => \"$abspath_\""
369 function stringInFile
371 local to_test_="$1" # matching pattern
372 local source_="$2" # source-file to grep
374 if [ ! -e "$source_" ]; then
375 eprint "stringInFile(): \"$source_\" does not exist"
379 case "$(cat $source_)" in *$to_test_*) return 0;; esac
384 function stringInString
386 local to_test_="$1" # matching pattern
387 local source_="$2" # string to search in
389 case "$source_" in *$to_test_*) return 0;; esac
393 # get value for bootparam given as first param
394 function getBootParam
396 local param_to_search_="$1"
399 stringInString " $param_to_search_=" "$CMD_LINE__" || return 1
400 result_="${CMD_LINE__##*$param_to_search_=}"
401 result_="${result_%%[ ]*}"
406 # Check boot commandline for specified option
407 function checkBootParam
409 stringInString " $1" "$CMD_LINE__"
416 # validates an IP FIXME
419 local ip_="$1" # ip addresse to validate
420 local error_function_=${2:-"eprint"} # function to call on error
421 local message_="$3" # user supplied error message
425 echo "$ip_" | grep -E -q -e '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}.[0-9]{1,3}' \
428 if [ $ret_ -ne 0 ]; then
429 if [ -z "$message_" ]; then
430 "$error_function_" "ip-addresse \"$ip_\" is NOT valied" $ret_
432 "$error_function_" "$message_" $ret_
437 dprint "ip-addresse \"$ip_\" is valied" $ret_
441 function netGetIfaces
443 local error_function_=${1:-"eprint"} # function to call on error
444 local message_="$2" # user supplied error message
448 #ip a|grep 'inet ' |awk '$NF !~ /lo/{print $NF}'
449 if_="`ip a|grep 'inet ' |awk '{print $NF}'`"
451 if [ -z "$if_" ]; then
452 if [ -z "$message_" ]; then
453 "$error_function_" "no interfaces found" $ret_
455 "$error_function_" "$message_" $ret_
459 dprint "interfaces found" $ret_
464 function netGetDefaultGateway
466 local error_function_=${1:-"eprint"} # function to call on error
467 local message_="$2" # user supplied error message
473 ip_=`route -n | awk '/^0\.0\.0\.0/{print $2; exit}'`
476 if [ -z "$ip_" ]; then
477 if [ -z "$message_" ]; then
478 "$error_function_" "no default gateway found" $ret_
480 "$error_function_" "$message_" $ret_
484 dprint "default gateway is \"$ip_\"" $ret_
490 function netGetNetmask
493 local error_function_=${2:-"eprint"} # function to call on error
494 local message_="$3" # user supplied error message
500 nm_=`ifconfig "$iface_" | awk '/[Mm]ask/{FS="[: ]*"; $0=$0; print $8; exit}'`
503 if [ -z "$nm_" ]; then
504 if [ -z "$message_" ]; then
505 "$error_function_" "could not find a netmask for \"$iface_\"" $ret_
507 "$error_function_" "$message_" $ret_
511 dprint "netmask on \"$iface_\" is \"$nm_\"" $ret_
520 local error_function_=${2:-"eprint"} # function to call on error
521 local message_="$3" # user supplied error message
527 #ip_=`ip addr list eth0 |mawk '/inet/{split($2,A,"/"); print A[1]}'`
528 ip_=`ifconfig "$iface_" | awk '/[Ii]net [Aa]ddr/{FS="[: ]*"; $0=$0; print $4; exit}'`
531 if [ -z "$ip_" ]; then
532 if [ -z "$message_" ]; then
533 "$error_function_" "no ip for \"$iface_\" found" $ret_
535 "$error_function_" "$message_" $ret_
539 dprint "addresse for \"$iface_\" is \"$ip_\"" $ret_
544 function netGetNameservers
546 local error_function_=${1:-"eprint"} # function to call on error
547 local message_="$2" # user supplied error message
549 local file_="/etc/resolv.conf"
552 if [ ! -e $file_ ]; then
553 warn "file \"$file_\" does not exist, could not get nameservers"
558 ns_=`awk '/^nameserver/{printf "%s ",$2}' $file_ |xargs echo`
560 if [ -z "$ns_" ]; then
561 if [ -z "$message_" ]; then
562 "$error_function_" "no nameservers found" $ret_
564 "$error_function_" "$message_" $ret_
568 dprint "nameservers: \"$ns_\"" $ret_
576 function _touchService
578 local action_="${1:-"start"}"
580 local error_function_=${3:-"eprint"} # function to call on error
581 local message_="$4" # user supplied error message
584 local known_action_='false'
585 for i in "start" "stop" "restart" "reload" "force-reload"; do
586 if [[ $i == $action_ ]]; then
591 $known_action_ || warn "_touchService(): unknown action \"$action_\""
594 local service_path_=""
595 service_path_="${INITD_DIR__}/$service_"
596 if [ ! -e "$service_path_" ]; then
597 warn "_touchService(): service does not exist: \"$service_\""
600 if [ ! -x "$service_path_" ]; then
601 warn "_touchService(): service is not executable: \"$service_\""
605 "$service_path_" "$action_"
607 if [[ $ret_ != 0 ]]; then
608 if [ -z "$message_" ]; then
609 "$error_function_" "Problems ${action_}ing service \"$service_\"" $ret_
611 "$error_function_" "$message_" $ret_
615 dprint "_touchService(): successfully started service \"$service_\""
619 function _createServiceFunctions
621 for i in "start" "stop" "restart" "reload"; do
622 eval "function ${i}Service { _touchService ${i} \"\$1\" \"\$2\" \"\$3\"; }"
624 eval "function forceReloadService { _touchService force-reload \"\$1\" \"\$2\" \"\$3\"; }"
626 _createServiceFunctions
629 # prints the next free /dev/loop* to stdout
630 function findNextFreeLoop
632 local error_function_=${1:-"eprint"} # function to call on error
633 local message_="$2" # user supplied error message
637 local ret_='' # saved return value
639 for i in 'losetup' 'losetup.orig'; do
640 tmp_=`$i -f 2>/dev/null`
641 if [ $? -eq 0 ]; then
648 dprint 'findNextFreeLoop(): losetup does not recognice option -f, searching next free loop device'
649 for i in `seq 0 100`; do
650 test -e /dev/loop$i || continue
651 losetup /dev/loop$i &>/dev/null
654 2) continue ;; # losetup could not get status of loopdevice (EPERM)
655 0) continue ;; # device exist
656 1) echo "/dev/loop$i"; return 0 ;; # device does not exist and no error
657 ?) continue ;; # return value not available in 'man losetup'
661 # hmm... could not find a loopdevice
662 if [ -z "$message_" ]; then
663 $error_function_ "could not find a free loop device"
665 $error_function_ "$message_"
673 function _initProgName
675 local name_="$1" # program name
677 local tmp_name_=`basename "$name_"` || \
678 logger -p user.alert -i "Init-initProgName: problems executing ( basename \"$name_\" ) ret($?)" >/dev/null
680 secureInput "$tmp_name_"
682 PROG_NAME__=`_initProgName "$0"`
685 function _checkExecutables
688 for i in tr dirname basename id logger kill cat grep route awk ifconfig; do
689 type -p $i &>/dev/null || tmp_="${tmp_}$i "
691 if [ -n "$tmp_" ]; then
692 eprint "Init-checkExecutables: following executables not found or not executable:\n$tmp_"
693 #syslog "Init-checkExecutables: following executables not found or not executable: $tmp_"
699 function _checkBootParam
701 local path_="/proc/cmdline"
702 if [ -e "$path_" ]; then
703 CMD_LINE__=`execute "cat $path_" warnLog`
706 warnLog "$path_ does not exist, thus sh-lib may not work reliable!"
712 function _setDebugLevel
714 local debug_="${DEBUG:-0}"
721 ################################################################################
722 # vim:foldmethod=marker expandtab shiftwidth=2 tabstop=2