2016-12-24 05:55:17 +01:00
|
|
|
package net.ME1312.SubServers.Bungee.Host;
|
2016-12-05 04:21:04 +01:00
|
|
|
|
2019-01-09 22:41:37 +01:00
|
|
|
import net.ME1312.SubServers.Bungee.Library.Compatibility.JNA;
|
2019-01-09 18:37:02 +01:00
|
|
|
|
2016-12-05 04:21:04 +01:00
|
|
|
import java.io.File;
|
2019-01-09 18:37:02 +01:00
|
|
|
import java.lang.reflect.Field;
|
2016-12-05 04:21:04 +01:00
|
|
|
|
|
|
|
/**
|
2019-01-10 21:52:38 +01:00
|
|
|
* Executable Handler Class
|
2016-12-05 04:21:04 +01:00
|
|
|
*/
|
2019-01-04 22:51:58 +01:00
|
|
|
public class Executable {
|
|
|
|
private Executable() {}
|
2016-12-05 04:21:04 +01:00
|
|
|
|
|
|
|
/**
|
2019-01-04 22:51:58 +01:00
|
|
|
* Format a command to be executed
|
2016-12-05 04:21:04 +01:00
|
|
|
*
|
2019-01-04 22:51:58 +01:00
|
|
|
* @param gitbash Git Bash location (optional)
|
|
|
|
* @param exec Executable String
|
|
|
|
* @return
|
2016-12-05 04:21:04 +01:00
|
|
|
*/
|
2019-01-04 22:51:58 +01:00
|
|
|
public static String[] parse(String gitbash, String exec) {
|
|
|
|
String[] cmd;
|
2019-01-05 00:32:01 +01:00
|
|
|
if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
|
2019-01-04 22:51:58 +01:00
|
|
|
if (gitbash != null && (exec.startsWith("bash ") || exec.startsWith("sh ")))
|
2019-01-05 05:35:20 +01:00
|
|
|
exec = '"' + gitbash + ((gitbash.endsWith(File.separator))?"":File.separator) + "bin" + File.separatorChar + "sh.exe\" -lc \"" +
|
|
|
|
exec.replace("\\", "/\\").replace("\"", "\\\"").replace("^", "^^").replace("%", "^%").replace("&", "^&").replace("<", "^<").replace(">", "^>").replace("|", "^|") + '"';
|
2019-01-04 22:51:58 +01:00
|
|
|
cmd = new String[]{"cmd.exe", "/q", "/c", '"'+exec+'"'};
|
2016-12-05 04:21:04 +01:00
|
|
|
} else {
|
2019-01-05 00:32:01 +01:00
|
|
|
cmd = new String[]{"sh", "-lc", exec};
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|
2019-01-04 22:51:58 +01:00
|
|
|
return cmd;
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|
2019-01-09 18:37:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the PID of a currently running process
|
|
|
|
*
|
|
|
|
* @param process Process
|
2019-01-09 22:41:37 +01:00
|
|
|
* @return Process ID (null if unknown)
|
2019-01-09 18:37:02 +01:00
|
|
|
*/
|
|
|
|
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")) {
|
|
|
|
Field f = process.getClass().getDeclaredField("handle");
|
|
|
|
f.setAccessible(true);
|
|
|
|
long handle = f.getLong(process);
|
|
|
|
f.setAccessible(false);
|
|
|
|
|
2019-01-09 22:41:37 +01:00
|
|
|
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");
|
|
|
|
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();
|
2019-01-09 18:37:02 +01:00
|
|
|
} else if (process.getClass().getName().equals("java.lang.UNIXProcess")) {
|
|
|
|
Field f = process.getClass().getDeclaredField("pid");
|
|
|
|
f.setAccessible(true);
|
|
|
|
Object response = f.get(process);
|
|
|
|
f.setAccessible(false);
|
|
|
|
|
2019-01-09 22:41:37 +01:00
|
|
|
if (response instanceof Number)
|
|
|
|
return ((Number) response).longValue();
|
2019-01-09 18:37:02 +01:00
|
|
|
}
|
|
|
|
} catch (Throwable e) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Terminate a currently running process
|
|
|
|
*
|
|
|
|
* @param 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 {
|
2019-01-09 22:41:37 +01:00
|
|
|
Process terminator = Runtime.getRuntime().exec(new String[]{"taskkill.exe", "/T", "/F", "/PID", pid.toString()});
|
2019-01-09 18:37:02 +01:00
|
|
|
terminator.waitFor();
|
|
|
|
} catch (Throwable e) {}
|
|
|
|
}
|
|
|
|
if (process.isAlive()) process.destroyForcibly();
|
|
|
|
}
|
|
|
|
}
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|