include <unistd.h>
[util-vserver.git] / util-vserver / scripts / functions
index 4b50d0a..e794173 100644 (file)
@@ -71,6 +71,7 @@ function getPhysicalDir
     ( set -P && cd "$1" && pwd )
 }
 
+## Usage: logging <message>
 function logging
 {
     if test "$_VS_LOGFILE"; then
@@ -80,6 +81,7 @@ function logging
     fi
 }
 
+## Usage: warning <message>
 function warning
 {
     if test "$_VS_ERRFILE"; then
@@ -89,6 +91,7 @@ function warning
     fi
 }
 
+## Usage: panic <message>
 function panic
 {
     if test "$_VS_ERRFILE"; then
@@ -100,6 +103,7 @@ function panic
     exit 1
 }
 
+## Usage: execute <message>
 function execute
 {
     test -z "${DEBUG_EXEC:-}"       || echo "$@"
@@ -115,7 +119,43 @@ function isNumber
     return 0
 }
 
-_UV_LOCKS=''
+## Usage: colorize <style> <command>
+function colorize
+{
+    local      style=$1
+    shift
+    
+    if ! tty -s; then
+       "$@"
+    else
+       local   cfile
+       findFile cfile "$CONFDIR"/.defaults/styles/"$style" ''
+       if test "$cfile"; then
+         cat "$cfile"
+       else
+         case "$style" in
+           (bold)      echo -ne "\e[1m";;
+           (emph)      echo -ne "\e[34m";;
+           (info)      echo -ne "\e[0;34m";;
+           (warn*)     echo -ne "\e[1;31m";;
+           (error)     echo -ne "\e[1;33;41m";;
+           (*)         ;;
+         esac
+       fi
+           
+       "$@"
+       echo -ne "\e[m"
+    fi
+}
+
+## Usage: xtermTitle <title>
+function xtermTitle
+{
+    tty -s || return 0
+    echo -ne "\e]0;$@\007"
+}
+
+_VS_LOCKS=''
 ## Usage: lock <lockfile> [<timeout>]
 function lock
 {
@@ -123,11 +163,10 @@ function lock
     rm -f $tmp
     mkfifo -m600 $tmp
 
-    sleep 10
     $_LOCKFILE "$1" $tmp $2 &
-    cat <$tmp >/dev/null
+    grep -q true $tmp 2>/dev/null || return 1
     
-    _UV_LOCKS="$! $_UV_LOCKS"
+    _VS_LOCKS="$! $_VS_LOCKS"
 }
 
 ## Usage: unlock [<num>]
@@ -136,14 +175,14 @@ function unlock
     local num=$1
     local i
 
-    set -- $_UV_LOCKS
+    set -- $_VS_LOCKS
     while test "$#" -gt 0; do
        kill -HUP "$1" >/dev/null || :
        shift
        test "$num" != 1 || break
        test -z "$num"   || let --num
     done
-    _UV_LOCKS="$@"
+    _VS_LOCKS="$@"
 }
 
 function _pkgMountBindDir()
@@ -252,11 +291,15 @@ function _pkgMountRPM
     _pkgMountBindDir "$RPMETCDIR" /etc/rpm
     test "$RPMLIBDIR" = "/" || _pkgMountBindDir "$RPMLIBDIR" /usr/lib/rpm
 
-    "$_SECURE_MOUNT" --chroot "$VDIR" -n --secure --bind "$RPMSTATEDIR" "$_rpmdb_mntpoint"
+    pushd "$VDIR" >/dev/null
+
+    "$_SECURE_MOUNT" --chroot -n --bind "$RPMSTATEDIR" "$_rpmdb_mntpoint"
     test -z "$WORKAROUND_106057" || mount -n --bind "$RPMSTATEDIR" "$_rpmdb_mntpoint"
 
     test -e "$VDIR"/proc/self/status || \
-       "$_SECURE_MOUNT" --chroot "$VDIR" -n --secure -t proc none /proc
+       "$_SECURE_MOUNT" --chroot -n -t proc none /proc
+
+    popd >/dev/null
 }
 
 function _pkgSetEnvBase
@@ -348,6 +391,24 @@ function getAllVservers
     eval $1='( "${_tmp[@]}" )'
 }
 
+## Usage: _getProcNumberCount <ctx> <var>
+function _getProcNumberCount
+{
+    local var=$2
+    local procnr_cnt=0
+
+    # Use /proc/virtual from kernel 2.6 when possible
+    if test -d "/proc/virtual"; then
+       set -- $($_GREP '^PROC:' "/proc/virtual/$1/limit" 2>/dev/null)
+       procnr_cnt=$2
+    else
+       procnr_cnt=$($_VPS ax | $_AWK '{print $2}' | $_GREP -x "$1" | $_WC -l )
+    fi
+
+    let procnr_cnt=procnr_cnt+0
+    eval $var=\$procnr_cnt
+}
+
 ## Usage: getVserverCtx <vdir> <result-varname> [<procnumber-varname> [<do-cleanup>]]
 ## Returns: 0 iff vserver is running
 function getVserverStatus
@@ -359,8 +420,8 @@ function getVserverStatus
     eval "$2"=\$_ctx
 
     test "$3"        || return 0
-    local _tmp=$($_VPS ax | $_AWK '{print $2}' | $_GREP -x "$_ctx" | $_WC -l )
-    let _tmp=_tmp+0
+    local _tmp
+    _getProcNumberCount "$_ctx" _tmp
     eval "$3"=\$_tmp
 
     if test "$_tmp" = 0; then
@@ -375,7 +436,8 @@ function getVserverStatus
 ## Usage: isCtxRunning <ctx>
 function isCtxRunning
 {
-    local _tmp=$($_VPS ax | $_AWK '{print $2}' | $_GREP -x "$1" | $_WC -l )
+    local _tmp
+    _getProcNumberCount "$1" _tmp
     test $_tmp -gt 0
 }
 
@@ -390,11 +452,16 @@ function isVserverRunning
     return 0
 }
 
-## Called as 'getFileValue <varname> <filename>'
+## Called as 'getFileValue <varname> <filename>+'
 function getFileValue
 {
-    test -r "$2" || return 0
-    eval read "$1" <"$2"
+    local var=$1
+    local file
+    shift
+
+    findFile file "$@" ''
+    test "$file" -a -r "$file" || return 0
+    eval read "$var" <"$file"
 }
 
 ## Called as 'getFileArray <varname> <filename>'
@@ -448,7 +515,7 @@ function callInNamespace
     local ctx=
     
     isAvoidNamespace "$1" || \
-    ctx=$( $_VSERVER_INFO "$1" CONTEXT t ) || ctx=
+    ctx=$( $_VSERVER_INFO "$1" CONTEXT f ) || ctx=
 
     shift
     if test "$ctx"; then
@@ -458,6 +525,21 @@ function callInNamespace
     fi
 }
 
+## Usage: setDefaultTTY <vdir> [<fallback-tty>]
+function setDefaultTTY
+{
+    local cfgdir ttyname
+
+    cfgdir=$($_VSERVER_INFO "$1" APPDIR init) || cfgdir=
+    findObject -e ttyname \
+       ${cfgdir:+"$cfgdir"/tty} \
+       "$CONFDIR/.defaults/apps/init/tty" \
+       $2 /dev/null
+
+    exec   <$ttyname
+    exec  &>$ttyname
+}
+
 ## Usage: pkgmgmt.guessStyle <vserver> <resultvar>
 function pkgmgmt.guessStyle()
 {
@@ -557,9 +639,9 @@ function vshelper.doSanityCheck
     test -d "$VSHELPERSTATEDIR" || {
        warnings=( "${warnings[@]}"
                   $"\
-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" )
+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
     }
 
@@ -661,8 +743,8 @@ function vshelper.doInit
 {
     vshelper.isEnabled || return 0
     
-    local xid=$($_VSERVER_INFO "$1" CONTEXT false) || {
-       warning $"vshelper.init: can not determine xid of vserver '$vserver'"
+    local xid=$($_VSERVER_INFO "$1" CONTEXT false) && test "$xid" || {
+       warning $"vshelper.init: can not determine xid of vserver '$vserver'; returned value was '$xid'"
        return 1
     }
 
@@ -704,7 +786,7 @@ function vshelper.initSync
 
     local fifo="$tmpdir"/pipe
     mkfifo -m700 "$fifo"
-    vshelper.doInit "$VSERVER_DIR" sync "$fifo"
+    vshelper.doInit "$1" sync "$fifo"
     eval $2=\$fifo
 }