minor optimizations
[util-vserver.git] / util-vserver / scripts / vserver.start
index 4ae7c46..8c6301b 100644 (file)
@@ -15,6 +15,9 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
+
+lock "$LOCKDIR"/vserver."$($_VSERVER_INFO "$VSERVER_DIR" CANONIFY)".startup
+
 if isVserverRunning "$VSERVER_DIR"; then
     echo $"vserver '$VSERVER_NAME' already running; aborting..." >&2
     exit 1
@@ -22,7 +25,10 @@ fi
 
 function cleanup()
 {
-    test -z "$is_started"      || return 0
+    set +e
+
+    test -z "$startsync_pipe"  || rm -f "$startsync_pipe"
+    test -z "$is_started"      || { unlock; return 0; }
 
     if test "$is_configured" -a -z "$is_executed"; then
        warning $"
@@ -42,7 +48,7 @@ An error occured after executing the vserver startup sequence. This
 means that some processes may exist in the created context and the
 manual execution of
 
-  $_VSERVER '$VSERVER_NAME' stop
+  $_VSERVER '$VSERVER_DIR' stop
 
 is recommended to fix this."
     else
@@ -50,10 +56,12 @@ is recommended to fix this."
 
 Failed to start vserver '$VSERVER_NAME'"
     fi
-    
+
     test    "$OPTION_DEBUG"    || exec 2>/dev/null >/dev/null
     test -z "$is_mounted"      || umountVserver     "$VSERVER_DIR" || :
     test -z "$have_interfaces" || disableInterfaces "$VSERVER_DIR" || :
+
+    unlock || :
 }
 
 is_configured=
@@ -62,6 +70,7 @@ is_started=
 is_mounted=
 is_vshelper_init=
 have_interfaces=
+startsync_pipe=
 
 set -e
 trap "cleanup" EXIT
@@ -75,13 +84,16 @@ enableInterfaces "$VSERVER_DIR" && have_interfaces=1
 mountVserver "$VSERVER_DIR"     && is_mounted=1
 prepareInit  "$VSERVER_DIR"
 
-cd "$VSERVER_DIR"/vdir/
+pushd "$VSERVER_DIR"/vdir/ >/dev/null
 execScriptlets   "$VSERVER_DIR" "$VSERVER_NAME" pre-start
+popd >/dev/null
 
-cd "$VSERVER_DIR"/vdir/
+test -z "$OPTION_DEFAULTTTY" || setDefaultTTY "$VSERVER_DIR"
 
+pushd "$VSERVER_DIR"/vdir/ >/dev/null
 is_configured=1
 if $_VSERVER_INFO - FEATURE migrate; then
+    ${NICE_CMD[@]} \
     $_CHBIND     "${CHBIND_OPTS[@]}" -- \
     $_VCONTEXT   --create "${OPTS_VCONTEXT_CREATE[@]}" -- \
     ${USE_VNAMESPACE:+$_VNAMESPACE --set -- } \
@@ -92,23 +104,39 @@ if $_VSERVER_INFO - FEATURE migrate; then
     $_VUNAME     --xid self --set -t context="$VSERVER_DIR" -- \
     $_VATTRIBUTE --set "${OPTS_VATTRIBUTE[@]}" -- \
     $_SAVE_CTXINFO "$VSERVER_DIR" \
+    $_ENV -i -- \
     $_VCONTEXT   --migrate-self --endsetup --chroot $SILENT_OPT "${OPTS_VCONTEXT_MIGRATE[@]}" -- \
     "${INITCMD_START[@]}"
 else
+    if test "$_IS_FAKEINIT"; then
+       startsync_pipe=$(mktemp /tmp/vserver-start.XXXXXX)
+       rm -f "$startsync_pipe"
+       mkfifo -m600 "$startsync_pipe"  ## safe, since mkfifo does not follow symlinks
+    fi
+    
     ${NICE_CMD[@]} \
     $_CHBIND           "${CHBIND_OPTS[@]}" \
     $_EXEC_ULIMIT      "$VSERVER_DIR/ulimits" \
     $_CHCONTEXT_COMPAT "${CHCONTEXT_OPTS[@]}" "${CHCONTEXT_INIT_OPTS[@]}" \
     $_SAVE_CTXINFO     "$VSERVER_DIR" \
-    $_CLEARENV \
+    $_ENV -i -- \
+    $_CHAINECHO "${_IS_FAKEINIT:+$startsync_pipe}" "" \
     $_CAPCHROOT        "${CAPCHROOT_OPTS[@]}" . \
     "${INITCMD_START[@]}"
+
+    ## the additional synchronization step is needed since vshelper.doInit
+    ## below might be executed before needed context information were
+    ## saved by $_SAVE_CTXINFO
+    ${_IS_FAKEINIT:+cat "$startsync_pipe"} >/dev/null
 fi
 is_executed=1
 
 "${INITCMD_START_SYNC[@]}"
+popd >/dev/null
 
+pushd "$VSERVER_DIR"/vdir/ >/dev/null
 execScriptlets   "$VSERVER_DIR" "$VSERVER_NAME" post-start
+popd >/dev/null
 
 is_vshelper_init=1
 ! vshelper.isEnabled "" "$VSERVER_DIR" || vshelper.doInit "$VSERVER_DIR"