Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(315)

Unified Diff: content/browser/child_process_launcher_linux.cc

Issue 2594203004: Unifying ChildProcessLauncher across platforms. (Closed)
Patch Set: Addressed boliu@'s comments Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/child_process_launcher_linux.cc
diff --git a/content/browser/child_process_launcher_linux.cc b/content/browser/child_process_launcher_linux.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d129c7a9af8c4fc144e8c7641067248fdc216e24
--- /dev/null
+++ b/content/browser/child_process_launcher_linux.cc
@@ -0,0 +1,177 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/posix/global_descriptors.h"
+#include "content/browser/child_process_launcher.h"
+#include "content/browser/child_process_launcher_posix.h"
+#include "content/browser/renderer_host/render_sandbox_host_linux.h"
+#include "content/browser/zygote_host/zygote_communication_linux.h"
+#include "content/browser/zygote_host/zygote_host_impl_linux.h"
+#include "content/common/child_process_sandbox_support_impl_linux.h"
+#include "content/public/browser/content_browser_client.h"
+#include "content/public/browser/zygote_handle_linux.h"
+#include "content/public/common/content_client.h"
+#include "content/public/common/content_switches.h"
+#include "content/public/common/result_codes.h"
+#include "content/public/common/sandboxed_process_launcher_delegate.h"
+#include "gin/v8_initializer.h"
+
+namespace content {
+
+mojo::edk::ScopedPlatformHandle
+ChildProcessLauncher::Helper::PrepareMojoPipeHandlesOnClientThread() {
+ DCHECK_CURRENTLY_ON(client_thread_id_);
+ return mojo::edk::ScopedPlatformHandle();
+}
+
+void ChildProcessLauncher::Helper::BeforeLaunchOnClientThread() {
+ DCHECK_CURRENTLY_ON(client_thread_id_);
+}
+
+bool ChildProcessLauncher::Helper::ShouldForkAsZygote() {
+ return !base::CommandLine::ForCurrentProcess()->HasSwitch(
+ switches::kNoZygote) && delegate_->GetZygote() != nullptr;
+}
+
+ZygoteHandle ChildProcessLauncher::Helper::ForkAsZygote(
+ std::unique_ptr<FileDescriptorInfo> files_to_register,
+ base::Process* process) {
+ ZygoteHandle* zygote_handle = delegate_->GetZygote();
+ DCHECK(zygote_handle); // This should only be called when ZygoteSuppported()
+ // returned true.
+
+ // This code runs on the PROCESS_LAUNCHER thread so race conditions are not
+ // an issue with the lazy initialization.
+ if (*zygote_handle == nullptr) {
+ *zygote_handle = CreateZygote();
+ }
+ base::ProcessHandle handle = (*zygote_handle)->ForkRequest(
+ command_line()->argv(),
+ std::move(files_to_register),
+ GetProcessType());
+ *process = base::Process(handle);
+
+ return *zygote_handle;
+}
+
+std::unique_ptr<FileDescriptorInfo>
+ChildProcessLauncher::Helper::GetFilesToMap() {
+ DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER);
+
+ std::unique_ptr<FileDescriptorInfo> files_to_register =
+ CreateDefaultPosixFilesToMap(*command_line(), child_process_id(),
+ mojo_client_handle());
+
+#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
+ bool snapshot_loaded = false;
+ base::MemoryMappedFile::Region unused_region;
+ base::PlatformFile natives_pf =
+ gin::V8Initializer::GetOpenNativesFileForChildProcesses(&unused_region);
+ DCHECK_GE(natives_pf, 0);
+ files_to_register->Share(kV8NativesDataDescriptor, natives_pf);
+
+ base::MemoryMappedFile::Region snapshot_region;
+ base::PlatformFile snapshot_pf =
+ gin::V8Initializer::GetOpenSnapshotFileForChildProcesses(
+ &snapshot_region);
+ // Failure to load the V8 snapshot is not necessarily an error. V8 can start
+ // up (slower) without the snapshot.
+ if (snapshot_pf != -1) {
+ snapshot_loaded = true;
+ files_to_register->Share(kV8SnapshotDataDescriptor, snapshot_pf);
+ }
+ if (GetProcessType() != switches::kZygoteProcess) {
+ command_line()->AppendSwitch(::switches::kV8NativesPassedByFD);
+ if (snapshot_loaded) {
+ command_line()->AppendSwitch(::switches::kV8SnapshotPassedByFD);
+ }
+ }
+#endif // defined(V8_USE_EXTERNAL_STARTUP_DATA)
+
+ return files_to_register;
+}
+
+void ChildProcessLauncher::Helper::BeforeLaunchOnLauncherThread(
+ const FileDescriptorInfo& files_to_register,
+ base::LaunchOptions* options) {
+ // Convert FD mapping to FileHandleMappingVector
+ std::unique_ptr<base::FileHandleMappingVector> fds_to_map =
+ files_to_register.GetMappingWithIDAdjustment(
+ base::GlobalDescriptors::kBaseDescriptor);
+
+ if (GetProcessType() == switches::kRendererProcess) {
+ const int sandbox_fd =
+ RenderSandboxHostLinux::GetInstance()->GetRendererSocket();
+ fds_to_map->push_back(std::make_pair(sandbox_fd, GetSandboxFD()));
+ }
+
+ options->environ = delegate_->GetEnvironment();
+ // fds_to_remap will de deleted in AfterLaunchOnLauncherThread() below.
+ options->fds_to_remap = fds_to_map.release();
+}
+
+base::Process ChildProcessLauncher::Helper::LaunchProcessOnLauncherThread(
+ const base::LaunchOptions& options,
+ FileDescriptorInfo* files_to_register,
+ bool* is_synchronous_launch,
+ int* launch_result) {
+ *is_synchronous_launch = true;
+ base::Process process = base::LaunchProcess(*command_line(), options);
+ *launch_result = process.IsValid() ? LAUNCH_RESULT_SUCCESS
+ : LAUNCH_RESULT_FAILURE;
+ return process;
+}
+
+void ChildProcessLauncher::Helper::AfterLaunchOnLauncherThread(
+ const base::Process& process,
+ const base::LaunchOptions& options) {
+ delete options.fds_to_remap;
+}
+
+void ChildProcessLauncher::UpdateTerminationStatus(bool known_dead) {
+ DCHECK(CalledOnValidThread());
+ if (zygote_) {
+ termination_status_ = zygote_->GetTerminationStatus(
+ process_.Handle(), known_dead, &exit_code_);
+ } else if (known_dead) {
+ termination_status_ =
+ base::GetKnownDeadTerminationStatus(process_.Handle(), &exit_code_);
+ } else {
+ termination_status_ =
+ base::GetTerminationStatus(process_.Handle(), &exit_code_);
+ }
+}
+
+// static
+bool ChildProcessLauncher::TerminateProcess(
+ const base::Process& process, int exit_code, bool wait) {
+ return process.Terminate(exit_code, wait);
+}
+
+// static
+void ChildProcessLauncher::TerminateOnLauncherThread(
+ ZygoteHandle zygote, base::Process process) {
+ DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER);
+ // Client has gone away, so just kill the process. Using exit code 0
+ // means that UMA won't treat this as a crash.
+ process.Terminate(RESULT_CODE_NORMAL_EXIT, false);
+ // On POSIX, we must additionally reap the child.
+ if (zygote) {
+ // If the renderer was created via a zygote, we have to proxy the reaping
+ // through the zygote process.
+ zygote->EnsureProcessTerminated(process.Handle());
+ } else {
+ base::EnsureProcessTerminated(std::move(process));
+ }
+}
+
+// static
+void ChildProcessLauncher::SetProcessBackgroundedOnLauncherThread(
+ base::Process process, bool background) {
+ DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER);
+ if (process.CanBackgroundProcesses())
+ process.SetProcessBackgrounded(background);
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698