File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\lifecycle\UnixLifecycle.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.lifecycle;
25
26 import com.sun.akuma.JavaVMArguments;
27 import com.sun.akuma.Daemon;
28 import com.sun.jna.Native;
29 import com.sun.jna.StringArray;
30
31 import java.io.IOException;
32
33 import static hudson.util.jna.GNUCLibrary.*;
34
35 import hudson.Platform;
36 import jenkins.model.Jenkins;
37
38 /**
39 * {@link Lifecycle} implementation when Hudson runs on the embedded
40 * servlet container on Unix.
41 *
42 * <p>
43 * Restart by exec to self.
44 *
45 * @author Kohsuke Kawaguchi
46 * @since 1.304
47 */
48 public class UnixLifecycle extends Lifecycle {
49 private JavaVMArguments args;
50 private Throwable failedToObtainArgs;
51
52 public UnixLifecycle() throws IOException {
53 try {
54 args = JavaVMArguments.current();
55
56 // if we are running as daemon, don't fork into background one more time during restart
57 args.remove("--daemon");
58 } catch (UnsupportedOperationException e) {
59 // can't restart
60 failedToObtainArgs = e;
61 } catch (LinkageError e) {
62 // see HUDSON-3875
63 failedToObtainArgs = e;
64 }
65 }
66
67 @Override
68 public void restart() throws IOException, InterruptedException {
69 Jenkins h = Jenkins.getInstance();
70 if (h != null)
71 h.cleanUp();
72
73 // close all files upon exec, except stdin, stdout, and stderr
74 int sz = LIBC.getdtablesize();
75 for(int i=3; i<sz; i++) {
76 int flags = LIBC.fcntl(i, F_GETFD);
77 if(flags<0) continue;
78 LIBC.fcntl(i, F_SETFD,flags| FD_CLOEXEC);
79 }
80
81 // exec to self
82 String exe = Daemon.getCurrentExecutable();
83 LIBC.execv(exe, new StringArray(args.toArray(new String[args.size()])));
84 throw new IOException("Failed to exec '"+exe+"' "+LIBC.strerror(Native.getLastError()));
85 }
86
87 @Override
88 public void verifyRestartable() throws RestartNotSupportedException {
89 // see http://lists.apple.com/archives/cocoa-dev/2005/Oct/msg00836.html and
90 // http://factor-language.blogspot.com/2007/07/execve-returning-enotsup-on-mac-os-x.html
91 // on Mac, execv fails with ENOTSUP if the caller is multi-threaded, resulting in an error like
92 // the one described in http://www.nabble.com/Restarting-hudson-not-working-on-MacOS--to24641779.html
93 //
94 // according to http://www.mail-archive.com/wine-devel@winehq.org/msg66797.html this now works on Snow Leopard
95 if (Platform.isDarwin() && !Platform.isSnowLeopardOrLater())
96 throw new RestartNotSupportedException("Restart is not supported on Mac OS X");
97 if (args==null)
98 throw new RestartNotSupportedException("Failed to obtain the command line arguments of the process",failedToObtainArgs);
99 }
100 }
101