// Copyright 2018 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 static com.google.common.base.Preconditions.checkNotNull; import static java.nio.charset.StandardCharsets.UTF_8; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.devtools.build.lib.shell.Subprocess; import com.google.devtools.build.lib.shell.SubprocessBuilder; import com.google.devtools.build.lib.util.OS; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.List; import java.util.function.Function; import java.util.logging.Logger; import java.util.stream.Collectors; import javax.annotation.Nullable; /** A sandboxfs implementation that uses an external sandboxfs binary to manage the mount point. */ final class RealSandboxfsProcess implements SandboxfsProcess { private static final Logger log = Logger.getLogger(RealSandboxfsProcess.class.getName()); /** Directory on which the sandboxfs is serving. */ private final Path mountPoint; /** * Process handle to the sandboxfs instance. Null only after {@link #destroy()} has been invoked. */ private @Nullable Subprocess process; /** * Writer with which to send data to the sandboxfs instance. Null only after {@link #destroy()} * has been invoked. */ private @Nullable BufferedWriter processStdIn; /** * Reader with which to receive data from the sandboxfs instance. Null only after * {@link #destroy()} has been invoked. */ private @Nullable BufferedReader processStdOut; /** * Shutdown hook to stop the sandboxfs instance on abrupt termination. Null only after * {@link #destroy()} has been invoked. */ private @Nullable Thread shutdownHook; /** * Initializes a new sandboxfs process instance. * * @param process process handle for the already-running sandboxfs instance */ private RealSandboxfsProcess(Path mountPoint, Subprocess process) { this.mountPoint = mountPoint; this.process = process; this.processStdIn = new BufferedWriter( new OutputStreamWriter(process.getOutputStream(), UTF_8)); this.processStdOut = new BufferedReader( new InputStreamReader(process.getInputStream(), UTF_8)); this.shutdownHook = new Thread( () -> { try { this.destroy(); } catch (Exception e) { log.warning("Failed to destroy running sandboxfs instance; mount point may have " + "been left behind: " + e); } }); Runtime.getRuntime().addShutdownHook(shutdownHook); } /** * Mounts a new sandboxfs instance. * *
The root of the file system instance is left unmapped which means that it remains as
* read-only throughout the lifetime of this instance. Writable subdirectories can later be
* mapped via {@link #map(List)}.
*
* @param binary path to the sandboxfs binary. This is a {@link PathFragment} and not a
* {@link Path} because we want to support "bare" (non-absolute) names for the location of
* the sandboxfs binary; such names are automatically looked for in the {@code PATH}.
* @param mountPoint directory on which to mount the sandboxfs instance
* @param logFile path to the file that will receive all sandboxfs logging output
* @return a new handle that represents the running process
* @throws IOException if there is a problem starting the process
*/
static SandboxfsProcess mount(PathFragment binary, Path mountPoint, Path logFile)
throws IOException {
log.info("Mounting sandboxfs (" + binary + ") onto " + mountPoint);
// TODO(jmmv): Before starting a sandboxfs serving instance, we must query the current version
// of sandboxfs and check if we support its communication protocol.
ImmutableList.Builder