gentoo: use /var/run for new /run compatibility
[util-vserver.git] / scripts / chcontext
index 7ffbd45..d30f645 100755 (executable)
@@ -27,7 +27,7 @@ test -e "$UTIL_VSERVER_VARS" || {
 function showHelp()
 {
     echo \
-$"Usage: /usr/sbin/chcontext [--cap [!]<cap_name>] [--secure] [--xid <num>] [--disconnect]
+$"Usage: $1 [--cap [!]<cap_name>] [--secure] [--xid <num>] [--disconnect]
        [--domainname <name>] [--hostname <name>] [--flag <flags>+]
        [--silent] [--] command arguments ...
 
@@ -84,7 +84,7 @@ By default, a new/unused context is allocated
     Do not print the allocated context number.
 
 Report bugs to <$PACKAGE_BUGREPORT>."
-    exit $1
+    exit $2
 }
 
 function showVersion()
@@ -101,7 +101,7 @@ the GNU General Public License.  This program has absolutely no warranty."
 
 $_VSERVER_INFO - FEATURE migrate || exec $_CHCONTEXT_COMPAT "$@"
 
-tmp=$(getopt -o + --long cap:,ctx:,xid:,disconnect,domainname:,flag:,hostname:,secure,silent,help,version -n "$0" -- "$@") || exit 1
+tmp=$(getopt -o + --long cap:,ctx:,xid:,disconnect,domainname:,flag:,hostname:,secure,silent,help,version,spaces: -n "$0" -- "$@") || exit 1
 eval set -- "$tmp"
 
 OPT_CAPS=()
@@ -111,11 +111,12 @@ OPT_FLAGS=()
 OPT_SECURE=
 OPT_SILENT=
 OPT_INITPID=
+OPT_SPACES=--default
 
 while true; do
     case "$1" in
-       --help)         showHelp $0 ;;
-       --version)      showVersion ;;
+       --help)         showHelp $0 0;;
+       --version)      showVersion 0;;
        --cap)          OPT_CAPS=( "${OPT_CAPS[@]}" "$2" ); shift;;
        --ctx|--xid)    OPT_CTX=$2; shift;;
        --disconnect)   OPT_DISCONNECT=1;;
@@ -128,13 +129,16 @@ while true; do
            ;;
        --secure)       OPT_SECURE=1;;
        --silent)       OPT_SILENT=1;;
+       --spaces)       OPT_SPACES=$2; shift;;
        --)             shift; break;;
        *)              echo $"chcontext: internal error; arg=='$1'" >&2; exit 1;;
     esac
     shift
 done
 
-create_cmd=( $_VCONTEXT --create --silentexist
+create_cmd=( ${OPT_CTX:+$_VTAG --create --tag "$OPT_CTX" --silentexist --silent --}
+            $_VSPACE --new $OPT_SPACES --
+            $_VCONTEXT --create --silentexist
             ${OPT_SILENT:+--silent}
             ${OPT_CTX:+--xid "$OPT_CTX"} )
 
@@ -143,6 +147,10 @@ chain_cmd=()
 old_IFS=$IFS
 IFS=,$IFS
 
+chain_cmd=( "${chain_cmd[@]}"
+               --
+               $_VSPACE --set $OPT_SPACES )
+
 test -z "$OPT_DOMAINNAME$OPT_HOSTNAME" || \
     chain_cmd=( "${chain_cmd[@]}"
                --
@@ -164,7 +172,12 @@ migrate_cmd=( $_VCONTEXT
 
 IFS=$old_IFS
 
-if test -z "$OPT_CTX" || $_VSERVER_INFO -q "$OPT_CTX" XIDTYPE static; then
+$_VSERVER_INFO -q "$OPT_CTX" XIDTYPE static
+is_static=$?
+test -z "$OPT_CTX"
+is_dynamic=$?
+
+if test "$is_dynamic" -eq 0 || test "$is_static" -eq 0; then
     "${create_cmd[@]}" "${chain_cmd[@]}" -- \
        "${migrate_cmd[@]}" --endsetup --migrate-self -- "$@"
     rc=$?
@@ -172,5 +185,13 @@ else
     rc=254
 fi
 
-test "$rc" -ne 254 || exec "${migrate_cmd[@]}" --xid "$OPT_CTX" --migrate -- "$@"
+if test "$is_static" -eq 0; then
+    migrate_cmd=( $_VTAG --migrate --tag "$OPT_CTX" --silent -- \
+                 $_VSPACE --enter "$OPT_CTX" $OPT_SPACES -- \
+                 "${migrate_cmd[@]}" )
+fi
+
+
+test "$rc" -ne 254 || exec "${migrate_cmd[@]}" --xid "$OPT_CTX" --migrate -- \
+                          "$@"
 exit $rc