blob: 0eeada1de5206d0b49fe40d301d5c54fafe320be [file] [log] [blame]
Lukacs Berki8b074c02016-07-01 13:36:38 +00001// 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
15package com.google.devtools.build.lib.shell;
16
Lukacs Berki5a2655a2016-07-05 12:23:48 +000017import java.io.Closeable;
Lukacs Berki8b074c02016-07-01 13:36:38 +000018import java.io.InputStream;
19import java.io.OutputStream;
20
wilwellc6741012021-10-21 10:03:54 -070021/** A process started by Bazel. */
Lukacs Berki5a2655a2016-07-05 12:23:48 +000022public interface Subprocess extends Closeable {
Lukacs Berki8b074c02016-07-01 13:36:38 +000023
wilwellc6741012021-10-21 10:03:54 -070024 /** Kill the process. */
Lukacs Berki8b074c02016-07-01 13:36:38 +000025 boolean destroy();
26
27 /**
28 * Returns the exit value of the process.
29 *
larsrc28ae4552020-07-23 03:13:55 -070030 * <p>Throws {@code IllegalThreadStateException} if the process has not terminated yet.
Lukacs Berki8b074c02016-07-01 13:36:38 +000031 */
32 int exitValue();
33
34 /**
Lukacs Berki0451cf02016-07-01 14:32:53 +000035 * Returns the if the process has finished.
larsrc28ae4552020-07-23 03:13:55 -070036 *
37 * <p>This may cause the process to be destroyed as a side effect, for example due to a timeout.
Lukacs Berki0451cf02016-07-01 14:32:53 +000038 */
39 boolean finished();
40
larsrc28ae4552020-07-23 03:13:55 -070041 /** Returns true if the process is still alive. Does not block or cause any side effects. */
42 boolean isAlive();
43
wilwellc6741012021-10-21 10:03:54 -070044 /** Returns if the process timed out. */
Lukacs Berki3d97e222016-08-19 14:40:20 +000045 boolean timedout();
46
wilwellc6741012021-10-21 10:03:54 -070047 /** Waits for the process to finish. */
Lukacs Berki0451cf02016-07-01 14:32:53 +000048 void waitFor() throws InterruptedException;
Lukacs Berki8b074c02016-07-01 13:36:38 +000049
wilwellc6741012021-10-21 10:03:54 -070050 /** Returns a stream into which data can be written that the process will get on its stdin. */
Lukacs Berki8b074c02016-07-01 13:36:38 +000051 OutputStream getOutputStream();
52
wilwellc6741012021-10-21 10:03:54 -070053 /** Returns a stream from which the stdout of the process can be read. */
Lukacs Berki8b074c02016-07-01 13:36:38 +000054 InputStream getInputStream();
55
wilwellc6741012021-10-21 10:03:54 -070056 /** Returns a stream from which the stderr of the process can be read. */
Lukacs Berki8b074c02016-07-01 13:36:38 +000057 InputStream getErrorStream();
Lukacs Berki5a2655a2016-07-05 12:23:48 +000058
wilwellc6741012021-10-21 10:03:54 -070059 /** Returns the PID of the current process. */
60 long getProcessId();
61
Lukacs Berki5a2655a2016-07-05 12:23:48 +000062 /*
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();
jmmvde8d4c72019-09-12 11:12:25 -070068
jmmv5d1339d2019-10-21 18:08:53 -070069 /** Waits for the process to finish in a non-interruptible manner. */
70 default void waitForUninterruptibly() {
jmmvde8d4c72019-09-12 11:12:25 -070071 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 }
jmmv5d1339d2019-10-21 18:08:53 -070088
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 Berki8b074c02016-07-01 13:36:38 +000097}