From 994ea128560743c126ec7431d68f983954829695 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torbj=C3=B6rn=20Svensson?= Date: Wed, 20 Oct 2021 20:53:24 +0200 Subject: [PATCH] Bug 574131: Make the graceful exit time configurable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Contributed by STMicroelectronics Change-Id: I8ca32b2440ef1cc81bf5b9ddd70b6b4222a29261 Signed-off-by: Torbjörn Svensson --- .../cdt/utils/spawner/ProcessFactory.java | 41 ++++++++++++++ .../eclipse/cdt/utils/spawner/Spawner.java | 53 ++++++++++++++++++- 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/spawner/ProcessFactory.java b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/spawner/ProcessFactory.java index a8dda6be4de..cab299b909e 100644 --- a/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/spawner/ProcessFactory.java +++ b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/spawner/ProcessFactory.java @@ -76,6 +76,16 @@ public class ProcessFactory { return runtime.exec(cmdarray); } + /** + * @since 6.2 + */ + public Process exec(String[] cmdarray, int gracefulExitTimeMs) throws IOException { + cmdarray = modifyCmdArrayIfFlatpak(cmdarray); + if (hasSpawner) + return new Spawner(cmdarray, gracefulExitTimeMs); + return runtime.exec(cmdarray); + } + public Process exec(String[] cmdarray, String[] envp) throws IOException { cmdarray = modifyCmdArrayIfFlatpak(cmdarray); if (hasSpawner) @@ -83,6 +93,16 @@ public class ProcessFactory { return runtime.exec(cmdarray, envp); } + /** + * @since 6.2 + */ + public Process exec(String[] cmdarray, String[] envp, int gracefulExitTimeMs) throws IOException { + cmdarray = modifyCmdArrayIfFlatpak(cmdarray); + if (hasSpawner) + return new Spawner(cmdarray, envp, gracefulExitTimeMs); + return runtime.exec(cmdarray, envp); + } + /** * @deprecated Do not use this method it splits command line arguments on whitespace with no regard to quoting rules. See Bug 573677 */ @@ -112,6 +132,16 @@ public class ProcessFactory { return runtime.exec(cmdarray, envp, dir); } + /** + * @since 6.2 + */ + public Process exec(String cmdarray[], String[] envp, File dir, int gracefulExitTimeMs) throws IOException { + cmdarray = modifyCmdArrayIfFlatpak(cmdarray); + if (hasSpawner) + return new Spawner(cmdarray, envp, dir, gracefulExitTimeMs); + return runtime.exec(cmdarray, envp, dir); + } + public Process exec(String cmdarray[], String[] envp, File dir, PTY pty) throws IOException { cmdarray = modifyCmdArrayIfFlatpak(cmdarray); if (hasSpawner) @@ -119,6 +149,17 @@ public class ProcessFactory { throw new UnsupportedOperationException(Messages.Util_exception_cannotCreatePty); } + /** + * @since 6.2 + */ + public Process exec(String cmdarray[], String[] envp, File dir, PTY pty, int gracefulExitTimeMs) + throws IOException { + cmdarray = modifyCmdArrayIfFlatpak(cmdarray); + if (hasSpawner) + return new Spawner(cmdarray, envp, dir, pty, gracefulExitTimeMs); + throw new UnsupportedOperationException(Messages.Util_exception_cannotCreatePty); + } + private String modifyCmdIfFlatpak(String cmd) { if (System.getenv("FLATPAK_SANDBOX_DIR") != null) { //$NON-NLS-1$ cmd = FLATPAK_CMD + cmd; diff --git a/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/spawner/Spawner.java b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/spawner/Spawner.java index 304fc6d2904..172a93aca0e 100644 --- a/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/spawner/Spawner.java +++ b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/spawner/Spawner.java @@ -81,6 +81,23 @@ public class Spawner extends Process { */ private final static int SIG_CTRLC = 1000; // arbitrary high number to avoid collision + private final static int DEFAULT_GRACEFUL_EXIT_TIME_MS; + + static { + String timeStr = System.getProperty("org.eclipse.cdt.core.graceful_exit_time_ms"); //$NON-NLS-1$ + int time = 1000; + if (timeStr != null) { + try { + time = Integer.parseInt(timeStr); + } catch (NumberFormatException e) { + CNativePlugin.log( + "Failed to parse system property. Falling back to " + time + " ms graceful exit time.", //$NON-NLS-1$ //$NON-NLS-2$ + e); + } + } + DEFAULT_GRACEFUL_EXIT_TIME_MS = time; + } + int pid = 0; int status; final IChannel[] fChannels = { null, null, null }; @@ -89,6 +106,8 @@ public class Spawner extends Process { InputStream err; private PTY fPty; + private final int fGracefulExitTimeMs; + private static enum State { RUNNING, DESTROYING, DONE } @@ -100,6 +119,7 @@ public class Spawner extends Process { */ @Deprecated public Spawner(String command, boolean bNoRedirect) throws IOException { + fGracefulExitTimeMs = DEFAULT_GRACEFUL_EXIT_TIME_MS; StringTokenizer tokenizer = new StringTokenizer(command); String[] cmdarray = new String[tokenizer.countTokens()]; for (int n = 0; tokenizer.hasMoreTokens(); n++) @@ -110,11 +130,17 @@ public class Spawner extends Process { exec(cmdarray, new String[0], "."); //$NON-NLS-1$ } + protected Spawner(String[] cmdarray, String[] envp, File dir) throws IOException { + this(cmdarray, envp, dir, DEFAULT_GRACEFUL_EXIT_TIME_MS); + } + /** * Executes the specified command and arguments in a separate process with the * specified environment and working directory. + * @since 6.2 **/ - protected Spawner(String[] cmdarray, String[] envp, File dir) throws IOException { + protected Spawner(String[] cmdarray, String[] envp, File dir, int gracefulExitTimeMs) throws IOException { + fGracefulExitTimeMs = gracefulExitTimeMs; String dirpath = "."; //$NON-NLS-1$ if (dir != null) dirpath = dir.getAbsolutePath(); @@ -122,6 +148,14 @@ public class Spawner extends Process { } protected Spawner(String[] cmdarray, String[] envp, File dir, PTY pty) throws IOException { + this(cmdarray, envp, dir, pty, DEFAULT_GRACEFUL_EXIT_TIME_MS); + } + + /** + * @since 6.2 + */ + protected Spawner(String[] cmdarray, String[] envp, File dir, PTY pty, int gracefulExitTimeMs) throws IOException { + fGracefulExitTimeMs = gracefulExitTimeMs; String dirpath = "."; //$NON-NLS-1$ if (dir != null) dirpath = dir.getAbsolutePath(); @@ -145,6 +179,13 @@ public class Spawner extends Process { this(cmdarray, null); } + /** + * @since 6.2 + */ + protected Spawner(String[] cmdarray, int gracefulExitTimeMs) throws IOException { + this(cmdarray, null, gracefulExitTimeMs); + } + /** * Executes the specified command and arguments in a separate process with the * specified environment. @@ -153,6 +194,13 @@ public class Spawner extends Process { this(cmdarray, envp, null); } + /** + * @since 6.2 + */ + protected Spawner(String[] cmdarray, String[] envp, int gracefulExitTimeMs) throws IOException { + this(cmdarray, envp, null, gracefulExitTimeMs); + } + /** * Executes the specified string command in a separate process with the specified * environment. @@ -170,6 +218,7 @@ public class Spawner extends Process { */ @Deprecated protected Spawner(String command, String[] envp, File dir) throws IOException { + fGracefulExitTimeMs = DEFAULT_GRACEFUL_EXIT_TIME_MS; StringTokenizer tokenizer = new StringTokenizer(command); String[] cmdarray = new String[tokenizer.countTokens()]; for (int n = 0; tokenizer.hasMoreTokens(); n++) @@ -314,7 +363,7 @@ public class Spawner extends Process { // Grace before using the heavy gun. if (fState != State.DONE) { try { - wait(1000); + wait(fGracefulExitTimeMs); } catch (InterruptedException e) { } }