Port to FreeBSD.

--
Change-Id: I4e65cc583e758d2f7e45209ffcb37f6a871e2ed7
Reviewed-on: https://bazel-review.git.corp.google.com/#/c/1840
MOS_MIGRATED_REVID=101462155
diff --git a/src/BUILD b/src/BUILD
index c3623c3..994eab5 100644
--- a/src/BUILD
+++ b/src/BUILD
@@ -117,3 +117,9 @@
     values = {"cpu": "darwin"},
     visibility = ["//visibility:public"],
 )
+
+config_setting(
+    name = "freebsd",
+    values = {"cpu": "freebsd"},
+    visibility = ["//visibility:public"],
+)
diff --git a/src/main/cpp/BUILD b/src/main/cpp/BUILD
index dc1b732..c604272 100644
--- a/src/main/cpp/BUILD
+++ b/src/main/cpp/BUILD
@@ -8,6 +8,10 @@
             "blaze_util_darwin.cc",
             "blaze_util_posix.cc",
         ],
+        "//src:freebsd": [
+            "blaze_util_freebsd.cc",
+            "blaze_util_posix.cc",
+        ],
         "//conditions:default": [
             "blaze_util_linux.cc",
             "blaze_util_posix.cc",
@@ -36,6 +40,9 @@
     linkopts = select({
         "//src:darwin": [
         ],
+        "//src:freebsd": [
+            "-lprocstat",
+        ],
         "//conditions:default": [
             "-lrt",
         ],
diff --git a/src/main/cpp/blaze.cc b/src/main/cpp/blaze.cc
index ad6b476..b3ec5ae 100644
--- a/src/main/cpp/blaze.cc
+++ b/src/main/cpp/blaze.cc
@@ -152,6 +152,7 @@
   globals = new GlobalVariables;
   globals->server_pid = -1;
   globals->sigint_count = 0;
+  globals->received_signal = 0;
   globals->startup_time = 0;
   globals->extract_data_time = 0;
   globals->command_wait_time = 0;
diff --git a/src/main/cpp/blaze_util.cc b/src/main/cpp/blaze_util.cc
index a6d96de..0eade65 100644
--- a/src/main/cpp/blaze_util.cc
+++ b/src/main/cpp/blaze_util.cc
@@ -27,7 +27,6 @@
 #include <sys/socket.h>
 #include <sys/stat.h>
 #include <sys/types.h>
-#include <sys/xattr.h>
 #include <unistd.h>
 
 #include <sstream>
diff --git a/src/main/cpp/blaze_util_freebsd.cc b/src/main/cpp/blaze_util_freebsd.cc
new file mode 100644
index 0000000..0d167cf
--- /dev/null
+++ b/src/main/cpp/blaze_util_freebsd.cc
@@ -0,0 +1,156 @@
+// Copyright 2015 Google Inc. 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 <limits.h>
+#include <pwd.h>
+#include <string.h>  // strerror
+#include <sys/mount.h>
+#include <sys/param.h>
+#include <sys/queue.h>
+#include <sys/socket.h>
+#include <sys/sysctl.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#include <unistd.h>
+#include <signal.h>
+#include <libprocstat.h>
+
+#include "src/main/cpp/blaze_util.h"
+#include "src/main/cpp/blaze_util_platform.h"
+#include "src/main/cpp/util/errors.h"
+#include "src/main/cpp/util/exit_code.h"
+#include "src/main/cpp/util/file.h"
+#include "src/main/cpp/util/port.h"
+#include "src/main/cpp/util/strings.h"
+
+namespace blaze {
+
+using blaze_util::die;
+using blaze_util::pdie;
+using std::string;
+
+string GetOutputRoot() {
+  char buf[2048];
+  struct passwd pwbuf;
+  struct passwd *pw = NULL;
+  int uid = getuid();
+  int r = getpwuid_r(uid, &pwbuf, buf, 2048, &pw);
+  if (r != -1 && pw != NULL) {
+    return blaze_util::JoinPath(pw->pw_dir, ".cache/bazel");
+  } else {
+    return "/tmp";
+  }
+}
+
+void WarnFilesystemType(const string &output_base) {
+  struct statfs buf = {};
+  if (statfs(output_base.c_str(), &buf) < 0) {
+    fprintf(stderr,
+            "WARNING: couldn't get file system type information for '%s': %s\n",
+            output_base.c_str(), strerror(errno));
+    return;
+  }
+
+  if (strcmp(buf.f_fstypename, "nfs") == 0) {
+    fprintf(stderr,
+            "WARNING: Output base '%s' is on NFS. This may lead "
+            "to surprising failures and undetermined behavior.\n",
+            output_base.c_str());
+  }
+}
+
+string GetSelfPath() {
+  char buffer[PATH_MAX] = {};
+  ssize_t bytes = readlink("/proc/curproc/file", buffer, sizeof(buffer));
+  if (bytes == sizeof(buffer)) {
+    // symlink contents truncated
+    bytes = -1;
+    errno = ENAMETOOLONG;
+  }
+  if (bytes == -1) {
+    pdie(blaze_exit_code::INTERNAL_ERROR, "error reading /proc/curproc/file");
+  }
+  buffer[bytes] = '\0';  // readlink does not NUL-terminate
+  return string(buffer);
+}
+
+pid_t GetPeerProcessId(int socket) { return -1; }
+
+uint64_t MonotonicClock() {
+  struct timespec ts = {};
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+  return ts.tv_sec * 1000000000LL + ts.tv_nsec;
+}
+
+uint64_t ProcessClock() {
+  struct timespec ts = {};
+  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
+  return ts.tv_sec * 1000000000LL + ts.tv_nsec;
+}
+
+void SetScheduling(bool batch_cpu_scheduling, int io_nice_level) {
+  // Move ourself into a low priority CPU scheduling group if the
+  // machine is configured appropriately.  Fail silently, because this
+  // isn't available on all kernels.
+
+  if (io_nice_level >= 0) {
+    if (blaze_util::sys_ioprio_set(
+            IOPRIO_WHO_PROCESS, getpid(),
+            IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, io_nice_level)) < 0) {
+      pdie(blaze_exit_code::INTERNAL_ERROR,
+           "ioprio_set() with class %d and level %d failed", IOPRIO_CLASS_BE,
+           io_nice_level);
+    }
+  }
+}
+
+string GetProcessCWD(int pid) {
+  if (kill(pid, 0) < 0) return "";
+  auto procstat = procstat_open_sysctl();
+  unsigned int n;
+  auto p = procstat_getprocs(procstat, KERN_PROC_PID, pid, &n);
+  string cwd;
+  if (p) {
+    if (n != 1) {
+      pdie(blaze_exit_code::INTERNAL_ERROR,
+           "expected exactly one process from procstat_getprocs, got %d", n);
+    }
+    auto files = procstat_getfiles(procstat, p, false);
+    filestat *entry;
+    STAILQ_FOREACH(entry, files, next) {
+      if (entry->fs_uflags & PS_FST_UFLAG_CDIR) {
+        cwd = entry->fs_path;
+      }
+    }
+    procstat_freefiles(procstat, files);
+    procstat_freeprocs(procstat, p);
+  }
+  procstat_close(procstat);
+  return cwd;
+}
+
+bool IsSharedLibrary(const string &filename) {
+  return blaze_util::ends_with(filename, ".so");
+}
+
+string GetDefaultHostJavabase() {
+  // if JAVA_HOME is defined, then use it as default.
+  const char *javahome = getenv("JAVA_HOME");
+  if (javahome != NULL) {
+    return string(javahome);
+  }
+  return "/usr/local/openjdk8";
+}
+
+}  // namespace blaze
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java b/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java
index f3694b5..4cf3b12 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java
@@ -399,6 +399,8 @@
         switch (OS.getCurrent()) {
           case DARWIN:
             return "darwin";
+          case FREEBSD:
+            return "freebsd";
           case LINUX:
             switch (CPU.getCurrent()) {
               case X86_32:
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE
index 82def35..2421b45 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE
@@ -30,6 +30,11 @@
 )
 
 bind(
+    name = "jni_md_header-freebsd",
+    actual = "@local-jdk//:jni_md_header-freebsd",
+)
+
+bind(
     name = "java",
     actual = "@local-jdk//:java",
 )
diff --git a/src/main/java/com/google/devtools/build/lib/util/OS.java b/src/main/java/com/google/devtools/build/lib/util/OS.java
index 97e6c3a..e425936 100644
--- a/src/main/java/com/google/devtools/build/lib/util/OS.java
+++ b/src/main/java/com/google/devtools/build/lib/util/OS.java
@@ -18,6 +18,7 @@
  */
 public enum OS {
   DARWIN("osx", "Mac OS X"),
+  FREEBSD("freebsd", "FreeBSD"),
   LINUX("linux", "Linux"),
   WINDOWS("windows", "Windows"),
   UNKNOWN("unknown", "");
diff --git a/src/main/native/BUILD b/src/main/native/BUILD
index f090a6f..97a4e9a 100644
--- a/src/main/native/BUILD
+++ b/src/main/native/BUILD
@@ -2,6 +2,7 @@
     name = "copy_link_jni_md_header",
     srcs = select({
         "//src:darwin": ["//tools/jdk:jni_md_header-darwin"],
+        "//src:freebsd": ["//tools/jdk:jni_md_header-freebsd"],
         "//conditions:default": ["//tools/jdk:jni_md_header-linux"],
     }),
     outs = ["jni_md.h"],
@@ -19,6 +20,7 @@
     name = "jni_os",
     srcs = select({
         "//src:darwin": ["unix_jni_darwin.cc"],
+        "//src:freebsd": ["unix_jni_freebsd.cc"],
         "//conditions:default": ["unix_jni_linux.cc"],
     }),
 )
diff --git a/src/main/native/unix_jni.h b/src/main/native/unix_jni.h
index 6667001..426e205 100644
--- a/src/main/native/unix_jni.h
+++ b/src/main/native/unix_jni.h
@@ -31,8 +31,8 @@
       } \
     } while (0)
 
-#if defined(__APPLE__)
-// stat64 is deprecated on OS X.
+#if defined(__APPLE__) || defined(__FreeBSD__)
+// stat64 is deprecated on OS X/BSD.
 typedef struct stat portable_stat_struct;
 #define portable_stat ::stat
 #define portable_lstat ::lstat
@@ -42,6 +42,10 @@
 #define portable_lstat ::lstat64
 #endif
 
+#if defined(__FreeBSD__)
+#define ENODATA ENOATTR
+#endif
+
 // Posts a JNI exception to the current thread with the specified
 // message; the exception's class is determined by the specified UNIX
 // error number.  See package-info.html for details.
diff --git a/src/main/native/unix_jni_freebsd.cc b/src/main/native/unix_jni_freebsd.cc
new file mode 100644
index 0000000..56e0c9d
--- /dev/null
+++ b/src/main/native/unix_jni_freebsd.cc
@@ -0,0 +1,81 @@
+// Copyright 2015 Google Inc. 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/native/unix_jni.h"
+
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/extattr.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+
+#include <string>
+
+const int PATH_MAX2 = PATH_MAX * 2;
+
+using std::string;
+
+// See unix_jni.h.
+string ErrorMessage(int error_number) {
+  char buf[1024] = "";
+  if (strerror_r(error_number, buf, sizeof buf) < 0) {
+    snprintf(buf, sizeof buf, "strerror_r(%d): errno %d", error_number, errno);
+  }
+
+  return string(buf);
+}
+
+int portable_fstatat(int dirfd, char *name, portable_stat_struct *statbuf,
+                     int flags) {
+  return fstatat(dirfd, name, statbuf, flags);
+}
+
+int StatSeconds(const portable_stat_struct &statbuf, StatTimes t) {
+  switch (t) {
+    case STAT_ATIME:
+      return statbuf.st_atime;
+    case STAT_CTIME:
+      return statbuf.st_ctime;
+    case STAT_MTIME:
+      return statbuf.st_mtime;
+    default:
+      CHECK(false);
+  }
+}
+
+int StatNanoSeconds(const portable_stat_struct &statbuf, StatTimes t) {
+  switch (t) {
+    case STAT_ATIME:
+      return statbuf.st_atimespec.tv_nsec;
+    case STAT_CTIME:
+      return statbuf.st_ctimespec.tv_nsec;
+    case STAT_MTIME:
+      return statbuf.st_mtimespec.tv_nsec;
+    default:
+      CHECK(false);
+  }
+}
+
+ssize_t portable_getxattr(const char *path, const char *name, void *value,
+                          size_t size) {
+  return extattr_get_file(path, EXTATTR_NAMESPACE_SYSTEM, name, value, size);
+}
+
+ssize_t portable_lgetxattr(const char *path, const char *name, void *value,
+                           size_t size) {
+  return extattr_get_link(path, EXTATTR_NAMESPACE_SYSTEM, name, value, size);
+}
diff --git a/src/main/tools/BUILD b/src/main/tools/BUILD
index 02f0b8d..67b3888 100644
--- a/src/main/tools/BUILD
+++ b/src/main/tools/BUILD
@@ -24,6 +24,7 @@
     name = "namespace-sandbox",
     srcs = select({
         "//src:darwin": ["namespace-sandbox-dummy.c"],
+        "//src:freebsd": ["namespace-sandbox-dummy.c"],
         "//conditions:default": ["namespace-sandbox.c"],
     }),
     copts = ["-std=c99"],
diff --git a/src/main/tools/jdk.BUILD b/src/main/tools/jdk.BUILD
index f67ac2f..2828add 100644
--- a/src/main/tools/jdk.BUILD
+++ b/src/main/tools/jdk.BUILD
@@ -16,6 +16,11 @@
 )
 
 filegroup(
+    name = "jni_md_header-freebsd",
+    srcs = ["include/freebsd/jni_md.h"],
+)
+
+filegroup(
     name = "java",
     srcs = ["bin/java"],
 )
diff --git a/src/test/shell/unittest.bash b/src/test/shell/unittest.bash
index e7af5b2..82aeb5d 100644
--- a/src/test/shell/unittest.bash
+++ b/src/test/shell/unittest.bash
@@ -458,14 +458,14 @@
 }
 
 # Multi-platform timestamp function
-if [ "$(uname -s | tr 'A-Z' 'a-z')" = "darwin" ]; then
+if [ "$(uname -s | tr 'A-Z' 'a-z')" = "linux" ]; then
     function timestamp() {
-      # OS X does not have %N so python is the best we can do
-      python -c 'import time; print int(round(time.time() * 1000))'
+      echo $(($(date +%s%N)/1000000))
     }
 else
     function timestamp() {
-      echo $(($(date +%s%N)/1000000))
+      # OS X and FreeBSD do not have %N so python is the best we can do
+      python -c 'import time; print int(round(time.time() * 1000))'
     }
 fi