Changes to base/ from a combination of FreeBSD and OpenBSD patches.

Review URL: http://codereview.chromium.org/495002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@35321 0039d316-1c4b-4281-b951-d872f2087c98


CrOS-Libchrome-Original-Commit: e43eddf1a5887c4b249af3daa024c1107ce254a1
diff --git a/base/base.gypi b/base/base.gypi
index 03c04f7..9421717 100644
--- a/base/base.gypi
+++ b/base/base.gypi
@@ -377,6 +377,14 @@
                 },
               },
             ],
+            [ 'OS == "linux"', {
+              'link_settings': {
+                'libraries': [
+                  # We need rt for clock_gettime().
+                  '-lrt',
+                ],
+              },
+            }],
           ],
           'dependencies': [
             '../build/util/build_util.gyp:lastchange',
@@ -386,12 +394,6 @@
           'cflags': [
             '-Wno-write-strings',
           ],
-          'link_settings': {
-            'libraries': [
-              # We need rt for clock_gettime().
-              '-lrt',
-            ],
-          },
           'export_dependent_settings': [
             '../build/linux/system.gyp:gtk',
           ],
@@ -403,6 +405,11 @@
             'sources': [
               'directory_watcher_stub.cc',
             ],
+            'link_settings': {
+              'libraries': [
+                '-L/usr/local/lib -lexecinfo',
+              ],
+            },
           },
         ],
         [ 'OS == "mac"', {
diff --git a/base/command_line.cc b/base/command_line.cc
index 4e3bb3a..35f839c 100644
--- a/base/command_line.cc
+++ b/base/command_line.cc
@@ -200,7 +200,7 @@
 #endif
 }
 
-#if defined(OS_LINUX) || defined(OS_FREEBSD)
+#if defined(OS_POSIX) && !defined(OS_MACOSX)
 // static
 void CommandLine::SetProcTitle() {
   // Build a single string which consists of all the arguments separated
diff --git a/base/command_line.h b/base/command_line.h
index 37a6a4e..b49fe0f 100644
--- a/base/command_line.h
+++ b/base/command_line.h
@@ -68,7 +68,7 @@
   // line, but it still must be called to set up the command line.
   static void Init(int argc, const char* const* argv);
 
-#if defined(OS_LINUX) || defined(OS_FREEBSD)
+#if defined(OS_POSIX) && !defined(OS_MACOSX)
   // Sets the current process' arguments that show in "ps" etc. to those
   // in |current_process_commandline_|. Used by the zygote host so that
   // renderers show up with --type=renderer.
diff --git a/base/debug_util_posix.cc b/base/debug_util_posix.cc
index 20916a0..73b756b 100644
--- a/base/debug_util_posix.cc
+++ b/base/debug_util_posix.cc
@@ -172,7 +172,15 @@
   return pid_index < status.size() && status[pid_index] != '0';
 }
 
-#endif  // OS_LINUX
+#elif defined(OS_FREEBSD)
+
+bool DebugUtil::BeingDebugged() {
+  // TODO(benl): can we determine this under FreeBSD?
+  NOTIMPLEMENTED();
+  return false;
+}
+
+#endif
 
 // static
 void DebugUtil::BreakDebugger() {
diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc
index cdf7be9..579e79a 100644
--- a/base/file_util_posix.cc
+++ b/base/file_util_posix.cc
@@ -86,8 +86,8 @@
   DIR* dir = opendir(path.value().c_str());
   if (dir) {
 #if !defined(OS_LINUX) && !defined(OS_MACOSX) && !defined(OS_FREEBSD)
-  #error Depending on the definition of struct dirent, additional space for \
-      pathname may be needed
+  #error Port warning: depending on the definition of struct dirent, \
+         additional space for pathname may be needed
 #endif
     struct dirent ent_buf;
     struct dirent* ent;
@@ -635,9 +635,10 @@
     return false;
 
 #if !defined(OS_LINUX) && !defined(OS_MACOSX) && !defined(OS_FREEBSD)
-  #error Depending on the definition of struct dirent, additional space for \
-      pathname may be needed
+  #error Port warning: depending on the definition of struct dirent, \
+         additional space for pathname may be needed
 #endif
+
   struct dirent dent_buf;
   struct dirent* dent;
   while (readdir_r(dir, &dent_buf, &dent) == 0 && dent) {
diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc
index 200c386..35233ba 100644
--- a/base/file_util_unittest.cc
+++ b/base/file_util_unittest.cc
@@ -1373,19 +1373,19 @@
   EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
   EXPECT_FALSE(file_util::ContainsPath(foo, foo));
 
-// Platform-specific concerns
+  // Platform-specific concerns.
   FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
 #if defined(OS_WIN)
   EXPECT_TRUE(file_util::ContainsPath(foo,
       foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
   EXPECT_TRUE(file_util::ContainsPath(foo,
       FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
-#elif defined(OS_LINUX)
+#elif defined(OS_MACOSX)
+  // We can't really do this test on OS X since the case-sensitivity of the
+  // filesystem is configurable.
+#elif defined(OS_POSIX)
   EXPECT_FALSE(file_util::ContainsPath(foo,
       foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
-#else
-  // We can't really do this test on osx since the case-sensitivity of the
-  // filesystem is configurable.
 #endif
 }
 
diff --git a/base/file_version_info.h b/base/file_version_info.h
index 7e86639..561c324 100644
--- a/base/file_version_info.h
+++ b/base/file_version_info.h
@@ -87,7 +87,7 @@
   explicit FileVersionInfo(NSBundle *bundle);
 
   NSBundle *bundle_;
-#elif defined(OS_LINUX)
+#elif defined(OS_POSIX)
   FileVersionInfo();
 #endif
 
diff --git a/base/gfx/rect.cc b/base/gfx/rect.cc
index 2a35575..961fa03 100644
--- a/base/gfx/rect.cc
+++ b/base/gfx/rect.cc
@@ -8,7 +8,7 @@
 #include <windows.h>
 #elif defined(OS_MACOSX)
 #include <CoreGraphics/CGGeometry.h>
-#elif defined(OS_LINUX)
+#elif defined(OS_POSIX)
 #include <gdk/gdk.h>
 #endif
 
@@ -76,7 +76,7 @@
   set_height(r.size.height);
   return *this;
 }
-#elif defined(OS_LINUX)
+#elif defined(OS_POSIX)
 Rect::Rect(const GdkRectangle& r)
     : origin_(r.x, r.y) {
   set_width(r.width);
@@ -128,15 +128,15 @@
   r.bottom = bottom();
   return r;
 }
-#elif defined(OS_LINUX)
-GdkRectangle Rect::ToGdkRectangle() const {
-  GdkRectangle r = {x(), y(), width(), height()};
-  return r;
-}
 #elif defined(OS_MACOSX)
 CGRect Rect::ToCGRect() const {
   return CGRectMake(x(), y(), width(), height());
 }
+#elif defined(OS_POSIX)
+GdkRectangle Rect::ToGdkRectangle() const {
+  GdkRectangle r = {x(), y(), width(), height()};
+  return r;
+}
 #endif
 
 bool Rect::Contains(int point_x, int point_y) const {
diff --git a/base/i18n/file_util_icu.cc b/base/i18n/file_util_icu.cc
index 4d33e3a..f62a05e 100644
--- a/base/i18n/file_util_icu.cc
+++ b/base/i18n/file_util_icu.cc
@@ -146,7 +146,7 @@
     // Windows uses UTF-16 encoding for filenames.
     U16_NEXT(file_name->data(), cursor, static_cast<int>(file_name->length()),
              code_point);
-#elif defined(OS_LINUX)
+#elif defined(OS_POSIX)
     // Linux doesn't actually define an encoding. It basically allows anything
     // except for a few special ASCII characters.
     unsigned char cur_char = static_cast<unsigned char>((*file_name)[cursor++]);
diff --git a/base/i18n/file_util_icu_unittest.cc b/base/i18n/file_util_icu_unittest.cc
index b46fe55..1da8a93 100644
--- a/base/i18n/file_util_icu_unittest.cc
+++ b/base/i18n/file_util_icu_unittest.cc
@@ -35,7 +35,7 @@
   FilePath test_dir_;
 };
 
-#if defined(OS_LINUX)
+#if defined(OS_POSIX) && !defined(OS_MACOSX)
 
 // Linux disallows some evil ASCII characters, but passes all non-ASCII.
 static const struct goodbad_pair {
diff --git a/base/i18n/icu_util.cc b/base/i18n/icu_util.cc
index bccc1ab..066534c 100644
--- a/base/i18n/icu_util.cc
+++ b/base/i18n/icu_util.cc
@@ -31,7 +31,7 @@
 #define ICU_UTIL_DATA_IMPL ICU_UTIL_DATA_SHARED
 #elif defined(OS_MACOSX)
 #define ICU_UTIL_DATA_IMPL ICU_UTIL_DATA_STATIC
-#elif defined(OS_LINUX)
+#elif defined(OS_POSIX)
 #define ICU_UTIL_DATA_IMPL ICU_UTIL_DATA_FILE
 #endif
 
diff --git a/base/logging.cc b/base/logging.cc
index 706dbb1..72630fc 100644
--- a/base/logging.cc
+++ b/base/logging.cc
@@ -18,7 +18,7 @@
 #include <mach/mach.h>
 #include <mach/mach_time.h>
 #include <mach-o/dyld.h>
-#elif defined(OS_LINUX)
+#elif defined(OS_POSIX)
 #include <sys/syscall.h>
 #include <time.h>
 #endif
@@ -137,6 +137,9 @@
   return mach_thread_self();
 #elif defined(OS_LINUX)
   return syscall(__NR_gettid);
+#elif defined(OS_FREEBSD)
+  // TODO(BSD): find a better thread ID
+  return reinterpret_cast<int64>(pthread_self());
 #endif
 }
 
@@ -145,7 +148,7 @@
   return GetTickCount();
 #elif defined(OS_MACOSX)
   return mach_absolute_time();
-#elif defined(OS_LINUX)
+#elif defined(OS_POSIX)
   struct timespec ts;
   clock_gettime(CLOCK_MONOTONIC, &ts);
 
@@ -226,7 +229,7 @@
   return true;
 }
 
-#if defined(OS_LINUX)
+#if defined(OS_POSIX) && !defined(OS_MACOSX)
 int GetLoggingFileDescriptor() {
   // No locking needed, since this is only called by the zygote server,
   // which is single-threaded.
diff --git a/base/logging.h b/base/logging.h
index cac9ca3..1aa1162 100644
--- a/base/logging.h
+++ b/base/logging.h
@@ -153,7 +153,7 @@
 // Gets the current log level.
 int GetMinLogLevel();
 
-#if defined(OS_LINUX)
+#if defined(OS_POSIX) && !defined(OS_MACOSX)
 // Get the file descriptor used for logging.
 // Returns -1 if none open.
 // Needed by ZygoteManager.
diff --git a/base/message_loop.cc b/base/message_loop.cc
index 9799100..314a7a0 100644
--- a/base/message_loop.cc
+++ b/base/message_loop.cc
@@ -20,7 +20,7 @@
 #include "base/message_pump_libevent.h"
 #include "base/third_party/valgrind/valgrind.h"
 #endif
-#if defined(OS_LINUX)
+#if defined(OS_POSIX) && !defined(OS_MACOSX)
 #include "base/message_pump_glib.h"
 #endif
 
@@ -99,9 +99,9 @@
   if (type_ == TYPE_UI) {
 #if defined(OS_MACOSX)
     pump_ = base::MessagePumpMac::Create();
-#elif defined(OS_LINUX)
+#else
     pump_ = new base::MessagePumpForUI();
-#endif  // OS_LINUX
+#endif
   } else if (type_ == TYPE_IO) {
     pump_ = new base::MessagePumpLibevent();
   } else {
@@ -194,7 +194,7 @@
 
   StartHistogrammer();
 
-#if defined(OS_WIN) || defined(OS_LINUX)
+#if !defined(OS_MACOSX)
   if (state_->dispatcher && type() == TYPE_UI) {
     static_cast<base::MessagePumpForUI*>(pump_.get())->
         RunWithDispatcher(this, state_->dispatcher);
@@ -487,7 +487,7 @@
 
   // Initialize the other fields:
   quit_received = false;
-#if defined(OS_WIN) || defined(OS_LINUX)
+#if !defined(OS_MACOSX)
   dispatcher = NULL;
 #endif
 }
@@ -588,7 +588,7 @@
 
 #endif  // defined(OS_WIN)
 
-#if defined(OS_LINUX) || defined(OS_WIN)
+#if !defined(OS_MACOSX)
 void MessageLoopForUI::AddObserver(Observer* observer) {
   pump_ui()->AddObserver(observer);
 }
@@ -602,7 +602,7 @@
   state_->dispatcher = dispatcher;
   RunHandler();
 }
-#endif  // defined(OS_LINUX) || defined(OS_WIN)
+#endif  // !defined(OS_MACOSX)
 
 //------------------------------------------------------------------------------
 // MessageLoopForIO
diff --git a/base/message_loop.h b/base/message_loop.h
index fe28179..0338963 100644
--- a/base/message_loop.h
+++ b/base/message_loop.h
@@ -21,10 +21,10 @@
 #include "base/message_pump_win.h"
 #elif defined(OS_POSIX)
 #include "base/message_pump_libevent.h"
-#endif
-#if defined(OS_LINUX)
+#if !defined(OS_MACOSX)
 #include "base/message_pump_glib.h"
 #endif
+#endif
 
 // A MessageLoop is used to process events for a particular thread.  There is
 // at most one MessageLoop instance per thread.
@@ -254,7 +254,7 @@
 #if defined(OS_WIN)
   typedef base::MessagePumpWin::Dispatcher Dispatcher;
   typedef base::MessagePumpWin::Observer Observer;
-#elif defined(OS_LINUX)
+#elif !defined(OS_MACOSX)
   typedef base::MessagePumpForUI::Dispatcher Dispatcher;
   typedef base::MessagePumpForUI::Observer Observer;
 #endif
@@ -269,7 +269,7 @@
     // once it becomes idle.
     bool quit_received;
 
-#if defined(OS_WIN) || defined(OS_LINUX)
+#if !defined(OS_MACOSX)
     Dispatcher* dispatcher;
 #endif
   };
@@ -457,7 +457,7 @@
   void PumpOutPendingPaintMessages();
 #endif
 
-#if defined(OS_WIN) || defined(OS_LINUX)
+#if !defined(OS_MACOSX)
   // Please see message_pump_win/message_pump_glib for definitions of these
   // methods.
   void AddObserver(Observer* observer);
@@ -469,7 +469,7 @@
   base::MessagePumpForUI* pump_ui() {
     return static_cast<base::MessagePumpForUI*>(pump_.get());
   }
-#endif  // defined(OS_WIN) || defined(OS_LINUX)
+#endif  // defined(OS_MACOSX)
 };
 
 // Do not add any member variables to MessageLoopForUI!  This is important b/c
diff --git a/base/message_loop_unittest.cc b/base/message_loop_unittest.cc
index 16d1c1a..74491ce 100644
--- a/base/message_loop_unittest.cc
+++ b/base/message_loop_unittest.cc
@@ -1502,4 +1502,4 @@
   close(pipefds[1]);
 }
 
-#endif  // defined(OS_LINUX)
+#endif  // defined(OS_POSIX)
diff --git a/base/mime_util.h b/base/mime_util.h
index 7e1593b..58cb630 100644
--- a/base/mime_util.h
+++ b/base/mime_util.h
@@ -20,7 +20,7 @@
 // Get the mime type for a byte vector.
 std::string GetDataMimeType(const std::string& data);
 
-#if defined(OS_LINUX)
+#if defined(TOOLKIT_GTK)
 // This detects the current GTK theme by calling gtk_settings_get_default().
 // It should only be executed on the UI thread and must be called before
 // GetMimeIcon().
diff --git a/base/native_library.h b/base/native_library.h
index dc4000b..182b3ed 100644
--- a/base/native_library.h
+++ b/base/native_library.h
@@ -44,7 +44,7 @@
   };
 };
 typedef NativeLibraryStruct* NativeLibrary;
-#elif defined(OS_LINUX) || defined(OS_FREEBSD)
+#elif defined(OS_POSIX)
 typedef void* NativeLibrary;
 #endif  // OS_*
 
diff --git a/base/platform_thread_posix.cc b/base/platform_thread_posix.cc
index 3789715..92a7e22 100644
--- a/base/platform_thread_posix.cc
+++ b/base/platform_thread_posix.cc
@@ -9,7 +9,7 @@
 
 #if defined(OS_MACOSX)
 #include <mach/mach.h>
-#elif defined(OS_LINUX)
+#else
 #include <sys/syscall.h>
 #include <unistd.h>
 #endif
@@ -35,6 +35,9 @@
   return mach_thread_self();
 #elif defined(OS_LINUX)
   return syscall(__NR_gettid);
+#elif defined(OS_FREEBSD)
+  // TODO(BSD): find a better thread ID
+  return reinterpret_cast<int64>(pthread_self());
 #endif
 }
 
diff --git a/base/process.h b/base/process.h
index 53512ce..6ac29e9 100644
--- a/base/process.h
+++ b/base/process.h
@@ -29,7 +29,7 @@
 const ProcessHandle kNullProcessHandle = 0;
 #endif
 
-#if defined(OS_LINUX)
+#if defined(OS_POSIX) && !defined(OS_MACOSX)
 // saved_priority_ will be set to this to indicate that it's not holding
 // a valid value. -20 to 19 are valid process priorities.
 const int kUnsetProcessPriority = 256;
@@ -38,13 +38,13 @@
 class Process {
  public:
   Process() : process_(kNullProcessHandle) {
-#if defined(OS_LINUX)
+#if defined(OS_POSIX) && !defined(OS_MACOSX)
     saved_priority_ = kUnsetProcessPriority;
 #endif
   }
 
   explicit Process(ProcessHandle handle) : process_(handle) {
-#if defined(OS_LINUX)
+#if defined(OS_POSIX) && !defined(OS_MACOSX)
     saved_priority_ = kUnsetProcessPriority;
 #endif
   }
@@ -57,7 +57,7 @@
   ProcessHandle handle() const { return process_; }
   void set_handle(ProcessHandle handle) {
     process_ = handle;
-#if defined(OS_LINUX)
+#if defined(OS_POSIX) && !defined(OS_MACOSX)
     saved_priority_ = kUnsetProcessPriority;
 #endif
   }
@@ -93,7 +93,7 @@
 
  private:
   ProcessHandle process_;
-#if defined(OS_LINUX)
+#if defined(OS_POSIX) && !defined(OS_MACOSX)
   // Holds the priority that the process was set to when it was backgrounded.
   // If the process wasn't backgrounded it will be kUnsetProcessPriority.
   int saved_priority_;
diff --git a/base/process_util_unittest.cc b/base/process_util_unittest.cc
index fe28502..8bbd784 100644
--- a/base/process_util_unittest.cc
+++ b/base/process_util_unittest.cc
@@ -22,6 +22,7 @@
 #endif
 #if defined(OS_POSIX)
 #include <fcntl.h>
+#include <sys/resource.h>
 #include <sys/socket.h>
 #endif
 #if defined(OS_WIN)
diff --git a/base/scoped_handle.h b/base/scoped_handle.h
index 913271d..0b97ff0 100644
--- a/base/scoped_handle.h
+++ b/base/scoped_handle.h
@@ -11,7 +11,7 @@
 
 #if defined(OS_WIN)
 #include "base/scoped_handle_win.h"
-#elif defined(OS_LINUX)
+#elif defined(OS_POSIX) && !defined(OS_MACOSX)
 #include "base/scoped_handle_gtk.h"
 #endif
 
diff --git a/base/sys_info.h b/base/sys_info.h
index 7460fe0..415dc81 100644
--- a/base/sys_info.h
+++ b/base/sys_info.h
@@ -67,7 +67,7 @@
   // allocate.
   static size_t VMAllocationGranularity();
 
-#if defined(OS_LINUX)
+#if defined(OS_POSIX) && !defined(OS_MACOSX)
   // Returns the maximum SysV shared memory segment size.
   static size_t MaxSharedMemorySize();
 #endif
diff --git a/base/sys_info_posix.cc b/base/sys_info_posix.cc
index af33331..b2526ba 100644
--- a/base/sys_info_posix.cc
+++ b/base/sys_info_posix.cc
@@ -6,7 +6,9 @@
 
 #include <errno.h>
 #include <string.h>
+#include <sys/param.h>
 #include <sys/statvfs.h>
+#include <sys/sysctl.h>
 #include <sys/utsname.h>
 #include <unistd.h>
 
@@ -14,11 +16,6 @@
 #include <gdk/gdk.h>
 #endif
 
-#if defined(OS_OPENBSD)
-#include <sys/param.h>
-#include <sys/sysctl.h>
-#endif
-
 #include "base/basictypes.h"
 #include "base/file_util.h"
 #include "base/logging.h"
diff --git a/base/test/test_file_util_posix.cc b/base/test/test_file_util_posix.cc
index 8f7d69d..876e882 100644
--- a/base/test/test_file_util_posix.cc
+++ b/base/test/test_file_util_posix.cc
@@ -5,6 +5,7 @@
 #include "base/test/test_file_util.h"
 
 #include <errno.h>
+#include <fcntl.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 
@@ -100,4 +101,12 @@
   return success;
 }
 
+#if !defined(OS_LINUX) && !defined(OS_MACOSX)
+bool EvictFileFromSystemCache(const FilePath& file) {
+  // There doesn't seem to be a POSIX way to cool the disk cache.
+  NOTIMPLEMENTED();
+  return false;
+}
+#endif
+
 }  // namespace file_util
diff --git a/base/test/test_suite.h b/base/test/test_suite.h
index 03cbca2..aa006fc 100644
--- a/base/test/test_suite.h
+++ b/base/test/test_suite.h
@@ -23,7 +23,7 @@
 #include "testing/gtest/include/gtest/gtest.h"
 #include "testing/multiprocess_func_list.h"
 
-#if defined(OS_LINUX)
+#if defined(OS_POSIX) && !defined(OS_MACOSX)
 #include <gtk/gtk.h>
 #endif
 
@@ -55,7 +55,7 @@
     base::EnableTerminationOnHeapCorruption();
     CommandLine::Init(argc, argv);
     testing::InitGoogleTest(&argc, argv);
-#if defined(OS_LINUX)
+#if defined(OS_POSIX) && !defined(OS_MACOSX)
     g_thread_init(NULL);
     gtk_init_check(&argc, &argv);
 #endif  // defined(OS_LINUX)
@@ -212,13 +212,13 @@
 
     icu_util::Initialize();
 
-#if defined(OS_LINUX)
+#if defined(OS_POSIX) && !defined(OS_MACOSX)
     // Trying to repeatedly initialize and cleanup NSS and NSPR may result in
     // a deadlock. Such repeated initialization will happen when using test
     // isolation. Prevent problems by initializing NSS here, so that the cleanup
     // will be done only on process exit.
     base::EnsureNSSInit();
-#endif  // defined(OS_LINUX)
+#endif  // defined(OS_POSIX) && !defined(OS_MACOSX)
   }
 
   virtual void Shutdown() {
diff --git a/base/third_party/nspr/prtime.cc b/base/third_party/nspr/prtime.cc
index 2407884..65f93ee 100644
--- a/base/third_party/nspr/prtime.cc
+++ b/base/third_party/nspr/prtime.cc
@@ -149,7 +149,7 @@
     result *= kSecondsToMicroseconds;
     result += exploded->tm_usec;
     return result;
-#elif defined(OS_LINUX) || defined(OS_FREEBSD)
+#elif defined(OS_POSIX)
     struct tm exp_tm = {0};
     exp_tm.tm_sec  = exploded->tm_sec;
     exp_tm.tm_min  = exploded->tm_min;