Skip to content

Commit

Permalink
Clean up Executable class
Browse files Browse the repository at this point in the history
  • Loading branch information
ME1312 committed Feb 25, 2021
1 parent 2beec3a commit 15a4f10
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 40 deletions.
@@ -1,9 +1,11 @@
package net.ME1312.SubServers.Bungee.Host;

import net.ME1312.Galaxi.Library.Platform;
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.SubServers.Bungee.Library.Compatibility.JNA;

import java.io.File;
import java.io.IOException;

/**
* Executable Handler Class
Expand Down Expand Up @@ -41,30 +43,34 @@ public static String[] parse(String gitbash, String exec) {
* @param process Process
* @return Process ID (null if unknown)
*/
@SuppressWarnings("JavaReflectionMemberAccess")
public static Long pid(Process process) {
if (process.isAlive()) {
try {
return (long) Process.class.getDeclaredMethod("pid").invoke(process);
} catch (Throwable ex) {
try {
if (process.getClass().getName().equals("java.lang.Win32Process") || process.getClass().getName().equals("java.lang.ProcessImpl")) {
try { // Java 9 Standard
return (long) Process.class.getMethod("pid").invoke(process);
} catch (Throwable e) {
try { // Java 8 Not-so-standard
Object response = Util.reflect(process.getClass().getDeclaredField("pid"), process);

if (response instanceof Number) {
return ((Number) response).longValue();
} else throw e;
} catch (Throwable e2) {
if (Platform.getSystem() == Platform.WINDOWS) try {
long handle = Util.reflect(process.getClass().getDeclaredField("handle"), process);

ClassLoader jna = JNA.get();
Class<?> pc = jna.loadClass("com.sun.jna.Pointer"),
ntc = jna.loadClass("com.sun.jna.platform.win32.WinNT$HANDLE"),
k32c = jna.loadClass("com.sun.jna.platform.win32.Kernel32");
k32c = jna.loadClass("com.sun.jna.platform.win32.Kernel32");
Object k32 = k32c.getField("INSTANCE").get(null),
nt = ntc.getConstructor().newInstance();
ntc.getMethod("setPointer", pc).invoke(nt, pc.getMethod("createConstant", long.class).invoke(null, handle));
return ((Number) k32c.getMethod("GetProcessId", ntc).invoke(k32, nt)).longValue();
} else if (process.getClass().getName().equals("java.lang.UNIXProcess")) {
Object response = Util.reflect(process.getClass().getDeclaredField("pid"), process);

if (response instanceof Number)
return ((Number) response).longValue();
} catch (Throwable e3) {
// No way to find pid, I suppose.
}
} catch (Throwable e) {}
}
}
}
return null;
Expand All @@ -77,14 +83,16 @@ public static Long pid(Process process) {
*/
public static void terminate(Process process) {
if (process.isAlive()) {
if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
Long pid = pid(process);
if (pid != null) try {
Process terminator = Runtime.getRuntime().exec(new String[]{"taskkill.exe", "/T", "/F", "/PID", pid.toString()});
terminator.waitFor();
} catch (Throwable e) {}
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();
}
} catch (IOException | InterruptedException e) {}

if (process.isAlive()) {
process.destroyForcibly();
}
if (process.isAlive()) process.destroyForcibly();
}
}
}
Expand Up @@ -137,6 +137,7 @@ private void init(InternalHost host, String name, boolean enabled, int port, Str
}

private void run() {
boolean locked = lock;
allowrestart = true;
started = false;
try {
Expand All @@ -150,7 +151,7 @@ private void run() {
Logger.get("SubServers").info("Now starting " + getName());
logger.process = process;
logger.start();
lock = false;
lock = locked = false;
command = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
for (LoggedCommand command : history) if (process.isAlive()) {
this.command.write(command.getCommand());
Expand All @@ -161,8 +162,8 @@ private void run() {
if (process.isAlive()) process.waitFor();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
if (locked) lock = false;
allowrestart = false;
lock = false;
}

Logger.get("SubServers").info(getName() + " has stopped");
Expand Down
@@ -1,9 +1,11 @@
package net.ME1312.SubServers.Host.Executable;

import net.ME1312.Galaxi.Library.Platform;
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.SubServers.Host.Library.Compatibility.JNA;

import java.io.File;
import java.io.IOException;

/**
* Executable Handler Class
Expand Down Expand Up @@ -41,30 +43,34 @@ public static String[] parse(String gitbash, String exec) {
* @param process Process
* @return Process ID (null if unknown)
*/
@SuppressWarnings("JavaReflectionMemberAccess")
public static Long pid(Process process) {
if (process.isAlive()) {
try {
return (long) Process.class.getDeclaredMethod("pid").invoke(process);
} catch (Throwable ex) {
try {
if (process.getClass().getName().equals("java.lang.Win32Process") || process.getClass().getName().equals("java.lang.ProcessImpl")) {
try { // Java 9 Standard
return (long) Process.class.getMethod("pid").invoke(process);
} catch (Throwable e) {
try { // Java 8 Not-so-standard
Object response = Util.reflect(process.getClass().getDeclaredField("pid"), process);

if (response instanceof Number) {
return ((Number) response).longValue();
} else throw e;
} catch (Throwable e2) {
if (Platform.getSystem() == Platform.WINDOWS) try {
long handle = Util.reflect(process.getClass().getDeclaredField("handle"), process);

ClassLoader jna = JNA.get();
Class<?> pc = jna.loadClass("com.sun.jna.Pointer"),
ntc = jna.loadClass("com.sun.jna.platform.win32.WinNT$HANDLE"),
k32c = jna.loadClass("com.sun.jna.platform.win32.Kernel32");
k32c = jna.loadClass("com.sun.jna.platform.win32.Kernel32");
Object k32 = k32c.getField("INSTANCE").get(null),
nt = ntc.getConstructor().newInstance();
ntc.getMethod("setPointer", pc).invoke(nt, pc.getMethod("createConstant", long.class).invoke(null, handle));
return ((Number) k32c.getMethod("GetProcessId", ntc).invoke(k32, nt)).longValue();
} else if (process.getClass().getName().equals("java.lang.UNIXProcess")) {
Object response = Util.reflect(process.getClass().getDeclaredField("pid"), process);

if (response instanceof Number)
return ((Number) response).longValue();
} catch (Throwable e3) {
// No way to find pid, I suppose.
}
} catch (Throwable e) {}
}
}
}
return null;
Expand All @@ -77,14 +83,16 @@ public static Long pid(Process process) {
*/
public static void terminate(Process process) {
if (process.isAlive()) {
if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
Long pid = pid(process);
if (pid != null) try {
Process terminator = Runtime.getRuntime().exec(new String[]{"taskkill.exe", "/T", "/F", "/PID", pid.toString()});
terminator.waitFor();
} catch (Throwable e) {}
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();
}
} catch (IOException | InterruptedException e) {}

if (process.isAlive()) {
process.destroyForcibly();
}
if (process.isAlive()) process.destroyForcibly();
}
}
}

0 comments on commit 15a4f10

Please sign in to comment.