Linux Zygote: add sanity checks and cleanup

- Put FontConfigIPC creation in PreSandboxInit().
- Add a sanity check for being single threaded.

BUG=269816
[email protected]

Review URL: https://codereview.chromium.org/23830004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@221320 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/content/zygote/zygote_main_linux.cc b/content/zygote/zygote_main_linux.cc
index 1f0e9f5..3ae08e5 100644
--- a/content/zygote/zygote_main_linux.cc
+++ b/content/zygote/zygote_main_linux.cc
@@ -16,10 +16,7 @@
 
 #include "base/basictypes.h"
 #include "base/command_line.h"
-#include "base/containers/hash_tables.h"
-#include "base/files/file_path.h"
 #include "base/linux_util.h"
-#include "base/memory/scoped_ptr.h"
 #include "base/native_library.h"
 #include "base/pickle.h"
 #include "base/posix/eintr_wrapper.h"
@@ -304,6 +301,8 @@
 #if defined(ENABLE_WEBRTC)
   InitializeWebRtcModule();
 #endif
+  SkFontConfigInterface::SetGlobal(
+      new FontConfigIPC(Zygote::kMagicSandboxIPCDescriptor))->unref();
 }
 
 // Do nothing here
@@ -384,16 +383,22 @@
 
 // This will set the *using_suid_sandbox variable to true if the SUID sandbox
 // is enabled. This does not necessarily exclude other types of sandboxing.
-static bool EnterSandbox(sandbox::SetuidSandboxClient* setuid_sandbox,
-                         bool* using_suid_sandbox, bool* has_started_new_init) {
+static bool EnterSuidSandbox(LinuxSandbox* linux_sandbox,
+                             bool* using_suid_sandbox,
+                             bool* has_started_new_init) {
   *using_suid_sandbox = false;
   *has_started_new_init = false;
+
+  sandbox::SetuidSandboxClient* setuid_sandbox =
+      linux_sandbox->setuid_sandbox_client();
+
   if (!setuid_sandbox)
     return false;
 
   PreSandboxInit();
-  SkFontConfigInterface::SetGlobal(
-      new FontConfigIPC(Zygote::kMagicSandboxIPCDescriptor))->unref();
+
+  // Check that the pre-sandbox initialization didn't spawn threads.
+  DCHECK(linux_sandbox->IsSingleThreaded());
 
   if (setuid_sandbox->IsSuidSandboxChild()) {
     // Use the SUID sandbox.  This still allows the seccomp sandbox to
@@ -461,9 +466,6 @@
   // This will pre-initialize the various sandboxes that need it.
   linux_sandbox->PreinitializeSandbox();
 
-  sandbox::SetuidSandboxClient* setuid_sandbox =
-      linux_sandbox->setuid_sandbox_client();
-
   if (forkdelegate != NULL) {
     VLOG(1) << "ZygoteMain: initializing fork delegate";
     forkdelegate->Init(Zygote::kMagicSandboxIPCDescriptor);
@@ -475,14 +477,17 @@
   bool using_suid_sandbox = false;
   bool has_started_new_init = false;
 
-  if (!EnterSandbox(setuid_sandbox,
-                    &using_suid_sandbox,
-                    &has_started_new_init)) {
+  if (!EnterSuidSandbox(linux_sandbox,
+                        &using_suid_sandbox,
+                        &has_started_new_init)) {
     LOG(FATAL) << "Failed to enter sandbox. Fail safe abort. (errno: "
                << errno << ")";
     return false;
   }
 
+  sandbox::SetuidSandboxClient* setuid_sandbox =
+      linux_sandbox->setuid_sandbox_client();
+
   if (setuid_sandbox->IsInNewPIDNamespace() && !has_started_new_init) {
     LOG(ERROR) << "The SUID sandbox created a new PID namespace but Zygote "
                   "is not the init process. Please, make sure the SUID "