mirror of
https://github.com/ME1312/SubServers-2.git
synced 2025-01-11 02:17:43 +01:00
Properly terminate child processes on windows
This applies to both SubCreator and SubServers. Only works on Java 9+ because Java 8 lacks a way to get the PIDs.
This commit is contained in:
parent
1845e9d8f9
commit
9cb499c4ec
@ -17,13 +17,13 @@ public class Executable {
|
|||||||
*/
|
*/
|
||||||
public static String[] parse(String gitbash, String exec) {
|
public static String[] parse(String gitbash, String exec) {
|
||||||
String[] cmd;
|
String[] cmd;
|
||||||
if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) {
|
if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
|
||||||
if (gitbash != null && (exec.startsWith("bash ") || exec.startsWith("sh ")))
|
if (gitbash != null && (exec.startsWith("bash ") || exec.startsWith("sh ")))
|
||||||
exec = "\"" + gitbash + ((gitbash.endsWith(File.separator))?"":File.separator) + "bin" + File.separatorChar + "sh.exe\" -lic \"" +
|
exec = "\"" + gitbash + ((gitbash.endsWith(File.separator))?"":File.separator) + "bin" + File.separatorChar + "sh.exe\" -lc \"" +
|
||||||
exec.replace("\\", "/\\").replace("\"", "\\\"").replace("^", "^^").replace("%", "^%").replace("&", "^&").replace("<", "^<").replace(">", "^>").replace("|", "^|") + "\"";
|
exec.replace("\\", "/\\").replace("\"", "\\\"").replace("^", "^^").replace("%", "^%").replace("&", "^&").replace("<", "^<").replace(">", "^>").replace("|", "^|") + "\"";
|
||||||
cmd = new String[]{"cmd.exe", "/q", "/c", '"'+exec+'"'};
|
cmd = new String[]{"cmd.exe", "/q", "/c", '"'+exec+'"'};
|
||||||
} else {
|
} else {
|
||||||
cmd = new String[]{"sh", "-lic", exec};
|
cmd = new String[]{"sh", "-lc", exec};
|
||||||
}
|
}
|
||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
@ -128,7 +128,7 @@ public class InternalSubCreator extends SubCreator {
|
|||||||
String command = "bash \"" + template.getBuildOptions().getRawString("Shell-Location") + '\"';
|
String command = "bash \"" + template.getBuildOptions().getRawString("Shell-Location") + '\"';
|
||||||
for (String arg : args) command += ' ' + arg;
|
for (String arg : args) command += ' ' + arg;
|
||||||
|
|
||||||
if (System.getProperty("os.name").toLowerCase().indexOf("win") < 0 && template.getBuildOptions().contains("Permission")) {
|
if (!System.getProperty("os.name").toLowerCase().startsWith("windows") && template.getBuildOptions().contains("Permission")) {
|
||||||
try {
|
try {
|
||||||
Process process = Runtime.getRuntime().exec("chmod " + template.getBuildOptions().getRawString("Permission") + ' ' + template.getBuildOptions().getRawString("Shell-Location"), null, dir);
|
Process process = Runtime.getRuntime().exec("chmod " + template.getBuildOptions().getRawString("Permission") + ' ' + template.getBuildOptions().getRawString("Shell-Location"), null, dir);
|
||||||
Thread.sleep(500);
|
Thread.sleep(500);
|
||||||
@ -347,9 +347,20 @@ public class InternalSubCreator extends SubCreator {
|
|||||||
@Override
|
@Override
|
||||||
public void terminate(String name) {
|
public void terminate(String name) {
|
||||||
if (this.thread.keySet().contains(name.toLowerCase())) {
|
if (this.thread.keySet().contains(name.toLowerCase())) {
|
||||||
|
boolean success = false;
|
||||||
|
if (this.thread.get(name.toLowerCase()).process != null && this.thread.get(name.toLowerCase()).process.isAlive() && System.getProperty("os.name").toLowerCase().startsWith("windows")) try {
|
||||||
|
Process terminator = Runtime.getRuntime().exec(new String[]{"taskkill", "/T", "/F", "/PID", Long.toString((long) Process.class.getDeclaredMethod("pid").invoke(this.thread.get(name.toLowerCase()).process))});
|
||||||
|
terminator.waitFor();
|
||||||
|
if (terminator.exitValue() != 0) throw new IllegalStateException("taskkill exited with code " + terminator.exitValue());
|
||||||
|
success = true;
|
||||||
|
} catch (Exception e) {}
|
||||||
|
|
||||||
if (this.thread.get(name.toLowerCase()).process != null && this.thread.get(name.toLowerCase()).process.isAlive()) {
|
if (this.thread.get(name.toLowerCase()).process != null && this.thread.get(name.toLowerCase()).process.isAlive()) {
|
||||||
this.thread.get(name.toLowerCase()).process.destroyForcibly();
|
this.thread.get(name.toLowerCase()).process.destroyForcibly();
|
||||||
} else if (this.thread.get(name.toLowerCase()).isAlive()) {
|
success = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!success && this.thread.get(name.toLowerCase()).isAlive()) {
|
||||||
this.thread.get(name.toLowerCase()).interrupt();
|
this.thread.get(name.toLowerCase()).interrupt();
|
||||||
this.thread.remove(name.toLowerCase());
|
this.thread.remove(name.toLowerCase());
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ import net.md_5.bungee.BungeeServerInfo;
|
|||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -220,6 +221,11 @@ public class InternalSubServer extends SubServerContainer {
|
|||||||
host.plugin.getPluginManager().callEvent(event);
|
host.plugin.getPluginManager().callEvent(event);
|
||||||
if (!event.isCancelled()) {
|
if (!event.isCancelled()) {
|
||||||
allowrestart = false;
|
allowrestart = false;
|
||||||
|
if (process != null && process.isAlive() && System.getProperty("os.name").toLowerCase().startsWith("windows")) try {
|
||||||
|
Process terminator = Runtime.getRuntime().exec(new String[]{"taskkill", "/T", "/F", "/PID", Long.toString((long) Process.class.getDeclaredMethod("pid").invoke(process))});
|
||||||
|
terminator.waitFor();
|
||||||
|
if (terminator.exitValue() != 0) throw new IllegalStateException("taskkill exited with code " + terminator.exitValue());
|
||||||
|
} catch (Exception e) {}
|
||||||
if (process != null && process.isAlive()) process.destroyForcibly();
|
if (process != null && process.isAlive()) process.destroyForcibly();
|
||||||
return true;
|
return true;
|
||||||
} else return false;
|
} else return false;
|
||||||
|
@ -17,13 +17,13 @@ public class Executable {
|
|||||||
*/
|
*/
|
||||||
public static String[] parse(String gitbash, String exec) {
|
public static String[] parse(String gitbash, String exec) {
|
||||||
String[] cmd;
|
String[] cmd;
|
||||||
if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) {
|
if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
|
||||||
if (gitbash != null && (exec.startsWith("bash ") || exec.startsWith("sh ")))
|
if (gitbash != null && (exec.startsWith("bash ") || exec.startsWith("sh ")))
|
||||||
exec = "\"" + gitbash + ((gitbash.endsWith(File.separator))?"":File.separator) + "bin" + File.separatorChar + "sh.exe\" -lic \"" +
|
exec = "\"" + gitbash + ((gitbash.endsWith(File.separator))?"":File.separator) + "bin" + File.separatorChar + "sh.exe\" -lc \"" +
|
||||||
exec.replace("\\", "/\\").replace("\"", "\\\"").replace("^", "^^").replace("%", "^%").replace("&", "^&").replace("<", "^<").replace(">", "^>").replace("|", "^|") + "\"";
|
exec.replace("\\", "/\\").replace("\"", "\\\"").replace("^", "^^").replace("%", "^%").replace("&", "^&").replace("<", "^<").replace(">", "^>").replace("|", "^|") + "\"";
|
||||||
cmd = new String[]{"cmd.exe", "/q", "/c", '"'+exec+'"'};
|
cmd = new String[]{"cmd.exe", "/q", "/c", '"'+exec+'"'};
|
||||||
} else {
|
} else {
|
||||||
cmd = new String[]{"sh", "-lic", exec};
|
cmd = new String[]{"sh", "-lc", exec};
|
||||||
}
|
}
|
||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
@ -278,7 +278,7 @@ public class SubCreator {
|
|||||||
String command = "bash \"" + template.getBuildOptions().getRawString("Shell-Location") + '\"';
|
String command = "bash \"" + template.getBuildOptions().getRawString("Shell-Location") + '\"';
|
||||||
for (String arg : args) command += ' ' + arg;
|
for (String arg : args) command += ' ' + arg;
|
||||||
|
|
||||||
if (System.getProperty("os.name").toLowerCase().indexOf("win") < 0 && template.getBuildOptions().contains("Permission")) {
|
if (!System.getProperty("os.name").toLowerCase().startsWith("windows") && template.getBuildOptions().contains("Permission")) {
|
||||||
try {
|
try {
|
||||||
Process process = Runtime.getRuntime().exec("chmod " + template.getBuildOptions().getRawString("Permission") + ' ' + template.getBuildOptions().getRawString("Shell-Location"), null, dir);
|
Process process = Runtime.getRuntime().exec("chmod " + template.getBuildOptions().getRawString("Permission") + ' ' + template.getBuildOptions().getRawString("Shell-Location"), null, dir);
|
||||||
Thread.sleep(500);
|
Thread.sleep(500);
|
||||||
@ -380,9 +380,20 @@ public class SubCreator {
|
|||||||
|
|
||||||
public void terminate(String name) {
|
public void terminate(String name) {
|
||||||
if (this.thread.keySet().contains(name.toLowerCase())) {
|
if (this.thread.keySet().contains(name.toLowerCase())) {
|
||||||
|
boolean success = false;
|
||||||
|
if (this.thread.get(name.toLowerCase()).process != null && this.thread.get(name.toLowerCase()).process.isAlive() && System.getProperty("os.name").toLowerCase().startsWith("windows")) try {
|
||||||
|
Process terminator = Runtime.getRuntime().exec(new String[]{"taskkill", "/T", "/F", "/PID", Long.toString((long) Process.class.getDeclaredMethod("pid").invoke(this.thread.get(name.toLowerCase()).process))});
|
||||||
|
terminator.waitFor();
|
||||||
|
if (terminator.exitValue() != 0) throw new IllegalStateException("taskkill exited with code " + terminator.exitValue());
|
||||||
|
success = true;
|
||||||
|
} catch (Exception e) {}
|
||||||
|
|
||||||
if (this.thread.get(name.toLowerCase()).process != null && this.thread.get(name.toLowerCase()).process.isAlive()) {
|
if (this.thread.get(name.toLowerCase()).process != null && this.thread.get(name.toLowerCase()).process.isAlive()) {
|
||||||
this.thread.get(name.toLowerCase()).process.destroyForcibly();
|
this.thread.get(name.toLowerCase()).process.destroyForcibly();
|
||||||
} else if (this.thread.get(name.toLowerCase()).isAlive()) {
|
success = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!success && this.thread.get(name.toLowerCase()).isAlive()) {
|
||||||
this.thread.get(name.toLowerCase()).interrupt();
|
this.thread.get(name.toLowerCase()).interrupt();
|
||||||
this.thread.remove(name.toLowerCase());
|
this.thread.remove(name.toLowerCase());
|
||||||
}
|
}
|
||||||
|
@ -167,6 +167,11 @@ public class SubServer {
|
|||||||
*/
|
*/
|
||||||
public void terminate() {
|
public void terminate() {
|
||||||
allowrestart = false;
|
allowrestart = false;
|
||||||
|
if (process != null && process.isAlive() && System.getProperty("os.name").toLowerCase().startsWith("windows")) try {
|
||||||
|
Process terminator = Runtime.getRuntime().exec(new String[]{"taskkill", "/T", "/F", "/PID", Long.toString((long) Process.class.getDeclaredMethod("pid").invoke(process))});
|
||||||
|
terminator.waitFor();
|
||||||
|
if (terminator.exitValue() != 0) throw new IllegalStateException("taskkill exited with code " + terminator.exitValue());
|
||||||
|
} catch (Exception e) {}
|
||||||
if (process != null && process.isAlive()) process.destroyForcibly();
|
if (process != null && process.isAlive()) process.destroyForcibly();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user