shrike | 8fbe9d3 | 2015-06-02 19:53:57 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors. All rights reserved. |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 5 | #include "content/browser/child_process_launcher.h" |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 6 | |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 7 | #include <memory> |
danakj | e3de838f | 2015-12-03 01:49:40 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
[email protected] | 037edb5 | 2011-11-15 21:14:06 | [diff] [blame] | 10 | #include "base/bind.h" |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 11 | #include "base/command_line.h" |
thestig | b7aad54f | 2014-09-05 18:25:39 | [diff] [blame] | 12 | #include "base/files/file_util.h" |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 13 | #include "base/i18n/icu_util.h" |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 14 | #include "base/logging.h" |
[email protected] | 323fdc31 | 2013-02-25 18:44:26 | [diff] [blame] | 15 | #include "base/metrics/histogram.h" |
rockot | b3b0dfa0 | 2016-02-26 22:43:28 | [diff] [blame] | 16 | #include "base/process/launch.h" |
[email protected] | dd4b5126 | 2013-07-25 21:38:23 | [diff] [blame] | 17 | #include "base/process/process.h" |
ben | 03b7b9d | 2015-11-15 12:13:09 | [diff] [blame] | 18 | #include "base/strings/string_number_conversions.h" |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 19 | #include "base/synchronization/lock.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 20 | #include "base/threading/thread.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 21 | #include "build/build_config.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 22 | #include "content/public/browser/content_browser_client.h" |
[email protected] | b3aabd34 | 2012-06-04 19:33:56 | [diff] [blame] | 23 | #include "content/public/common/content_descriptors.h" |
[email protected] | c08950d2 | 2011-10-13 22:20:29 | [diff] [blame] | 24 | #include "content/public/common/content_switches.h" |
[email protected] | b39ef1cb | 2011-10-25 04:46:55 | [diff] [blame] | 25 | #include "content/public/common/result_codes.h" |
[email protected] | 121e6138 | 2014-03-13 11:35:15 | [diff] [blame] | 26 | #include "content/public/common/sandboxed_process_launcher_delegate.h" |
rockot | e8b8da4 | 2016-03-02 06:20:23 | [diff] [blame] | 27 | #include "mojo/edk/embedder/embedder.h" |
amistry | b613891 | 2016-05-31 01:54:12 | [diff] [blame] | 28 | #include "mojo/edk/embedder/named_platform_channel_pair.h" |
| 29 | #include "mojo/edk/embedder/platform_channel_pair.h" |
rockot | e8b8da4 | 2016-03-02 06:20:23 | [diff] [blame] | 30 | #include "mojo/edk/embedder/scoped_platform_handle.h" |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 31 | |
| 32 | #if defined(OS_WIN) |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 33 | #include "base/files/file_path.h" |
rockot | e8b8da4 | 2016-03-02 06:20:23 | [diff] [blame] | 34 | #include "base/win/scoped_handle.h" |
| 35 | #include "base/win/win_util.h" |
[email protected] | 34f4868 | 2013-03-20 00:30:18 | [diff] [blame] | 36 | #include "content/common/sandbox_win.h" |
[email protected] | e25b04c9 | 2012-10-23 20:05:06 | [diff] [blame] | 37 | #include "content/public/common/sandbox_init.h" |
wfh | 3adf87d | 2016-05-03 23:26:06 | [diff] [blame] | 38 | #include "sandbox/win/src/sandbox_types.h" |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 39 | #elif defined(OS_MACOSX) |
rsesek | dba8411 | 2015-09-18 19:22:07 | [diff] [blame] | 40 | #include "content/browser/bootstrap_sandbox_manager_mac.h" |
[email protected] | ed0fbe6 | 2011-06-23 18:32:11 | [diff] [blame] | 41 | #include "content/browser/mach_broker_mac.h" |
[email protected] | f5bcd0f | 2014-06-10 14:50:41 | [diff] [blame] | 42 | #include "sandbox/mac/bootstrap_sandbox.h" |
rsesek | 408d2ee5 | 2015-09-18 01:18:04 | [diff] [blame] | 43 | #include "sandbox/mac/pre_exec_delegate.h" |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 44 | #elif defined(OS_ANDROID) |
| 45 | #include "base/android/jni_android.h" |
[email protected] | 5e44a14 | 2013-03-19 09:48:39 | [diff] [blame] | 46 | #include "content/browser/android/child_process_launcher_android.h" |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 47 | #elif defined(OS_POSIX) |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 48 | #include "base/memory/singleton.h" |
[email protected] | a01efd2 | 2011-03-01 00:38:32 | [diff] [blame] | 49 | #include "content/browser/renderer_host/render_sandbox_host_linux.h" |
kerrnel | afd49a83b | 2016-01-22 21:16:15 | [diff] [blame] | 50 | #include "content/browser/zygote_host/zygote_communication_linux.h" |
[email protected] | 13d6b3c | 2012-07-24 01:31:31 | [diff] [blame] | 51 | #include "content/browser/zygote_host/zygote_host_impl_linux.h" |
[email protected] | a0c900e | 2013-09-11 18:16:10 | [diff] [blame] | 52 | #include "content/common/child_process_sandbox_support_impl_linux.h" |
kerrnel | afd49a83b | 2016-01-22 21:16:15 | [diff] [blame] | 53 | #include "content/public/browser/zygote_handle_linux.h" |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 54 | #endif |
| 55 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 56 | #if defined(OS_POSIX) |
[email protected] | 613eef6 | 2012-11-09 23:46:54 | [diff] [blame] | 57 | #include "base/posix/global_descriptors.h" |
morrita | d95714f | 2014-10-01 02:37:24 | [diff] [blame] | 58 | #include "content/browser/file_descriptor_info_impl.h" |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 59 | #include "gin/v8_initializer.h" |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 60 | #endif |
| 61 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 62 | namespace content { |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 63 | |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 64 | namespace { |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 65 | |
kerrnel | afd49a83b | 2016-01-22 21:16:15 | [diff] [blame] | 66 | typedef base::Callback<void(ZygoteHandle, |
tkent | dd53cf732 | 2015-03-23 00:54:50 | [diff] [blame] | 67 | #if defined(OS_ANDROID) |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 68 | base::ScopedFD, |
rockot | e8b8da4 | 2016-03-02 06:20:23 | [diff] [blame] | 69 | base::ScopedFD, |
tkent | dd53cf732 | 2015-03-23 00:54:50 | [diff] [blame] | 70 | #endif |
wfh | 3adf87d | 2016-05-03 23:26:06 | [diff] [blame] | 71 | base::Process, |
| 72 | int)> NotifyCallback; |
tkent | dd53cf732 | 2015-03-23 00:54:50 | [diff] [blame] | 73 | |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 74 | void RecordHistogramsOnLauncherThread(base::TimeDelta launch_time) { |
| 75 | DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 76 | // Log the launch time, separating out the first one (which will likely be |
| 77 | // slower due to the rest of the browser initializing at the same time). |
| 78 | static bool done_first_launch = false; |
| 79 | if (done_first_launch) { |
| 80 | UMA_HISTOGRAM_TIMES("MPArch.ChildProcessLaunchSubsequent", launch_time); |
| 81 | } else { |
| 82 | UMA_HISTOGRAM_TIMES("MPArch.ChildProcessLaunchFirst", launch_time); |
| 83 | done_first_launch = true; |
| 84 | } |
| 85 | } |
| 86 | |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 87 | #if defined(OS_ANDROID) |
| 88 | // TODO(sievers): Remove this by defining better what happens on what |
| 89 | // thread in the corresponding Java code. |
| 90 | void OnChildProcessStartedAndroid(const NotifyCallback& callback, |
| 91 | BrowserThread::ID client_thread_id, |
| 92 | const base::TimeTicks begin_launch_time, |
| 93 | base::ScopedFD ipcfd, |
rockot | e8b8da4 | 2016-03-02 06:20:23 | [diff] [blame] | 94 | base::ScopedFD mojo_fd, |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 95 | base::ProcessHandle handle) { |
wfh | 3adf87d | 2016-05-03 23:26:06 | [diff] [blame] | 96 | int launch_result = (handle == base::kNullProcessHandle) |
| 97 | ? LAUNCH_RESULT_FAILURE |
| 98 | : LAUNCH_RESULT_SUCCESS; |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 99 | // This can be called on the launcher thread or UI thread. |
| 100 | base::TimeDelta launch_time = base::TimeTicks::Now() - begin_launch_time; |
| 101 | BrowserThread::PostTask( |
| 102 | BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
| 103 | base::Bind(&RecordHistogramsOnLauncherThread, launch_time)); |
| 104 | |
wfh | 3adf87d | 2016-05-03 23:26:06 | [diff] [blame] | 105 | base::Closure callback_on_client_thread(base::Bind( |
| 106 | callback, nullptr, base::Passed(&ipcfd), base::Passed(&mojo_fd), |
| 107 | base::Passed(base::Process(handle)), launch_result)); |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 108 | if (BrowserThread::CurrentlyOn(client_thread_id)) { |
| 109 | callback_on_client_thread.Run(); |
| 110 | } else { |
| 111 | BrowserThread::PostTask( |
| 112 | client_thread_id, FROM_HERE, callback_on_client_thread); |
thestig | 774686b | 2015-09-15 19:34:31 | [diff] [blame] | 113 | } |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 114 | } |
| 115 | #endif |
| 116 | |
| 117 | void LaunchOnLauncherThread(const NotifyCallback& callback, |
| 118 | BrowserThread::ID client_thread_id, |
| 119 | int child_process_id, |
| 120 | SandboxedProcessLauncherDelegate* delegate, |
| 121 | #if defined(OS_ANDROID) |
| 122 | base::ScopedFD ipcfd, |
| 123 | #endif |
rockot | e8b8da4 | 2016-03-02 06:20:23 | [diff] [blame] | 124 | mojo::edk::ScopedPlatformHandle client_handle, |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 125 | base::CommandLine* cmd_line) { |
| 126 | DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 127 | std::unique_ptr<SandboxedProcessLauncherDelegate> delegate_deleter(delegate); |
kerrnel | afd49a83b | 2016-01-22 21:16:15 | [diff] [blame] | 128 | #if !defined(OS_ANDROID) |
| 129 | ZygoteHandle zygote = nullptr; |
wfh | 3adf87d | 2016-05-03 23:26:06 | [diff] [blame] | 130 | int launch_result = LAUNCH_RESULT_FAILURE; |
kerrnel | afd49a83b | 2016-01-22 21:16:15 | [diff] [blame] | 131 | #endif |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 132 | #if defined(OS_WIN) |
| 133 | bool launch_elevated = delegate->ShouldLaunchElevated(); |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 134 | #elif defined(OS_MACOSX) |
| 135 | base::EnvironmentMap env = delegate->GetEnvironment(); |
| 136 | base::ScopedFD ipcfd = delegate->TakeIpcFd(); |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 137 | #elif defined(OS_POSIX) && !defined(OS_ANDROID) |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 138 | base::EnvironmentMap env = delegate->GetEnvironment(); |
| 139 | base::ScopedFD ipcfd = delegate->TakeIpcFd(); |
| 140 | #endif |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 141 | std::unique_ptr<base::CommandLine> cmd_line_deleter(cmd_line); |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 142 | base::TimeTicks begin_launch_time = base::TimeTicks::Now(); |
| 143 | |
rvargas | 3e5d445 | 2014-11-24 19:30:02 | [diff] [blame] | 144 | base::Process process; |
rvargas | 0bf966a | 2015-01-10 04:16:33 | [diff] [blame] | 145 | #if defined(OS_WIN) |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 146 | if (launch_elevated) { |
amistry | b613891 | 2016-05-31 01:54:12 | [diff] [blame] | 147 | // When establishing a Mojo connection, the pipe path has already been added |
| 148 | // to the command line. |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 149 | base::LaunchOptions options; |
| 150 | options.start_hidden = true; |
rvargas | 6293e5b | 2014-12-01 22:53:09 | [diff] [blame] | 151 | process = base::LaunchElevatedProcess(*cmd_line, options); |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 152 | } else { |
rockot | e8b8da4 | 2016-03-02 06:20:23 | [diff] [blame] | 153 | base::HandlesToInheritVector handles; |
| 154 | handles.push_back(client_handle.get().handle); |
| 155 | cmd_line->AppendSwitchASCII( |
| 156 | mojo::edk::PlatformChannelPair::kMojoPlatformChannelHandleSwitch, |
| 157 | base::UintToString(base::win::HandleToUint32(handles[0]))); |
wfh | 3adf87d | 2016-05-03 23:26:06 | [diff] [blame] | 158 | launch_result = |
| 159 | StartSandboxedProcess(delegate, cmd_line, handles, &process); |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 160 | } |
| 161 | #elif defined(OS_POSIX) |
| 162 | std::string process_type = |
| 163 | cmd_line->GetSwitchValueASCII(switches::kProcessType); |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 164 | std::unique_ptr<FileDescriptorInfo> files_to_register( |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 165 | FileDescriptorInfoImpl::Create()); |
| 166 | |
rockot | e8b8da4 | 2016-03-02 06:20:23 | [diff] [blame] | 167 | base::ScopedFD mojo_fd(client_handle.release().handle); |
| 168 | DCHECK(mojo_fd.is_valid()); |
| 169 | |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 170 | #if defined(OS_ANDROID) |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 171 | if (ipcfd.get() != -1) |
| 172 | files_to_register->Share(kPrimaryIPCChannel, ipcfd.get()); |
rockot | e8b8da4 | 2016-03-02 06:20:23 | [diff] [blame] | 173 | files_to_register->Share(kMojoIPCChannel, mojo_fd.get()); |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 174 | #else |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 175 | if (ipcfd.get() != -1) |
| 176 | files_to_register->Transfer(kPrimaryIPCChannel, std::move(ipcfd)); |
rockot | e8b8da4 | 2016-03-02 06:20:23 | [diff] [blame] | 177 | files_to_register->Transfer(kMojoIPCChannel, std::move(mojo_fd)); |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 178 | #endif |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 179 | #endif |
| 180 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 181 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
mek | 39ae2159 | 2015-06-16 22:30:38 | [diff] [blame] | 182 | std::map<int, base::MemoryMappedFile::Region> regions; |
| 183 | GetContentClient()->browser()->GetAdditionalMappedFilesForChildProcess( |
agrieve | 05398f5 | 2015-06-24 18:59:00 | [diff] [blame] | 184 | *cmd_line, child_process_id, files_to_register.get() |
| 185 | #if defined(OS_ANDROID) |
| 186 | , ®ions |
| 187 | #endif |
| 188 | ); |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 189 | #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 190 | bool snapshot_loaded = false; |
| 191 | #if defined(OS_ANDROID) |
| 192 | base::MemoryMappedFile::Region region; |
| 193 | auto maybe_register = [®ion, ®ions, &files_to_register](int key, |
| 194 | int fd) { |
| 195 | if (fd != -1) { |
| 196 | files_to_register->Share(key, fd); |
| 197 | regions.insert(std::make_pair(key, region)); |
| 198 | } |
| 199 | }; |
| 200 | maybe_register( |
michaelbai | 02037588 | 2016-06-21 16:08:15 | [diff] [blame] | 201 | kV8NativesDataDescriptor, |
| 202 | gin::V8Initializer::GetOpenNativesFileForChildProcesses(®ion)); |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 203 | maybe_register( |
| 204 | kV8SnapshotDataDescriptor32, |
| 205 | gin::V8Initializer::GetOpenSnapshotFileForChildProcesses(®ion, true)); |
| 206 | maybe_register( |
| 207 | kV8SnapshotDataDescriptor64, |
| 208 | gin::V8Initializer::GetOpenSnapshotFileForChildProcesses(®ion, false)); |
| 209 | |
| 210 | snapshot_loaded = true; |
| 211 | #else |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 212 | base::PlatformFile natives_pf = |
| 213 | gin::V8Initializer::GetOpenNativesFileForChildProcesses( |
| 214 | ®ions[kV8NativesDataDescriptor]); |
| 215 | DCHECK_GE(natives_pf, 0); |
| 216 | files_to_register->Share(kV8NativesDataDescriptor, natives_pf); |
mek | 39ae2159 | 2015-06-16 22:30:38 | [diff] [blame] | 217 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 218 | base::MemoryMappedFile::Region snapshot_region; |
| 219 | base::PlatformFile snapshot_pf = |
| 220 | gin::V8Initializer::GetOpenSnapshotFileForChildProcesses( |
| 221 | &snapshot_region); |
| 222 | // Failure to load the V8 snapshot is not necessarily an error. V8 can start |
| 223 | // up (slower) without the snapshot. |
| 224 | if (snapshot_pf != -1) { |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 225 | snapshot_loaded = true; |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 226 | files_to_register->Share(kV8SnapshotDataDescriptor, snapshot_pf); |
| 227 | regions.insert(std::make_pair(kV8SnapshotDataDescriptor, snapshot_region)); |
| 228 | } |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 229 | #endif |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 230 | |
| 231 | if (process_type != switches::kZygoteProcess) { |
| 232 | cmd_line->AppendSwitch(::switches::kV8NativesPassedByFD); |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 233 | if (snapshot_loaded) { |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 234 | cmd_line->AppendSwitch(::switches::kV8SnapshotPassedByFD); |
| 235 | } |
| 236 | } |
| 237 | #endif // defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 238 | #endif // defined(OS_POSIX) && !defined(OS_MACOSX) |
| 239 | |
| 240 | #if defined(OS_ANDROID) |
| 241 | files_to_register->Share( |
| 242 | kAndroidICUDataDescriptor, |
| 243 | base::i18n::GetIcuDataFileHandle(®ions[kAndroidICUDataDescriptor])); |
| 244 | |
| 245 | // Android WebView runs in single process, ensure that we never get here |
| 246 | // when running in single process mode. |
| 247 | CHECK(!cmd_line->HasSwitch(switches::kSingleProcess)); |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 248 | |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 249 | StartChildProcess( |
danakj | e3de838f | 2015-12-03 01:49:40 | [diff] [blame] | 250 | cmd_line->argv(), child_process_id, std::move(files_to_register), regions, |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 251 | base::Bind(&OnChildProcessStartedAndroid, callback, client_thread_id, |
rockot | e8b8da4 | 2016-03-02 06:20:23 | [diff] [blame] | 252 | begin_launch_time, base::Passed(&ipcfd), |
| 253 | base::Passed(&mojo_fd))); |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 254 | |
| 255 | #elif defined(OS_POSIX) |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 256 | // We need to close the client end of the IPC channel to reliably detect |
| 257 | // child termination. |
| 258 | |
| 259 | #if !defined(OS_MACOSX) |
kerrnel | afd49a83b | 2016-01-22 21:16:15 | [diff] [blame] | 260 | ZygoteHandle* zygote_handle = delegate->GetZygote(); |
| 261 | // If |zygote_handle| is null, a zygote should not be used. |
| 262 | if (zygote_handle) { |
| 263 | // This code runs on the PROCESS_LAUNCHER thread so race conditions are not |
| 264 | // an issue with the lazy initialization. |
| 265 | if (*zygote_handle == nullptr) { |
| 266 | *zygote_handle = CreateZygote(); |
| 267 | } |
| 268 | zygote = *zygote_handle; |
| 269 | base::ProcessHandle handle = zygote->ForkRequest( |
danakj | e3de838f | 2015-12-03 01:49:40 | [diff] [blame] | 270 | cmd_line->argv(), std::move(files_to_register), process_type); |
rvargas | 0bf966a | 2015-01-10 04:16:33 | [diff] [blame] | 271 | process = base::Process(handle); |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 272 | } else |
| 273 | // Fall through to the normal posix case below when we're not zygoting. |
| 274 | #endif // !defined(OS_MACOSX) |
| 275 | { |
| 276 | // Convert FD mapping to FileHandleMappingVector |
| 277 | base::FileHandleMappingVector fds_to_map = |
| 278 | files_to_register->GetMappingWithIDAdjustment( |
| 279 | base::GlobalDescriptors::kBaseDescriptor); |
| 280 | |
| 281 | #if !defined(OS_MACOSX) |
| 282 | if (process_type == switches::kRendererProcess) { |
| 283 | const int sandbox_fd = |
| 284 | RenderSandboxHostLinux::GetInstance()->GetRendererSocket(); |
| 285 | fds_to_map.push_back(std::make_pair( |
| 286 | sandbox_fd, |
| 287 | GetSandboxFD())); |
| 288 | } |
| 289 | #endif // defined(OS_MACOSX) |
| 290 | |
| 291 | // Actually launch the app. |
| 292 | base::LaunchOptions options; |
| 293 | options.environ = env; |
| 294 | options.fds_to_remap = &fds_to_map; |
| 295 | |
| 296 | #if defined(OS_MACOSX) |
| 297 | // Hold the MachBroker lock for the duration of LaunchProcess. The child |
| 298 | // will send its task port to the parent almost immediately after startup. |
| 299 | // The Mach message will be delivered to the parent, but updating the |
| 300 | // record of the launch will wait until after the placeholder PID is |
| 301 | // inserted below. This ensures that while the child process may send its |
| 302 | // port to the parent prior to the parent leaving LaunchProcess, the |
| 303 | // order in which the record in MachBroker is updated is correct. |
| 304 | MachBroker* broker = MachBroker::GetInstance(); |
| 305 | broker->GetLock().Acquire(); |
| 306 | |
| 307 | // Make sure the MachBroker is running, and inform it to expect a |
| 308 | // check-in from the new process. |
| 309 | broker->EnsureRunning(); |
| 310 | |
rsesek | dba8411 | 2015-09-18 19:22:07 | [diff] [blame] | 311 | const SandboxType sandbox_type = delegate->GetSandboxType(); |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 312 | std::unique_ptr<sandbox::PreExecDelegate> pre_exec_delegate; |
rsesek | dba8411 | 2015-09-18 19:22:07 | [diff] [blame] | 313 | if (BootstrapSandboxManager::ShouldEnable()) { |
| 314 | BootstrapSandboxManager* sandbox_manager = |
| 315 | BootstrapSandboxManager::GetInstance(); |
| 316 | if (sandbox_manager->EnabledForSandbox(sandbox_type)) { |
dcheng | f26eed3 | 2016-01-13 10:58:14 | [diff] [blame] | 317 | pre_exec_delegate = sandbox_manager->sandbox()->NewClient(sandbox_type); |
rsesek | dba8411 | 2015-09-18 19:22:07 | [diff] [blame] | 318 | } |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 319 | } |
rsesek | 408d2ee5 | 2015-09-18 01:18:04 | [diff] [blame] | 320 | options.pre_exec_delegate = pre_exec_delegate.get(); |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 321 | #endif // defined(OS_MACOSX) |
| 322 | |
rvargas | 0bf966a | 2015-01-10 04:16:33 | [diff] [blame] | 323 | process = base::LaunchProcess(*cmd_line, options); |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 324 | |
| 325 | #if defined(OS_MACOSX) |
rsesek | 408d2ee5 | 2015-09-18 01:18:04 | [diff] [blame] | 326 | if (process.IsValid()) { |
rvargas | 960db88 | 2015-01-24 00:27:25 | [diff] [blame] | 327 | broker->AddPlaceholderForPid(process.Pid(), child_process_id); |
rsesek | 408d2ee5 | 2015-09-18 01:18:04 | [diff] [blame] | 328 | } else { |
| 329 | if (pre_exec_delegate) { |
rsesek | dba8411 | 2015-09-18 19:22:07 | [diff] [blame] | 330 | BootstrapSandboxManager::GetInstance()->sandbox()->RevokeToken( |
rsesek | 408d2ee5 | 2015-09-18 01:18:04 | [diff] [blame] | 331 | pre_exec_delegate->sandbox_token()); |
| 332 | } |
| 333 | } |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 334 | |
| 335 | // After updating the broker, release the lock and let the child's |
| 336 | // messasge be processed on the broker's thread. |
| 337 | broker->GetLock().Release(); |
| 338 | #endif // defined(OS_MACOSX) |
| 339 | } |
| 340 | #endif // else defined(OS_POSIX) |
| 341 | #if !defined(OS_ANDROID) |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 342 | if (process.IsValid()) { |
wfh | 3adf87d | 2016-05-03 23:26:06 | [diff] [blame] | 343 | launch_result = LAUNCH_RESULT_SUCCESS; |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 344 | RecordHistogramsOnLauncherThread(base::TimeTicks::Now() - |
| 345 | begin_launch_time); |
| 346 | } |
| 347 | BrowserThread::PostTask(client_thread_id, FROM_HERE, |
wfh | 3adf87d | 2016-05-03 23:26:06 | [diff] [blame] | 348 | base::Bind(callback, zygote, base::Passed(&process), |
| 349 | launch_result)); |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 350 | #endif // !defined(OS_ANDROID) |
| 351 | } |
| 352 | |
kerrnel | afd49a83b | 2016-01-22 21:16:15 | [diff] [blame] | 353 | void TerminateOnLauncherThread(ZygoteHandle zygote, base::Process process) { |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 354 | DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 355 | #if defined(OS_ANDROID) |
| 356 | VLOG(1) << "ChromeProcess: Stopping process with handle " |
| 357 | << process.Handle(); |
| 358 | StopChildProcess(process.Handle()); |
| 359 | #else |
| 360 | // Client has gone away, so just kill the process. Using exit code 0 |
| 361 | // means that UMA won't treat this as a crash. |
rvargas | eedb763 | 2015-03-09 23:53:45 | [diff] [blame] | 362 | process.Terminate(RESULT_CODE_NORMAL_EXIT, false); |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 363 | // On POSIX, we must additionally reap the child. |
| 364 | #if defined(OS_POSIX) |
| 365 | #if !defined(OS_MACOSX) |
| 366 | if (zygote) { |
| 367 | // If the renderer was created via a zygote, we have to proxy the reaping |
| 368 | // through the zygote process. |
kerrnel | afd49a83b | 2016-01-22 21:16:15 | [diff] [blame] | 369 | zygote->EnsureProcessTerminated(process.Handle()); |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 370 | } else |
| 371 | #endif // !OS_MACOSX |
danakj | e3de838f | 2015-12-03 01:49:40 | [diff] [blame] | 372 | base::EnsureProcessTerminated(std::move(process)); |
rvargas | 7951bf6 | 2014-11-14 04:09:23 | [diff] [blame] | 373 | #endif // OS_POSIX |
| 374 | #endif // defined(OS_ANDROID) |
| 375 | } |
| 376 | |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 377 | void SetProcessBackgroundedOnLauncherThread(base::Process process, |
| 378 | bool background) { |
| 379 | DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); |
shrike | 7714401 | 2015-10-02 02:46:58 | [diff] [blame] | 380 | if (process.CanBackgroundProcesses()) { |
| 381 | process.SetProcessBackgrounded(background); |
| 382 | } |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 383 | #if defined(OS_ANDROID) |
| 384 | SetChildProcessInForeground(process.Handle(), !background); |
| 385 | #endif |
| 386 | } |
| 387 | |
ben | 03b7b9d | 2015-11-15 12:13:09 | [diff] [blame] | 388 | } // namespace |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 389 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 390 | ChildProcessLauncher::ChildProcessLauncher( |
[email protected] | 34f4868 | 2013-03-20 00:30:18 | [diff] [blame] | 391 | SandboxedProcessLauncherDelegate* delegate, |
[email protected] | 47927870 | 2014-08-11 20:32:09 | [diff] [blame] | 392 | base::CommandLine* cmd_line, |
[email protected] | 40da3e0c | 2012-10-24 22:03:38 | [diff] [blame] | 393 | int child_process_id, |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 394 | Client* client, |
amistry | 6ad1e81 | 2016-06-06 05:36:30 | [diff] [blame] | 395 | const std::string& mojo_child_token, |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 396 | const mojo::edk::ProcessErrorCallback& process_error_callback, |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 397 | bool terminate_on_shutdown) |
| 398 | : client_(client), |
| 399 | termination_status_(base::TERMINATION_STATUS_NORMAL_TERMINATION), |
| 400 | exit_code_(RESULT_CODE_NORMAL_EXIT), |
kerrnel | afd49a83b | 2016-01-22 21:16:15 | [diff] [blame] | 401 | zygote_(nullptr), |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 402 | starting_(true), |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 403 | process_error_callback_(process_error_callback), |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 404 | #if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \ |
| 405 | defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \ |
| 406 | defined(UNDEFINED_SANITIZER) |
| 407 | terminate_child_on_shutdown_(false), |
| 408 | #else |
| 409 | terminate_child_on_shutdown_(terminate_on_shutdown), |
| 410 | #endif |
amistry | 6ad1e81 | 2016-06-06 05:36:30 | [diff] [blame] | 411 | mojo_child_token_(mojo_child_token), |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 412 | weak_factory_(this) { |
| 413 | DCHECK(CalledOnValidThread()); |
| 414 | CHECK(BrowserThread::GetCurrentThreadIdentifier(&client_thread_id_)); |
| 415 | Launch(delegate, cmd_line, child_process_id); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | ChildProcessLauncher::~ChildProcessLauncher() { |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 419 | DCHECK(CalledOnValidThread()); |
| 420 | if (process_.IsValid() && terminate_child_on_shutdown_) { |
| 421 | // On Posix, EnsureProcessTerminated can lead to 2 seconds of sleep! So |
| 422 | // don't this on the UI/IO threads. |
| 423 | BrowserThread::PostTask(BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
| 424 | base::Bind(&TerminateOnLauncherThread, zygote_, |
| 425 | base::Passed(&process_))); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | void ChildProcessLauncher::Launch( |
| 430 | SandboxedProcessLauncherDelegate* delegate, |
| 431 | base::CommandLine* cmd_line, |
| 432 | int child_process_id) { |
| 433 | DCHECK(CalledOnValidThread()); |
| 434 | |
| 435 | #if defined(OS_ANDROID) |
| 436 | // Android only supports renderer, sandboxed utility and gpu. |
| 437 | std::string process_type = |
| 438 | cmd_line->GetSwitchValueASCII(switches::kProcessType); |
| 439 | CHECK(process_type == switches::kGpuProcess || |
| 440 | process_type == switches::kRendererProcess || |
hshi | 11e9711 | 2015-08-29 00:14:36 | [diff] [blame] | 441 | #if defined(ENABLE_PLUGINS) |
| 442 | process_type == switches::kPpapiPluginProcess || |
| 443 | #endif |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 444 | process_type == switches::kUtilityProcess) |
| 445 | << "Unsupported process type: " << process_type; |
| 446 | |
| 447 | // Non-sandboxed utility or renderer process are currently not supported. |
| 448 | DCHECK(process_type == switches::kGpuProcess || |
| 449 | !cmd_line->HasSwitch(switches::kNoSandbox)); |
| 450 | |
| 451 | // We need to close the client end of the IPC channel to reliably detect |
| 452 | // child termination. We will close this fd after we create the child |
| 453 | // process which is asynchronous on Android. |
| 454 | base::ScopedFD ipcfd(delegate->TakeIpcFd().release()); |
| 455 | #endif |
| 456 | NotifyCallback reply_callback(base::Bind(&ChildProcessLauncher::DidLaunch, |
| 457 | weak_factory_.GetWeakPtr(), |
| 458 | terminate_child_on_shutdown_)); |
amistry | b613891 | 2016-05-31 01:54:12 | [diff] [blame] | 459 | mojo::edk::ScopedPlatformHandle client_handle; |
| 460 | #if defined(OS_WIN) |
| 461 | if (delegate->ShouldLaunchElevated()) { |
| 462 | mojo::edk::NamedPlatformChannelPair named_pair; |
| 463 | mojo_host_platform_handle_ = named_pair.PassServerHandle(); |
| 464 | named_pair.PrepareToPassClientHandleToChildProcess(cmd_line); |
| 465 | } else |
| 466 | #endif |
| 467 | { |
| 468 | mojo::edk::PlatformChannelPair channel_pair; |
| 469 | mojo_host_platform_handle_ = channel_pair.PassServerHandle(); |
| 470 | client_handle = channel_pair.PassClientHandle(); |
| 471 | } |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 472 | BrowserThread::PostTask( |
| 473 | BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
| 474 | base::Bind(&LaunchOnLauncherThread, reply_callback, client_thread_id_, |
| 475 | child_process_id, delegate, |
| 476 | #if defined(OS_ANDROID) |
| 477 | base::Passed(&ipcfd), |
| 478 | #endif |
rockot | e8b8da4 | 2016-03-02 06:20:23 | [diff] [blame] | 479 | base::Passed(&client_handle), cmd_line)); |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | void ChildProcessLauncher::UpdateTerminationStatus(bool known_dead) { |
| 483 | DCHECK(CalledOnValidThread()); |
| 484 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 485 | if (zygote_) { |
kerrnel | afd49a83b | 2016-01-22 21:16:15 | [diff] [blame] | 486 | termination_status_ = zygote_->GetTerminationStatus( |
| 487 | process_.Handle(), known_dead, &exit_code_); |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 488 | } else if (known_dead) { |
| 489 | termination_status_ = |
| 490 | base::GetKnownDeadTerminationStatus(process_.Handle(), &exit_code_); |
| 491 | } else { |
| 492 | #elif defined(OS_MACOSX) |
| 493 | if (known_dead) { |
| 494 | termination_status_ = |
| 495 | base::GetKnownDeadTerminationStatus(process_.Handle(), &exit_code_); |
| 496 | } else { |
| 497 | #elif defined(OS_ANDROID) |
| 498 | if (IsChildProcessOomProtected(process_.Handle())) { |
| 499 | termination_status_ = base::TERMINATION_STATUS_OOM_PROTECTED; |
| 500 | } else { |
| 501 | #else |
| 502 | { |
| 503 | #endif |
| 504 | termination_status_ = |
| 505 | base::GetTerminationStatus(process_.Handle(), &exit_code_); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | void ChildProcessLauncher::SetProcessBackgrounded(bool background) { |
| 510 | DCHECK(CalledOnValidThread()); |
| 511 | base::Process to_pass = process_.Duplicate(); |
| 512 | BrowserThread::PostTask(BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
| 513 | base::Bind(&SetProcessBackgroundedOnLauncherThread, |
| 514 | base::Passed(&to_pass), background)); |
| 515 | } |
| 516 | |
| 517 | void ChildProcessLauncher::DidLaunch( |
| 518 | base::WeakPtr<ChildProcessLauncher> instance, |
| 519 | bool terminate_on_shutdown, |
kerrnel | afd49a83b | 2016-01-22 21:16:15 | [diff] [blame] | 520 | ZygoteHandle zygote, |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 521 | #if defined(OS_ANDROID) |
| 522 | base::ScopedFD ipcfd, |
rockot | e8b8da4 | 2016-03-02 06:20:23 | [diff] [blame] | 523 | base::ScopedFD mojo_fd, |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 524 | #endif |
wfh | 3adf87d | 2016-05-03 23:26:06 | [diff] [blame] | 525 | base::Process process, |
| 526 | int error_code) { |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 527 | if (!process.IsValid()) |
| 528 | LOG(ERROR) << "Failed to launch child process"; |
| 529 | |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 530 | if (instance.get()) { |
| 531 | instance->Notify(zygote, |
| 532 | #if defined(OS_ANDROID) |
danakj | e3de838f | 2015-12-03 01:49:40 | [diff] [blame] | 533 | std::move(ipcfd), |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 534 | #endif |
wfh | 3adf87d | 2016-05-03 23:26:06 | [diff] [blame] | 535 | std::move(process), |
| 536 | error_code); |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 537 | } else { |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 538 | if (process.IsValid() && terminate_on_shutdown) { |
| 539 | // On Posix, EnsureProcessTerminated can lead to 2 seconds of sleep! So |
| 540 | // don't this on the UI/IO threads. |
| 541 | BrowserThread::PostTask(BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
| 542 | base::Bind(&TerminateOnLauncherThread, zygote, |
| 543 | base::Passed(&process))); |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | |
kerrnel | afd49a83b | 2016-01-22 21:16:15 | [diff] [blame] | 548 | void ChildProcessLauncher::Notify(ZygoteHandle zygote, |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 549 | #if defined(OS_ANDROID) |
kerrnel | afd49a83b | 2016-01-22 21:16:15 | [diff] [blame] | 550 | base::ScopedFD ipcfd, |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 551 | #endif |
wfh | 3adf87d | 2016-05-03 23:26:06 | [diff] [blame] | 552 | base::Process process, |
| 553 | int error_code) { |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 554 | DCHECK(CalledOnValidThread()); |
| 555 | starting_ = false; |
danakj | e3de838f | 2015-12-03 01:49:40 | [diff] [blame] | 556 | process_ = std::move(process); |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 557 | |
rockot | e8b8da4 | 2016-03-02 06:20:23 | [diff] [blame] | 558 | if (process_.IsValid()) { |
| 559 | // Set up Mojo IPC to the new process. |
| 560 | mojo::edk::ChildProcessLaunched(process_.Handle(), |
amistry | 6ad1e81 | 2016-06-06 05:36:30 | [diff] [blame] | 561 | std::move(mojo_host_platform_handle_), |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 562 | mojo_child_token_, |
| 563 | process_error_callback_); |
rockot | e8b8da4 | 2016-03-02 06:20:23 | [diff] [blame] | 564 | } |
| 565 | |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 566 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 567 | zygote_ = zygote; |
| 568 | #endif |
| 569 | if (process_.IsValid()) { |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 570 | client_->OnProcessLaunched(); |
| 571 | } else { |
amistry | 6ad1e81 | 2016-06-06 05:36:30 | [diff] [blame] | 572 | mojo::edk::ChildProcessLaunchFailed(mojo_child_token_); |
wfh | 0d9532a | 2015-09-02 23:18:58 | [diff] [blame] | 573 | termination_status_ = base::TERMINATION_STATUS_LAUNCH_FAILED; |
wfh | 3adf87d | 2016-05-03 23:26:06 | [diff] [blame] | 574 | client_->OnProcessLaunchFailed(error_code); |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 575 | } |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | bool ChildProcessLauncher::IsStarting() { |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 579 | // TODO(crbug.com/469248): This fails in some tests. |
| 580 | // DCHECK(CalledOnValidThread()); |
| 581 | return starting_; |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 582 | } |
| 583 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 584 | const base::Process& ChildProcessLauncher::GetProcess() const { |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 585 | // TODO(crbug.com/469248): This fails in some tests. |
| 586 | // DCHECK(CalledOnValidThread()); |
| 587 | return process_; |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 588 | } |
| 589 | |
[email protected] | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 590 | base::TerminationStatus ChildProcessLauncher::GetChildTerminationStatus( |
[email protected] | c7691de | 2012-12-06 08:31:51 | [diff] [blame] | 591 | bool known_dead, |
[email protected] | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 592 | int* exit_code) { |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 593 | DCHECK(CalledOnValidThread()); |
| 594 | if (!process_.IsValid()) { |
[email protected] | f3c1d3c | 2011-07-25 18:50:48 | [diff] [blame] | 595 | // Process is already gone, so return the cached termination status. |
| 596 | if (exit_code) |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 597 | *exit_code = exit_code_; |
| 598 | return termination_status_; |
[email protected] | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 599 | } |
| 600 | |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 601 | UpdateTerminationStatus(known_dead); |
[email protected] | f3c1d3c | 2011-07-25 18:50:48 | [diff] [blame] | 602 | if (exit_code) |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 603 | *exit_code = exit_code_; |
[email protected] | f3c1d3c | 2011-07-25 18:50:48 | [diff] [blame] | 604 | |
[email protected] | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 605 | // POSIX: If the process crashed, then the kernel closed the socket |
| 606 | // for it and so the child has already died by the time we get |
| 607 | // here. Since GetTerminationStatus called waitpid with WNOHANG, |
| 608 | // it'll reap the process. However, if GetTerminationStatus didn't |
| 609 | // reap the child (because it was still running), we'll need to |
[email protected] | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 610 | // Terminate via ProcessWatcher. So we can't close the handle here. |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 611 | if (termination_status_ != base::TERMINATION_STATUS_STILL_RUNNING) |
| 612 | process_.Close(); |
[email protected] | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 613 | |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 614 | return termination_status_; |
[email protected] | 358cb8e | 2011-05-25 02:12:45 | [diff] [blame] | 615 | } |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 616 | |
lfg | 06aac85 | 2015-03-23 22:36:10 | [diff] [blame] | 617 | ChildProcessLauncher::Client* ChildProcessLauncher::ReplaceClientForTest( |
| 618 | Client* client) { |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 619 | Client* ret = client_; |
| 620 | client_ = client; |
| 621 | return ret; |
lfg | 06aac85 | 2015-03-23 22:36:10 | [diff] [blame] | 622 | } |
| 623 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 624 | } // namespace content |