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 ################################################################################
13 # FIXME maybe PROG_PATH__ for better error reporting?
14 PROG_NAME__="" # initialised within init section
16 # directory for init scripts
17 INITD_DIR__="/etc/init.d/"
19 # >= level and the function will print the message
20 EPRINT__=1 # eprint (error print)
21 EEPRINT__=2 # 2print (intern error print)
22 DPRINT__=3 # dprint (debug print)
24 EXIT_FUNCTION__="_syslog" # function to call upon die (can be set by user)
28 CMD_LINE__="" # /proc/cmdline
32 LANGUAGE__="$LANGUAGE"
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"; LANGUAGE__="$LANGUAGE"; }
45 function restoreLang { LANG="$LANG__"; LC_ALL="$LC_ALL__"; LANGUAGE="$LANGUAGE__"; }
46 function setCLang { saveLang; LANG="C"; LC_ALL="C"; LANGUAGE="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_"
181 # ATTENTION... THIS FUNCTION IS A BIG SECURITY HOLE
182 # this function will be changed in future release
184 # I don't want to write exit status control stuff every time
187 local to_exec_="$1" # command to execute
188 local error_function_=${2:-"eprint"} # function to call on error
189 local message_="$3" # user supplied error message
197 if [ $ret_ -eq 127 ]; then
198 syslog "problems executing ( $to_exec_ )" $ret_
200 if [ $ret_ -ne 0 ]; then
201 if [ -z "$message_" ]; then
202 $error_function_ "problems executing ( $to_exec_ )" "$ret_"
204 $error_function_ "$message_" "$ret_"
207 dprint "exec-$error_function_: ( $to_exec_ ) ret($ret_)"
211 function silentExecute
227 # if the file DOES exist, everything is fine
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 not exist \"$file_to_test_\"" 66
238 $error_function_ "$message_"
242 dprint "isExistent(): file \"$1\" does exist => ready to go"
246 function isNotExistent
248 local file_to_test_="$1" # file to test
249 local error_function_=${2:-"eprint"} # function to call on error
250 local message_="$3" # user supplied error message
252 if [ -e "$file_to_test_" ]; then
253 if [ -z "$message_" ]; then
254 $error_function_ "file does already exist \"$file_to_test_\"" 67
256 $error_function_ "$message_"
260 dprint "isNotExistent(): file \"$1\" does not exist => ready to go"
267 local to_check_="$1" # username to check against running process
268 local error_function_=${2:-"eprint"} # function to call on error
269 local message_="$3" # user supplied error message
274 if [ $user_ != "$to_check_" ]; then
275 if [ -z "$message_" ]; then
276 $error_function_ "username \"$user_\" is not \"$to_check_\"" 77 $exit_function_
278 $error_function_ "$message_"
282 dprint "checkUser(): accepted, username matches \"$to_check_\""
289 local to_check_="$1" # user-id to check against running process
290 local error_function_=${2:-"eprint"} # function to call on error
291 local message_="$3" # user supplied error message
296 if [ $user_id_ != "$to_check_" ]; then
297 if [ -z "$message_" ]; then
298 $error_function_ "UID \"$user_id_\" is not \"$to_check_\"" 77
300 $error_function_ "$message_"
304 dprint "checkId(): accepted, UID matches \"$to_check_\""
316 if [ -f /etc/grml_version ] ; then
317 dprint "isGrml(): this seems to be a grml system"
320 dprint "isGrml(): this is not a grml system"
327 if [ -e "/etc/grml_cd" ]; then
328 dprint "runsFromHd(): grml is on CD"
331 dprint "runsFromHd(): grml is on HD"
338 if [ -e "/etc/grml_cd" ]; then
339 dprint "runsFromCd(): grml is on CD"
342 dprint "runsFromCd(): grml is on HD"
348 # secure input from console
351 local to_secure_="$1"
355 secured_=`echo -n "$to_secure_" |tr -c '[:alnum:]/.\-,\(\)' '_'`
356 dprint "secureInput(): \"$to_secure_\" => \"$secured_\""
361 # convert all possible path formats to absolute paths
367 abspath_="`readlink -f \"$relpath_\"`" || \
368 warn "relToAbs(): Problems getting absolute path" "$?" || return 1
369 dprint "relToAbs(): \"$relpath_\" => \"$abspath_\""
374 # Trim off white-space characters
375 # white-space in the "C" and "POSIX" locales are:
379 # carriage return ('\r')
380 # horizontal tab ('\t')
381 # vertical tab ('\v')
387 result_="`echo "$str_" | sed -e 's/^\s*//' -e 's/\s*$//'`" || \
388 warn "stringTrim(): Problems stripping of blanks" || return 1
389 dprint "stringTrim(): \"$str_\" => \"$result_\""
394 function stringInFile
396 local to_test_="$1" # matching pattern
397 local source_="$2" # source-file to grep
399 if [ ! -e "$source_" ]; then
400 eprint "stringInFile(): \"$source_\" does not exist"
404 case "$(cat $source_)" in *$to_test_*) return 0;; esac
409 function stringInString
411 local to_test_="$1" # matching pattern
412 local source_="$2" # string to search in
414 case "$source_" in *$to_test_*) return 0;; esac
418 # get value for bootparam given as first param
419 function getBootParam
421 local param_to_search_="$1"
424 stringInString " $param_to_search_=" "$CMD_LINE__" || return 1
425 result_="${CMD_LINE__##*$param_to_search_=}"
426 result_="${result_%%[ ]*}"
431 # Check boot commandline for specified option
432 function checkBootParam
434 stringInString " $1" "$CMD_LINE__"
441 # validates an IP FIXME
444 local ip_="$1" # ip address to validate
445 local error_function_=${2:-"eprint"} # function to call on error
446 local message_="$3" # user supplied error message
450 echo "$ip_" | grep -E -q -e '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}.[0-9]{1,3}' \
453 if [ $ret_ -ne 0 ]; then
454 if [ -z "$message_" ]; then
455 "$error_function_" "ip-address \"$ip_\" is NOT valid" $ret_
457 "$error_function_" "$message_" $ret_
462 dprint "ip-address \"$ip_\" is valid" $ret_
466 function netGetIfaces
468 local error_function_=${1:-"eprint"} # function to call on error
469 local message_="$2" # user supplied error message
473 #ip a|grep 'inet ' |awk '$NF !~ /lo/{print $NF}'
474 if_="`ip a|grep 'inet ' |awk '{print $NF}'`"
476 if [ -z "$if_" ]; then
477 if [ -z "$message_" ]; then
478 "$error_function_" "no interfaces found" $ret_
480 "$error_function_" "$message_" $ret_
484 dprint "interfaces found" $ret_
489 function netGetDefaultGateway
491 local error_function_=${1:-"eprint"} # function to call on error
492 local message_="$2" # user supplied error message
498 ip_=`route -n | awk '/^0\.0\.0\.0/{print $2; exit}'`
501 if [ -z "$ip_" ]; then
502 if [ -z "$message_" ]; then
503 "$error_function_" "no default gateway found" $ret_
505 "$error_function_" "$message_" $ret_
509 dprint "default gateway is \"$ip_\"" $ret_
515 function netGetNetmask
518 local error_function_=${2:-"eprint"} # function to call on error
519 local message_="$3" # user supplied error message
525 nm_=`ifconfig "$iface_" | awk '/[Mm]ask/{FS="[: ]*"; $0=$0; print $8; exit}'`
528 if [ -z "$nm_" ]; then
529 if [ -z "$message_" ]; then
530 "$error_function_" "could not find a netmask for \"$iface_\"" $ret_
532 "$error_function_" "$message_" $ret_
536 dprint "netmask on \"$iface_\" is \"$nm_\"" $ret_
545 local error_function_=${2:-"eprint"} # function to call on error
546 local message_="$3" # user supplied error message
552 #ip_=`ip addr list eth0 |mawk '/inet/{split($2,A,"/"); print A[1]}'`
553 ip_=`ifconfig "$iface_" | awk '/[Ii]net [Aa]ddr/{FS="[: ]*"; $0=$0; print $4; exit}'`
556 if [ -z "$ip_" ]; then
557 if [ -z "$message_" ]; then
558 "$error_function_" "no ip for \"$iface_\" found" $ret_
560 "$error_function_" "$message_" $ret_
564 dprint "address for \"$iface_\" is \"$ip_\"" $ret_
569 function netGetNameservers
571 local error_function_=${1:-"eprint"} # function to call on error
572 local message_="$2" # user supplied error message
574 local file_="/etc/resolv.conf"
577 if [ ! -e $file_ ]; then
578 warn "file \"$file_\" does not exist, could not get nameservers"
583 ns_=`awk '/^nameserver/{printf "%s ",$2}' $file_ |xargs echo`
585 if [ -z "$ns_" ]; then
586 if [ -z "$message_" ]; then
587 "$error_function_" "no nameservers found" $ret_
589 "$error_function_" "$message_" $ret_
593 dprint "nameservers: \"$ns_\"" $ret_
601 function _touchService
603 local action_="${1:-"start"}"
605 local error_function_=${3:-"eprint"} # function to call on error
606 local message_="$4" # user supplied error message
609 local known_action_='false'
610 for i in "start" "stop" "restart" "reload" "force-reload"; do
611 if [[ $i == $action_ ]]; then
616 $known_action_ || warn "_touchService(): unknown action \"$action_\""
619 local service_path_=""
620 service_path_="${INITD_DIR__}/$service_"
621 if [ ! -e "$service_path_" ]; then
622 warn "_touchService(): service does not exist: \"$service_\""
625 if [ ! -x "$service_path_" ]; then
626 warn "_touchService(): service is not executable: \"$service_\""
630 "$service_path_" "$action_"
632 if [[ $ret_ != 0 ]]; then
633 if [ -z "$message_" ]; then
634 "$error_function_" "Problems ${action_}ing service \"$service_\"" $ret_
636 "$error_function_" "$message_" $ret_
640 dprint "_touchService(): successfully started service \"$service_\""
644 function _createServiceFunctions
646 for i in "start" "stop" "restart" "reload"; do
647 eval "function ${i}Service { _touchService ${i} \"\$1\" \"\$2\" \"\$3\"; }"
649 eval "function forceReloadService { _touchService force-reload \"\$1\" \"\$2\" \"\$3\"; }"
651 _createServiceFunctions
654 # prints the next free /dev/loop* to stdout
655 function findNextFreeLoop
657 local error_function_=${1:-"eprint"} # function to call on error
658 local message_="$2" # user supplied error message
662 local ret_='' # saved return value
664 for i in 'losetup' 'losetup.orig'; do
665 tmp_=`$i -f 2>/dev/null`
666 if [ $? -eq 0 ]; then
673 dprint 'findNextFreeLoop(): losetup does not recognice option -f, searching next free loop device'
674 for i in `seq 0 100`; do
675 test -e /dev/loop$i || continue
676 losetup /dev/loop$i &>/dev/null
679 2) continue ;; # losetup could not get status of loopdevice (EPERM)
680 0) continue ;; # device exist
681 1) echo "/dev/loop$i"; return 0 ;; # device does not exist and no error
682 ?) continue ;; # return value not available in 'man losetup'
686 # hmm... could not find a loopdevice
687 if [ -z "$message_" ]; then
688 $error_function_ "could not find a free loop device"
690 $error_function_ "$message_"
698 function _initProgName
700 local name_="$1" # program name
702 local tmp_name_=`basename "$name_"` || \
703 logger -p user.alert -i "Init-initProgName: problems executing ( basename \"$name_\" ) ret($?)" >/dev/null
705 secureInput "$tmp_name_"
707 PROG_NAME__=`_initProgName "$0"`
710 function _checkExecutables
713 for i in tr dirname basename id logger kill cat grep route awk ifconfig; do
714 type -p $i &>/dev/null || tmp_="${tmp_}$i "
716 if [ -n "$tmp_" ]; then
717 eprint "Init-checkExecutables: following executables not found or not executable:\n$tmp_"
718 #syslog "Init-checkExecutables: following executables not found or not executable: $tmp_"
724 function _checkBootParam
726 local path_="/proc/cmdline"
727 if [ -e "$path_" ]; then
728 CMD_LINE__=`execute "cat $path_" warnLog`
731 warnLog "$path_ does not exist, thus sh-lib may not work reliable!"
737 function _setDebugLevel
739 local debug_="${DEBUG:-0}"
746 ################################################################################
747 # vim:foldmethod=marker expandtab shiftwidth=2 tabstop=2