Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.shell; |
| 16 | |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 17 | import com.google.common.base.Preconditions; |
Lukacs Berki | 8b074c0 | 2016-07-01 13:36:38 +0000 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 19 | import com.google.common.io.ByteStreams; |
| 20 | import com.google.devtools.build.lib.shell.Consumers.OutErrConsumers; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import java.io.File; |
| 22 | import java.io.IOException; |
| 23 | import java.io.InputStream; |
| 24 | import java.io.OutputStream; |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 25 | import java.time.Duration; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 26 | import java.util.List; |
| 27 | import java.util.Map; |
| 28 | import java.util.logging.Level; |
| 29 | import java.util.logging.Logger; |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 30 | import javax.annotation.Nullable; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 31 | |
| 32 | /** |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 33 | * An executable command, including its arguments and runtime environment (environment variables, |
| 34 | * working directory). It lets a caller execute a command, get its results, and optionally forward |
| 35 | * interrupts to the subprocess. This class creates threads to ensure timely reading of subprocess |
| 36 | * outputs. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 37 | * |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 38 | * <p>This class is immutable and thread-safe. |
| 39 | * |
| 40 | * <p>The use of "shell" in the package name of this class is a misnomer. In terms of the way its |
| 41 | * arguments are interpreted, this class is closer to {@code execve(2)} than to {@code system(3)}. |
| 42 | * No shell is executed. |
| 43 | * |
| 44 | * <h4>Examples</h4> |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 45 | * |
| 46 | * <p>The most basic use-case for this class is as follows: |
| 47 | * <pre> |
| 48 | * String[] args = { "/bin/du", "-s", directory }; |
lberki | 4741aa8 | 2018-02-05 07:59:13 -0800 | [diff] [blame] | 49 | * BlazeCommandResult result = new Command(args).execute(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 50 | * String output = new String(result.getStdout()); |
| 51 | * </pre> |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 52 | * which writes the output of the {@code du(1)} command into {@code output}. More complex cases |
| 53 | * might inspect the stderr stream, kill the subprocess asynchronously, feed input to its standard |
| 54 | * input, handle the exceptions thrown if the command fails, or print the termination status (exit |
| 55 | * code or signal name). |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 56 | * |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 57 | * <h4>Other Features</h4> |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 58 | * |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 59 | * <p>A caller can optionally specify bytes to be written to the process's "stdin". The returned |
| 60 | * {@link CommandResult} object gives the caller access to the exit status, as well as output from |
| 61 | * "stdout" and "stderr". To use this class with processes that generate very large amounts of |
| 62 | * input/output, consider {@link #execute(OutputStream, OutputStream)}, |
| 63 | * {@link #executeAsync(OutputStream, OutputStream)}, or |
| 64 | * {@link #executeAsync(InputStream, OutputStream, OutputStream, boolean)}. |
| 65 | * |
| 66 | * <p>This class ensures that stdout and stderr streams are read promptly, avoiding potential |
| 67 | * deadlock if the output is large. See |
| 68 | * <a href="http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html"> when |
| 69 | * <code>Runtime.exec()</code> won't</a>. |
| 70 | * |
| 71 | * <h4>Caution: Invoking Shell Commands</h4> |
| 72 | * |
| 73 | * <p>Perhaps the most common command invoked programmatically is the UNIX shell, {@code /bin/sh}. |
| 74 | * Because the shell is a general-purpose programming language, care must be taken to ensure that |
| 75 | * variable parts of the shell command (e.g. strings entered by the user) do not contain shell |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 76 | * metacharacters, as this poses a correctness and/or security risk. |
| 77 | * |
| 78 | * <p>To execute a shell command directly, use the following pattern: |
| 79 | * <pre> |
| 80 | * String[] args = { "/bin/sh", "-c", shellCommand }; |
lberki | 4741aa8 | 2018-02-05 07:59:13 -0800 | [diff] [blame] | 81 | * BlazeCommandResult result = new Command(args).execute(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 82 | * </pre> |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 83 | * {@code shellCommand} is a complete Bourne shell program, possibly containing all kinds of |
| 84 | * unescaped metacharacters. For example, here's a shell command that enumerates the working |
| 85 | * directories of all processes named "foo": |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 86 | * <pre>ps auxx | grep foo | awk '{print $1}' | |
| 87 | * while read pid; do readlink /proc/$pid/cwd; done</pre> |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 88 | * It is the responsibility of the caller to ensure that this string means what they intend. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 89 | * |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 90 | * <p>Consider the risk posed by allowing the "foo" part of the previous command to be some |
| 91 | * arbitrary (untrusted) string called {@code processName}: |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 92 | * <pre> |
| 93 | * // WARNING: unsafe! |
| 94 | * String shellCommand = "ps auxx | grep " + processName + " | awk '{print $1}' | " |
| 95 | * + "while read pid; do readlink /proc/$pid/cwd; done";</pre> |
| 96 | * </pre> |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 97 | * Passing this string to {@link Command} is unsafe because if the string {@processName} contains |
| 98 | * shell metacharacters, the meaning of the command can be arbitrarily changed; consider: |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 99 | * <pre>String processName = ". ; rm -fr $HOME & ";</pre> |
| 100 | * |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 101 | * <p>To defend against this possibility, it is essential to properly quote the variable portions of |
| 102 | * the shell command so that shell metacharacters are escaped. Use {@link ShellUtils#shellEscape} |
| 103 | * for this purpose: |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 104 | * <pre> |
| 105 | * // Safe. |
| 106 | * String shellCommand = "ps auxx | grep " + ShellUtils.shellEscape(processName) |
| 107 | * + " | awk '{print $1}' | while read pid; do readlink /proc/$pid/cwd; done"; |
| 108 | * </pre> |
| 109 | * |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 110 | * <p>Tip: if you are only invoking a single known command, and no shell features (e.g. $PATH |
| 111 | * lookup, output redirection, pipelines, etc) are needed, call it directly without using a shell, |
| 112 | * as in the {@code du(1)} example above. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 113 | */ |
| 114 | public final class Command { |
| 115 | |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 116 | private static final Logger logger = |
| 117 | Logger.getLogger("com.google.devtools.build.lib.shell.Command"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 118 | |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 119 | /** Pass this value to {@link #execute} to indicate that no input should be written to stdin. */ |
| 120 | public static final InputStream NO_INPUT = new NullInputStream(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 121 | |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 122 | public static final boolean KILL_SUBPROCESS_ON_INTERRUPT = true; |
| 123 | public static final boolean CONTINUE_SUBPROCESS_ON_INTERRUPT = false; |
| 124 | |
Lukacs Berki | 8b074c0 | 2016-07-01 13:36:38 +0000 | [diff] [blame] | 125 | private final SubprocessBuilder subprocessBuilder; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 126 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 127 | /** |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 128 | * Creates a new {@link Command} for the given command line. The environment is inherited from the |
| 129 | * current process, as is the working directory. No timeout is enforced. The command line is |
| 130 | * executed exactly as given, without a shell. Subsequent calls to {@link #execute()} will use the |
| 131 | * JVM's working directory and environment. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 132 | * |
| 133 | * @param commandLineElements elements of raw command line to execute |
| 134 | * @throws IllegalArgumentException if commandLine is null or empty |
| 135 | */ |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 136 | public Command(String[] commandLineElements) { |
| 137 | this(commandLineElements, null, null, Duration.ZERO); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 141 | * Just like {@link #Command(String[], Map, File, Duration)}, but without a timeout. |
Lukacs Berki | 3d97e22 | 2016-08-19 14:40:20 +0000 | [diff] [blame] | 142 | */ |
| 143 | public Command( |
| 144 | String[] commandLineElements, |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 145 | @Nullable Map<String, String> environmentVariables, |
| 146 | @Nullable File workingDirectory) { |
| 147 | this(commandLineElements, environmentVariables, workingDirectory, Duration.ZERO); |
Lukacs Berki | 3d97e22 | 2016-08-19 14:40:20 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 151 | * Creates a new {@link Command} for the given command line elements. The command line is executed |
| 152 | * without a shell. |
Dmitry Lomov | 8efc3ef | 2016-02-17 09:50:03 +0000 | [diff] [blame] | 153 | * |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 154 | * <p>The given environment variables and working directory are used in subsequent calls to |
| 155 | * {@link #execute()}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 156 | * |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 157 | * <p>This command treats the 0-th element of {@code commandLineElement} (the name of an |
| 158 | * executable to run) specially. |
Dmitry Lomov | 8efc3ef | 2016-02-17 09:50:03 +0000 | [diff] [blame] | 159 | * <ul> |
| 160 | * <li>If it is an absolute path, it is used as it</li> |
| 161 | * <li>If it is a single file name, the PATH lookup is performed</li> |
| 162 | * <li>If it is a relative path that is not a single file name, the command will attempt to |
| 163 | * execute the the binary at that path relative to {@code workingDirectory}.</li> |
| 164 | * </ul> |
| 165 | * |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 166 | * @param commandLineElements elements of raw command line to execute |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 167 | * @param environmentVariables environment variables to replace JVM's environment variables; may |
| 168 | * be null |
| 169 | * @param workingDirectory working directory for execution; if null, the VM's current working |
| 170 | * directory is used |
| 171 | * @param timeout timeout; a value less than or equal to 0 is treated as no timeout |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 172 | * @throws IllegalArgumentException if commandLine is null or empty |
| 173 | */ |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 174 | // TODO(ulfjack): Throw a special exception if there was a timeout. |
Dmitry Lomov | 8efc3ef | 2016-02-17 09:50:03 +0000 | [diff] [blame] | 175 | public Command( |
| 176 | String[] commandLineElements, |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 177 | @Nullable Map<String, String> environmentVariables, |
| 178 | @Nullable File workingDirectory, |
| 179 | Duration timeout) { |
| 180 | Preconditions.checkNotNull(commandLineElements); |
| 181 | Preconditions.checkArgument( |
| 182 | commandLineElements.length != 0, "cannot run an empty command line"); |
Dmitry Lomov | 8efc3ef | 2016-02-17 09:50:03 +0000 | [diff] [blame] | 183 | |
| 184 | File executable = new File(commandLineElements[0]); |
| 185 | if (!executable.isAbsolute() && executable.getParent() != null) { |
| 186 | commandLineElements = commandLineElements.clone(); |
| 187 | commandLineElements[0] = new File(workingDirectory, commandLineElements[0]).getAbsolutePath(); |
| 188 | } |
| 189 | |
Lukacs Berki | 8b074c0 | 2016-07-01 13:36:38 +0000 | [diff] [blame] | 190 | this.subprocessBuilder = new SubprocessBuilder(); |
| 191 | subprocessBuilder.setArgv(ImmutableList.copyOf(commandLineElements)); |
| 192 | subprocessBuilder.setEnv(environmentVariables); |
| 193 | subprocessBuilder.setWorkingDirectory(workingDirectory); |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 194 | subprocessBuilder.setTimeoutMillis(timeout.toMillis()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 195 | } |
| 196 | |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 197 | /** Returns the raw command line elements to be executed */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 198 | public String[] getCommandLineElements() { |
Lukacs Berki | 8b074c0 | 2016-07-01 13:36:38 +0000 | [diff] [blame] | 199 | final List<String> elements = subprocessBuilder.getArgv(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 200 | return elements.toArray(new String[elements.size()]); |
| 201 | } |
| 202 | |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 203 | /** Returns an (unmodifiable) {@link Map} view of command's environment variables or null. */ |
| 204 | @Nullable public Map<String, String> getEnvironmentVariables() { |
Lukacs Berki | 8b074c0 | 2016-07-01 13:36:38 +0000 | [diff] [blame] | 205 | return subprocessBuilder.getEnv(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 206 | } |
| 207 | |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 208 | /** Returns the working directory to be used for execution, or null. */ |
| 209 | @Nullable public File getWorkingDirectory() { |
Lukacs Berki | 8b074c0 | 2016-07-01 13:36:38 +0000 | [diff] [blame] | 210 | return subprocessBuilder.getWorkingDirectory(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /** |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 214 | * Execute this command with no input to stdin, and with the output captured in memory. If the |
| 215 | * current process is interrupted, then the subprocess is also interrupted. This call blocks until |
| 216 | * the subprocess completes or an error occurs. |
| 217 | * |
| 218 | * <p>This method is a convenience wrapper for <code>executeAsync().get()</code>. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 219 | * |
| 220 | * @return {@link CommandResult} representing result of the execution |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 221 | * @throws ExecFailedException if {@link Runtime#exec(String[])} fails for any reason |
| 222 | * @throws AbnormalTerminationException if an {@link IOException} is encountered while reading |
| 223 | * from the process, or the process was terminated due to a signal |
| 224 | * @throws BadExitStatusException if the process exits with a non-zero status |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 225 | */ |
| 226 | public CommandResult execute() throws CommandException { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 227 | return executeAsync().get(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /** |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 231 | * Execute this command with no input to stdin, and with the output streamed to the given output |
| 232 | * streams, which must be thread-safe. If the current process is interrupted, then the subprocess |
| 233 | * is also interrupted. This call blocks until the subprocess completes or an error occurs. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 234 | * |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 235 | * <p>Note that the given output streams are never closed by this class. |
| 236 | * |
| 237 | * <p>This method is a convenience wrapper for <code>executeAsync(stdOut, stdErr).get()</code>. |
| 238 | * |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 239 | * @return {@link CommandResult} representing result of the execution |
Philipp Wollermann | 1e37a53 | 2016-06-30 08:45:05 +0000 | [diff] [blame] | 240 | * @throws ExecFailedException if {@link Runtime#exec(String[])} fails for any reason |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 241 | * @throws AbnormalTerminationException if an {@link IOException} is encountered while reading |
| 242 | * from the process, or the process was terminated due to a signal |
Philipp Wollermann | 1e37a53 | 2016-06-30 08:45:05 +0000 | [diff] [blame] | 243 | * @throws BadExitStatusException if the process exits with a non-zero status |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 244 | */ |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 245 | public CommandResult execute(OutputStream stdOut, OutputStream stdErr) throws CommandException { |
| 246 | return doExecute( |
| 247 | NO_INPUT, Consumers.createStreamingConsumers(stdOut, stdErr), KILL_SUBPROCESS_ON_INTERRUPT) |
| 248 | .get(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /** |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 252 | * Execute this command with no input to stdin, and with the output captured in memory. If the |
| 253 | * current process is interrupted, then the subprocess is also interrupted. This call blocks until |
| 254 | * the subprocess is started or throws an error if that fails, but does not wait for the |
| 255 | * subprocess to exit. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 256 | * |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 257 | * @return {@link CommandResult} representing result of the execution |
Philipp Wollermann | 1e37a53 | 2016-06-30 08:45:05 +0000 | [diff] [blame] | 258 | * @throws ExecFailedException if {@link Runtime#exec(String[])} fails for any reason |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 259 | * @throws AbnormalTerminationException if an {@link IOException} is encountered while reading |
| 260 | * from the process, or the process was terminated due to a signal |
| 261 | * @throws BadExitStatusException if the process exits with a non-zero status |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 262 | */ |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 263 | public FutureCommandResult executeAsync() throws CommandException { |
| 264 | return doExecute( |
| 265 | NO_INPUT, Consumers.createAccumulatingConsumers(), KILL_SUBPROCESS_ON_INTERRUPT); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /** |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 269 | * Execute this command with no input to stdin, and with the output streamed to the given output |
| 270 | * streams, which must be thread-safe. If the current process is interrupted, then the subprocess |
| 271 | * is also interrupted. This call blocks until the subprocess is started or throws an error if |
| 272 | * that fails, but does not wait for the subprocess to exit. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 273 | * |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 274 | * <p>Note that the given output streams are never closed by this class. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 275 | * |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 276 | * @return {@link CommandResult} representing result of the execution |
| 277 | * @throws ExecFailedException if {@link Runtime#exec(String[])} fails for any reason |
| 278 | * @throws AbnormalTerminationException if an {@link IOException} is encountered while reading |
| 279 | * from the process, or the process was terminated due to a signal |
| 280 | * @throws BadExitStatusException if the process exits with a non-zero status |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 281 | */ |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 282 | public FutureCommandResult executeAsync(OutputStream stdOut, OutputStream stdErr) |
| 283 | throws CommandException { |
| 284 | return doExecute( |
| 285 | NO_INPUT, Consumers.createStreamingConsumers(stdOut, stdErr), KILL_SUBPROCESS_ON_INTERRUPT); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /** |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 289 | * Execute this command with no input to stdin, and with the output captured in memory. This call |
| 290 | * blocks until the subprocess is started or throws an error if that fails, but does not wait for |
| 291 | * the subprocess to exit. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 292 | * |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 293 | * @param killSubprocessOnInterrupt whether the subprocess should be killed if the current process |
| 294 | * is interrupted |
| 295 | * @return {@link CommandResult} representing result of the execution |
| 296 | * @throws ExecFailedException if {@link Runtime#exec(String[])} fails for any reason |
| 297 | * @throws AbnormalTerminationException if an {@link IOException} is encountered while reading |
| 298 | * from the process, or the process was terminated due to a signal |
| 299 | * @throws BadExitStatusException if the process exits with a non-zero status |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 300 | */ |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 301 | public FutureCommandResult executeAsync( |
| 302 | InputStream stdinInput, boolean killSubprocessOnInterrupt) throws CommandException { |
| 303 | return doExecute( |
| 304 | stdinInput, Consumers.createAccumulatingConsumers(), killSubprocessOnInterrupt); |
Eric Fellheimer | 9a6c2eb | 2016-02-19 15:48:42 +0000 | [diff] [blame] | 305 | } |
Michajlo Matijkiw | c3386bf | 2016-08-29 19:10:53 +0000 | [diff] [blame] | 306 | |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 307 | /** |
| 308 | * Execute this command with no input to stdin, and with the output streamed to the given output |
| 309 | * streams, which must be thread-safe. This call blocks until the subprocess is started or throws |
| 310 | * an error if that fails, but does not wait for the subprocess to exit. |
| 311 | * |
| 312 | * <p>Note that the given output streams are never closed by this class. |
| 313 | * |
| 314 | * @param killSubprocessOnInterrupt whether the subprocess should be killed if the current process |
| 315 | * is interrupted |
| 316 | * @return {@link CommandResult} representing result of the execution |
| 317 | * @throws ExecFailedException if {@link Runtime#exec(String[])} fails for any reason |
| 318 | * @throws AbnormalTerminationException if an {@link IOException} is encountered while reading |
| 319 | * from the process, or the process was terminated due to a signal |
| 320 | * @throws BadExitStatusException if the process exits with a non-zero status |
| 321 | */ |
| 322 | public FutureCommandResult executeAsync( |
| 323 | InputStream stdinInput, |
| 324 | OutputStream stdOut, |
| 325 | OutputStream stdErr, |
| 326 | boolean killSubprocessOnInterrupt) |
| 327 | throws CommandException { |
| 328 | return doExecute( |
| 329 | stdinInput, Consumers.createStreamingConsumers(stdOut, stdErr), killSubprocessOnInterrupt); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 330 | } |
| 331 | |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 332 | /** |
| 333 | * A string representation of this command object which includes the arguments, the environment, |
| 334 | * and the working directory. Avoid relying on the specifics of this format. Note that the size of |
| 335 | * the result string will reflect the size of the command. |
| 336 | */ |
| 337 | public String toDebugString() { |
| 338 | StringBuilder message = new StringBuilder(128); |
| 339 | message.append("Executing (without brackets):"); |
| 340 | for (String arg : subprocessBuilder.getArgv()) { |
| 341 | message.append(" ["); |
| 342 | message.append(arg); |
| 343 | message.append(']'); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 344 | } |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 345 | message.append("; environment: "); |
| 346 | message.append(subprocessBuilder.getEnv()); |
| 347 | message.append("; working dir: "); |
| 348 | File workingDirectory = subprocessBuilder.getWorkingDirectory(); |
| 349 | message.append(workingDirectory == null ? |
| 350 | "(current)" : |
| 351 | workingDirectory.toString()); |
| 352 | return message.toString(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 353 | } |
| 354 | |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 355 | private FutureCommandResult doExecute( |
| 356 | InputStream stdinInput, OutErrConsumers outErrConsumers, boolean killSubprocessOnInterrupt) |
| 357 | throws ExecFailedException { |
| 358 | Preconditions.checkNotNull(stdinInput, "stdinInput"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 359 | logCommand(); |
| 360 | |
ulfjack | 27758e4 | 2017-09-07 13:41:33 +0200 | [diff] [blame] | 361 | Subprocess process = startProcess(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 362 | |
Dmitry Lomov | 9bf3f6a | 2016-07-07 13:50:06 +0000 | [diff] [blame] | 363 | outErrConsumers.logConsumptionStrategy(); |
Dmitry Lomov | 9bf3f6a | 2016-07-07 13:50:06 +0000 | [diff] [blame] | 364 | outErrConsumers.registerInputs( |
cushon | 03e7018 | 2017-09-15 09:33:27 +0200 | [diff] [blame] | 365 | process.getInputStream(), process.getErrorStream(), /* closeStreams= */ false); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 366 | |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 367 | // TODO(ulfjack): This call blocks until all input is written. If stdinInput is large (or |
| 368 | // unbounded), then the async calls can block for a long time, and the timeout is not properly |
| 369 | // enforced. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 370 | processInput(stdinInput, process); |
| 371 | |
ulfjack | 27758e4 | 2017-09-07 13:41:33 +0200 | [diff] [blame] | 372 | return new FutureCommandResultImpl(this, process, outErrConsumers, killSubprocessOnInterrupt); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 373 | } |
| 374 | |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 375 | private Subprocess startProcess() throws ExecFailedException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 376 | try { |
Lukacs Berki | 8b074c0 | 2016-07-01 13:36:38 +0000 | [diff] [blame] | 377 | return subprocessBuilder.start(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 378 | } catch (IOException ioe) { |
| 379 | throw new ExecFailedException(this, ioe); |
| 380 | } |
| 381 | } |
| 382 | |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 383 | private static class NullInputStream extends InputStream { |
| 384 | @Override |
| 385 | public int read() { |
| 386 | return -1; |
| 387 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 388 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 389 | @Override |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 390 | public int available() { |
| 391 | return 0; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 395 | private static void processInput(InputStream stdinInput, Subprocess process) { |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 396 | if (logger.isLoggable(Level.FINER)) { |
| 397 | logger.finer(stdinInput.toString()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 398 | } |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 399 | try (OutputStream out = process.getOutputStream()) { |
| 400 | ByteStreams.copy(stdinInput, out); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 401 | } catch (IOException ioe) { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 402 | // Note: this is not an error! Perhaps the command just isn't hungry for our input and exited |
| 403 | // with success. Process.waitFor (later) will tell us. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 404 | // |
| 405 | // (Unlike out/err streams, which are read asynchronously, the input stream is written |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 406 | // synchronously, in its entirety, before processInput returns. If the input is infinite, and |
| 407 | // is passed through e.g. "cat" subprocess and back into the ByteArrayOutputStream, that will |
| 408 | // eventually run out of memory, causing the output stream to be closed, "cat" to terminate |
| 409 | // with SIGPIPE, and processInput to receive an IOException. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 413 | private void logCommand() { |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 414 | if (!logger.isLoggable(Level.FINE)) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 415 | return; |
| 416 | } |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 417 | logger.fine(toDebugString()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 418 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 419 | } |