Lukacs Berki | 8b074c0 | 2016-07-01 13:36:38 +0000 | [diff] [blame] | 1 | // Copyright 2016 The Bazel Authors. All rights reserved. |
| 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 | |
Lukacs Berki | 5a2655a | 2016-07-05 12:23:48 +0000 | [diff] [blame] | 17 | import java.io.Closeable; |
Lukacs Berki | 8b074c0 | 2016-07-01 13:36:38 +0000 | [diff] [blame] | 18 | import java.io.InputStream; |
| 19 | import java.io.OutputStream; |
| 20 | |
wilwell | c674101 | 2021-10-21 10:03:54 -0700 | [diff] [blame] | 21 | /** A process started by Bazel. */ |
Lukacs Berki | 5a2655a | 2016-07-05 12:23:48 +0000 | [diff] [blame] | 22 | public interface Subprocess extends Closeable { |
Lukacs Berki | 8b074c0 | 2016-07-01 13:36:38 +0000 | [diff] [blame] | 23 | |
wilwell | c674101 | 2021-10-21 10:03:54 -0700 | [diff] [blame] | 24 | /** Kill the process. */ |
Lukacs Berki | 8b074c0 | 2016-07-01 13:36:38 +0000 | [diff] [blame] | 25 | boolean destroy(); |
| 26 | |
| 27 | /** |
| 28 | * Returns the exit value of the process. |
| 29 | * |
larsrc | 28ae455 | 2020-07-23 03:13:55 -0700 | [diff] [blame] | 30 | * <p>Throws {@code IllegalThreadStateException} if the process has not terminated yet. |
Lukacs Berki | 8b074c0 | 2016-07-01 13:36:38 +0000 | [diff] [blame] | 31 | */ |
| 32 | int exitValue(); |
| 33 | |
| 34 | /** |
Lukacs Berki | 0451cf0 | 2016-07-01 14:32:53 +0000 | [diff] [blame] | 35 | * Returns the if the process has finished. |
larsrc | 28ae455 | 2020-07-23 03:13:55 -0700 | [diff] [blame] | 36 | * |
| 37 | * <p>This may cause the process to be destroyed as a side effect, for example due to a timeout. |
Lukacs Berki | 0451cf0 | 2016-07-01 14:32:53 +0000 | [diff] [blame] | 38 | */ |
| 39 | boolean finished(); |
| 40 | |
larsrc | 28ae455 | 2020-07-23 03:13:55 -0700 | [diff] [blame] | 41 | /** Returns true if the process is still alive. Does not block or cause any side effects. */ |
| 42 | boolean isAlive(); |
| 43 | |
wilwell | c674101 | 2021-10-21 10:03:54 -0700 | [diff] [blame] | 44 | /** Returns if the process timed out. */ |
Lukacs Berki | 3d97e22 | 2016-08-19 14:40:20 +0000 | [diff] [blame] | 45 | boolean timedout(); |
| 46 | |
wilwell | c674101 | 2021-10-21 10:03:54 -0700 | [diff] [blame] | 47 | /** Waits for the process to finish. */ |
Lukacs Berki | 0451cf0 | 2016-07-01 14:32:53 +0000 | [diff] [blame] | 48 | void waitFor() throws InterruptedException; |
Lukacs Berki | 8b074c0 | 2016-07-01 13:36:38 +0000 | [diff] [blame] | 49 | |
wilwell | c674101 | 2021-10-21 10:03:54 -0700 | [diff] [blame] | 50 | /** Returns a stream into which data can be written that the process will get on its stdin. */ |
Lukacs Berki | 8b074c0 | 2016-07-01 13:36:38 +0000 | [diff] [blame] | 51 | OutputStream getOutputStream(); |
| 52 | |
wilwell | c674101 | 2021-10-21 10:03:54 -0700 | [diff] [blame] | 53 | /** Returns a stream from which the stdout of the process can be read. */ |
Lukacs Berki | 8b074c0 | 2016-07-01 13:36:38 +0000 | [diff] [blame] | 54 | InputStream getInputStream(); |
| 55 | |
wilwell | c674101 | 2021-10-21 10:03:54 -0700 | [diff] [blame] | 56 | /** Returns a stream from which the stderr of the process can be read. */ |
Lukacs Berki | 8b074c0 | 2016-07-01 13:36:38 +0000 | [diff] [blame] | 57 | InputStream getErrorStream(); |
Lukacs Berki | 5a2655a | 2016-07-05 12:23:48 +0000 | [diff] [blame] | 58 | |
wilwell | c674101 | 2021-10-21 10:03:54 -0700 | [diff] [blame] | 59 | /** Returns the PID of the current process. */ |
| 60 | long getProcessId(); |
| 61 | |
Lukacs Berki | 5a2655a | 2016-07-05 12:23:48 +0000 | [diff] [blame] | 62 | /* |
| 63 | * Terminates the process as thoroughly as the underlying implementation allows and releases |
| 64 | * native data structures associated with the process. |
| 65 | */ |
| 66 | @Override |
| 67 | void close(); |
jmmv | de8d4c7 | 2019-09-12 11:12:25 -0700 | [diff] [blame] | 68 | |
jmmv | 5d1339d | 2019-10-21 18:08:53 -0700 | [diff] [blame] | 69 | /** Waits for the process to finish in a non-interruptible manner. */ |
| 70 | default void waitForUninterruptibly() { |
jmmv | de8d4c7 | 2019-09-12 11:12:25 -0700 | [diff] [blame] | 71 | boolean wasInterrupted = false; |
| 72 | try { |
| 73 | while (true) { |
| 74 | try { |
| 75 | waitFor(); |
| 76 | return; |
| 77 | } catch (InterruptedException ie) { |
| 78 | wasInterrupted = true; |
| 79 | } |
| 80 | } |
| 81 | } finally { |
| 82 | // Read this for detailed explanation: http://www.ibm.com/developerworks/library/j-jtp05236/ |
| 83 | if (wasInterrupted) { |
| 84 | Thread.currentThread().interrupt(); // preserve interrupted status |
| 85 | } |
| 86 | } |
| 87 | } |
jmmv | 5d1339d | 2019-10-21 18:08:53 -0700 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * Kills the subprocess and awaits for its termination so that we know it has released any |
| 91 | * resources it may have held. |
| 92 | */ |
| 93 | default void destroyAndWait() { |
| 94 | destroy(); |
| 95 | waitForUninterruptibly(); |
| 96 | } |
Lukacs Berki | 8b074c0 | 2016-07-01 13:36:38 +0000 | [diff] [blame] | 97 | } |