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_"
181 # ATTENTION... THIS FUNCTINOS IS A BIG SECURITY HOLE
182 # this function will be changed in future release
184 # i don't want to write exit status controle 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 allready 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 function stringInFile
376 local to_test_="$1" # matching pattern
377 local source_="$2" # source-file to grep
379 if [ ! -e "$source_" ]; then
380 eprint "stringInFile(): \"$source_\" does not exist"
384 case "$(cat $source_)" in *$to_test_*) return 0;; esac
389 function stringInString
391 local to_test_="$1" # matching pattern
392 local source_="$2" # string to search in
394 case "$source_" in *$to_test_*) return 0;; esac
398 # get value for bootparam given as first param
399 function getBootParam
401 local param_to_search_="$1"
404 stringInString " $param_to_search_=" "$CMD_LINE__" || return 1
405 result_="${CMD_LINE__##*$param_to_search_=}"
406 result_="${result_%%[ ]*}"
411 # Check boot commandline for specified option
412 function checkBootParam
414 stringInString " $1" "$CMD_LINE__"
421 # validates an IP FIXME
424 local ip_="$1" # ip addresse to validate
425 local error_function_=${2:-"eprint"} # function to call on error
426 local message_="$3" # user supplied error message
430 echo "$ip_" | grep -E -q -e '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}.[0-9]{1,3}' \
433 if [ $ret_ -ne 0 ]; then
434 if [ -z "$message_" ]; then
435 "$error_function_" "ip-addresse \"$ip_\" is NOT valied" $ret_
437 "$error_function_" "$message_" $ret_
442 dprint "ip-addresse \"$ip_\" is valied" $ret_
446 function netGetIfaces
448 local error_function_=${1:-"eprint"} # function to call on error
449 local message_="$2" # user supplied error message
453 #ip a|grep 'inet ' |awk '$NF !~ /lo/{print $NF}'
454 if_="`ip a|grep 'inet ' |awk '{print $NF}'`"
456 if [ -z "$if_" ]; then
457 if [ -z "$message_" ]; then
458 "$error_function_" "no interfaces found" $ret_
460 "$error_function_" "$message_" $ret_
464 dprint "interfaces found" $ret_
469 function netGetDefaultGateway
471 local error_function_=${1:-"eprint"} # function to call on error
472 local message_="$2" # user supplied error message
478 ip_=`route -n | awk '/^0\.0\.0\.0/{print $2; exit}'`
481 if [ -z "$ip_" ]; then
482 if [ -z "$message_" ]; then
483 "$error_function_" "no default gateway found" $ret_
485 "$error_function_" "$message_" $ret_
489 dprint "default gateway is \"$ip_\"" $ret_
495 function netGetNetmask
498 local error_function_=${2:-"eprint"} # function to call on error
499 local message_="$3" # user supplied error message
505 nm_=`ifconfig "$iface_" | awk '/[Mm]ask/{FS="[: ]*"; $0=$0; print $8; exit}'`
508 if [ -z "$nm_" ]; then
509 if [ -z "$message_" ]; then
510 "$error_function_" "could not find a netmask for \"$iface_\"" $ret_
512 "$error_function_" "$message_" $ret_
516 dprint "netmask on \"$iface_\" is \"$nm_\"" $ret_
525 local error_function_=${2:-"eprint"} # function to call on error
526 local message_="$3" # user supplied error message
532 #ip_=`ip addr list eth0 |mawk '/inet/{split($2,A,"/"); print A[1]}'`
533 ip_=`ifconfig "$iface_" | awk '/[Ii]net [Aa]ddr/{FS="[: ]*"; $0=$0; print $4; exit}'`
536 if [ -z "$ip_" ]; then
537 if [ -z "$message_" ]; then
538 "$error_function_" "no ip for \"$iface_\" found" $ret_
540 "$error_function_" "$message_" $ret_
544 dprint "addresse for \"$iface_\" is \"$ip_\"" $ret_
549 function netGetNameservers
551 local error_function_=${1:-"eprint"} # function to call on error
552 local message_="$2" # user supplied error message
554 local file_="/etc/resolv.conf"
557 if [ ! -e $file_ ]; then
558 warn "file \"$file_\" does not exist, could not get nameservers"
563 ns_=`awk '/^nameserver/{printf "%s ",$2}' $file_ |xargs echo`
565 if [ -z "$ns_" ]; then
566 if [ -z "$message_" ]; then
567 "$error_function_" "no nameservers found" $ret_
569 "$error_function_" "$message_" $ret_
573 dprint "nameservers: \"$ns_\"" $ret_
581 function _touchService
583 local action_="${1:-"start"}"
585 local error_function_=${3:-"eprint"} # function to call on error
586 local message_="$4" # user supplied error message
589 local known_action_='false'
590 for i in "start" "stop" "restart" "reload" "force-reload"; do
591 if [[ $i == $action_ ]]; then
596 $known_action_ || warn "_touchService(): unknown action \"$action_\""
599 local service_path_=""
600 service_path_="${INITD_DIR__}/$service_"
601 if [ ! -e "$service_path_" ]; then
602 warn "_touchService(): service does not exist: \"$service_\""
605 if [ ! -x "$service_path_" ]; then
606 warn "_touchService(): service is not executable: \"$service_\""
610 "$service_path_" "$action_"
612 if [[ $ret_ != 0 ]]; then
613 if [ -z "$message_" ]; then
614 "$error_function_" "Problems ${action_}ing service \"$service_\"" $ret_
616 "$error_function_" "$message_" $ret_
620 dprint "_touchService(): successfully started service \"$service_\""
624 function _createServiceFunctions
626 for i in "start" "stop" "restart" "reload"; do
627 eval "function ${i}Service { _touchService ${i} \"\$1\" \"\$2\" \"\$3\"; }"
629 eval "function forceReloadService { _touchService force-reload \"\$1\" \"\$2\" \"\$3\"; }"
631 _createServiceFunctions
634 # prints the next free /dev/loop* to stdout
635 function findNextFreeLoop
637 local error_function_=${1:-"eprint"} # function to call on error
638 local message_="$2" # user supplied error message
642 local ret_='' # saved return value
644 for i in 'losetup' 'losetup.orig'; do
645 tmp_=`$i -f 2>/dev/null`
646 if [ $? -eq 0 ]; then
653 dprint 'findNextFreeLoop(): losetup does not recognice option -f, searching next free loop device'
654 for i in `seq 0 100`; do
655 test -e /dev/loop$i || continue
656 losetup /dev/loop$i &>/dev/null
659 2) continue ;; # losetup could not get status of loopdevice (EPERM)
660 0) continue ;; # device exist
661 1) echo "/dev/loop$i"; return 0 ;; # device does not exist and no error
662 ?) continue ;; # return value not available in 'man losetup'
666 # hmm... could not find a loopdevice
667 if [ -z "$message_" ]; then
668 $error_function_ "could not find a free loop device"
670 $error_function_ "$message_"
678 function _initProgName
680 local name_="$1" # program name
682 local tmp_name_=`basename "$name_"` || \
683 logger -p user.alert -i "Init-initProgName: problems executing ( basename \"$name_\" ) ret($?)" >/dev/null
685 secureInput "$tmp_name_"
687 PROG_NAME__=`_initProgName "$0"`
690 function _checkExecutables
693 for i in tr dirname basename id logger kill cat grep route awk ifconfig; do
694 type -p $i &>/dev/null || tmp_="${tmp_}$i "
696 if [ -n "$tmp_" ]; then
697 eprint "Init-checkExecutables: following executables not found or not executable:\n$tmp_"
698 #syslog "Init-checkExecutables: following executables not found or not executable: $tmp_"
704 function _checkBootParam
706 local path_="/proc/cmdline"
707 if [ -e "$path_" ]; then
708 CMD_LINE__=`execute "cat $path_" warnLog`
711 warnLog "$path_ does not exist, thus sh-lib may not work reliable!"
717 function _setDebugLevel
719 local debug_="${DEBUG:-0}"
726 ################################################################################
727 # vim:foldmethod=marker expandtab shiftwidth=2 tabstop=2