IDEA-CR-54283: Ability to gracefully terminate process by pid

GitOrigin-RevId: aefcfae5dec522b25efac2b171bd5c6b235fb9b0
This commit is contained in:
Dmitry Neverov
2019-11-07 02:40:58 +00:00
committed by intellij-monorepo-bot
parent 4de9a302de
commit 79ed8fd79b
@@ -10,6 +10,7 @@ import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jvnet.winp.WinProcess;
import org.jvnet.winp.WinpException;
import java.util.ArrayList;
import java.util.Collections;
@@ -83,6 +84,39 @@ public final class OSProcessUtil {
}
}
/**
* Terminates the process with the specified pid gracefully: on windows sends Ctrl-C,
* on unix sends the SIGINT signal.
*
* @throws UnsupportedOperationException if cannot interrupt the process
*
* @see KillableProcessHandler#destroyProcessGracefully()
*/
public static void terminateProcessGracefully(int pid) throws RuntimeException {
if (SystemInfo.isWindows) {
if (Registry.is("disable.winp")) {
throw new UnsupportedOperationException("Cannot terminate process, disable.winp=true");
}
else {
try {
// there is no need to check return value: `sendCtrlC` either returns
// true or throws exception.
//noinspection ResultOfMethodCallIgnored
createWinProcess(pid).sendCtrlC();
}
catch (WinpException e) {
throw new UnsupportedOperationException("Failed to terminate process", e);
}
}
}
else if (SystemInfo.isUnix) {
UnixProcessManager.sendSignal(pid, UnixProcessManager.SIGINT);
}
else {
throw new UnsupportedOperationException("Graceful termination is not supported for " + SystemInfo.getOsNameAndVersion());
}
}
static void logSkippedActionWithTerminatedProcess(@NotNull Process process, @NotNull String actionName, @Nullable String commandLine) {
Integer pid = null;
try {