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