s!/etc/slackware-release!/etc/slackware-version! (reported by bubulak)
[util-vserver.git] / util-vserver / scripts / functions
index 44afc5a..32afcd3 100644 (file)
@@ -21,6 +21,7 @@ _VS_ERRFILE=
 _VS_NEWLINE='
 '
 declare -r _VS_NEWLINE=${_VS_NEWLINE:0-1}
+declare -r VS_ALLVSERVERS_ARGS=all,marked,unmarked,stopped,running
 
 function findObject
 {
@@ -31,11 +32,11 @@ function findObject
     shift 2
     
     for _fo_i; do
-       test "$_fo_i"            || continue
+       test -n "$_fo_i"         || continue
        test ! $_fo_mod "$_fo_i" || { _fo_file=$_fo_i; break; }
     done
 
-    test -z "$_fo_i" -o "$_fo_file" || {
+    test -z "$_fo_i" -o -n "$_fo_file" || {
        echo "Can not find file for '$_fo_var'; aborting"
        exit 1
     } >&2
@@ -62,19 +63,54 @@ function findAndCopy
     shift
     findFile tmp "$@"
 
-    test "$tmp" -a -s "$tmp" || return 0
+    test -n "$tmp" -a -s "$tmp" || return 0
     $_CP -af "$tmp" "$dst"
 }
 
+## Usage: isRegularFile <filename> [<mod>]
+function isRegularFile
+{
+    test ${2:--f} "$1" || return 1
+
+    case $1 in
+       (*.rpmsave|*.rpmnew|*.rpmorig|*.cfsaved*|*.~*~) return 1;;
+    esac
+
+    return 0
+}
+
 function getPhysicalDir
 {
     ( set -P && cd "$1" && pwd )
 }
 
+## Usage:: isDirectoryEmpty <dir> [<ignore-lost+found>]
+function isDirectoryEmpty
+{
+    (
+       shopt -s dotglob  || *
+       shopt -s nullglob || *
+
+       ignore_lostfound=1
+       test -z "$2" -o "$2" = 0 || ignore_lostfound=
+       
+       for i in "$1"/*; do
+           case "$i" in
+               ($1/lost+found)
+                   test "$ignore_lostfound" || exit 1
+                   ;;
+               (*)     echo "$i"; exit 1
+           esac
+        done
+
+       exit 0
+    )
+}
+
 ## Usage: logging <message>
 function logging
 {
-    if test "$_VS_LOGFILE"; then
+    if test -n "$_VS_LOGFILE"; then
        echo "$@" >>"$_VS_LOGFILE"
     else
        echo "$@"
@@ -84,7 +120,7 @@ function logging
 ## Usage: warning <message>
 function warning
 {
-    if test "$_VS_ERRFILE"; then
+    if test -n "$_VS_ERRFILE"; then
        echo "$@" >>"$_VS_ERRFILE"
     else
        echo "$@" >&2
@@ -94,7 +130,7 @@ function warning
 ## Usage: panic <message>
 function panic
 {
-    if test "$_VS_ERRFILE"; then
+    if test -n "$_VS_ERRFILE"; then
        echo "$@" >>"$_VS_ERRFILE"
     else
        echo "$@" >&2
@@ -111,14 +147,38 @@ function execute
     exit 0
 }
 
+
+## Usage: spawn <message>
+function spawn
+{
+    test -z "${DEBUG_EXEC:-}"       || echo  "$@"
+    test "${DEBUG_EXEC:-}" = noexec || "$@"
+}
+
 ## Usage: isNumber <arg>
 function isNumber
 {
     local tmp
-    let tmp=$1+0 2>/dev/null || test -z "${1//0/}" -a "$1" || return 1
+    let tmp=$1+0 2>/dev/null || test -z "${1//0/}" -a -n "$1" || return 1
     return 0
 }
 
+## Usage: hasSubstring <haystack> <needle>+
+function hasSubstring
+{
+    local pat=$1
+    local i
+    
+    shift
+
+    for i; do
+       test x"${pat/*$i*/$i}" = x"$i" || continue
+       return 0
+    done
+
+    return 1
+}
+
 ## Usage: colorize <style> <command>
 function colorize
 {
@@ -129,8 +189,8 @@ function colorize
        "$@"
     else
        local   cfile
-       findFile cfile "$CONFDIR"/.defaults/styles/"$style" ''
-       if test "$cfile"; then
+       findFile cfile "$__CONFDIR"/.defaults/styles/"$style" ''
+       if test -n "$cfile"; then
          $_CAT "$cfile"
        else
          case "$style" in
@@ -143,11 +203,34 @@ function colorize
          esac
        fi
            
-       "$@"
+       ( "$@" )
        echo -ne "\e[m"
     fi
 }
 
+function colpanic
+{
+    if test -n "$_VS_ERRFILE"; then
+       echo "$@" >>"$_VS_ERRFILE"
+    else
+       colorize error echo -n "$@" >&2
+    fi
+    echo
+
+    exit 1
+}
+
+function colwarning
+{
+    colorize warning warning "$@"
+}
+
+function colinfo
+{
+    colorize info    echo "$@"
+}
+
+
 ## Usage: xtermTitle <title>
 function xtermTitle
 {
@@ -212,14 +295,14 @@ function _pkgSetVarsBase
            fi
            ;;
         *)
-           BASEDIR=$CONFDIR/$vserver
+           BASEDIR=$__CONFDIR/$vserver
            test -d "$BASEDIR" || {
                echo "Can not find configuration-directory"
                exit 1
            } >&2
            
            VDIR=$BASEDIR/vdir
-           test -d "$VDIR"   || VDIR=$DEFAULT_VSERVERDIR/$vserver
+           test -d "$VDIR"   || VDIR=$__DEFAULT_VSERVERDIR/$vserver
            VDIR=$(getPhysicalDir "$VDIR")
            
            PKGDIR=$BASEDIR/apps/pkgmgmt
@@ -242,7 +325,7 @@ function _pkgSetVarsBase
 
 function _pkgSetVarsRPM
 {
-    if test "$PKGDIR"; then
+    if test -n "$PKGDIR"; then
        findDir RPMETCDIR    $PKGDIR/rpmetc      $PKGDIR/base/rpm/etc       /etc/rpm
        findDir RPMSTATEDIR  $PKGDIR/rpmstate    $PKGDIR/base/rpm/state
 
@@ -260,7 +343,7 @@ function _pkgSetVarsRPM
 
 function _pkgSetVarsApt
 {
-    if test "$PKGDIR"; then
+    if test -n "$PKGDIR"; then
        findDir APTETCDIR    $PKGDIR/aptetc      $PKGDIR/base/apt/etc       /etc/apt
        findDir APTSTATEDIR  $PKGDIR/aptstate    $PKGDIR/base/apt/state
        findDir APTCACHEDIR  $PKGDIR/aptcache    $PKGDIR/base/apt/cache
@@ -278,7 +361,7 @@ function _pkgSetVarsApt
 
 function _pkgSetVarsYum
 {
-    if test "$PKGDIR"; then
+    if test -n "$PKGDIR"; then
        findDir YUMETCDIR    $PKGDIR/yumetc      $PKGDIR/base/yum/etc       /etc
        findDir YUMCACHEDIR  $PKGDIR/yumcache    $PKGDIR/base/yum/cache
     else
@@ -321,7 +404,7 @@ function _pkgMountRPM
 
 function _pkgSetEnvBase
 {
-    test "$EXECDIR"   = "/" || {
+    test "$EXECDIR" = "/" || {
        PATH=$EXECDIR:$PATH
        LD_LIBRARY_PATH=$EXECDIR${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
     }
@@ -393,29 +476,57 @@ function isAvoidNamespace
     $_VSERVER_INFO - FEATURE namespace   || return 0
     cfgdir=$($_VSERVER_INFO "$1" CFGDIR) || return 0
     test ! -e "$cfgdir"/namespace        || return 1
-    test -e "$CONFDIR"/.defaults/nonamespace -o \
+    test -e "$__CONFDIR"/.defaults/nonamespace -o \
          -e "$cfgdir"/nonamespace
 }
 
+## Usage: getAllVservers <var> [<KIND>*]
 function getAllVservers
 {
     local _ga_i
     declare -a _ga_tmp=()
 
-    for _ga_i in $CONFDIR/*; do
-       test   -d "$_ga_i"          || continue
+    for _ga_i in $__CONFDIR/*; do
+       isRegularFile "$_ga_i" -d   || continue
+
        test ! -e "$_ga_i"/disabled || continue
        test   -d "$_ga_i"/vdir     || continue
-       case "$_ga_i" in
-           *.~*~) continue;;
+
+       local _ga_doadd=1
+       local _ga_markfile=$_ga_i/apps/init/mark
+       
+       case ${2:-ALL} in
+           (MARKED)    test   -s "$_ga_markfile" || _ga_doadd=;;
+           (UNMARKED)  test ! -s "$_ga_markfile" || _ga_doadd=;;
+           (STOPPED)   ! $_VSERVER "$_ga_i" running &>/dev/null || _ga_doadd=;;
+           (RUNNING)     $_VSERVER "$_ga_i" running &>/dev/null || _ga_doadd=;;
+           (ALL)       ;;
+           (*)         panic $"Unknown vserver tagging '$2'";;
        esac
 
-       _ga_tmp=( "${_ga_tmp[@]}" "${_ga_i##$CONFDIR/}")
+       test -z "$_ga_doadd" || _ga_tmp=( "${_ga_tmp[@]}" "${_ga_i##$__CONFDIR/}")
     done
 
     eval $1='( "${_ga_tmp[@]}" )'
 }
 
+## Usage: getAllVserversByArg <var> <arg>
+function getAllVserversByArg
+{
+    local _gav_mark=
+    
+    case $2 in
+       (--all)         _gav_mark=ALL;;
+       (--marked)      _gav_mark=MARKED;;
+       (--unmarked)    _gav_mark=UNMARKED;;
+       (--stopped)     _gav_mark=STOPPED;;
+       (--running)     _gav_mark=RUNNING;;
+       (*)             return 1;;
+    esac
+
+    getAllVservers "$1" "$_gav_mark"
+}
+
 ## Usage: _getProcNumberCount <ctx> <var>
 function _getProcNumberCount
 {
@@ -444,7 +555,7 @@ function getVserverStatus
     read _gvs_ctx <"$1"/run
     eval "$2"=\$_gvs_ctx
 
-    test "$3"        || return 0
+    test -n "$3"     || return 0
     local _gvs_tmp
     _getProcNumberCount "$_gvs_ctx" _gvs_tmp
     eval "$3"=\$_gvs_tmp
@@ -472,7 +583,7 @@ function isVserverRunning
     local _ivr_ctx _ivr_procnum
 
     getVserverStatus "$1" _ivr_ctx _ivr_procnum 1 || return 1
-    test $_ivr_procnum != 0                       || return 1
+    test "$_ivr_procnum" != 0                     || return 1
     test -z "$2" || eval "$2"=\$_ivr_ctx
     return 0
 }
@@ -485,7 +596,7 @@ function getFileValue
     shift
 
     findFile _gfv_file "$@" ''
-    test "$_gfv_file" -a -r "$_gfv_file" || return 0
+    test -n "$_gfv_file" -a -r "$_gfv_file" || return 0
     eval read "$_gfv_var" <"$_gfv_file"
 }
 
@@ -511,8 +622,8 @@ function checkComponents
        case "$i" in
            (core)      test -x "$_CHBIND"           || failed=1;;
            (build)     test -x "$_VSERVER_BUILD"    || failed=1;;
-           (sysv)      test -x "$INITRDDIR/vserver" || failed=1;;
-           (devel)     test -d "$INCLUDEDIR/vserver.h" || failed=1;;
+           (sysv)      test -x "$__INITRDDIR/vserver"    || failed=1;;
+           (devel)     test -d "$__INCLUDEDIR/vserver.h" || failed=1;;
            (*)         echo "Unknown component '$i'" >&2
                        return false
                        ;;
@@ -530,7 +641,8 @@ function checkComponents
 ## Usage: isKernelAPI <ver> [<cmp-modifier>]
 function isKernelAPI
 {
-    local api=$($_VSERVER_INFO - APIVER) || api=0
+    local api
+    api=$($_VSERVER_INFO - APIVER) || api=0
     test $[ $api ] -${2:-ge} $[ $1 ]
 }
 
@@ -543,7 +655,7 @@ function callInNamespace
     ctx=$( $_VSERVER_INFO "$1" CONTEXT f ) || ctx=
 
     shift
-    if test "$ctx"; then
+    if test -n "$ctx"; then
        $_VNAMESPACE --enter "$ctx" -- "$@"
     else
        "$@"
@@ -558,23 +670,36 @@ function setDefaultTTY
     cfgdir=$($_VSERVER_INFO "$1" APPDIR init) || cfgdir=
     findObject -e ttyname \
        ${cfgdir:+"$cfgdir"/tty} \
-       "$CONFDIR/.defaults/apps/init/tty" \
+       "$__CONFDIR/.defaults/apps/init/tty" \
        $2 /dev/null
 
     exec   <$ttyname
     exec  &>$ttyname
 }
 
+## Usage: killContext <XID> [<SIG>]
+function killContext
+{
+    local sig=${2:-9}
+
+    #$_VKILL -s STOP   --xid "$1" 1 &>/dev/null || :
+    $_VKILL -s "$sig" --xid "$1" 1 &>/dev/null || :
+    $_VKILL -s "$sig" --xid "$1"   &>/dev/null || :
+    #$_VKILL -s "$sig" --xid "$1" 1 &>/dev/null || :
+    #$_VKILL -s CONT   --xid "$1" 1 &>/dev/null || :
+}
+
 ## Usage: pkgmgmt.guessStyle <vserver> <resultvar>
 function pkgmgmt.guessStyle()
 {
-    local _pgs_vdir=$($_VSERVER_INFO "$1" VDIR) || {
+    local _pgs_vdir
+    _pgs_vdir=$($_VSERVER_INFO "$1" VDIR) || {
        echo $"Can not determine vserver-root" >&2
        return 1
     }
     local _pgs_cfgdir=$($_VSERVER_INFO "$1" APPDIR pkgmgmt) || :
 
-    if test "$_pgs_cfgdir" -a -e "$_pgs_cfgdir"/style; then
+    if test -n "$_pgs_cfgdir" -a -e "$_pgs_cfgdir"/style; then
        read style <"$_pgs_cfgdir"/style
     elif test -e "$_pgs_vdir"/etc/redhat-release -o -e "$_pgs_vdir"/etc/fedora-release; then
        style=redhat
@@ -613,7 +738,7 @@ function pkgmgmt.isAptAvailable
     local is_internal="$3"
     
     local have_apt i
-    if test "$is_internal"; then
+    if test -n "$is_internal"; then
        have_apt=1
        test -d "$cfgdir"/base/apt -o -d "$cfgdir"/aptetc || have_apt=
     else
@@ -623,7 +748,7 @@ function pkgmgmt.isAptAvailable
        done
     fi
 
-    test "$have_apt" && return 0 || return 1
+    test -n "$have_apt" && return 0 || return 1
 }
 
 ## Usage: pkgmgmt.isYumAvailable <cfgdir> <vdir> [<is-internal>]
@@ -634,7 +759,7 @@ function pkgmgmt.isYumAvailable
     local is_internal="$3"
     
     local have_yum i
-    if test "$is_internal"; then
+    if test -n "$is_internal"; then
        have_yum=1
        test -d "$cfgdir"/base/yum -o -d "$cfgdir"/yumetc || have_yum=
     else
@@ -644,7 +769,7 @@ function pkgmgmt.isYumAvailable
        done
     fi
 
-    test "$have_yum" && return 0 || return 1
+    test -n "$have_yum" && return 0 || return 1
 }
 
 
@@ -668,7 +793,7 @@ function vshelper.doSanityCheck
     if ! test -r "$proc_file"; then
        vshelper=
        warnings=( "${warnings[@]}"
-                  $"File '$proc_file' does not exists but is required for vshelper setup" )
+                  $"File '$proc_file' does not exist but is required for vshelper setup" )
         solution_disable=1
     else
        vshelper=$(cat "$proc_file")
@@ -683,10 +808,10 @@ function vshelper.doSanityCheck
        }
     fi
 
-    test -d "$VSHELPERSTATEDIR" || {
+    test -d "$__VSHELPERSTATEDIR" || {
        warnings=( "${warnings[@]}"
                   $"\
-The vshelper state-directory '$VSHELPERSTATEDIR' does not exist; since
+The vshelper state-directory '$__VSHELPERSTATEDIR' does not exist; since
 it is created by 'make install', this indicates a serious problem with
 your util-vserver installation" )
        solution_disable=1
@@ -707,13 +832,13 @@ To fix this, you can:"
 
        test -z "$solution_disable" || warning $"\
 * disable vshelper entirely by executing
-  | touch \"$CONFDIR/.defaults/apps/vshelper/disabled\"
+  | touch \"$__CONFDIR/.defaults/apps/vshelper/disabled\"
 * disable only this message by executing
-  | touch \"$CONFDIR/.defaults/apps/vshelper/warning-disabled\""
+  | touch \"$__CONFDIR/.defaults/apps/vshelper/warning-disabled\""
 
        test -x "$solution_sysctl" || warning $"\
 * configure the util-vserver vshelper script, e.g. by adding
-  | kernel.vshelper = \"$_VSHELPER\"
+  | kernel.vshelper = $_VSHELPER
   to /etc/sysctl.conf and rebooting the machine, or by executing
   | echo \"$_VSHELPER\" >$proc_file"
 
@@ -729,16 +854,31 @@ To fix this, you can:"
 function vshelper.isEnabled
 {
     local f=${1:+$1-}disabled
-    test ! -e "$CONFDIR"/.defaults/apps/vshelper/"$f"       || return 1
+    test ! -e "$__CONFDIR"/.defaults/apps/vshelper/"$f"     || return 1
     $_VSERVER_INFO - FEATURE vshelper                       || return 1
-    if test "$2"; then
-       local appdir=$($_VSERVER_INFO "$2" APPDIR vshelper) || return 0
+    if test -n "$2"; then
+       local appdir
+       appdir=$($_VSERVER_INFO "$2" APPDIR vshelper)       || return 0
        test -z "$2" -o ! -e "$appdir/$f"                   || return 1
     fi
 
     return 0
 }
 
+## Usage: vshelper.isDebug [<vserver>]
+function vshelper.isDebug
+{
+    test ! -e "$__CONFDIR"/.defaults/apps/vshelper/debug    || return 0
+    $_VSERVER_INFO - FEATURE vshelper                       || return 1
+    if test -n "$1"; then
+       local appdir
+       appdir=$($_VSERVER_INFO "$1" APPDIR vshelper)       || return 1
+       test ! -e "$appdir/debug"                           || return 0
+    fi
+
+    return 1
+}
+
 function vshelper._getHandlerInternal
 {
     local _vghi_var=$1
@@ -771,14 +911,16 @@ function vshelper._getHandlerInternal
 ## Usage: vshelper.getHandler <result-var> <vserver> <action>
 function vshelper.getHandler
 {
-    local _vgh_appdir=$($_VSERVER_INFO "$2" APPDIR vshelper) || _vgh_appdir=
+    local _vgh_appdir
+    _vgh_appdir=$($_VSERVER_INFO "$2" APPDIR vshelper) || _vgh_appdir=
+
     declare -a _vgh_search_list=( X )
     ## HACK: when we are in 'set -u' mode, empty lists are causing errors
 
     test -z "$_vgh_appdir" || _vgh_search_list=( "${_vgh_search_list[@]}" -x "$_vgh_appdir/$3" )
     test -z "$_vgh_appdir" || _vgh_search_list=( "${_vgh_search_list[@]}" -e "$_vgh_appdir/action" )
-    _vgh_search_list=( "${_vgh_search_list[@]}" -x "$CONFDIR"/.defaults/apps/vshelper/"$3" )
-    _vgh_search_list=( "${_vgh_search_list[@]}" -e "$CONFDIR"/.defaults/apps/vshelper/action )
+    _vgh_search_list=( "${_vgh_search_list[@]}" -x "$__CONFDIR"/.defaults/apps/vshelper/"$3" )
+    _vgh_search_list=( "${_vgh_search_list[@]}" -e "$__CONFDIR"/.defaults/apps/vshelper/action )
 
     ! vshelper._getHandlerInternal "$1" "${_vgh_search_list[@]}" || return 0
     eval $1=':restart'
@@ -789,19 +931,20 @@ function vshelper.doInit
 {
     vshelper.isEnabled || return 0
     
-    local xid=$($_VSERVER_INFO "$1" CONTEXT false) && test "$xid" || {
+    local xid
+    xid=$($_VSERVER_INFO "$1" CONTEXT false) && test -n "$xid" || {
        warning $"vshelper.init: can not determine xid of vserver '$vserver'; returned value was '$xid'"
        return 1
     }
 
-    local f="$VSHELPERSTATEDIR/$xid"
+    local f="$__VSHELPERSTATEDIR/$xid"
     
     set -C
     $_RM -f "$f"
     echo "$1" >"$f"
     set +C
     
-    if test "$2"; then
+    if test -n "$2"; then
        shift 1
        local i
        for i; do
@@ -819,31 +962,32 @@ function vshelper.doDestroy
 {
     vshelper.isEnabled || return 0
 
-    $_RM -f "$VSHELPERSTATEDIR/$2"
+    $_RM -f "$__VSHELPERSTATEDIR/$2"
 }
 
-## Usage: vshelper.initSync <vserver> <pipe-varname>
+## Usage: vshelper.initSync <vserver> <pipe-varname> [<method>]
 function vshelper.initSync
 {
-    local _vis_tmpdir=$($_MKTEMPDIR /tmp/vserver-stop.XXXXXX) || {
+    local _vis_tmpdir
+    _vis_tmpdir=$($_MKTEMPDIR /tmp/vserver-stop.XXXXXX) || {
        warning $"Failed to generate temporary directory for vshelper sync"
        return 1
     }
 
     local _vis_fifo="$_vis_tmpdir"/pipe
     $_MKFIFO -m700 "$_vis_fifo"
-    vshelper.doInit "$1" sync "$_vis_fifo"
+    vshelper.doInit "$1" "${3:-sync}" "$_vis_fifo"
     eval $2=\$_vis_fifo
 }
 
 ## Usage: vshelper.getSyncTimeout <vserver> <varname>
 function vshelper.getSyncTimeout
 {
-    local _vgst_appdir=$($_VSERVER_INFO "$1" APPDIR vshelper) || _vgst_appdir=
-    local _vgst_file _vgst_tmp
+    local _vgst_appdir _vgst_file _vgst_tmp
+    _vgst_appdir=$($_VSERVER_INFO "$1" APPDIR vshelper) || _vgst_appdir=
 
-    findFile _vgst_file ${_vgst_appdir:+"$_vgst_appdir"/sync-timeout} "$CONFDIR"/.defaults/apps/vshelper/sync-timeout ''
-    test "$_vgst_file" || return 1
+    findFile _vgst_file ${_vgst_appdir:+"$_vgst_appdir"/sync-timeout} "$__CONFDIR"/.defaults/apps/vshelper/sync-timeout ''
+    test -n "$_vgst_file" || return 1
     read _vgst_tmp <"$_vgst_file"
     eval $2=\$_vgst_tmp
 }
@@ -853,7 +997,7 @@ function _rpmFake.getCapFlags
 {
     local ctx=$1
     
-    if test "$ctx" && ! $_VSERVER_INFO - FEATURE migrate; then
+    if test -n "$ctx" && ! $_VSERVER_INFO - FEATURE migrate; then
        set -- $($_CHCONTEXT_COMPAT --xid 1 \
            $_SH -c "$_CAT /proc/[0-9]*/status | $_EGREP '^(CapBset|s_context|ctxflags)'" | \
            $_GREP -B 1 -A 1 "^s_context: $ctx " | \
@@ -862,7 +1006,7 @@ function _rpmFake.getCapFlags
        set --
     fi
 
-    if test "$3"; then
+    if test -n "$3"; then
        RPM_FAKE_CAP=$[ ~0x$1 ]
        RPM_FAKE_FLAGS=$3
     else