added start,stop,restart,reload,force-reload - Service functions
[grml-shlib.git] / sh-lib
diff --git a/sh-lib b/sh-lib
index 437d60c..27e8ff3 100644 (file)
--- a/sh-lib
+++ b/sh-lib
@@ -51,6 +51,9 @@ function unsetVerbose   { VERBOSE_TMP__=$VERBOSE__; VERBOSE__=0; }
 function restoreVerbose { VERBOSE__=$VERBOSE_TMP__; }
 function getVerbose     { echo "$VERBOSE__"; }
 
+function setDebug       { setVerbose "$DPRINT__"; }
+function unsetDebug     { restoreVerbose; }
+
 function setExitFunction    { EXIT_FUNCTION__="$1"; }
 function resetExitFunction  { EXIT_FUNCTION__="_syslog"; }
 # }}}
@@ -428,19 +431,41 @@ function netValidIp
   return $ret_
 }
 
+function netGetIfaces
+{
+  local error_function_=${1:-"eprint"}    # function to call on error
+  local message_="$2"    # user supplied error message
+  local if_=''
+  local ret_=''
+
+  #ip a|grep 'inet ' |awk '$NF !~ /lo/{print $NF}'
+  if_="`ip a|grep 'inet ' |awk '{print $NF}'`"
+  ret_=$?
+  if [ -z "$if_" ]; then
+    if [ -z "$message_" ]; then
+      "$error_function_" "no interfaces found" $ret_
+    else
+      "$error_function_" "$message_" $ret_
+    fi
+    return 1
+  fi
+  dprint "interfaces found" $ret_
+  echo "$if_"
+}
+
 # FIXME
 function netGetDefaultGateway
 {
   local error_function_=${1:-"eprint"}    # function to call on error
   local message_="$2"    # user supplied error message
   
-  local LANG=C
-  local LC_ALL=C
   local ip_=''
   local ret_=''
   
+  setCLang
   ip_=`route -n | awk '/^0\.0\.0\.0/{print $2; exit}'`
   ret_=$?
+  restoreLang
   if [ -z "$ip_" ]; then
     if [ -z "$message_" ]; then
       "$error_function_" "no default gateway found" $ret_
@@ -461,13 +486,13 @@ function netGetNetmask
   local error_function_=${2:-"eprint"}    # function to call on error
   local message_="$3"    # user supplied error message
   
-  local LANG=C
-  local LC_ALL=C
   local nm_=''
   local ret_=''
   
+  setCLang
   nm_=`ifconfig "$iface_" | awk '/[Mm]ask/{FS="[:   ]*"; $0=$0; print $8; exit}'`
   ret_=$?
+  restoreLang
   if [ -z "$nm_" ]; then
     if [ -z "$message_" ]; then
       "$error_function_" "could not find a netmask for \"$iface_\"" $ret_
@@ -488,14 +513,14 @@ function netGetIp
   local error_function_=${2:-"eprint"}    # function to call on error
   local message_="$3"    # user supplied error message
 
-  local LANG=C
-  local LC_ALL=C
   local ip_=""
   local ret_=""
 
+  setCLang
   #ip_=`ip addr list eth0 |mawk '/inet/{split($2,A,"/"); print A[1]}'`
   ip_=`ifconfig "$iface_" | awk '/[Ii]net [Aa]ddr/{FS="[:  ]*"; $0=$0; print $4; exit}'`
   ret_=$?
+  restoreLang
   if [ -z "$ip_" ]; then
     if [ -z "$message_" ]; then
       "$error_function_" "no ip for \"$iface_\" found" $ret_
@@ -522,7 +547,9 @@ function netGetNameservers
     return 1
   fi
   
+  setCLang
   ns_=`awk '/^nameserver/{printf "%s ",$2}' $file_`
+  restoreLang
   if [ -z "$ns_" ]; then
     if [ -z "$message_" ]; then
       "$error_function_" "no nameservers found" $ret_
@@ -538,6 +565,60 @@ function netGetNameservers
 
 # }}}
 
+# SERVICES {{{
+function _touchService
+{
+  local action_="${1:-"start"}"
+  local service_="$2"
+  local error_function_=${3:-"eprint"}    # function to call on error
+
+  local i=""
+  for i in "start" "stop" "restart" "reload" "force-reload"; do
+    if [[ $i == $action_ ]]; then
+      break
+    fi
+    $error_function_ "unknown action: \"$action\""
+    return 1
+  done
+
+  if [ ! -e "$service_" ]; then
+    $error_function_ "service does not exist: \"$service_\""
+    return 1
+  fi
+  if [ ! -x "$service_" ]; then
+    $error_function_ "service is not executable: \"$service_\""
+  fi
+  
+  /etc/init.d/$service_ $action_
+}
+
+function _createServiceFunctions
+{
+  for i in "start" "stop" "restart" "reload" "force-reload"; do
+    eval "\
+function ${i}Service
+{
+  local service_=\"\$1\"
+  local error_function_=\${2:-\"eprint\"}    # function to call on error
+  local message_=\"\$3\"    # user supplied error message
+  
+  local ret_=\"\"
+  _touchService ${i} \"\$service_\"
+  ret_=\$?
+  if [[ \$ret_ != 0 ]]; then
+    if [ -z \"\$message_\" ]; then
+      \"\$error_function_\" \"Problems ${i}ing service \"\$service_\"\" \$ret_
+    else
+      \"\$error_function_\" \"\$message_\" \$ret_
+    fi
+    return 1
+  fi
+}"
+  done
+}
+_createServiceFunctions
+
+# }}}
 
 # prints the next free /dev/loop* to stdout
 function findNextFreeLoop
@@ -620,8 +701,16 @@ function _checkBootParam
   return 1
 }
 _checkBootParam
+
+
+function _setDebugLevel
+{
+  local debug_="${DEBUG:-0}"
+  VERBOSE__="$debug_"
+}
+_checkBootParam
 # }}}
 
 # END OF FILE
 ################################################################################
-# vim:foldmethod=marker
+# vim:foldmethod=marker expandtab shiftwidth=2 tabstop=2