From 0a05c6b8610936025fb7a8f40751f587bc7d0bd5 Mon Sep 17 00:00:00 2001 From: Peter Liljenberg Date: Mon, 31 Mar 2014 15:46:23 +0200 Subject: [PATCH 1/3] Ugly hack to fix destroyProcess for Java8 --- core/src/main/java/hudson/util/ProcessTree.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/hudson/util/ProcessTree.java b/core/src/main/java/hudson/util/ProcessTree.java index 23e5cb3b91fa..c8b6e3bb12fe 100644 --- a/core/src/main/java/hudson/util/ProcessTree.java +++ b/core/src/main/java/hudson/util/ProcessTree.java @@ -549,7 +549,11 @@ public void kill() throws InterruptedException { try { int pid = getPid(); LOGGER.fine("Killing pid="+pid); - UnixReflection.DESTROY_PROCESS.invoke(null, pid); + if (UnixReflection.isJava8()) { + UnixReflection.DESTROY_PROCESS.invoke(null, pid, false); + } else { + UnixReflection.DESTROY_PROCESS.invoke(null, pid); + } } catch (IllegalAccessException e) { // this is impossible IllegalAccessError x = new IllegalAccessError(); @@ -604,7 +608,11 @@ private static final class UnixReflection { PID_FIELD = clazz.getDeclaredField("pid"); PID_FIELD.setAccessible(true); - DESTROY_PROCESS = clazz.getDeclaredMethod("destroyProcess",int.class); + if (isJava8()) { + DESTROY_PROCESS = clazz.getDeclaredMethod("destroyProcess",int.class, boolean.class); + } else { + DESTROY_PROCESS = clazz.getDeclaredMethod("destroyProcess",int.class); + } DESTROY_PROCESS.setAccessible(true); } catch (ClassNotFoundException e) { LinkageError x = new LinkageError(); @@ -620,6 +628,10 @@ private static final class UnixReflection { throw x; } } + public static boolean isJava8() { + return (System.getProperty("java.version").startsWith("1.8")); + } + } From 95917370d9d6fa1dab559ec35a663c4f3731b72a Mon Sep 17 00:00:00 2001 From: Peter Liljenberg Date: Mon, 31 Mar 2014 15:46:23 +0200 Subject: [PATCH 2/3] Ugly hack to fix destroyProcess for Java8 --- core/src/main/java/hudson/util/ProcessTree.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/hudson/util/ProcessTree.java b/core/src/main/java/hudson/util/ProcessTree.java index c8b6e3bb12fe..3c7d795fc68d 100644 --- a/core/src/main/java/hudson/util/ProcessTree.java +++ b/core/src/main/java/hudson/util/ProcessTree.java @@ -549,11 +549,7 @@ public void kill() throws InterruptedException { try { int pid = getPid(); LOGGER.fine("Killing pid="+pid); - if (UnixReflection.isJava8()) { - UnixReflection.DESTROY_PROCESS.invoke(null, pid, false); - } else { - UnixReflection.DESTROY_PROCESS.invoke(null, pid); - } + UnixReflection.destroy(pid); } catch (IllegalAccessException e) { // this is impossible IllegalAccessError x = new IllegalAccessError(); @@ -628,7 +624,16 @@ private static final class UnixReflection { throw x; } } - public static boolean isJava8() { + + public static void destroy(int pid) throws IllegalAccessException, InvocationTargetException { + if (isJava8()) { + DESTROY_PROCESS.invoke(null, pid, false); + } else { + DESTROY_PROCESS.invoke(null, pid); + } + } + + private static boolean isJava8() { return (System.getProperty("java.version").startsWith("1.8")); } From 0c440099ce4eb3dc9a9b3e67ceb95450c2c4e492 Mon Sep 17 00:00:00 2001 From: Peter Liljenberg Date: Wed, 9 Apr 2014 21:47:08 +0200 Subject: [PATCH 3/3] Ugly hack to fix destroyProcess for Java8 --- .../main/java/hudson/util/ProcessTree.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/hudson/util/ProcessTree.java b/core/src/main/java/hudson/util/ProcessTree.java index 3c7d795fc68d..e367f71fadae 100644 --- a/core/src/main/java/hudson/util/ProcessTree.java +++ b/core/src/main/java/hudson/util/ProcessTree.java @@ -428,15 +428,15 @@ public synchronized List 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; } }); @@ -604,10 +604,10 @@ private static final class UnixReflection { PID_FIELD = clazz.getDeclaredField("pid"); PID_FIELD.setAccessible(true); - if (isJava8()) { - DESTROY_PROCESS = clazz.getDeclaredMethod("destroyProcess",int.class, boolean.class); - } else { + 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) { @@ -626,17 +626,17 @@ private static final class UnixReflection { } public static void destroy(int pid) throws IllegalAccessException, InvocationTargetException { - if (isJava8()) { - DESTROY_PROCESS.invoke(null, pid, false); - } else { + if (isPreJava8()) { DESTROY_PROCESS.invoke(null, pid); + } else { + DESTROY_PROCESS.invoke(null, pid, false); } } - private static boolean isJava8() { - return (System.getProperty("java.version").startsWith("1.8")); + private static boolean isPreJava8() { + int javaVersionAsAnInteger = Integer.parseInt(System.getProperty("java.version").replaceAll("\\.", "").replaceAll("_", "").substring(0, 2)); + return javaVersionAsAnInteger < 18; } - }