Skip to content

Commit

Permalink
[JENKINS-72833] Do not attempt to self-exec on systems without `lib…
Browse files Browse the repository at this point in the history
…c` (#9025)
  • Loading branch information
basil committed Mar 10, 2024
1 parent c9af352 commit 434bf2b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
6 changes: 3 additions & 3 deletions core/src/main/java/hudson/lifecycle/Lifecycle.java
Expand Up @@ -120,13 +120,13 @@ public void verifyRestartable() throws RestartNotSupportedException {
// if run on Unix, we can do restart
try {
instance = new UnixLifecycle();
} catch (final IOException e) {
LOGGER.log(Level.WARNING, "Failed to install embedded lifecycle implementation", e);
} catch (final Throwable t) {
LOGGER.log(Level.WARNING, "Failed to install embedded lifecycle implementation", t);
instance = new Lifecycle() {
@Override
public void verifyRestartable() throws RestartNotSupportedException {
throw new RestartNotSupportedException(
"Failed to install embedded lifecycle implementation, so cannot restart: " + e, e);
"Failed to install embedded lifecycle implementation, so cannot restart: " + t, t);
}
};
}
Expand Down
20 changes: 10 additions & 10 deletions core/src/main/java/hudson/lifecycle/UnixLifecycle.java
Expand Up @@ -31,6 +31,8 @@

import com.sun.jna.Native;
import com.sun.jna.StringArray;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Functions;
import hudson.Platform;
import java.io.IOException;
import java.util.List;
Expand All @@ -50,16 +52,12 @@
* @since 1.304
*/
public class UnixLifecycle extends Lifecycle {

@NonNull
private List<String> args;
private Throwable failedToObtainArgs;

public UnixLifecycle() throws IOException {
try {
args = JavaVMArguments.current();
} catch (UnsupportedOperationException | LinkageError e) {
// can't restart / see JENKINS-3875
failedToObtainArgs = e;
}
public UnixLifecycle() {
args = JavaVMArguments.current();
}

@Override
Expand Down Expand Up @@ -89,6 +87,10 @@ public void restart() throws IOException, InterruptedException {

@Override
public void verifyRestartable() throws RestartNotSupportedException {
if (!Functions.isGlibcSupported()) {
throw new RestartNotSupportedException("Restart is not supported on platforms without libc");
}

// see http://lists.apple.com/archives/cocoa-dev/2005/Oct/msg00836.html and
// http://factor-language.blogspot.com/2007/07/execve-returning-enotsup-on-mac-os-x.html
// on Mac, execv fails with ENOTSUP if the caller is multi-threaded, resulting in an error like
Expand All @@ -97,8 +99,6 @@ public void verifyRestartable() throws RestartNotSupportedException {
// according to http://www.mail-archive.com/wine-devel@winehq.org/msg66797.html this now works on Snow Leopard
if (Platform.isDarwin() && !Platform.isSnowLeopardOrLater())
throw new RestartNotSupportedException("Restart is not supported on Mac OS X");
if (args == null)
throw new RestartNotSupportedException("Failed to obtain the command line arguments of the process", failedToObtainArgs);
}

private static final Logger LOGGER = Logger.getLogger(UnixLifecycle.class.getName());
Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/jenkins/util/JavaVMArguments.java
@@ -1,6 +1,6 @@
package jenkins.util;

import com.google.common.primitives.Ints;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Functions;
import hudson.util.ProcessTree;
import java.lang.management.ManagementFactory;
Expand All @@ -20,6 +20,7 @@ public class JavaVMArguments {
/**
* Gets the process argument list of the current process.
*/
@NonNull
public static List<String> current() {
ProcessHandle.Info info = ProcessHandle.current().info();
if (info.command().isPresent() && info.arguments().isPresent()) {
Expand All @@ -30,7 +31,7 @@ public static List<String> current() {
return args;
} else if (Functions.isGlibcSupported()) {
// Native approach
int pid = Ints.checkedCast(ProcessHandle.current().pid());
int pid = Math.toIntExact(ProcessHandle.current().pid());
ProcessTree.OSProcess process = ProcessTree.get().get(pid);
if (process != null) {
List<String> args = process.getArguments();
Expand Down

0 comments on commit 434bf2b

Please sign in to comment.