Skip to content

Commit

Permalink
[FIXED JENKINS-21341] Merge pull request #1169 from pliljenberg/master
Browse files Browse the repository at this point in the history
Ugly hack to fix destroyProcess for Java8
  • Loading branch information
olivergondza committed Apr 12, 2014
2 parents 54c13f1 + 0c44009 commit 19640e7
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions core/src/main/java/hudson/util/ProcessTree.java
Expand Up @@ -429,15 +429,15 @@ public synchronized List<String> getArguments() {
public synchronized EnvVars getEnvironmentVariables() {
if(env !=null)
return env;
env = new EnvVars();
try
env = new EnvVars();

try
{
env.putAll(p.getEnvironmentVariables());
} catch (WinpException e)
{
LOGGER.log(FINE, "Failed to get environment variable ", e);
}
}
return env;
}
});
Expand Down Expand Up @@ -550,7 +550,7 @@ public void kill() throws InterruptedException {
try {
int pid = getPid();
LOGGER.fine("Killing pid="+pid);
UnixReflection.DESTROY_PROCESS.invoke(null, pid);
UnixReflection.destroy(pid);
} catch (IllegalAccessException e) {
// this is impossible
IllegalAccessError x = new IllegalAccessError();
Expand Down Expand Up @@ -605,7 +605,11 @@ private static final class UnixReflection {
PID_FIELD = clazz.getDeclaredField("pid");
PID_FIELD.setAccessible(true);

DESTROY_PROCESS = clazz.getDeclaredMethod("destroyProcess",int.class);
if (isPreJava8()) {
DESTROY_PROCESS = clazz.getDeclaredMethod("destroyProcess",int.class);
} else {
DESTROY_PROCESS = clazz.getDeclaredMethod("destroyProcess",int.class, boolean.class);
}
DESTROY_PROCESS.setAccessible(true);
} catch (ClassNotFoundException e) {
LinkageError x = new LinkageError();
Expand All @@ -621,6 +625,19 @@ private static final class UnixReflection {
throw x;
}
}

public static void destroy(int pid) throws IllegalAccessException, InvocationTargetException {
if (isPreJava8()) {
DESTROY_PROCESS.invoke(null, pid);
} else {
DESTROY_PROCESS.invoke(null, pid, false);
}
}

private static boolean isPreJava8() {
int javaVersionAsAnInteger = Integer.parseInt(System.getProperty("java.version").replaceAll("\\.", "").replaceAll("_", "").substring(0, 2));
return javaVersionAsAnInteger < 18;
}
}


Expand Down

0 comments on commit 19640e7

Please sign in to comment.