1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
// Copyright 2016 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.sandbox;
import com.google.devtools.build.lib.actions.ExecException;
import com.google.devtools.build.lib.actions.UserExecException;
import com.google.devtools.build.lib.shell.AbnormalTerminationException;
import com.google.devtools.build.lib.shell.Command;
import com.google.devtools.build.lib.shell.CommandException;
import com.google.devtools.build.lib.shell.KillableObserver;
import com.google.devtools.build.lib.shell.TerminationStatus;
import com.google.devtools.build.lib.util.CommandFailureUtils;
import com.google.devtools.build.lib.util.io.OutErr;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/** A common interface of all sandbox runners, no matter which platform they're working on. */
abstract class SandboxRunner {
private static final String SANDBOX_DEBUG_SUGGESTION =
"\n\nUse --sandbox_debug to see verbose messages from the sandbox";
private final boolean verboseFailures;
SandboxRunner(boolean verboseFailures) {
this.verboseFailures = verboseFailures;
}
/**
* Runs the command specified via {@code arguments} and {@code env} inside the sandbox.
*
* @param arguments - arguments of spawn to run inside the sandbox.
* @param environment - environment variables to pass to the spawn.
* @param outErr - error output to capture sandbox's and command's stderr
* @param timeout - after how many seconds should the process be killed
* @param allowNetwork - whether networking should be allowed for the process
* @param sandboxDebug - whether debugging message should be printed
*/
void run(
List<String> arguments,
Map<String, String> environment,
OutErr outErr,
int timeout,
boolean allowNetwork,
boolean sandboxDebug)
throws ExecException {
Command cmd;
try {
cmd = getCommand(arguments, environment, timeout, allowNetwork);
} catch (IOException e) {
throw new UserExecException("I/O error during sandboxed execution", e);
}
TerminationStatus status = null;
try {
cmd.execute(
/* stdin */ new byte[] {},
getCommandObserver(timeout),
outErr.getOutputStream(),
outErr.getErrorStream(),
/* killSubprocessOnInterrupt */ true);
} catch (CommandException e) {
boolean timedOut = false;
if (e instanceof AbnormalTerminationException) {
status = ((AbnormalTerminationException) e).getResult().getTerminationStatus();
timedOut = !status.exited() && (status.getTerminatingSignal() == getSignalOnTimeout());
}
String statusMessage = status + " [sandboxed]";
if (!verboseFailures) {
// simplest error message
throw new UserExecException(statusMessage);
}
List<String> commandList;
if (!sandboxDebug) {
commandList = arguments;
} else {
commandList = Arrays.asList(cmd.getCommandLineElements());
}
String commandFailureMessage =
CommandFailureUtils.describeCommandFailure(
true,
commandList,
environment,
null)
+ (sandboxDebug ? "" : SANDBOX_DEBUG_SUGGESTION);
throw new UserExecException(commandFailureMessage, e, timedOut);
}
}
/**
* Returns the {@link Command} that the {@link #run} method will execute inside the sandbox.
*
* @param arguments - arguments of spawn to run inside the sandbox.
* @param environment - environment variables to pass to the spawn.
* @param timeout - after how many seconds should the process be killed
* @param allowNetwork - whether networking should be allowed for the process
*/
protected abstract Command getCommand(
List<String> arguments, Map<String, String> environment, int timeout, boolean allowNetwork)
throws IOException;
/**
* Returns a {@link KillableObserver} that the {@link #run} method will use when executing the
* command returned by {@link #getCommand}.
*/
protected KillableObserver getCommandObserver(int timeout) {
return Command.NO_OBSERVER;
}
/**
* Returns the signal code that the command returned by {@link #getCommand} exits with in case of
* a timeout.
*/
protected int getSignalOnTimeout() {
return 14; /* SIGALRM */
}
}
|