diff --git a/core/src/main/java/hudson/lifecycle/Lifecycle.java b/core/src/main/java/hudson/lifecycle/Lifecycle.java index bc089d789a..2f84e1ce84 100644 --- a/core/src/main/java/hudson/lifecycle/Lifecycle.java +++ b/core/src/main/java/hudson/lifecycle/Lifecycle.java @@ -28,8 +28,9 @@ import hudson.model.Hudson; import java.io.File; import java.io.IOException; +import java.util.logging.Logger; +import java.util.logging.Level; -import org.apache.commons.io.IOUtils; import org.apache.commons.io.FileUtils; /** @@ -72,9 +73,19 @@ public abstract class Lifecycle implements ExtensionPoint { throw x; } } else { - // no lifecycle given. use the default one - INSTANCE = new Lifecycle() { - }; + // if run on embedded container, attempt to use UnixEmbeddedContainerLifecycle + if(System.getProperty("executable-war")!=null && !Hudson.isWindows()) { + try { + INSTANCE = new UnixEmbeddedContainerLifecycle(); + } catch (IOException e) { + LOGGER.log(Level.WARNING, "Failed to install embedded lifecycle implementation",e); + } + } + + // use the default one. final fallback. + if(INSTANCE==null) + INSTANCE = new Lifecycle() { + }; } } @@ -154,4 +165,6 @@ public abstract class Lifecycle implements ExtensionPoint { public boolean canRestart() { return isOverridden("restart"); } + + private static final Logger LOGGER = Logger.getLogger(Lifecycle.class.getName()); } diff --git a/core/src/main/java/hudson/lifecycle/UnixEmbeddedContainerLifecycle.java b/core/src/main/java/hudson/lifecycle/UnixEmbeddedContainerLifecycle.java new file mode 100644 index 0000000000..7c996a046c --- /dev/null +++ b/core/src/main/java/hudson/lifecycle/UnixEmbeddedContainerLifecycle.java @@ -0,0 +1,77 @@ +/* + * The MIT License + * + * Copyright (c) 2004-2009, Sun Microsystems, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package hudson.lifecycle; + +import com.sun.akuma.JavaVMArguments; +import com.sun.akuma.Daemon; +import com.sun.jna.Native; +import com.sun.jna.StringArray; + +import java.io.IOException; + +import static hudson.util.jna.GNUCLibrary.*; + +/** + * {@link Lifecycle} implementation when Hudson runs on the embedded + * servlet container on Unix. + * + *
+ * Restart by exec to self.
+ *
+ * @author Kohsuke Kawaguchi
+ * @since 1.304
+ */
+public class UnixEmbeddedContainerLifecycle extends Lifecycle {
+ private JavaVMArguments args;
+
+ public UnixEmbeddedContainerLifecycle() throws IOException {
+ try {
+ args = JavaVMArguments.current();
+ } catch (UnsupportedOperationException e) {
+ // can't restart
+ }
+ }
+
+ @Override
+ public void restart() throws IOException, InterruptedException {
+ // close all files upon exec, except stdin, stdout, and stderr
+ int sz = LIBC.getdtablesize();
+ for(int i=3; i