Implement Java 9 process termination

Decided to finally use this now that people are updating their JDK versions.

We can never have too many ways to track down and terminate processes around here, can we?
This commit is contained in:
ME1312 2021-08-01 01:20:12 -04:00
parent 52fd155b5f
commit 6890b94b16
No known key found for this signature in database
GPG Key ID: FEFFE2F698E88FA8
3 changed files with 40 additions and 19 deletions

View File

@ -6,6 +6,7 @@ import net.ME1312.SubServers.Bungee.Library.Compatibility.JNA;
import java.io.File;
import java.io.IOException;
import java.util.stream.Stream;
/**
* Executable Handler Class
@ -93,18 +94,28 @@ public class Executable {
*/
public static void terminate(Process process) {
if (process.isAlive()) {
Long pid = pid(process);
if (pid != null) try {
if (Platform.getSystem() == Platform.WINDOWS) {
Runtime.getRuntime().exec(new String[]{"taskkill.exe", "/T", "/F", "/PID", pid.toString()}).waitFor();
} else if (USE_SESSION_TRACKING) {
Runtime.getRuntime().exec(new String[]{"bash", "-c", "kill -9 $(ps -s " + pid + " -o pid=)"}).waitFor();
}
} catch (IOException | InterruptedException e) {}
Long pid;
if (Platform.getSystem() == Platform.WINDOWS) {
if ((pid = pid(process)) != null) Util.isException(() -> Runtime.getRuntime().exec(new String[]{"taskkill.exe", "/T", "/F", "/PID", pid.toString()}).waitFor());
} else if (USE_SESSION_TRACKING) {
if ((pid = pid(process)) != null) Util.isException(() -> Runtime.getRuntime().exec(new String[]{"bash", "-c", "kill -9 $(ps -s " + pid + " -o pid=)"}).waitFor());
}
if (process.isAlive()) {
if (process.isAlive() && terminate9(process)) {
process.destroyForcibly();
}
}
}
private static boolean terminate9(Object handle) {
try { // Attempt iteration over Java 9 ProcessHandle objects
Class<?> clazz = handle.getClass();
Stream<?> children = (Stream<?>) clazz.getMethod("descendants").invoke(handle);
clazz.getMethod("destroyForcibly").invoke(handle);
children.forEach(Executable::terminate9);
return false;
} catch (Throwable e) {
return true;
}
}
}

View File

@ -25,7 +25,6 @@ import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.GZIPOutputStream;
import javax.net.ssl.HttpsURLConnection;

View File

@ -6,6 +6,7 @@ import net.ME1312.SubServers.Host.Library.Compatibility.JNA;
import java.io.File;
import java.io.IOException;
import java.util.stream.Stream;
/**
* Executable Handler Class
@ -93,18 +94,28 @@ public class Executable {
*/
public static void terminate(Process process) {
if (process.isAlive()) {
Long pid = pid(process);
if (pid != null) try {
if (Platform.getSystem() == Platform.WINDOWS) {
Runtime.getRuntime().exec(new String[]{"taskkill.exe", "/T", "/F", "/PID", pid.toString()}).waitFor();
} else if (USE_SESSION_TRACKING) {
Runtime.getRuntime().exec(new String[]{"bash", "-c", "kill -9 $(ps -s " + pid + " -o pid=)"}).waitFor();
}
} catch (IOException | InterruptedException e) {}
Long pid;
if (Platform.getSystem() == Platform.WINDOWS) {
if ((pid = pid(process)) != null) Util.isException(() -> Runtime.getRuntime().exec(new String[]{"taskkill.exe", "/T", "/F", "/PID", pid.toString()}).waitFor());
} else if (USE_SESSION_TRACKING) {
if ((pid = pid(process)) != null) Util.isException(() -> Runtime.getRuntime().exec(new String[]{"bash", "-c", "kill -9 $(ps -s " + pid + " -o pid=)"}).waitFor());
}
if (process.isAlive()) {
if (process.isAlive() && terminate9(process)) {
process.destroyForcibly();
}
}
}
private static boolean terminate9(Object handle) {
try { // Attempt iteration over Java 9 ProcessHandle objects
Class<?> clazz = handle.getClass();
Stream<?> children = (Stream<?>) clazz.getMethod("descendants").invoke(handle);
clazz.getMethod("destroyForcibly").invoke(handle);
children.forEach(Executable::terminate9);
return false;
} catch (Throwable e) {
return true;
}
}
}