| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/memory/ptr_util.h" |
| 6 #include "base/posix/global_descriptors.h" |
| 7 #include "content/browser/bootstrap_sandbox_manager_mac.h" |
| 8 #include "content/browser/child_process_launcher.h" |
| 9 #include "content/browser/child_process_launcher_posix.h" |
| 10 #include "content/browser/mach_broker_mac.h" |
| 11 #include "content/public/common/result_codes.h" |
| 12 #include "content/public/common/sandboxed_process_launcher_delegate.h" |
| 13 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 14 #include "sandbox/mac/bootstrap_sandbox.h" |
| 15 #include "sandbox/mac/pre_exec_delegate.h" |
| 16 |
| 17 namespace content { |
| 18 |
| 19 mojo::edk::ScopedPlatformHandle |
| 20 ChildProcessLauncher::Helper::PrepareMojoPipeHandlesOnClientThread() { |
| 21 DCHECK_CURRENTLY_ON(client_thread_id_); |
| 22 return mojo::edk::ScopedPlatformHandle(); |
| 23 } |
| 24 |
| 25 void ChildProcessLauncher::Helper::BeforeLaunchOnClientThread() { |
| 26 DCHECK_CURRENTLY_ON(client_thread_id_); |
| 27 } |
| 28 |
| 29 bool ChildProcessLauncher::Helper::ShouldForkAsZygote() { |
| 30 return false; |
| 31 } |
| 32 |
| 33 ZygoteHandle ChildProcessLauncher::Helper::ForkAsZygote( |
| 34 std::unique_ptr<FileMappedForLaunch> files_to_register, |
| 35 base::Process* process) { |
| 36 NOTREACHED(); |
| 37 return nullptr; |
| 38 } |
| 39 |
| 40 std::unique_ptr<FileDescriptorInfo> |
| 41 ChildProcessLauncher::Helper::GetFilesToMap() { |
| 42 DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); |
| 43 return CreateDefaultPosixFilesToMap(*command_line(), child_process_id(), |
| 44 mojo_client_handle()); |
| 45 } |
| 46 |
| 47 void ChildProcessLauncher::Helper::BeforeLaunchOnLauncherThread( |
| 48 const FileMappedForLaunch& files_to_register, |
| 49 base::LaunchOptions* options) { |
| 50 // Convert FD mapping to FileHandleMappingVector. |
| 51 std::unique_ptr<base::FileHandleMappingVector> fds_to_map = |
| 52 files_to_register.GetMappingWithIDAdjustment( |
| 53 base::GlobalDescriptors::kBaseDescriptor); |
| 54 |
| 55 options->environ = delegate_->GetEnvironment(); |
| 56 // fds_to_remap will de deleted in AfterLaunchOnLauncherThread() below. |
| 57 options->fds_to_remap = fds_to_map.release(); |
| 58 |
| 59 // Hold the MachBroker lock for the duration of LaunchProcess. The child will |
| 60 // send its task port to the parent almost immediately after startup. The Mach |
| 61 // message will be delivered to the parent, but updating the record of the |
| 62 // launch will wait until after the placeholder PID is inserted below. This |
| 63 // ensures that while the child process may send its port to the parent prior |
| 64 // to the parent leaving LaunchProcess, the order in which the record in |
| 65 // MachBroker is updated is correct. |
| 66 MachBroker* broker = MachBroker::GetInstance(); |
| 67 broker->GetLock().Acquire(); |
| 68 |
| 69 // Make sure the MachBroker is running, and inform it to expect a check-in |
| 70 // from the new process. |
| 71 broker->EnsureRunning(); |
| 72 |
| 73 const SandboxType sandbox_type = delegate_->GetSandboxType(); |
| 74 std::unique_ptr<sandbox::PreExecDelegate> pre_exec_delegate; |
| 75 if (BootstrapSandboxManager::ShouldEnable()) { |
| 76 BootstrapSandboxManager* sandbox_manager = |
| 77 BootstrapSandboxManager::GetInstance(); |
| 78 if (sandbox_manager->EnabledForSandbox(sandbox_type)) { |
| 79 pre_exec_delegate = sandbox_manager->sandbox()->NewClient(sandbox_type); |
| 80 } |
| 81 } |
| 82 // options now owns the pre_exec_delegate which will be delete on |
| 83 // AfterLaunchOnLauncherThread below. |
| 84 options->pre_exec_delegate = pre_exec_delegate.release(); |
| 85 } |
| 86 |
| 87 base::Process ChildProcessLauncher::Helper::LaunchProcessOnLauncherThread( |
| 88 const base::LaunchOptions& options, |
| 89 FileDescriptorInfo* files_to_register, |
| 90 bool* is_synchronous_launch, |
| 91 int* launch_result) { |
| 92 *is_synchronous_launch = true; |
| 93 base::Process process = base::LaunchProcess(*command_line(), options); |
| 94 *launch_result = process.IsValid() ? LAUNCH_RESULT_SUCCESS |
| 95 : LAUNCH_RESULT_FAILURE; |
| 96 return process; |
| 97 } |
| 98 |
| 99 void ChildProcessLauncher::Helper::AfterLaunchOnLauncherThread( |
| 100 const base::Process& process, |
| 101 const base::LaunchOptions& options) { |
| 102 delete options.fds_to_remap; |
| 103 |
| 104 std::unique_ptr<sandbox::PreExecDelegate> pre_exec_delegate = |
| 105 base::WrapUnique(static_cast<sandbox::PreExecDelegate*>( |
| 106 options.pre_exec_delegate)); |
| 107 |
| 108 MachBroker* broker = MachBroker::GetInstance(); |
| 109 if (process.IsValid()) { |
| 110 broker->AddPlaceholderForPid(process.Pid(), child_process_id()); |
| 111 } else { |
| 112 if (pre_exec_delegate) { |
| 113 BootstrapSandboxManager::GetInstance()->sandbox()->RevokeToken( |
| 114 pre_exec_delegate->sandbox_token()); |
| 115 } |
| 116 } |
| 117 |
| 118 // After updating the broker, release the lock and let the child's message be |
| 119 // processed on the broker's thread. |
| 120 broker->GetLock().Release(); |
| 121 } |
| 122 |
| 123 void ChildProcessLauncher::UpdateTerminationStatus(bool known_dead) { |
| 124 DCHECK(CalledOnValidThread()); |
| 125 if (known_dead) { |
| 126 termination_status_ = |
| 127 base::GetKnownDeadTerminationStatus(process_.Handle(), &exit_code_); |
| 128 } else { |
| 129 termination_status_ = |
| 130 base::GetTerminationStatus(process_.Handle(), &exit_code_); |
| 131 } |
| 132 } |
| 133 |
| 134 // static |
| 135 bool ChildProcessLauncher::TerminateProcess( |
| 136 const base::Process& process, int exit_code, bool wait) { |
| 137 return process.Terminate(exit_code, wait); |
| 138 } |
| 139 |
| 140 // static |
| 141 void ChildProcessLauncher::TerminateOnLauncherThread( |
| 142 ZygoteHandle zygote, base::Process process) { |
| 143 DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); |
| 144 // Client has gone away, so just kill the process. Using exit code 0 means |
| 145 // that UMA won't treat this as a crash. |
| 146 process.Terminate(RESULT_CODE_NORMAL_EXIT, false); |
| 147 base::EnsureProcessTerminated(std::move(process)); |
| 148 } |
| 149 |
| 150 // static |
| 151 void ChildProcessLauncher::SetProcessBackgroundedOnLauncherThread( |
| 152 base::Process process, bool background) { |
| 153 if (process.CanBackgroundProcesses()) |
| 154 process.SetProcessBackgrounded(MachBroker::GetInstance(), background); |
| 155 } |
| 156 |
| 157 } // namespace content |
| OLD | NEW |