Add the ability to customize the bazel client's exit code used when the bazel server exits abruptly.

--
MOS_MIGRATED_REVID=111641619
diff --git a/scripts/bootstrap/compile.sh b/scripts/bootstrap/compile.sh
index ea21eba..abb03ce 100755
--- a/scripts/bootstrap/compile.sh
+++ b/scripts/bootstrap/compile.sh
@@ -32,6 +32,7 @@
 esac
 
 BLAZE_CC_FILES=(
+src/main/cpp/blaze_abrupt_exit.cc
 src/main/cpp/blaze_startup_options.cc
 src/main/cpp/blaze_startup_options_common.cc
 src/main/cpp/blaze_util.cc
diff --git a/src/main/cpp/BUILD b/src/main/cpp/BUILD
index e6b3c74..3fb6719 100644
--- a/src/main/cpp/BUILD
+++ b/src/main/cpp/BUILD
@@ -51,10 +51,24 @@
     ],
 )
 
+cc_library(
+    name = "blaze_abrupt_exit",
+    srcs = [
+        "blaze_abrupt_exit.cc",
+    ],
+    hdrs = [
+        "blaze_abrupt_exit.h",
+    ],
+    deps = [
+        "//src/main/cpp/util:blaze_exit_code",
+    ],
+)
+
 cc_binary(
     name = "client",
     srcs = [
         "blaze.cc",
+        "blaze_globals.h",
         "blaze_startup_options.cc",
         "blaze_startup_options.h",
         "blaze_startup_options_common.cc",
@@ -80,6 +94,7 @@
     }),
     visibility = ["//src:__pkg__"],
     deps = [
+        ":blaze_abrupt_exit",
         ":blaze_util",
         "//src/main/cpp/util",
         "//src/main/cpp/util:md5",
diff --git a/src/main/cpp/blaze.cc b/src/main/cpp/blaze.cc
index 0651351..7548c89 100644
--- a/src/main/cpp/blaze.cc
+++ b/src/main/cpp/blaze.cc
@@ -54,6 +54,8 @@
 #include <utility>
 #include <vector>
 
+#include "src/main/cpp/blaze_abrupt_exit.h"
+#include "src/main/cpp/blaze_globals.h"
 #include "src/main/cpp/blaze_startup_options.h"
 #include "src/main/cpp/blaze_util.h"
 #include "src/main/cpp/blaze_util_platform.h"
@@ -82,70 +84,6 @@
 
 ////////////////////////////////////////////////////////////////////////
 // Global Variables
-
-// The reason for a blaze server restart.
-// Keep in sync with logging.proto
-enum RestartReason {
-  NO_RESTART = 0,
-  NO_DAEMON,
-  NEW_VERSION,
-  NEW_OPTIONS
-};
-
-struct GlobalVariables {
-  // Used to make concurrent invocations of this program safe.
-  string lockfile;  // = <output_base>/lock
-  int lockfd;
-
-  string jvm_log_file;  // = <output_base>/server/jvm.out
-
-  string cwd;
-
-  // The nearest enclosing workspace directory, starting from cwd.
-  // If not under a workspace directory, this is equal to cwd.
-  string workspace;
-
-  // Option processor responsible for parsing RC files and converting them into
-  // the argument list passed on to the server.
-  OptionProcessor option_processor;
-
-  pid_t server_pid;
-
-  volatile sig_atomic_t sigint_count;
-
-  // The number of the last received signal that should cause the client
-  // to shutdown.  This is saved so that the client's WTERMSIG can be set
-  // correctly.  (Currently only SIGPIPE uses this mechanism.)
-  volatile sig_atomic_t received_signal;
-
-  // Contains the relative paths of all the files in the attached zip, and is
-  // populated during GetInstallDir().
-  vector<string> extracted_binaries;
-
-  // Parsed startup options
-  BlazeStartupOptions options;
-
-  // The time in ms the launcher spends before sending the request to the Blaze
-  uint64_t startup_time;
-
-  // The time spent on extracting the new blaze version
-  // This is part of startup_time
-  uint64_t extract_data_time;
-
-  // The time in ms if a command had to wait on a busy Blaze server process
-  // This is part of startup_time
-  uint64_t command_wait_time;
-
-  RestartReason restart_reason;
-
-  // Absolute path of the blaze binary
-  string binary_path;
-
-  // MD5 hash of the Blaze binary (includes deploy.jar, extracted binaries, and
-  // anything else that ends up under the install_base).
-  string install_md5;
-};
-
 static GlobalVariables *globals;
 
 static void InitGlobals() {
@@ -1178,7 +1116,7 @@
             "Contents of '%s':\n", globals->options.GetProductName().c_str(),
             globals->jvm_log_file.c_str());
     WriteFileToStreamOrDie(stderr, globals->jvm_log_file.c_str());
-    exit(blaze_exit_code::INTERNAL_ERROR);
+    exit(GetExitCodeForAbruptExit(*globals));
   }
   return static_cast<char>(c);
 }
diff --git a/src/main/cpp/blaze_abrupt_exit.cc b/src/main/cpp/blaze_abrupt_exit.cc
new file mode 100644
index 0000000..e0b5b17
--- /dev/null
+++ b/src/main/cpp/blaze_abrupt_exit.cc
@@ -0,0 +1,23 @@
+// 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.
+#include "src/main/cpp/blaze_abrupt_exit.h"
+#include "src/main/cpp/util/exit_code.h"
+
+namespace blaze {
+
+int GetExitCodeForAbruptExit(const GlobalVariables& globals) {
+  return blaze_exit_code::INTERNAL_ERROR;
+}
+
+}  // namespace blaze
diff --git a/src/main/cpp/blaze_abrupt_exit.h b/src/main/cpp/blaze_abrupt_exit.h
new file mode 100644
index 0000000..15b9fed
--- /dev/null
+++ b/src/main/cpp/blaze_abrupt_exit.h
@@ -0,0 +1,29 @@
+// 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.
+//
+//
+// blaze_abrupt_exit.h: Deals with abrupt exits of the Blaze server.
+//
+#ifndef THIRD_PARTY_BAZEL_SRC_MAIN_CPP_BLAZE_ABRUPT_EXIT_H_
+#define THIRD_PARTY_BAZEL_SRC_MAIN_CPP_BLAZE_ABRUPT_EXIT_H_
+
+namespace blaze {
+
+class GlobalVariables;
+
+// Returns the exit code to use for when the Blaze server exits abruptly.
+int GetExitCodeForAbruptExit(const GlobalVariables& globals);
+
+}  // namespace blaze
+#endif  // THIRD_PARTY_BAZEL_SRC_MAIN_CPP_BLAZE_ABRUPT_EXIT_H_
diff --git a/src/main/cpp/blaze_globals.h b/src/main/cpp/blaze_globals.h
new file mode 100644
index 0000000..71242c3
--- /dev/null
+++ b/src/main/cpp/blaze_globals.h
@@ -0,0 +1,97 @@
+// 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.
+//
+// blaze_globals.h: The global state in the blaze.cc Blaze client.
+//
+
+#ifndef THIRD_PARTY_BAZEL_SRC_MAIN_CPP_BLAZE_H_
+#define THIRD_PARTY_BAZEL_SRC_MAIN_CPP_BLAZE_H_
+
+#include <signal.h>
+#include <sys/types.h>
+#include <string>
+#include <vector>
+
+#include "src/main/cpp/blaze_startup_options.h"
+#include "src/main/cpp/option_processor.h"
+
+using std::vector;
+
+namespace blaze {
+
+// The reason for a blaze server restart.
+// Keep in sync with logging.proto
+enum RestartReason {
+  NO_RESTART = 0,
+  NO_DAEMON,
+  NEW_VERSION,
+  NEW_OPTIONS
+};
+
+struct GlobalVariables {
+  // Used to make concurrent invocations of this program safe.
+  string lockfile;  // = <output_base>/lock
+  int lockfd;
+
+  string jvm_log_file;  // = <output_base>/server/jvm.out
+
+  string cwd;
+
+  // The nearest enclosing workspace directory, starting from cwd.
+  // If not under a workspace directory, this is equal to cwd.
+  string workspace;
+
+  // Option processor responsible for parsing RC files and converting them into
+  // the argument list passed on to the server.
+  OptionProcessor option_processor;
+
+  pid_t server_pid;
+
+  volatile sig_atomic_t sigint_count;
+
+  // The number of the last received signal that should cause the client
+  // to shutdown.  This is saved so that the client's WTERMSIG can be set
+  // correctly.  (Currently only SIGPIPE uses this mechanism.)
+  volatile sig_atomic_t received_signal;
+
+  // Contains the relative paths of all the files in the attached zip, and is
+  // populated during GetInstallDir().
+  vector<string> extracted_binaries;
+
+  // Parsed startup options
+  BlazeStartupOptions options;
+
+  // The time in ms the launcher spends before sending the request to the Blaze
+  uint64_t startup_time;
+
+  // The time spent on extracting the new blaze version
+  // This is part of startup_time
+  uint64_t extract_data_time;
+
+  // The time in ms if a command had to wait on a busy Blaze server process
+  // This is part of startup_time
+  uint64_t command_wait_time;
+
+  RestartReason restart_reason;
+
+  // Absolute path of the blaze binary
+  string binary_path;
+
+  // MD5 hash of the Blaze binary (includes deploy.jar, extracted binaries, and
+  // anything else that ends up under the install_base).
+  string install_md5;
+};
+
+}  // namespace blaze
+#endif  // THIRD_PARTY_BAZEL_SRC_MAIN_CPP_BLAZE_H_
diff --git a/src/main/cpp/blaze_util.cc b/src/main/cpp/blaze_util.cc
index cde7523..f8d9c7a 100644
--- a/src/main/cpp/blaze_util.cc
+++ b/src/main/cpp/blaze_util.cc
@@ -201,7 +201,7 @@
 // Writes 'content' into file 'filename', and makes it executable.
 // Returns false on failure, sets errno.
 bool WriteFile(const string &content, const string &filename) {
-  unlink(filename.c_str());
+  DeleteFile(filename);  // We don't care about the success of this.
   int fd = open(filename.c_str(), O_CREAT|O_WRONLY|O_TRUNC, 0755);  // chmod +x
   if (fd == -1) {
     return false;
@@ -218,6 +218,12 @@
   return static_cast<uint>(r) == content.size();
 }
 
+// Deletes the file 'filename'.
+// Returns false on failure, sets errno.
+bool DeleteFile(const string &filename) {
+  return unlink(filename.c_str()) == 0;
+}
+
 // Returns true iff both stdout and stderr are connected to a
 // terminal, and it can support color and cursor movement
 // (this is computed heuristically based on the values of
diff --git a/src/main/cpp/blaze_util.h b/src/main/cpp/blaze_util.h
index 67e1b32..5cf8182 100644
--- a/src/main/cpp/blaze_util.h
+++ b/src/main/cpp/blaze_util.h
@@ -55,6 +55,10 @@
 // Returns false on failure, sets errno.
 bool WriteFile(const string &content, const string &filename);
 
+// Deletes the file 'filename'.
+// Returns false on failure, sets errno.
+bool DeleteFile(const string &filename);
+
 // Returns true iff the current terminal can support color and cursor movement.
 bool IsStandardTerminal();
 
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeExecutor.java b/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeExecutor.java
index f142e75..20b3777 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeExecutor.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeExecutor.java
@@ -1459,7 +1459,8 @@
    * Get metadata related to the prepareAndGet() lookup. Resulting data is specific to the
    * underlying evaluation implementation.
    */
-  public String prepareAndGetMetadata(Collection<String> patterns, String offset) {
+  public String prepareAndGetMetadata(Collection<String> patterns, String offset)
+      throws AbruptExitException, InterruptedException {
     return buildDriver.meta(ImmutableList.of(getUniverseKey(patterns, offset)));
   }
 
diff --git a/src/main/java/com/google/devtools/build/skyframe/BuildDriver.java b/src/main/java/com/google/devtools/build/skyframe/BuildDriver.java
index 129238b..a63afc7 100644
--- a/src/main/java/com/google/devtools/build/skyframe/BuildDriver.java
+++ b/src/main/java/com/google/devtools/build/skyframe/BuildDriver.java
@@ -15,6 +15,7 @@
 package com.google.devtools.build.skyframe;
 
 import com.google.devtools.build.lib.events.EventHandler;
+import com.google.devtools.build.lib.util.AbruptExitException;
 
 import javax.annotation.Nullable;
 
@@ -34,7 +35,7 @@
    * Retrieve metadata about the computation over the given roots. Data returned is specific to the
    * underlying evaluator implementation.
    */
-  String meta(Iterable<SkyKey> roots);
+  String meta(Iterable<SkyKey> roots) throws AbruptExitException, InterruptedException;
 
   MemoizingEvaluator getGraphForTesting();