use of com.google.devtools.build.lib.vfs.PathFragment in project bazel by bazelbuild.
the class DarwinSandboxedStrategy method actuallyExec.
private void actuallyExec(Spawn spawn, ActionExecutionContext actionExecutionContext, AtomicReference<Class<? extends SpawnActionContext>> writeOutputFiles) throws ExecException, InterruptedException {
Executor executor = actionExecutionContext.getExecutor();
SandboxHelpers.reportSubcommand(executor, spawn);
PrintWriter errWriter = sandboxDebug ? new PrintWriter(actionExecutionContext.getFileOutErr().getErrorStream()) : null;
// Each invocation of "exec" gets its own sandbox.
Path sandboxPath = SandboxHelpers.getSandboxRoot(blazeDirs, productName, uuid, execCounter);
Path sandboxExecRoot = sandboxPath.getRelative("execroot").getRelative(execRoot.getBaseName());
if (errWriter != null) {
errWriter.printf("sandbox root is %s\n", sandboxPath.toString());
errWriter.printf("working dir is %s\n", sandboxExecRoot.toString());
}
ImmutableMap<String, String> spawnEnvironment = StandaloneSpawnStrategy.locallyDeterminedEnv(execRoot, productName, spawn.getEnvironment());
Set<Path> writableDirs = getWritableDirs(sandboxExecRoot, spawn.getEnvironment());
Path runUnderPath = getRunUnderPath(spawn);
HardlinkedExecRoot hardlinkedExecRoot = new HardlinkedExecRoot(execRoot, sandboxPath, sandboxExecRoot, errWriter);
ImmutableSet<PathFragment> outputs = SandboxHelpers.getOutputFiles(spawn);
try {
hardlinkedExecRoot.createFileSystem(getMounts(spawn, actionExecutionContext), outputs, writableDirs);
} catch (IOException e) {
throw new UserExecException("Could not prepare sandbox directory", e);
}
// Flush our logs before executing the spawn, otherwise they might get overwritten.
if (errWriter != null) {
errWriter.flush();
}
DarwinSandboxRunner runner = new DarwinSandboxRunner(sandboxPath, sandboxExecRoot, getWritableDirs(sandboxExecRoot, spawnEnvironment), getInaccessiblePaths(), runUnderPath, verboseFailures);
try {
runSpawn(spawn, actionExecutionContext, spawnEnvironment, hardlinkedExecRoot, outputs, runner, writeOutputFiles);
} finally {
if (!sandboxDebug) {
try {
FileSystemUtils.deleteTree(sandboxPath);
} catch (IOException e) {
executor.getEventHandler().handle(Event.warn(String.format("Cannot delete sandbox directory after action execution: %s (%s)", sandboxPath.getPathString(), e)));
}
}
}
}
use of com.google.devtools.build.lib.vfs.PathFragment in project bazel by bazelbuild.
the class DarwinSandboxedStrategy method getMounts.
@Override
public Map<PathFragment, Path> getMounts(Spawn spawn, ActionExecutionContext executionContext) throws ExecException {
try {
Map<PathFragment, Path> mounts = new HashMap<>();
spawnHelpers.mountInputs(mounts, spawn, executionContext);
Map<PathFragment, Path> unfinalized = new HashMap<>();
spawnHelpers.mountRunfilesFromSuppliers(unfinalized, spawn);
spawnHelpers.mountFilesFromFilesetManifests(unfinalized, spawn, executionContext);
mounts.putAll(finalizeLinks(unfinalized));
return mounts;
} catch (IllegalArgumentException | IOException e) {
throw new EnvironmentalExecException("Could not prepare mounts for sandbox execution", e);
}
}
use of com.google.devtools.build.lib.vfs.PathFragment in project bazel by bazelbuild.
the class HardlinkedExecRoot method linkInputs.
/**
* Make all specified inputs available in the sandbox.
*
* <p>We want the sandboxed process to have access only to these input files and not anything else
* from the workspace. Furthermore, the process should not be able to modify these input files. We
* achieve this by hardlinking all input files into a temporary "inputs" directory, then
* symlinking them into their correct place inside the sandbox.
*
* <p>The hardlinks / symlinks combination (as opposed to simply directly hardlinking to the final
* destination) is necessary, because we build a solib symlink tree for shared libraries where the
* original file and the created symlink have two different file names (libblaze_util.so vs.
* src_Stest_Scpp_Sblaze_Uutil_Utest.so) and our cc_wrapper.sh needs to be able to figure out both
* names (by following solib symlinks back) to modify the paths to the shared libraries in
* cc_binaries.
*/
private void linkInputs(Map<PathFragment, Path> inputs, Set<Path> createdDirs) throws IOException {
// Create directory for input files.
Path inputsDir = sandboxPath.getRelative("inputs");
if (!inputsDir.exists()) {
inputsDir.createDirectory();
}
for (ImmutableMap.Entry<PathFragment, Path> entry : inputs.entrySet()) {
Path targetName = sandboxExecRoot.getRelative(entry.getKey());
FileSystemUtils.createDirectoryAndParentsWithCache(createdDirs, targetName.getParentDirectory());
// The target is supposed to be an empty file.
if (entry.getValue() == null) {
FileSystemUtils.createEmptyFile(targetName);
continue;
}
// Hardlink, resolve symlink here instead in finalizeLinks.
Path target = entry.getValue().resolveSymbolicLinks();
Path hardlinkName = target.startsWith(execRoot) ? inputsDir.getRelative(target.relativeTo(execRoot)) : inputsDir.getRelative(entry.getKey());
if (errWriter != null) {
errWriter.printf("hardlink: %s -> %s\n", hardlinkName, target);
}
try {
createHardLink(hardlinkName, target);
} catch (IOException e) {
// Creating a hardlink might fail when the input file and the sandbox directory are not on
// the same filesystem / device. Then we use symlink instead.
hardlinkName.createSymbolicLink(target);
}
// symlink
if (errWriter != null) {
errWriter.printf("symlink: %s -> %s\n", targetName, hardlinkName);
}
targetName.createSymbolicLink(hardlinkName);
}
}
use of com.google.devtools.build.lib.vfs.PathFragment in project bazel by bazelbuild.
the class HardlinkedExecRoot method copyOutputs.
@Override
public void copyOutputs(Path execRoot, Collection<PathFragment> outputs) throws IOException {
for (PathFragment output : outputs) {
Path source = sandboxExecRoot.getRelative(output);
Path target = execRoot.getRelative(output);
if (source.isFile() || source.isSymbolicLink()) {
Files.move(source.getPathFile(), target.getPathFile());
} else if (source.isDirectory()) {
try {
source.renameTo(target);
} catch (IOException e) {
// Failed to move directory directly, thus move it recursively.
target.createDirectory();
FileSystemUtils.moveTreesBelow(source, target);
}
}
}
}
use of com.google.devtools.build.lib.vfs.PathFragment in project bazel by bazelbuild.
the class LinuxSandboxRunner method isSupported.
static boolean isSupported(CommandEnvironment commandEnv) {
Path execRoot = commandEnv.getExecRoot();
PathFragment embeddedTool = commandEnv.getBlazeWorkspace().getBinTools().getExecPath(LINUX_SANDBOX);
if (embeddedTool == null) {
// bootstrapping).
return false;
}
List<String> args = new ArrayList<>();
args.add(execRoot.getRelative(embeddedTool).getPathString());
args.add("-C");
ImmutableMap<String, String> env = ImmutableMap.of();
File cwd = execRoot.getPathFile();
Command cmd = new Command(args.toArray(new String[0]), env, cwd);
try {
cmd.execute(/* stdin */
new byte[] {}, Command.NO_OBSERVER, ByteStreams.nullOutputStream(), ByteStreams.nullOutputStream(), /* killSubprocessOnInterrupt */
true);
} catch (CommandException e) {
return false;
}
return true;
}
Aggregations