This file is indexed.

/usr/lib/util-vserver/functions is in util-vserver 0.30.216-pre2864-2ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
# $Id: functions 2770 2008-08-24 20:09:08Z dhozac $	--*- sh -*--

# Copyright (C) 2003,2004,2005,2006 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
#  
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#  
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#  
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

_VS_LOGFILE=
_VS_ERRFILE=

_VS_NEWLINE='
'
declare -r _VS_NEWLINE=${_VS_NEWLINE:0-1}
declare -r VS_ALLVSERVERS_ARGS=all,marked,unmarked,stopped,running

function findObject
{
    local _fo_mod=$1
    local _fo_var=$2
    local _fo_file=
    local _fo_i=X
    shift 2
    
    for _fo_i; do
	test -n "$_fo_i"         || continue
	test ! $_fo_mod "$_fo_i" || { _fo_file=$_fo_i; break; }
    done

    test -z "$_fo_i" -o -n "$_fo_file" || {
	echo "Can not find file for '$_fo_var'; aborting"
	exit 1
    } >&2

    eval "$_fo_var=\"$_fo_file\""
}

function findFile
{
    findObject -f "$@"
}

function findDir
{
    findObject -d "$@"
}

function findAndCopy
{
    local dst=$1
    test ! -s "$dst"         || return 0
    
    local tmp
    shift
    findFile tmp "$@"

    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 -n "$_VS_LOGFILE"; then
	echo "$@" >>"$_VS_LOGFILE"
    else
	echo "$@"
    fi
}

## Usage: warning <message>
function warning
{
    if test -n "$_VS_ERRFILE"; then
	echo "$@" >>"$_VS_ERRFILE"
    else
	echo "$@" >&2
    fi
}

## Usage: panic <message>
function panic
{
    if test -n "$_VS_ERRFILE"; then
	echo "$@" >>"$_VS_ERRFILE"
    else
	echo "$@" >&2
    fi

    exit 1
}

## Usage: execute <message>
function execute
{
    test -z "${DEBUG_EXEC:-}"       || echo "$@"
    test "${DEBUG_EXEC:-}" = noexec || exec "$@"
    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 -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
{
    local	style=$1
    shift
    
    if ! $_TTY -s; then
	"$@"
    else
	local	cfile
	findFile cfile "$__CONFDIR"/.defaults/styles/"$style" ''
	if test -n "$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
}

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
{
    $_TTY -s || return 0
    echo -ne "\e]0;$@\007"
}

_VS_LOCKS=''
## Usage: lock <lockfile> [<timeout>]
function lock
{
    local tmp=$($_MKTEMP vserver-lock.XXXXXX)
    $_RM -f $tmp
    $_MKFIFO -m600 $tmp

    $_LOCKFILE "$1" $tmp $2 &
    $_GREP -q true $tmp 2>/dev/null || return 1
    
    _VS_LOCKS="$! $_VS_LOCKS"
}

## Usage: unlock [<num>]
function unlock
{
    local num=$1
    local i

    set -- $_VS_LOCKS
    while test "$#" -gt 0; do
	kill -HUP "$1" >/dev/null || :
	shift
	test "$num" != 1 || break
	test -z "$num"   || let --num
    done
    _VS_LOCKS="$@"
}

function get_init_cwd
{
    if test -n "$INIT_CWD"; then
	echo "$INIT_CWD"
    else
	pwd
    fi
}
function set_init_cwd
{
    INIT_CWD="`pwd`"
    export INIT_CWD
}

function _setVserverDir
{
    local vserver="$1"
    case "$vserver" in
	./*) VSERVER_DIR="`get_init_cwd`/$vserver";;
	/*)  VSERVER_DIR="$vserver"               ;;
	*)   VSERVER_DIR="$__CONFDIR/$vserver"    ;;
    esac
}

function _setVserverName
{
    if test -e "$VSERVER_DIR"/name; then
	read VSERVER_NAME <"$VSERVER_DIR"/name
    else
	VSERVER_NAME=$(basename "$VSERVER_DIR")
    fi
}

function _pkgMountBindDir()
{
    test "$1" != "$2" || return 0

    $_MOUNT -n --bind "$1" "$2"
}

function _pkgSetVarsBase
{
    case "$vserver" in
	./*|/*)
	    if test -d "$vserver/vdir"; then
		BASEDIR=$vserver
		VDIR=$(getPhysicalDir "$vserver/vdir")
		
		PKGDIR=$BASEDIR/apps/pkgmgmt
		test -d "$PKGDIR" || {
		    echo "Can not find configuration-directory for package-managment tools"
		    exit 1
		} >&2
		findDir EXECDIR      $PKGDIR/execdir     /
	    else
		VDIR=$(getPhysicalDir "$vserver")
		PKGDIR=
	    fi
	    ;;
        *)
    	    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
	    VDIR=$(getPhysicalDir "$VDIR")
    	    
    	    PKGDIR=$BASEDIR/apps/pkgmgmt
    	    test -d "$PKGDIR" || {
    	        echo "Can not find configuration-directory for package-managment tools"
    	        exit 1
    	    } >&2

	    findDir EXECDIR      $PKGDIR/execdir     /

	    ;;
    esac

    if test -z "$WORKAROUND_106057"; then
	_rpmdb_mntpoint=/dev
    else
	_rpmdb_mntpoint=/.rpmdb
    fi
}

function _pkgSetVarsRPM
{
    if test -n "$PKGDIR"; then
	findDir RPMETCDIR    $PKGDIR/rpmetc      $PKGDIR/base/rpm/etc       /etc/rpm
	findDir RPMSTATEDIR  $PKGDIR/rpmstate    $PKGDIR/base/rpm/state

	findDir RPMLIBDIR    $PKGDIR/rpmlib	 /

    else
	findDir RPMETCDIR    "$VDIR"/etc/rpm     /etc/rpm
	findDir RPMSTATEDIR  "$VDIR"/var/lib/rpm
	RPMLIBDIR=/
    fi
    
    RPMSTATEDIR=$(getPhysicalDir "$RPMSTATEDIR")
    RPMETCDIR=$(getPhysicalDir "$RPMETCDIR")
}

function _pkgSetVarsApt
{
    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
	findDir APTARCHIVDIR $PKGDIR/aptarchives $PKGDIR/base/apt/archives  /var/cache/apt/archives
    else
	findDir APTETCDIR    "$VDIR"/etc/apt       	/etc/apt
	findDir APTSTATEDIR  "$VDIR"/var/state/apt
	findDir APTCACHEDIR  "$VDIR"/var/cache/apt
	findDir APTARCHIVDIR "$VDIR"/var/cache/apt/archives /var/cache/apt/archives
    fi

    findFile APT_CONFIG "$APTETCDIR"/apt.conf ""
    test -z "$APT_CONFIG" || export APT_CONFIG
}

function _pkgSetVarsYum
{
    if test -n "$PKGDIR"; then
	findDir YUMETCDIR    $PKGDIR/yumetc      $PKGDIR/base/yum/etc       /etc
	findDir YUMCACHEDIR  $PKGDIR/yumcache    $PKGDIR/base/yum/cache
    else
	findDir YUMETCDIR    "$VDIR"/etc       	 /etc
	findDir YUMCACHEDIR  "$VDIR"/var/cache/yum
    fi
}


function _pkgMountBase
{
    :
}

function _pkgMountApt
{
    :
}

function _pkgMountYum
{
    :
}

function _pkgMountRPM
{
    _pkgMountBindDir "$RPMETCDIR" /etc/rpm
    test "$RPMLIBDIR" = "/" || _pkgMountBindDir "$RPMLIBDIR" /usr/lib/rpm

    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 -n -t proc none /proc

    popd >/dev/null
}

function _pkgSetEnvBase
{
    test "$EXECDIR" = "/" || {
	PATH=$EXECDIR:$PATH
	LD_LIBRARY_PATH=$EXECDIR${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
    }

    export PATH LD_LIBRARY_PATH
}

function _pkgSetEnvApt
{
    :
}

function _pkgSetEnvYum
{
    :
}

function _pkgSetEnvRPM
{
    CUR_VSERVER=$vserver
    RPM_FAKE_NAMESPACE_MOUNTS=$_rpmdb_mntpoint
    RPM_BINARY=$_VRPM_PRELOAD

    export CUR_VSERVER RPM_FAKE_NAMESPACE_MOUNTS RPM_BINARY
}

function pkgInit
{
    local i
    local vserver=$1
    shift
    
    _pkgSetVarsBase
    for i; do
	case "$i" in
	    rpm)	_pkgSetVarsRPM;;
	    apt)	_pkgSetVarsApt;;
	    yum)	_pkgSetVarsYum;;
	    *)		echo "Unknown packaging flavor" >&2; exit 1;;
	esac
    done

    _pkgMountBase
    for i; do
	case "$i" in
	    rpm)	_pkgMountRPM;;
	    apt)	_pkgMountApt;;
	    yum)	_pkgMountYum;;
	esac
    done

    _pkgSetEnvBase
    for i; do
	case "$i" in
	    rpm)	_pkgSetEnvRPM;;
	    apt)	_pkgSetEnvApt;;
	    yum)	_pkgSetEnvYum;;
	esac
    done

    _PKG_FLAVORS="$@"
    _PKG_VSERVER=$vserver
}

function isAvoidNamespace
{
    local cfgdir

    $_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 \
         -e "$cfgdir"/nonamespace
}

function isNamespaceCleanup
{
    local cfgdir

    $_VSERVER_INFO - FEATURE namespace   || return 1
    cfgdir=$($_VSERVER_INFO "$1" CFGDIR) || return 1
    isAvoidNamespace "$1"                && return 1
    test -e "$cfgdir"/namespace-cleanup  && return 0
    test -e "$cfgdir"/nonamespace-cleanup -o \
         -e "$__CONFDIR"/.defaults/nonamespace-cleanup && return 1
    return 0
}

## Usage: getAllVservers <var> [<KIND>*]
function getAllVservers
{
    local _ga_i
    declare -a _ga_tmp=()

    for _ga_i in $__CONFDIR/*; do
	isRegularFile "$_ga_i" -d   || continue

	test ! -e "$_ga_i"/disabled || continue
	test   -d "$_ga_i"/vdir     || 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

	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
{
    local _gp_var=$2
    local _gp_procnr_cnt=0

    # Use /proc/virtual from kernel 2.6 when possible
    if test -d "/proc/virtual"; then
	set -- $($_GREP '^Tasks:' "/proc/virtual/$1/status" 2>/dev/null)
	_gp_procnr_cnt=$2
    else
	_gp_procnr_cnt=$($_VPS ax | $_AWK '{print $2}' | $_GREP -x "$1" | $_WC -l )
    fi

    let _gp_procnr_cnt=_gp_procnr_cnt+0
    eval $_gp_var=\$_gp_procnr_cnt
}

## Usage: getVserverCtx <vdir> <result-varname> [<procnumber-varname> [<do-cleanup>]]
## Returns: 0 iff vserver is running
function getVserverStatus
{
    test -r "$1"/run || return 1

    local _gvs_ctx
    read _gvs_ctx <"$1"/run
    eval "$2"=\$_gvs_ctx

    if test "$1" = "$($_VUNAME -g --xid "$_gvs_ctx" context)"; then
	test -n "$3" || return 0
	local _gvs_tmp
	_getProcNumberCount "$_gvs_ctx" _gvs_tmp
	eval "$3"=\$_gvs_tmp
    else
	test -n "$3" || return 1
	eval "$3"=0
	_gvs_tmp=0
    fi

    if test "$_gvs_tmp" = 0; then
	local runfile=$($_READLINK "$1/run")
	test -z "$4" || $_RM -f "$runfile"
	return 1
    fi

    return 0
}

## Usage: isCtxRunning <ctx>
function isCtxRunning
{
    local _tmp
    _getProcNumberCount "$1" _tmp
    test $_tmp -gt 0
}

## Usage: isVserverRunning <vdir> [<ctx-varname>]
function isVserverRunning
{
    local _ivr_ctx _ivr_procnum

    getVserverStatus "$1" _ivr_ctx _ivr_procnum 1 || return 1
    test "$_ivr_procnum" != 0                     || return 1
    test -z "$2" || eval "$2"=\$_ivr_ctx
    return 0
}

## Called as 'getFileValue <varname> <filename>+'
function getFileValue
{
    local _gfv_var=$1
    local _gfv_file
    shift

    findFile _gfv_file "$@" ''
    test -n "$_gfv_file" -a -r "$_gfv_file" || return 0
    eval read "$_gfv_var" <"$_gfv_file"
}

## Called as 'getFileArray <varname> <filename>+'
function getFileArray
{
    local _gfa_var=$1
    local _gfa_file
    shift

    findFile _gfa_file "$@" ''
    test -n "$_gfa_file" -a -r "$_gfa_file" || return 0
    local IFS=$_VS_NEWLINE
    eval "$_gfa_var"='( $(< "$_gfa_file") )'
}

function checkComponents
{
    local	i
    local	msg=$1
    local	x_failed=

    shift
    
    for i; do
	local failed=
	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;;
	    (*)		echo "Unknown component '$i'" >&2
			return false
			;;
	esac

	test -z "$failed" || {
	    echo "$msg: $i"
	    x_failed=1
	} >&2
    done

    test -z "$x_failed"
}

## Usage: isKernelAPI <ver> [<cmp-modifier>]
function isKernelAPI
{
    local api
    api=$($_VSERVER_INFO - APIVER) || api=0
    test $[ $api ] -${2:-ge} $[ $1 ]
}

## Usage: callInNamespace <vserver> <command> <args>*
function callInNamespace
{
    local ctx=
    
    isAvoidNamespace "$1" || \
    ctx=$( $_VSERVER_INFO "$1" CONTEXT f ) || ctx=

    shift
    if test -n "$ctx"; then
	$_VNAMESPACE --enter "$ctx" -- "$@"
    else
	"$@"
    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

    if test -f "$ttyname"; then
	exec </dev/null
    else
	exec <$ttyname
    fi
    exec    >>$ttyname 2>&1
}

## 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 || :
}

function useVlogin
{
    test ! -e "$__CONFDIR/.defaults/apps/vlogin/disable"
}

## Usage: pkgmgmt.guessStyle <vserver> <resultvar>
function pkgmgmt.guessStyle()
{
    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 -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
    elif test -e "$_pgs_vdir"/etc/mandrake-release; then
	style=mandrake
    elif test -e "$_pgs_vdir"/etc/debian_version; then
	style=debian
    elif test -e "$_pgs_vdir"/etc/SuSE-release; then
	style=suse
    else
	echo $"Can not determine packagemanagement style" >&2
	return 1
    fi

    eval $2=\$style
    return 0
}

## Usage: pkgmgmt.isInternal <vserver>
## returns true iff <vserver> is configured for internal packagemanagement
## A typical application is
## | is_external=
## | pkgmgmt.isInternal "$vserver" || is_external=1
function pkgmgmt.isInternal
{
    local cfgdir=$($_VSERVER_INFO "$1" APPDIR pkgmgmt) || :

    test -z "$cfgdir" -o ! -d "$cfgdir" -o -e "$cfgdir"/internal
}

## Usage: pkgmgmt.isAptAvailable <cfgdir> <vdir> [<is-internal>]
function pkgmgmt.isAptAvailable
{
    local cfgdir="$1"
    local vdir="$2"
    local is_internal="$3"
    
    local have_apt i
    if test -n "$is_internal"; then
	have_apt=1
	test -d "$cfgdir"/base/apt -o -d "$cfgdir"/aptetc || have_apt=
    else
	have_apt=
	for i in /bin /usr/bin /usr/local/bin; do
	    test ! -x "$vdir$i"/apt-get || { have_apt=1; break; }
	done
    fi

    test -n "$have_apt" && return 0 || return 1
}

## Usage: pkgmgmt.isYumAvailable <cfgdir> <vdir> [<is-internal>]
function pkgmgmt.isYumAvailable
{
    local cfgdir="$1"
    local vdir="$2"
    local is_internal="$3"
    
    local have_yum i
    if test -n "$is_internal"; then
	have_yum=1
	test -d "$cfgdir"/base/yum -o -d "$cfgdir"/yumetc || have_yum=
    else
	have_yum=
	for i in /bin /usr/bin /usr/local/bin; do
	    test ! -x "$vdir$i"/yum || { have_yum=1; break; }
	done
    fi

    test -n "$have_yum" && return 0 || return 1
}


function vshelper.doSanityCheck
{
    local vshelper this_xid i
    declare -a warnings=()
    local solution_disable=
    local solution_sysctl=

    vshelper.isEnabled && vshelper.isEnabled warning || return 0
    
    this_xid=$($_VSERVER_INFO - XID) ||
	panic $"Failed to determine current context; aborting..."

    ## Do nothing in other xid's; the helper will be executed in xid 0 only
    test "$this_xid" = 0 || return 0

    local proc_file=/proc/sys/kernel/vshelper

    if ! test -r "$proc_file"; then
	vshelper=
	warnings=( "${warnings[@]}"
		   $"File '$proc_file' does not exist but is required for vshelper setup" )
        solution_disable=1
    else
	vshelper=$(cat "$proc_file")

	$_CMP -s "$vshelper" "$_VSHELPER" || {
	    local readable=""
	    test -r "$vshelper" && readable=1
	    warnings=( "${warnings[@]}"
		       $"The configured vshelper '$vshelper' does not match the 'vshelper'
  script of the util-vserver package.${readable:+ Maybe you have two versions installed?}"
		     )
	    solution_disable=1
	    solution_sysctl=1
	}
    fi

    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" )
	solution_disable=1
    }

    test "${#warnings[@]}" -eq 0 || {
	warning $"\
The following problem(s) were encountered while verifying vshelper
functionality:"

	for i in "${warnings[@]}"; do
	    warning "* $i"
	done

	warning $"\
	
To fix this, you can:"

	test -z "$solution_disable" || warning $"\
* disable vshelper entirely by executing
  | touch \"$__CONFDIR/.defaults/apps/vshelper/disabled\"
* disable only this message by executing
  | 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
  to /etc/sysctl.conf and rebooting the machine, or by executing
  | echo \"$_VSHELPER\" >$proc_file"

	warning ""

	return 1
    }

    return 0
}

## Usage: vshelper.isEnabled [<style>] [<vserver>]
function vshelper.isEnabled
{
    local f=${1:+$1-}disabled
    test ! -e "$__CONFDIR"/.defaults/apps/vshelper/"$f"     || return 1
    $_VSERVER_INFO - FEATURE vshelper                       || return 1
    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
    local _vghi_tmp
    shift
    shift	## HACK: see below the note about the 'set -u' mode
    
    while test "$#" -ge 2; do
	local _vghi_mod=$1
	local _vghi_obj=$2
	shift 2

	test "$_vghi_mod" "$_vghi_obj" || continue
	case "$_vghi_mod" in
	    (-x)
		eval $_vghi_var=\$_vghi_obj
		;;
	    (-e)
		read _vghi_tmp <"$_vghi_obj"
		eval $_vghi_var=:\$_vghi_tmp
		;;
	    (*)	panic $"Internal error, unexpected modifier '$_vghi_mod'"
	esac
	return 0
    done
    
    return 1
}

## Usage: vshelper.getHandler <result-var> <vserver> <action>
function vshelper.getHandler
{
    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 )

    ! vshelper._getHandlerInternal "$1" "${_vgh_search_list[@]}" || return 0
    eval $1=':restart'
}

## Usage: vshelper.init <vserver> [<method> <args>*]
function vshelper.doInit
{
    vshelper.isEnabled || return 0
    
    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'

This usually means that you're using an init-less init-style, but the
guest isn't configured to start any service. Try enabling a service,
changing the init-style, or making the contexts persistent."
	return 1
    }

    local f="$__VSHELPERSTATEDIR/$xid"
    
    set -C
    $_RM -f "$f"
    echo "$1" >"$f"
    set +C
    
    if test -n "$2"; then
	shift 1
	local i
	for i; do
	    echo "$i" 
	done
    else
	echo "default"
    fi >>"$f"

    return 0
}

## Usage: vshelper.doDestroy <vserver> <xid>
function vshelper.doDestroy
{
    vshelper.isEnabled || return 0

    $_RM -f "$__VSHELPERSTATEDIR/$2"
}

## Usage: vshelper.initSync <vserver> <pipe-varname> [<method>]
function vshelper.initSync
{
    local _vis_tmpdir
    _vis_tmpdir=$($_MKTEMPDIR 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" "${3:-sync}" "$_vis_fifo"
    eval $2=\$_vis_fifo
}

## Usage: vshelper.getSyncTimeout <vserver> <varname>
function vshelper.getSyncTimeout
{
    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 -n "$_vgst_file" || return 1
    read _vgst_tmp <"$_vgst_file"
    eval $2=\$_vgst_tmp
}

function vshelper.initStopSync
{
	local _iss_sync_dir=$($_MKTEMPDIR vshelper-stop-sync.XXXXXX) || {
		warning $"Failed to generate directory for vshelper sync"
		exit 1
	}
	$_MKFIFO -m700 "$_iss_sync_dir/pipe"

	eval "$1"=\$_iss_sync_dir
	VSHELPER_STOP_SYNC="$_iss_sync_dir/pipe"
	export VSHELPER_STOP_SYNC
}

function vshelper.waitForStopSync
{
	local sync_dir=$1
	cat "$sync_dir/pipe" &> /dev/null
	rm -fr "$sync_dir"
}

function vshelper.doStopSync
{
	test ! -p "$VSHELPER_STOP_SYNC" || echo stopped > "$VSHELPER_STOP_SYNC"
}

function vshelper.isStopSync
{
	test -p "$VSHELPER_STOP_SYNC" || return 1
	return 0
}

function _rpmFake.getCapFlags
{
    local ctx=$1
    
    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 " | \
	    $_SED -e '1,3p;d' | $_AWK '{ print $2 }')
    else
	set --
    fi

    if test -n "$3"; then
	RPM_FAKE_CAP=$[ ~0x$1 ]
	RPM_FAKE_FLAGS=$3
    else
	RPM_FAKE_CAP=$[ ~0xd40c04ff ]
	RPM_FAKE_FLAGS=4
    fi
}

function rpmFake.init
{
    local vdir ctx
    
    vdir=$($_VSERVER_INFO "$1" VDIR)    || vdir="$1"
    ctx=$($_VSERVER_INFO  "$1" CONTEXT) || ctx=

    test -d "$vdir" ||
	panic $"Can not find chroot environment at '$vdir' for '$1'"

    _rpmFake.getCapFlags "$ctx"

    RPM_FAKE_CHROOT=$vdir
    RPM_FAKE_CTX=$ctx
}

function rpmFake.exec
{
    export RPM_FAKE_CHROOT RPM_FAKE_CTX RPM_FAKE_CAP RPM_FAKE_FLAGS
    
    LD_PRELOAD=$_RPM_FAKE_SO${LD_PRELOAD:+:$LD_PRELOAD} \
	exec "$@"
}