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_\""
312 if [ -e "/etc/grml_cd" ]; then
313 dprint "runsFromHd(): grml is on CD"
316 dprint "runsFromHd(): grml is on HD"
323 if [ -e "/etc/grml_cd" ]; then
324 dprint "runsFromCd(): grml is on CD"
327 dprint "runsFromCd(): grml is on HD"
333 # secure input from console
336 local to_secure_="$1"
340 secured_=`echo -n "$to_secure_" |tr -c '[:alnum:]/.\-,\(\)' '_'`
341 dprint "secureInput(): \"$to_secure_\" => \"$secured_\""
346 # convert all possible path formats to absolute paths
352 abspath_="`readlink -f \"$relpath_\"`" || \
353 warn "relToAbs(): Problems getting absolute path" "$?" || return 1
354 dprint "relToAbs(): \"$relpath_\" => \"$abspath_\""
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 function _touchService
568 local action_="${1:-"start"}"
570 local error_function_=${3:-"eprint"} # function to call on error
571 local message_="$4" # user supplied error message
574 local known_action_='false'
575 for i in "start" "stop" "restart" "reload" "force-reload"; do
576 if [[ $i == $action_ ]]; then
581 $known_action_ || warn "_touchService(): unknown action \"$action_\""
584 local service_path_=""
585 service_path_="${INITD_DIR__}/$service_"
586 if [ ! -e "$service_path_" ]; then
587 warn "_touchService(): service does not exist: \"$service_\""
590 if [ ! -x "$service_path_" ]; then
591 warn "_touchService(): service is not executable: \"$service_\""
595 "$service_path_" "$action_"
597 if [[ $ret_ != 0 ]]; then
598 if [ -z "$message_" ]; then
599 "$error_function_" "Problems ${action_}ing service \"$service_\"" $ret_
601 "$error_function_" "$message_" $ret_
605 dprint "_touchService(): successfully started service \"$service_\""
609 function _createServiceFunctions
611 for i in "start" "stop" "restart" "reload" "force-reload"; do
612 eval "function ${i}Service { _touchService ${i} \"\$1\" \"\$2\" \"\$3\"; }"
615 _createServiceFunctions
618 # prints the next free /dev/loop* to stdout
619 function findNextFreeLoop
621 local error_function_=${1:-"eprint"} # function to call on error
622 local message_="$2" # user supplied error message
626 local ret_='' # saved return value
628 for i in 'losetup' 'losetup.orig'; do
629 tmp_=`$i -f 2>/dev/null`
630 if [ $? -eq 0 ]; then
637 dprint 'findNextFreeLoop(): losetup does not recognice option -f, searching next free loop device'
638 for i in `seq 0 100`; do
639 test -e /dev/loop$i || continue
640 losetup /dev/loop$i &>/dev/null
643 2) continue ;; # losetup could not get status of loopdevice (EPERM)
644 0) continue ;; # device exist
645 1) echo "/dev/loop$i"; return 0 ;; # device does not exist and no error
646 ?) continue ;; # return value not available in 'man losetup'
650 # hmm... could not find a loopdevice
651 if [ -z "$message_" ]; then
652 $error_function_ "could not find a free loop device"
654 $error_function_ "$message_"
662 function _initProgName
664 local name_="$1" # program name
666 local tmp_name_=`basename "$name_"` || \
667 logger -p user.alert -i "Init-initProgName: problems executing ( basename \"$name_\" ) ret($?)" >/dev/null
669 secureInput "$tmp_name_"
671 PROG_NAME__=`_initProgName "$0"`
674 function _checkExecutables
677 for i in tr dirname basename id logger kill cat grep route awk ifconfig; do
678 type -p $i &>/dev/null || tmp_="${tmp_}$i "
680 if [ -n "$tmp_" ]; then
681 eprint "Init-checkExecutables: following executables not found or not executable:\n$tmp_"
682 #syslog "Init-checkExecutables: following executables not found or not executable: $tmp_"
688 function _checkBootParam
690 local path_="/proc/cmdline"
691 if [ -e "$path_" ]; then
692 CMD_LINE__=`execute "cat $path_" warnLog`
695 warnLog "$path_ does not exist, thus sh-lib may not work reliable!"
701 function _setDebugLevel
703 local debug_="${DEBUG:-0}"
710 ################################################################################
711 # vim:foldmethod=marker expandtab shiftwidth=2 tabstop=2