| 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/posix/global_descriptors.h" |
| 6 #include "content/browser/child_process_launcher.h" |
| 7 #include "content/browser/child_process_launcher_helper.h" |
| 8 #include "content/browser/child_process_launcher_helper_posix.h" |
| 9 #include "content/browser/renderer_host/render_sandbox_host_linux.h" |
| 10 #include "content/browser/zygote_host/zygote_communication_linux.h" |
| 11 #include "content/browser/zygote_host/zygote_host_impl_linux.h" |
| 12 #include "content/common/child_process_sandbox_support_impl_linux.h" |
| 13 #include "content/public/browser/content_browser_client.h" |
| 14 #include "content/public/browser/zygote_handle_linux.h" |
| 15 #include "content/public/common/content_client.h" |
| 16 #include "content/public/common/content_switches.h" |
| 17 #include "content/public/common/result_codes.h" |
| 18 #include "content/public/common/sandboxed_process_launcher_delegate.h" |
| 19 #include "gin/v8_initializer.h" |
| 20 |
| 21 namespace content { |
| 22 namespace internal { |
| 23 |
| 24 mojo::edk::ScopedPlatformHandle |
| 25 ChildProcessLauncherHelper::PrepareMojoPipeHandlesOnClientThread() { |
| 26 DCHECK_CURRENTLY_ON(client_thread_id_); |
| 27 return mojo::edk::ScopedPlatformHandle(); |
| 28 } |
| 29 |
| 30 void ChildProcessLauncherHelper::BeforeLaunchOnClientThread() { |
| 31 DCHECK_CURRENTLY_ON(client_thread_id_); |
| 32 } |
| 33 |
| 34 std::unique_ptr<FileMappedForLaunch> |
| 35 ChildProcessLauncherHelper::GetFilesToMap() { |
| 36 DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); |
| 37 |
| 38 std::unique_ptr<FileDescriptorInfo> files_to_register = |
| 39 CreateDefaultPosixFilesToMap(*command_line(), child_process_id(), |
| 40 mojo_client_handle()); |
| 41 |
| 42 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 43 bool snapshot_loaded = false; |
| 44 base::MemoryMappedFile::Region unused_region; |
| 45 base::PlatformFile natives_pf = |
| 46 gin::V8Initializer::GetOpenNativesFileForChildProcesses(&unused_region); |
| 47 DCHECK_GE(natives_pf, 0); |
| 48 files_to_register->Share(kV8NativesDataDescriptor, natives_pf); |
| 49 |
| 50 base::MemoryMappedFile::Region snapshot_region; |
| 51 base::PlatformFile snapshot_pf = |
| 52 gin::V8Initializer::GetOpenSnapshotFileForChildProcesses( |
| 53 &snapshot_region); |
| 54 // Failure to load the V8 snapshot is not necessarily an error. V8 can start |
| 55 // up (slower) without the snapshot. |
| 56 if (snapshot_pf != -1) { |
| 57 snapshot_loaded = true; |
| 58 files_to_register->Share(kV8SnapshotDataDescriptor, snapshot_pf); |
| 59 } |
| 60 if (GetProcessType() != switches::kZygoteProcess) { |
| 61 command_line()->AppendSwitch(::switches::kV8NativesPassedByFD); |
| 62 if (snapshot_loaded) { |
| 63 command_line()->AppendSwitch(::switches::kV8SnapshotPassedByFD); |
| 64 } |
| 65 } |
| 66 #endif // defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 67 |
| 68 return files_to_register; |
| 69 } |
| 70 |
| 71 void ChildProcessLauncherHelper::BeforeLaunchOnLauncherThread( |
| 72 const FileDescriptorInfo& files_to_register, |
| 73 base::LaunchOptions* options) { |
| 74 // Convert FD mapping to FileHandleMappingVector |
| 75 std::unique_ptr<base::FileHandleMappingVector> fds_to_map = |
| 76 files_to_register.GetMappingWithIDAdjustment( |
| 77 base::GlobalDescriptors::kBaseDescriptor); |
| 78 |
| 79 if (GetProcessType() == switches::kRendererProcess) { |
| 80 const int sandbox_fd = |
| 81 RenderSandboxHostLinux::GetInstance()->GetRendererSocket(); |
| 82 fds_to_map->push_back(std::make_pair(sandbox_fd, GetSandboxFD())); |
| 83 } |
| 84 |
| 85 options->environ = delegate_->GetEnvironment(); |
| 86 // fds_to_remap will de deleted in AfterLaunchOnLauncherThread() below. |
| 87 options->fds_to_remap = fds_to_map.release(); |
| 88 } |
| 89 |
| 90 ChildProcessLauncherHelper::Process |
| 91 ChildProcessLauncherHelper::LaunchProcessOnLauncherThread( |
| 92 const base::LaunchOptions& options, |
| 93 std::unique_ptr<FileMappedForLaunch> files_to_register, |
| 94 bool* is_synchronous_launch, |
| 95 int* launch_result) { |
| 96 *is_synchronous_launch = true; |
| 97 |
| 98 ZygoteHandle* zygote_handle = |
| 99 base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoZygote) ? |
| 100 nullptr : delegate_->GetZygote(); |
| 101 if (zygote_handle) { |
| 102 // This code runs on the PROCESS_LAUNCHER thread so race conditions are not |
| 103 // an issue with the lazy initialization. |
| 104 if (*zygote_handle == nullptr) { |
| 105 *zygote_handle = CreateZygote(); |
| 106 } |
| 107 base::ProcessHandle handle = (*zygote_handle)->ForkRequest( |
| 108 command_line()->argv(), |
| 109 std::move(files_to_register), |
| 110 GetProcessType()); |
| 111 *launch_result = LAUNCH_RESULT_SUCCESS; |
| 112 Process process; |
| 113 process.process = base::Process(handle); |
| 114 process.zygote = *zygote_handle; |
| 115 return process; |
| 116 } |
| 117 |
| 118 Process process; |
| 119 process.process = base::LaunchProcess(*command_line(), options); |
| 120 *launch_result = process.process.IsValid() ? LAUNCH_RESULT_SUCCESS |
| 121 : LAUNCH_RESULT_FAILURE; |
| 122 return process; |
| 123 } |
| 124 |
| 125 void ChildProcessLauncherHelper::AfterLaunchOnLauncherThread( |
| 126 const ChildProcessLauncherHelper::Process& process, |
| 127 const base::LaunchOptions& options) { |
| 128 delete options.fds_to_remap; |
| 129 } |
| 130 |
| 131 // static |
| 132 base::TerminationStatus ChildProcessLauncherHelper::GetTerminationStatus( |
| 133 const ChildProcessLauncherHelper::Process& process, |
| 134 bool known_dead, |
| 135 int* exit_code) { |
| 136 if (process.zygote) { |
| 137 return process.zygote->GetTerminationStatus( |
| 138 process.process.Handle(), known_dead, exit_code); |
| 139 } |
| 140 if (known_dead) { |
| 141 return base::GetKnownDeadTerminationStatus( |
| 142 process.process.Handle(), exit_code); |
| 143 } |
| 144 return base::GetTerminationStatus(process.process.Handle(), exit_code); |
| 145 } |
| 146 |
| 147 // static |
| 148 bool ChildProcessLauncherHelper::TerminateProcess( |
| 149 const base::Process& process, int exit_code, bool wait) { |
| 150 return process.Terminate(exit_code, wait); |
| 151 } |
| 152 |
| 153 // static |
| 154 void ChildProcessLauncherHelper::ForceNormalProcessTerminationSync( |
| 155 ChildProcessLauncherHelper::Process process) { |
| 156 process.process.Terminate(RESULT_CODE_NORMAL_EXIT, false); |
| 157 // On POSIX, we must additionally reap the child. |
| 158 if (process.zygote) { |
| 159 // If the renderer was created via a zygote, we have to proxy the reaping |
| 160 // through the zygote process. |
| 161 process.zygote->EnsureProcessTerminated(process.process.Handle()); |
| 162 } else { |
| 163 base::EnsureProcessTerminated(std::move(process.process)); |
| 164 } |
| 165 } |
| 166 |
| 167 // static |
| 168 void ChildProcessLauncherHelper::SetProcessBackgroundedOnLauncherThread( |
| 169 base::Process process, bool background) { |
| 170 DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); |
| 171 if (process.CanBackgroundProcesses()) |
| 172 process.SetProcessBackgrounded(background); |
| 173 } |
| 174 |
| 175 } // namespace internal |
| 176 } // namespace content |
| OLD | NEW |