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
356 D_=`dirname "$relpath_"`
357 B_=`basename "$relpath_"`
358 abspath_=`cd "$D_" 2>/dev/null && pwd || echo "$D_"`/$B_
359 end_path_=`echo "$abspath_" |tr --squeeze-repeats /`
360 dprint "relToAbs(): \"$relpath_\" => \"$end_path_\""
365 function stringInFile
367 local to_test_="$1" # matching pattern
368 local source_="$2" # source-file to grep
370 if [ ! -e "$source_" ]; then
371 eprint "stringInFile(): \"$source_\" does not exist"
375 case "$(cat $source_)" in *$to_test_*) return 0;; esac
380 function stringInString
382 local to_test_="$1" # matching pattern
383 local source_="$2" # string to search in
385 case "$source_" in *$to_test_*) return 0;; esac
389 # get value for bootparam given as first param
390 function getBootParam
392 local param_to_search_="$1"
395 stringInString " $param_to_search_=" "$CMD_LINE__" || return 1
396 result_="${CMD_LINE__##*$param_to_search_=}"
397 result_="${result_%%[ ]*}"
402 # Check boot commandline for specified option
403 function checkBootParam
405 stringInString " $1" "$CMD_LINE__"
412 # validates an IP FIXME
415 local ip_="$1" # ip addresse to validate
416 local error_function_=${2:-"eprint"} # function to call on error
417 local message_="$3" # user supplied error message
421 echo "$ip_" | grep -E -q -e '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}.[0-9]{1,3}' \
424 if [ $ret_ -ne 0 ]; then
425 if [ -z "$message_" ]; then
426 "$error_function_" "ip-addresse \"$ip_\" is NOT valied" $ret_
428 "$error_function_" "$message_" $ret_
433 dprint "ip-addresse \"$ip_\" is valied" $ret_
437 function netGetIfaces
439 local error_function_=${1:-"eprint"} # function to call on error
440 local message_="$2" # user supplied error message
444 #ip a|grep 'inet ' |awk '$NF !~ /lo/{print $NF}'
445 if_="`ip a|grep 'inet ' |awk '{print $NF}'`"
447 if [ -z "$if_" ]; then
448 if [ -z "$message_" ]; then
449 "$error_function_" "no interfaces found" $ret_
451 "$error_function_" "$message_" $ret_
455 dprint "interfaces found" $ret_
460 function netGetDefaultGateway
462 local error_function_=${1:-"eprint"} # function to call on error
463 local message_="$2" # user supplied error message
469 ip_=`route -n | awk '/^0\.0\.0\.0/{print $2; exit}'`
472 if [ -z "$ip_" ]; then
473 if [ -z "$message_" ]; then
474 "$error_function_" "no default gateway found" $ret_
476 "$error_function_" "$message_" $ret_
480 dprint "default gateway is \"$ip_\"" $ret_
486 function netGetNetmask
489 local error_function_=${2:-"eprint"} # function to call on error
490 local message_="$3" # user supplied error message
496 nm_=`ifconfig "$iface_" | awk '/[Mm]ask/{FS="[: ]*"; $0=$0; print $8; exit}'`
499 if [ -z "$nm_" ]; then
500 if [ -z "$message_" ]; then
501 "$error_function_" "could not find a netmask for \"$iface_\"" $ret_
503 "$error_function_" "$message_" $ret_
507 dprint "netmask on \"$iface_\" is \"$nm_\"" $ret_
516 local error_function_=${2:-"eprint"} # function to call on error
517 local message_="$3" # user supplied error message
523 #ip_=`ip addr list eth0 |mawk '/inet/{split($2,A,"/"); print A[1]}'`
524 ip_=`ifconfig "$iface_" | awk '/[Ii]net [Aa]ddr/{FS="[: ]*"; $0=$0; print $4; exit}'`
527 if [ -z "$ip_" ]; then
528 if [ -z "$message_" ]; then
529 "$error_function_" "no ip for \"$iface_\" found" $ret_
531 "$error_function_" "$message_" $ret_
535 dprint "addresse for \"$iface_\" is \"$ip_\"" $ret_
540 function netGetNameservers
542 local error_function_=${1:-"eprint"} # function to call on error
543 local message_="$2" # user supplied error message
545 local file_="/etc/resolv.conf"
548 if [ ! -e $file_ ]; then
549 warn "file \"$file_\" does not exist, could not get nameservers"
554 ns_=`awk '/^nameserver/{printf "%s ",$2}' $file_`
556 if [ -z "$ns_" ]; then
557 if [ -z "$message_" ]; then
558 "$error_function_" "no nameservers found" $ret_
560 "$error_function_" "$message_" $ret_
564 dprint "nameservers: \"$ns_\"" $ret_
572 function _touchService
574 local action_="${1:-"start"}"
576 local error_function_=${3:-"eprint"} # function to call on error
579 for i in "start" "stop" "restart" "reload" "force-reload"; do
580 if [[ $i == $action_ ]]; then
583 $error_function_ "unknown action: \"$action\""
587 service_="${INITD_DIR__}/$service_"
588 if [ ! -e "$service_" ]; then
589 $error_function_ "service does not exist: \"$service_\""
592 if [ ! -x "$service_" ]; then
593 $error_function_ "service is not executable: \"$service_\""
596 "$service_" "$action_"
599 function _createServiceFunctions
601 for i in "start" "stop" "restart" "reload" "force-reload"; do
605 local service_=\"\$1\"
606 local error_function_=\${2:-\"eprint\"} # function to call on error
607 local message_=\"\$3\" # user supplied error message
610 _touchService ${i} \"\$service_\"
612 if [[ \$ret_ != 0 ]]; then
613 if [ -z \"\$message_\" ]; then
614 \"\$error_function_\" \"Problems ${i}ing service \"\$service_\"\" \$ret_
616 \"\$error_function_\" \"\$message_\" \$ret_
623 _createServiceFunctions
626 # prints the next free /dev/loop* to stdout
627 function findNextFreeLoop
629 local error_function_=${1:-"eprint"} # function to call on error
630 local message_="$2" # user supplied error message
634 local ret_='' # saved return value
636 for i in 'losetup' 'losetup.orig'; do
637 tmp_=`$i -f 2>/dev/null`
638 if [ $? -eq 0 ]; then
645 dprint 'findNextFreeLoop(): losetup does not recognice option -f, searching next free loop device'
646 for i in `seq 0 100`; do
647 test -e /dev/loop$i || continue
648 losetup /dev/loop$i &>/dev/null
651 2) continue ;; # losetup could not get status of loopdevice (EPERM)
652 0) continue ;; # device exist
653 1) echo "/dev/loop$i"; return 0 ;; # device does not exist and no error
654 ?) continue ;; # return value not available in 'man losetup'
658 # hmm... could not find a loopdevice
659 if [ -z "$message_" ]; then
660 $error_function_ "could not find a free loop device"
662 $error_function_ "$message_"
670 function _initProgName
672 local name_="$1" # program name
674 local tmp_name_=`basename "$name_"` || \
675 logger -p user.alert -i "Init-initProgName: problems executing ( basename \"$name_\" ) ret($?)" >/dev/null
677 secureInput "$tmp_name_"
679 PROG_NAME__=`_initProgName "$0"`
682 function _checkExecutables
685 for i in tr dirname basename id logger kill cat grep route awk ifconfig; do
686 type -p $i &>/dev/null || tmp_="${tmp_}$i "
688 if [ -n "$tmp_" ]; then
689 eprint "Init-checkExecutables: following executables not found or not executable:\n$tmp_"
690 #syslog "Init-checkExecutables: following executables not found or not executable: $tmp_"
696 function _checkBootParam
698 local path_="/proc/cmdline"
699 if [ -e "$path_" ]; then
700 CMD_LINE__=`execute "cat $path_" warnLog`
703 warnLog "$path_ does not exist, thus sh-lib may not work reliable!"
709 function _setDebugLevel
711 local debug_="${DEBUG:-0}"
718 ################################################################################
719 # vim:foldmethod=marker expandtab shiftwidth=2 tabstop=2