Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/child_process_launcher.h" | 5 #include "content/browser/child_process_launcher.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/i18n/icu_util.h" | 13 #include "base/i18n/icu_util.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/metrics/field_trial.h" | |
| 15 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
| 16 #include "base/process/launch.h" | 17 #include "base/process/launch.h" |
| 17 #include "base/process/process.h" | 18 #include "base/process/process.h" |
| 18 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/synchronization/lock.h" | 20 #include "base/synchronization/lock.h" |
| 20 #include "base/threading/thread.h" | 21 #include "base/threading/thread.h" |
| 21 #include "build/build_config.h" | 22 #include "build/build_config.h" |
| 22 #include "content/public/browser/content_browser_client.h" | 23 #include "content/public/browser/content_browser_client.h" |
| 23 #include "content/public/common/content_descriptors.h" | 24 #include "content/public/common/content_descriptors.h" |
| 24 #include "content/public/common/content_switches.h" | 25 #include "content/public/common/content_switches.h" |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 114 } | 115 } |
| 115 #endif | 116 #endif |
| 116 | 117 |
| 117 void LaunchOnLauncherThread(const NotifyCallback& callback, | 118 void LaunchOnLauncherThread(const NotifyCallback& callback, |
| 118 BrowserThread::ID client_thread_id, | 119 BrowserThread::ID client_thread_id, |
| 119 int child_process_id, | 120 int child_process_id, |
| 120 SandboxedProcessLauncherDelegate* delegate, | 121 SandboxedProcessLauncherDelegate* delegate, |
| 121 #if defined(OS_ANDROID) | 122 #if defined(OS_ANDROID) |
| 122 base::ScopedFD ipcfd, | 123 base::ScopedFD ipcfd, |
| 123 #endif | 124 #endif |
| 124 const base::SharedMemory* field_trial_state, | |
| 125 mojo::edk::ScopedPlatformHandle client_handle, | 125 mojo::edk::ScopedPlatformHandle client_handle, |
| 126 base::CommandLine* cmd_line) { | 126 base::CommandLine* cmd_line) { |
| 127 DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); | 127 DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); |
| 128 std::unique_ptr<SandboxedProcessLauncherDelegate> delegate_deleter(delegate); | 128 std::unique_ptr<SandboxedProcessLauncherDelegate> delegate_deleter(delegate); |
| 129 #if !defined(OS_ANDROID) | 129 #if !defined(OS_ANDROID) |
| 130 ZygoteHandle zygote = nullptr; | 130 ZygoteHandle zygote = nullptr; |
| 131 int launch_result = LAUNCH_RESULT_FAILURE; | 131 int launch_result = LAUNCH_RESULT_FAILURE; |
| 132 #endif | 132 #endif |
| 133 #if defined(OS_WIN) | 133 #if defined(OS_WIN) |
| 134 bool launch_elevated = delegate->ShouldLaunchElevated(); | 134 bool launch_elevated = delegate->ShouldLaunchElevated(); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 146 #if defined(OS_WIN) | 146 #if defined(OS_WIN) |
| 147 if (launch_elevated) { | 147 if (launch_elevated) { |
| 148 // When establishing a Mojo connection, the pipe path has already been added | 148 // When establishing a Mojo connection, the pipe path has already been added |
| 149 // to the command line. | 149 // to the command line. |
| 150 base::LaunchOptions options; | 150 base::LaunchOptions options; |
| 151 options.start_hidden = true; | 151 options.start_hidden = true; |
| 152 process = base::LaunchElevatedProcess(*cmd_line, options); | 152 process = base::LaunchElevatedProcess(*cmd_line, options); |
| 153 } else { | 153 } else { |
| 154 base::HandlesToInheritVector handles; | 154 base::HandlesToInheritVector handles; |
| 155 handles.push_back(client_handle.get().handle); | 155 handles.push_back(client_handle.get().handle); |
| 156 if (field_trial_state) | 156 if (base::FieldTrialList::field_trial_allocator) { |
|
Alexei Svitkine (slow)
2016/10/12 20:41:00
Instead of this construct, how about making a func
lawrencewu
2016/10/14 04:54:10
Done. Created a AppendFieldTrialHandleIfNeeded() f
| |
| 157 handles.push_back(field_trial_state->handle().GetHandle()); | 157 handles.push_back( |
| 158 base::FieldTrialList::field_trial_allocator->shared_memory() | |
|
bcwhite
2016/10/13 14:28:02
The shared memory MUST be passed read-only. Rende
lawrencewu
2016/10/14 04:54:10
If I understand the shared memory model correctly,
bcwhite
2016/10/14 13:56:56
Sounds reasonable but I don't know. You'll have t
| |
| 159 ->handle() | |
| 160 .GetHandle()); | |
| 161 } | |
| 158 cmd_line->AppendSwitchASCII( | 162 cmd_line->AppendSwitchASCII( |
| 159 mojo::edk::PlatformChannelPair::kMojoPlatformChannelHandleSwitch, | 163 mojo::edk::PlatformChannelPair::kMojoPlatformChannelHandleSwitch, |
| 160 base::UintToString(base::win::HandleToUint32(handles[0]))); | 164 base::UintToString(base::win::HandleToUint32(handles[0]))); |
| 161 launch_result = | 165 launch_result = |
| 162 StartSandboxedProcess(delegate, cmd_line, handles, &process); | 166 StartSandboxedProcess(delegate, cmd_line, handles, &process); |
| 163 } | 167 } |
| 164 #elif defined(OS_POSIX) | 168 #elif defined(OS_POSIX) |
| 165 std::string process_type = | 169 std::string process_type = |
| 166 cmd_line->GetSwitchValueASCII(switches::kProcessType); | 170 cmd_line->GetSwitchValueASCII(switches::kProcessType); |
| 167 std::unique_ptr<FileDescriptorInfo> files_to_register( | 171 std::unique_ptr<FileDescriptorInfo> files_to_register( |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 393 #endif | 397 #endif |
| 394 } | 398 } |
| 395 | 399 |
| 396 } // namespace | 400 } // namespace |
| 397 | 401 |
| 398 ChildProcessLauncher::ChildProcessLauncher( | 402 ChildProcessLauncher::ChildProcessLauncher( |
| 399 SandboxedProcessLauncherDelegate* delegate, | 403 SandboxedProcessLauncherDelegate* delegate, |
| 400 base::CommandLine* cmd_line, | 404 base::CommandLine* cmd_line, |
| 401 int child_process_id, | 405 int child_process_id, |
| 402 Client* client, | 406 Client* client, |
| 403 const base::SharedMemory* field_trial_state, | |
| 404 const std::string& mojo_child_token, | 407 const std::string& mojo_child_token, |
| 405 const mojo::edk::ProcessErrorCallback& process_error_callback, | 408 const mojo::edk::ProcessErrorCallback& process_error_callback, |
| 406 bool terminate_on_shutdown) | 409 bool terminate_on_shutdown) |
| 407 : client_(client), | 410 : client_(client), |
| 408 termination_status_(base::TERMINATION_STATUS_NORMAL_TERMINATION), | 411 termination_status_(base::TERMINATION_STATUS_NORMAL_TERMINATION), |
| 409 exit_code_(RESULT_CODE_NORMAL_EXIT), | 412 exit_code_(RESULT_CODE_NORMAL_EXIT), |
| 410 zygote_(nullptr), | 413 zygote_(nullptr), |
| 411 starting_(true), | 414 starting_(true), |
| 412 process_error_callback_(process_error_callback), | 415 process_error_callback_(process_error_callback), |
| 413 #if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \ | 416 #if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \ |
| 414 defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \ | 417 defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \ |
| 415 defined(UNDEFINED_SANITIZER) | 418 defined(UNDEFINED_SANITIZER) |
| 416 terminate_child_on_shutdown_(false), | 419 terminate_child_on_shutdown_(false), |
| 417 #else | 420 #else |
| 418 terminate_child_on_shutdown_(terminate_on_shutdown), | 421 terminate_child_on_shutdown_(terminate_on_shutdown), |
| 419 #endif | 422 #endif |
| 420 mojo_child_token_(mojo_child_token), | 423 mojo_child_token_(mojo_child_token), |
| 421 weak_factory_(this) { | 424 weak_factory_(this) { |
| 422 DCHECK(CalledOnValidThread()); | 425 DCHECK(CalledOnValidThread()); |
| 423 CHECK(BrowserThread::GetCurrentThreadIdentifier(&client_thread_id_)); | 426 CHECK(BrowserThread::GetCurrentThreadIdentifier(&client_thread_id_)); |
| 424 Launch(delegate, cmd_line, child_process_id, field_trial_state); | 427 Launch(delegate, cmd_line, child_process_id); |
| 425 } | 428 } |
| 426 | 429 |
| 427 ChildProcessLauncher::~ChildProcessLauncher() { | 430 ChildProcessLauncher::~ChildProcessLauncher() { |
| 428 DCHECK(CalledOnValidThread()); | 431 DCHECK(CalledOnValidThread()); |
| 429 if (process_.IsValid() && terminate_child_on_shutdown_) { | 432 if (process_.IsValid() && terminate_child_on_shutdown_) { |
| 430 // On Posix, EnsureProcessTerminated can lead to 2 seconds of sleep! So | 433 // On Posix, EnsureProcessTerminated can lead to 2 seconds of sleep! So |
| 431 // don't this on the UI/IO threads. | 434 // don't this on the UI/IO threads. |
| 432 BrowserThread::PostTask(BrowserThread::PROCESS_LAUNCHER, FROM_HERE, | 435 BrowserThread::PostTask(BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
| 433 base::Bind(&TerminateOnLauncherThread, zygote_, | 436 base::Bind(&TerminateOnLauncherThread, zygote_, |
| 434 base::Passed(&process_))); | 437 base::Passed(&process_))); |
| 435 } | 438 } |
| 436 } | 439 } |
| 437 | 440 |
| 438 void ChildProcessLauncher::Launch(SandboxedProcessLauncherDelegate* delegate, | 441 void ChildProcessLauncher::Launch(SandboxedProcessLauncherDelegate* delegate, |
| 439 base::CommandLine* cmd_line, | 442 base::CommandLine* cmd_line, |
| 440 int child_process_id, | 443 int child_process_id) { |
| 441 const base::SharedMemory* field_trial_state) { | |
| 442 DCHECK(CalledOnValidThread()); | 444 DCHECK(CalledOnValidThread()); |
| 443 | 445 |
| 444 #if defined(OS_ANDROID) | 446 #if defined(OS_ANDROID) |
| 445 // Android only supports renderer, sandboxed utility and gpu. | 447 // Android only supports renderer, sandboxed utility and gpu. |
| 446 std::string process_type = | 448 std::string process_type = |
| 447 cmd_line->GetSwitchValueASCII(switches::kProcessType); | 449 cmd_line->GetSwitchValueASCII(switches::kProcessType); |
| 448 CHECK(process_type == switches::kGpuProcess || | 450 CHECK(process_type == switches::kGpuProcess || |
| 449 process_type == switches::kRendererProcess || | 451 process_type == switches::kRendererProcess || |
| 450 #if defined(ENABLE_PLUGINS) | 452 #if defined(ENABLE_PLUGINS) |
| 451 process_type == switches::kPpapiPluginProcess || | 453 process_type == switches::kPpapiPluginProcess || |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 480 weak_factory_.GetWeakPtr(), | 482 weak_factory_.GetWeakPtr(), |
| 481 terminate_child_on_shutdown_, | 483 terminate_child_on_shutdown_, |
| 482 base::Passed(&server_handle))); | 484 base::Passed(&server_handle))); |
| 483 BrowserThread::PostTask( | 485 BrowserThread::PostTask( |
| 484 BrowserThread::PROCESS_LAUNCHER, FROM_HERE, | 486 BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
| 485 base::Bind(&LaunchOnLauncherThread, reply_callback, client_thread_id_, | 487 base::Bind(&LaunchOnLauncherThread, reply_callback, client_thread_id_, |
| 486 child_process_id, delegate, | 488 child_process_id, delegate, |
| 487 #if defined(OS_ANDROID) | 489 #if defined(OS_ANDROID) |
| 488 base::Passed(&ipcfd), | 490 base::Passed(&ipcfd), |
| 489 #endif | 491 #endif |
| 490 field_trial_state, base::Passed(&client_handle), cmd_line)); | 492 base::Passed(&client_handle), cmd_line)); |
| 491 } | 493 } |
| 492 | 494 |
| 493 void ChildProcessLauncher::UpdateTerminationStatus(bool known_dead) { | 495 void ChildProcessLauncher::UpdateTerminationStatus(bool known_dead) { |
| 494 DCHECK(CalledOnValidThread()); | 496 DCHECK(CalledOnValidThread()); |
| 495 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) | 497 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 496 if (zygote_) { | 498 if (zygote_) { |
| 497 termination_status_ = zygote_->GetTerminationStatus( | 499 termination_status_ = zygote_->GetTerminationStatus( |
| 498 process_.Handle(), known_dead, &exit_code_); | 500 process_.Handle(), known_dead, &exit_code_); |
| 499 } else if (known_dead) { | 501 } else if (known_dead) { |
| 500 termination_status_ = | 502 termination_status_ = |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 625 } | 627 } |
| 626 | 628 |
| 627 ChildProcessLauncher::Client* ChildProcessLauncher::ReplaceClientForTest( | 629 ChildProcessLauncher::Client* ChildProcessLauncher::ReplaceClientForTest( |
| 628 Client* client) { | 630 Client* client) { |
| 629 Client* ret = client_; | 631 Client* ret = client_; |
| 630 client_ = client; | 632 client_ = client; |
| 631 return ret; | 633 return ret; |
| 632 } | 634 } |
| 633 | 635 |
| 634 } // namespace content | 636 } // namespace content |
| OLD | NEW |