// 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.
   * @param useFakeHostname - whether the hostname should be set to 'localhost' inside the sandbox.
   * @param useFakeUsername - whether the username should be set to 'nobody' inside the sandbox.
   */
  void run(
      List<String> arguments,
      Map<String, String> environment,
      OutErr outErr,
      int timeout,
      boolean allowNetwork,
      boolean sandboxDebug,
      boolean useFakeHostname,
      boolean useFakeUsername)
      throws ExecException {
    Command cmd;
    try {
      cmd =
          getCommand(
              arguments, environment, timeout, allowNetwork, useFakeHostname, useFakeUsername);
    } 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, e, timedOut);
      }
      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.
   * @param useFakeHostname - whether the hostname should be set to 'localhost' inside the sandbox.
   * @param useFakeUsername - whether the username should be set to 'nobody' inside the sandbox.
   */
  protected abstract Command getCommand(
      List<String> arguments,
      Map<String, String> environment,
      int timeout,
      boolean allowNetwork,
      boolean useFakeHostname,
      boolean useFakeUsername)
      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 */
  }
}

@@ -3552,32 +3552,33 @@
 out());%0A      %7D%0A
+%0A
       String sta
@@ -3615,16 +3615,17 @@
 oxed%5D%22;%0A
+%0A
       if
@@ -3661,17 +3661,17 @@
  // 
-s
+S
 implest 
 erro
@@ -3666,16 +3666,25 @@
 implest 
+possible 
 error me
@@ -3688,16 +3688,17 @@
  message
+.
 %0A       
@@ -3759,24 +3759,25 @@
 t);%0A      %7D%0A
+%0A
       List%3CS
@@ -3794,16 +3794,28 @@
 mandList
+ = arguments
 ;%0A      
@@ -3818,17 +3818,16 @@
     if (
-!
 sandboxD
@@ -3846,47 +3846,138 @@
     
-commandList = arguments;%0A      %7D else %7B
+// When using --sandbox_debug, show the entire command-line including the part where we call%0A        // the sandbox helper binary.
 %0A   
@@ -4048,16 +4048,17 @@
       %7D%0A
+%0A
       St
@@ -4143,116 +4143,59 @@
 ure(
-%0A              true,%0A              commandList,%0A              environment,%0A             
+true, commandList, environment,
  null)
+;%0A
 %0A      
-    +
+if
  (
+!
 sand
@@ -4206,15 +4206,44 @@
 ebug
- ? %22%22 :
+) %7B%0A        commandFailureMessage +=
  SAN
@@ -4263,18 +4263,26 @@
 GGESTION
-)
 ;
+%0A      %7D%0A
 %0A      t

Clean up comments / code in SandboxRunner#run.