[email protected] | ffb6973 | 2012-02-08 17:33:23 | [diff] [blame] | 1 | // Copyright (c) 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 | |
[email protected] | eb1bd83 | 2010-05-18 20:39:58 | [diff] [blame] | 7 | #include <utility> // For std::pair. |
| 8 | |
[email protected] | 037edb5 | 2011-11-15 21:14:06 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 10 | #include "base/command_line.h" |
[email protected] | 2ce26c43 | 2011-09-19 17:08:12 | [diff] [blame] | 11 | #include "base/file_util.h" |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 13 | #include "base/memory/scoped_ptr.h" |
[email protected] | 323fdc31 | 2013-02-25 18:44:26 | [diff] [blame] | 14 | #include "base/metrics/histogram.h" |
[email protected] | eaac7159 | 2011-11-23 18:32:00 | [diff] [blame] | 15 | #include "base/process_util.h" |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 16 | #include "base/synchronization/lock.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 17 | #include "base/threading/thread.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 18 | #include "content/public/browser/browser_thread.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 19 | #include "content/public/browser/content_browser_client.h" |
[email protected] | b3aabd34 | 2012-06-04 19:33:56 | [diff] [blame] | 20 | #include "content/public/common/content_descriptors.h" |
[email protected] | c08950d2 | 2011-10-13 22:20:29 | [diff] [blame] | 21 | #include "content/public/common/content_switches.h" |
[email protected] | b39ef1cb | 2011-10-25 04:46:55 | [diff] [blame] | 22 | #include "content/public/common/result_codes.h" |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 23 | |
| 24 | #if defined(OS_WIN) |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 25 | #include "base/files/file_path.h" |
[email protected] | cd5fa1a | 2011-05-28 18:21:47 | [diff] [blame] | 26 | #include "content/common/sandbox_policy.h" |
[email protected] | e25b04c9 | 2012-10-23 20:05:06 | [diff] [blame] | 27 | #include "content/public/common/sandbox_init.h" |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 28 | #elif defined(OS_MACOSX) |
[email protected] | ed0fbe6 | 2011-06-23 18:32:11 | [diff] [blame] | 29 | #include "content/browser/mach_broker_mac.h" |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 30 | #elif defined(OS_ANDROID) |
| 31 | #include "base/android/jni_android.h" |
[email protected] | 5e44a14 | 2013-03-19 09:48:39 | [diff] [blame^] | 32 | #include "content/browser/android/child_process_launcher_android.h" |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 33 | #elif defined(OS_POSIX) |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 34 | #include "base/memory/singleton.h" |
[email protected] | a01efd2 | 2011-03-01 00:38:32 | [diff] [blame] | 35 | #include "content/browser/renderer_host/render_sandbox_host_linux.h" |
[email protected] | 13d6b3c | 2012-07-24 01:31:31 | [diff] [blame] | 36 | #include "content/browser/zygote_host/zygote_host_impl_linux.h" |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 37 | #endif |
| 38 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 39 | #if defined(OS_POSIX) |
[email protected] | 613eef6 | 2012-11-09 23:46:54 | [diff] [blame] | 40 | #include "base/posix/global_descriptors.h" |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 41 | #endif |
| 42 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 43 | namespace content { |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 44 | |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 45 | // Having the functionality of ChildProcessLauncher be in an internal |
| 46 | // ref counted object allows us to automatically terminate the process when the |
| 47 | // parent class destructs, while still holding on to state that we need. |
| 48 | class ChildProcessLauncher::Context |
| 49 | : public base::RefCountedThreadSafe<ChildProcessLauncher::Context> { |
| 50 | public: |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 51 | Context() |
[email protected] | 18cbea3 | 2010-10-13 19:56:00 | [diff] [blame] | 52 | : client_(NULL), |
| 53 | client_thread_id_(BrowserThread::UI), |
[email protected] | f3c1d3c | 2011-07-25 18:50:48 | [diff] [blame] | 54 | termination_status_(base::TERMINATION_STATUS_NORMAL_TERMINATION), |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 55 | exit_code_(RESULT_CODE_NORMAL_EXIT), |
[email protected] | 08c426a | 2012-04-10 20:37:33 | [diff] [blame] | 56 | starting_(true) |
[email protected] | ffb6973 | 2012-02-08 17:33:23 | [diff] [blame] | 57 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 58 | , zygote_(false) |
| 59 | #endif |
| 60 | { |
[email protected] | 08c426a | 2012-04-10 20:37:33 | [diff] [blame] | 61 | #if defined(OS_POSIX) |
| 62 | terminate_child_on_shutdown_ = !CommandLine::ForCurrentProcess()-> |
[email protected] | 719a205 | 2012-07-30 21:00:43 | [diff] [blame] | 63 | HasSwitch(switches::kChildCleanExit); |
[email protected] | 08c426a | 2012-04-10 20:37:33 | [diff] [blame] | 64 | #else |
| 65 | terminate_child_on_shutdown_ = true; |
| 66 | #endif |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 67 | } |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 68 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 69 | void Launch( |
| 70 | #if defined(OS_WIN) |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 71 | const base::FilePath& exposed_dir, |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 72 | #elif defined(OS_ANDROID) |
| 73 | int ipcfd, |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 74 | #elif defined(OS_POSIX) |
[email protected] | 7c4ea14 | 2010-01-26 05:15:42 | [diff] [blame] | 75 | bool use_zygote, |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 76 | const base::EnvironmentVector& environ, |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 77 | int ipcfd, |
| 78 | #endif |
| 79 | CommandLine* cmd_line, |
[email protected] | 40da3e0c | 2012-10-24 22:03:38 | [diff] [blame] | 80 | int child_process_id, |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 81 | Client* client) { |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 82 | client_ = client; |
| 83 | |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 84 | CHECK(BrowserThread::GetCurrentThreadIdentifier(&client_thread_id_)); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 85 | |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 86 | #if defined(OS_ANDROID) |
| 87 | // We need to close the client end of the IPC channel to reliably detect |
| 88 | // child termination. We will close this fd after we create the child |
| 89 | // process which is asynchronous on Android. |
| 90 | ipcfd_ = ipcfd; |
| 91 | #endif |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 92 | BrowserThread::PostTask( |
| 93 | BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
[email protected] | 037edb5 | 2011-11-15 21:14:06 | [diff] [blame] | 94 | base::Bind( |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 95 | &Context::LaunchInternal, |
[email protected] | e202478 | 2011-09-07 20:20:51 | [diff] [blame] | 96 | make_scoped_refptr(this), |
| 97 | client_thread_id_, |
[email protected] | 40da3e0c | 2012-10-24 22:03:38 | [diff] [blame] | 98 | child_process_id, |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 99 | #if defined(OS_WIN) |
| 100 | exposed_dir, |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 101 | #elif defined(OS_ANDROID) |
| 102 | ipcfd, |
[email protected] | eb1bd83 | 2010-05-18 20:39:58 | [diff] [blame] | 103 | #elif defined(OS_POSIX) |
[email protected] | 7c4ea14 | 2010-01-26 05:15:42 | [diff] [blame] | 104 | use_zygote, |
[email protected] | 3d2217d | 2009-11-23 21:26:47 | [diff] [blame] | 105 | environ, |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 106 | ipcfd, |
| 107 | #endif |
| 108 | cmd_line)); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 109 | } |
| 110 | |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 111 | #if defined(OS_ANDROID) |
[email protected] | cee179e | 2013-03-19 01:43:36 | [diff] [blame] | 112 | static void OnChildProcessStarted( |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 113 | // |this_object| is NOT thread safe. Only use it to post a task back. |
| 114 | scoped_refptr<Context> this_object, |
| 115 | BrowserThread::ID client_thread_id, |
[email protected] | 323fdc31 | 2013-02-25 18:44:26 | [diff] [blame] | 116 | const base::TimeTicks begin_launch_time, |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 117 | base::ProcessHandle handle) { |
[email protected] | 323fdc31 | 2013-02-25 18:44:26 | [diff] [blame] | 118 | RecordHistograms(begin_launch_time); |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 119 | if (BrowserThread::CurrentlyOn(client_thread_id)) { |
| 120 | // This is always invoked on the UI thread which is commonly the |
| 121 | // |client_thread_id| so we can shortcut one PostTask. |
| 122 | this_object->Notify(handle); |
| 123 | } else { |
| 124 | BrowserThread::PostTask( |
| 125 | client_thread_id, FROM_HERE, |
| 126 | base::Bind( |
| 127 | &ChildProcessLauncher::Context::Notify, |
| 128 | this_object, |
| 129 | handle)); |
| 130 | } |
| 131 | } |
| 132 | #endif |
| 133 | |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 134 | void ResetClient() { |
| 135 | // No need for locking as this function gets called on the same thread that |
| 136 | // client_ would be used. |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 137 | CHECK(BrowserThread::CurrentlyOn(client_thread_id_)); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 138 | client_ = NULL; |
| 139 | } |
| 140 | |
[email protected] | 358cb8e | 2011-05-25 02:12:45 | [diff] [blame] | 141 | void set_terminate_child_on_shutdown(bool terminate_on_shutdown) { |
| 142 | terminate_child_on_shutdown_ = terminate_on_shutdown; |
| 143 | } |
| 144 | |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 145 | private: |
| 146 | friend class base::RefCountedThreadSafe<ChildProcessLauncher::Context>; |
| 147 | friend class ChildProcessLauncher; |
| 148 | |
| 149 | ~Context() { |
| 150 | Terminate(); |
| 151 | } |
| 152 | |
[email protected] | 323fdc31 | 2013-02-25 18:44:26 | [diff] [blame] | 153 | static void RecordHistograms(const base::TimeTicks begin_launch_time) { |
| 154 | base::TimeDelta launch_time = base::TimeTicks::Now() - begin_launch_time; |
| 155 | if (BrowserThread::CurrentlyOn(BrowserThread::PROCESS_LAUNCHER)) { |
| 156 | RecordLaunchHistograms(launch_time); |
| 157 | } else { |
| 158 | BrowserThread::PostTask( |
| 159 | BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
| 160 | base::Bind(&ChildProcessLauncher::Context::RecordLaunchHistograms, |
| 161 | launch_time)); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static void RecordLaunchHistograms(const base::TimeDelta launch_time) { |
| 166 | // Log the launch time, separating out the first one (which will likely be |
| 167 | // slower due to the rest of the browser initializing at the same time). |
| 168 | static bool done_first_launch = false; |
| 169 | if (done_first_launch) { |
| 170 | UMA_HISTOGRAM_TIMES("MPArch.ChildProcessLaunchSubsequent", launch_time); |
| 171 | } else { |
| 172 | UMA_HISTOGRAM_TIMES("MPArch.ChildProcessLaunchFirst", launch_time); |
| 173 | done_first_launch = true; |
| 174 | } |
| 175 | } |
| 176 | |
[email protected] | e202478 | 2011-09-07 20:20:51 | [diff] [blame] | 177 | static void LaunchInternal( |
| 178 | // |this_object| is NOT thread safe. Only use it to post a task back. |
| 179 | scoped_refptr<Context> this_object, |
| 180 | BrowserThread::ID client_thread_id, |
[email protected] | 40da3e0c | 2012-10-24 22:03:38 | [diff] [blame] | 181 | int child_process_id, |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 182 | #if defined(OS_WIN) |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 183 | const base::FilePath& exposed_dir, |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 184 | #elif defined(OS_ANDROID) |
| 185 | int ipcfd, |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 186 | #elif defined(OS_POSIX) |
[email protected] | 7c4ea14 | 2010-01-26 05:15:42 | [diff] [blame] | 187 | bool use_zygote, |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 188 | const base::EnvironmentVector& env, |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 189 | int ipcfd, |
| 190 | #endif |
| 191 | CommandLine* cmd_line) { |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 192 | scoped_ptr<CommandLine> cmd_line_deleter(cmd_line); |
[email protected] | 323fdc31 | 2013-02-25 18:44:26 | [diff] [blame] | 193 | base::TimeTicks begin_launch_time = base::TimeTicks::Now(); |
[email protected] | 2ce26c43 | 2011-09-19 17:08:12 | [diff] [blame] | 194 | |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 195 | #if defined(OS_WIN) |
[email protected] | dcc72db | 2013-01-02 00:44:18 | [diff] [blame] | 196 | base::ProcessHandle handle = StartProcessWithAccess(cmd_line, exposed_dir); |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 197 | #elif defined(OS_ANDROID) |
[email protected] | 8b02bf17 | 2013-01-12 17:05:26 | [diff] [blame] | 198 | // Android WebView runs in single process, ensure that we never get here |
| 199 | // when running in single process mode. |
| 200 | CHECK(!cmd_line->HasSwitch(switches::kSingleProcess)); |
| 201 | |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 202 | std::string process_type = |
| 203 | cmd_line->GetSwitchValueASCII(switches::kProcessType); |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 204 | std::vector<FileDescriptorInfo> files_to_register; |
[email protected] | c7abd42 | 2012-09-25 00:20:08 | [diff] [blame] | 205 | files_to_register.push_back( |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 206 | FileDescriptorInfo(kPrimaryIPCChannel, |
[email protected] | c7abd42 | 2012-09-25 00:20:08 | [diff] [blame] | 207 | base::FileDescriptor(ipcfd, false))); |
| 208 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 209 | GetContentClient()->browser()-> |
[email protected] | 40da3e0c | 2012-10-24 22:03:38 | [diff] [blame] | 210 | GetAdditionalMappedFilesForChildProcess(*cmd_line, child_process_id, |
| 211 | &files_to_register); |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 212 | |
[email protected] | cee179e | 2013-03-19 01:43:36 | [diff] [blame] | 213 | StartChildProcess(cmd_line->argv(), files_to_register, |
| 214 | base::Bind(&ChildProcessLauncher::Context::OnChildProcessStarted, |
[email protected] | 323fdc31 | 2013-02-25 18:44:26 | [diff] [blame] | 215 | this_object, client_thread_id, begin_launch_time)); |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 216 | |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 217 | #elif defined(OS_POSIX) |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 218 | base::ProcessHandle handle = base::kNullProcessHandle; |
[email protected] | c7abd42 | 2012-09-25 00:20:08 | [diff] [blame] | 219 | // We need to close the client end of the IPC channel to reliably detect |
| 220 | // child termination. |
[email protected] | 2ce26c43 | 2011-09-19 17:08:12 | [diff] [blame] | 221 | file_util::ScopedFD ipcfd_closer(&ipcfd); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 222 | |
[email protected] | 54457f3 | 2011-04-15 22:05:29 | [diff] [blame] | 223 | std::string process_type = |
| 224 | cmd_line->GetSwitchValueASCII(switches::kProcessType); |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 225 | std::vector<FileDescriptorInfo> files_to_register; |
[email protected] | c7abd42 | 2012-09-25 00:20:08 | [diff] [blame] | 226 | files_to_register.push_back( |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 227 | FileDescriptorInfo(kPrimaryIPCChannel, |
[email protected] | c7abd42 | 2012-09-25 00:20:08 | [diff] [blame] | 228 | base::FileDescriptor(ipcfd, false))); |
| 229 | |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 230 | #if !defined(OS_MACOSX) |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 231 | GetContentClient()->browser()-> |
[email protected] | 40da3e0c | 2012-10-24 22:03:38 | [diff] [blame] | 232 | GetAdditionalMappedFilesForChildProcess(*cmd_line, child_process_id, |
| 233 | &files_to_register); |
[email protected] | 7c4ea14 | 2010-01-26 05:15:42 | [diff] [blame] | 234 | if (use_zygote) { |
[email protected] | c2c68b1f | 2012-02-25 00:29:15 | [diff] [blame] | 235 | handle = ZygoteHostImpl::GetInstance()->ForkRequest(cmd_line->argv(), |
[email protected] | a1733df | 2012-06-22 11:24:18 | [diff] [blame] | 236 | files_to_register, |
[email protected] | c2c68b1f | 2012-02-25 00:29:15 | [diff] [blame] | 237 | process_type); |
[email protected] | 7c4ea14 | 2010-01-26 05:15:42 | [diff] [blame] | 238 | } else |
| 239 | // Fall through to the normal posix case below when we're not zygoting. |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 240 | #endif // !defined(OS_MACOSX) |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 241 | { |
[email protected] | a1733df | 2012-06-22 11:24:18 | [diff] [blame] | 242 | // Convert FD mapping to FileHandleMappingVector |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 243 | base::FileHandleMappingVector fds_to_map; |
[email protected] | 40da3e0c | 2012-10-24 22:03:38 | [diff] [blame] | 244 | for (size_t i = 0; i < files_to_register.size(); ++i) { |
[email protected] | 54457f3 | 2011-04-15 22:05:29 | [diff] [blame] | 245 | fds_to_map.push_back(std::make_pair( |
[email protected] | 40da3e0c | 2012-10-24 22:03:38 | [diff] [blame] | 246 | files_to_register[i].fd.fd, |
| 247 | files_to_register[i].id + |
| 248 | base::GlobalDescriptors::kBaseDescriptor)); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 249 | } |
[email protected] | a1733df | 2012-06-22 11:24:18 | [diff] [blame] | 250 | |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 251 | #if !defined(OS_MACOSX) |
[email protected] | b80f6843 | 2011-05-02 17:22:30 | [diff] [blame] | 252 | if (process_type == switches::kRendererProcess) { |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 253 | const int sandbox_fd = |
[email protected] | d3c6c0d7 | 2010-12-09 08:15:04 | [diff] [blame] | 254 | RenderSandboxHostLinux::GetInstance()->GetRendererSocket(); |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 255 | fds_to_map.push_back(std::make_pair( |
| 256 | sandbox_fd, |
| 257 | kSandboxIPCChannel + base::GlobalDescriptors::kBaseDescriptor)); |
| 258 | } |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 259 | #endif // defined(OS_MACOSX) |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 260 | |
[email protected] | 84b2d1d | 2011-09-01 21:44:24 | [diff] [blame] | 261 | // Actually launch the app. |
| 262 | base::LaunchOptions options; |
| 263 | options.environ = &env; |
| 264 | options.fds_to_remap = &fds_to_map; |
| 265 | |
[email protected] | c002879 | 2010-01-12 00:39:15 | [diff] [blame] | 266 | #if defined(OS_MACOSX) |
[email protected] | 84b2d1d | 2011-09-01 21:44:24 | [diff] [blame] | 267 | // Use synchronization to make sure that the MachBroker is ready to |
| 268 | // receive a check-in from the new process before the new process |
| 269 | // actually tries to check in. |
| 270 | base::LaunchSynchronizationHandle synchronization_handle; |
| 271 | options.synchronize = &synchronization_handle; |
| 272 | #endif // defined(OS_MACOSX) |
| 273 | |
| 274 | bool launched = base::LaunchProcess(*cmd_line, options, &handle); |
| 275 | |
| 276 | #if defined(OS_MACOSX) |
| 277 | if (launched) { |
[email protected] | dc8caba | 2010-12-13 16:52:35 | [diff] [blame] | 278 | MachBroker* broker = MachBroker::GetInstance(); |
[email protected] | 84b2d1d | 2011-09-01 21:44:24 | [diff] [blame] | 279 | { |
| 280 | base::AutoLock lock(broker->GetLock()); |
[email protected] | b88a749 | 2010-09-17 12:28:32 | [diff] [blame] | 281 | |
[email protected] | 84b2d1d | 2011-09-01 21:44:24 | [diff] [blame] | 282 | // Make sure the MachBroker is running, and inform it to expect a |
| 283 | // check-in from the new process. |
| 284 | broker->EnsureRunning(); |
[email protected] | b88a749 | 2010-09-17 12:28:32 | [diff] [blame] | 285 | broker->AddPlaceholderForPid(handle); |
[email protected] | 84b2d1d | 2011-09-01 21:44:24 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | // Now that the MachBroker is ready, the child may continue. |
| 289 | base::LaunchSynchronize(synchronization_handle); |
| 290 | } |
| 291 | #endif // defined(OS_MACOSX) |
| 292 | |
[email protected] | c002879 | 2010-01-12 00:39:15 | [diff] [blame] | 293 | if (!launched) |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 294 | handle = base::kNullProcessHandle; |
| 295 | } |
[email protected] | c002879 | 2010-01-12 00:39:15 | [diff] [blame] | 296 | #endif // else defined(OS_POSIX) |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 297 | #if !defined(OS_ANDROID) |
[email protected] | 323fdc31 | 2013-02-25 18:44:26 | [diff] [blame] | 298 | if (handle) |
| 299 | RecordHistograms(begin_launch_time); |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 300 | BrowserThread::PostTask( |
| 301 | client_thread_id, FROM_HERE, |
| 302 | base::Bind( |
| 303 | &Context::Notify, |
| 304 | this_object.get(), |
| 305 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 306 | use_zygote, |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 307 | #endif |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 308 | handle)); |
| 309 | #endif // !defined(OS_ANDROID) |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 310 | } |
| 311 | |
[email protected] | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 312 | void Notify( |
[email protected] | ffb6973 | 2012-02-08 17:33:23 | [diff] [blame] | 313 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 314 | bool zygote, |
| 315 | #endif |
| 316 | base::ProcessHandle handle) { |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 317 | #if defined(OS_ANDROID) |
| 318 | // Finally close the ipcfd |
| 319 | file_util::ScopedFD ipcfd_closer(&ipcfd_); |
| 320 | #endif |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 321 | starting_ = false; |
| 322 | process_.set_handle(handle); |
[email protected] | e51ddf25 | 2011-10-12 17:33:21 | [diff] [blame] | 323 | if (!handle) |
| 324 | LOG(ERROR) << "Failed to launch child process"; |
| 325 | |
[email protected] | ffb6973 | 2012-02-08 17:33:23 | [diff] [blame] | 326 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 327 | zygote_ = zygote; |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 328 | #endif |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 329 | if (client_) { |
| 330 | client_->OnProcessLaunched(); |
| 331 | } else { |
| 332 | Terminate(); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | void Terminate() { |
| 337 | if (!process_.handle()) |
| 338 | return; |
| 339 | |
[email protected] | 358cb8e | 2011-05-25 02:12:45 | [diff] [blame] | 340 | if (!terminate_child_on_shutdown_) |
| 341 | return; |
| 342 | |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 343 | // On Posix, EnsureProcessTerminated can lead to 2 seconds of sleep! So |
| 344 | // don't this on the UI/IO threads. |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 345 | BrowserThread::PostTask( |
| 346 | BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
[email protected] | 037edb5 | 2011-11-15 21:14:06 | [diff] [blame] | 347 | base::Bind( |
[email protected] | e202478 | 2011-09-07 20:20:51 | [diff] [blame] | 348 | &Context::TerminateInternal, |
[email protected] | ffb6973 | 2012-02-08 17:33:23 | [diff] [blame] | 349 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 350 | zygote_, |
| 351 | #endif |
| 352 | process_.handle())); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 353 | process_.set_handle(base::kNullProcessHandle); |
| 354 | } |
| 355 | |
[email protected] | e202478 | 2011-09-07 20:20:51 | [diff] [blame] | 356 | static void SetProcessBackgrounded(base::ProcessHandle handle, |
| 357 | bool background) { |
| 358 | base::Process process(handle); |
| 359 | process.SetProcessBackgrounded(background); |
[email protected] | 3e55e21 | 2011-03-24 19:45:02 | [diff] [blame] | 360 | } |
| 361 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 362 | static void TerminateInternal( |
[email protected] | ffb6973 | 2012-02-08 17:33:23 | [diff] [blame] | 363 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 364 | bool zygote, |
| 365 | #endif |
| 366 | base::ProcessHandle handle) { |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 367 | #if defined(OS_ANDROID) |
| 368 | LOG(INFO) << "ChromeProcess: Stopping process with handle " << handle; |
[email protected] | cee179e | 2013-03-19 01:43:36 | [diff] [blame] | 369 | StopChildProcess(handle); |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 370 | #else |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 371 | base::Process process(handle); |
| 372 | // Client has gone away, so just kill the process. Using exit code 0 |
| 373 | // means that UMA won't treat this as a crash. |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 374 | process.Terminate(RESULT_CODE_NORMAL_EXIT); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 375 | // On POSIX, we must additionally reap the child. |
| 376 | #if defined(OS_POSIX) |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 377 | #if !defined(OS_MACOSX) |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 378 | if (zygote) { |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 379 | // If the renderer was created via a zygote, we have to proxy the reaping |
| 380 | // through the zygote process. |
[email protected] | c2c68b1f | 2012-02-25 00:29:15 | [diff] [blame] | 381 | ZygoteHostImpl::GetInstance()->EnsureProcessTerminated(handle); |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 382 | } else |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 383 | #endif // !OS_MACOSX |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 384 | { |
[email protected] | eaac7159 | 2011-11-23 18:32:00 | [diff] [blame] | 385 | base::EnsureProcessTerminated(handle); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 386 | } |
[email protected] | e36e86f | 2009-12-15 11:55:08 | [diff] [blame] | 387 | #endif // OS_POSIX |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 388 | process.Close(); |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 389 | #endif // defined(OS_ANDROID) |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | Client* client_; |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 393 | BrowserThread::ID client_thread_id_; |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 394 | base::Process process_; |
[email protected] | f3c1d3c | 2011-07-25 18:50:48 | [diff] [blame] | 395 | base::TerminationStatus termination_status_; |
| 396 | int exit_code_; |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 397 | bool starting_; |
[email protected] | 358cb8e | 2011-05-25 02:12:45 | [diff] [blame] | 398 | // Controls whether the child process should be terminated on browser |
| 399 | // shutdown. Default behavior is to terminate the child. |
| 400 | bool terminate_child_on_shutdown_; |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 401 | #if defined(OS_ANDROID) |
| 402 | // The fd to close after creating the process. |
| 403 | int ipcfd_; |
| 404 | #elif defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 405 | bool zygote_; |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 406 | #endif |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 407 | }; |
| 408 | |
| 409 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 410 | ChildProcessLauncher::ChildProcessLauncher( |
| 411 | #if defined(OS_WIN) |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 412 | const base::FilePath& exposed_dir, |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 413 | #elif defined(OS_POSIX) |
[email protected] | 7c4ea14 | 2010-01-26 05:15:42 | [diff] [blame] | 414 | bool use_zygote, |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 415 | const base::EnvironmentVector& environ, |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 416 | int ipcfd, |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 417 | #endif |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 418 | CommandLine* cmd_line, |
[email protected] | 40da3e0c | 2012-10-24 22:03:38 | [diff] [blame] | 419 | int child_process_id, |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 420 | Client* client) { |
| 421 | context_ = new Context(); |
| 422 | context_->Launch( |
| 423 | #if defined(OS_WIN) |
| 424 | exposed_dir, |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 425 | #elif defined(OS_ANDROID) |
| 426 | ipcfd, |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 427 | #elif defined(OS_POSIX) |
[email protected] | 7c4ea14 | 2010-01-26 05:15:42 | [diff] [blame] | 428 | use_zygote, |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 429 | environ, |
| 430 | ipcfd, |
| 431 | #endif |
| 432 | cmd_line, |
[email protected] | 40da3e0c | 2012-10-24 22:03:38 | [diff] [blame] | 433 | child_process_id, |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 434 | client); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | ChildProcessLauncher::~ChildProcessLauncher() { |
| 438 | context_->ResetClient(); |
| 439 | } |
| 440 | |
| 441 | bool ChildProcessLauncher::IsStarting() { |
| 442 | return context_->starting_; |
| 443 | } |
| 444 | |
| 445 | base::ProcessHandle ChildProcessLauncher::GetHandle() { |
| 446 | DCHECK(!context_->starting_); |
| 447 | return context_->process_.handle(); |
| 448 | } |
| 449 | |
[email protected] | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 450 | base::TerminationStatus ChildProcessLauncher::GetChildTerminationStatus( |
[email protected] | c7691de | 2012-12-06 08:31:51 | [diff] [blame] | 451 | bool known_dead, |
[email protected] | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 452 | int* exit_code) { |
[email protected] | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 453 | base::ProcessHandle handle = context_->process_.handle(); |
[email protected] | f3c1d3c | 2011-07-25 18:50:48 | [diff] [blame] | 454 | if (handle == base::kNullProcessHandle) { |
| 455 | // Process is already gone, so return the cached termination status. |
| 456 | if (exit_code) |
| 457 | *exit_code = context_->exit_code_; |
| 458 | return context_->termination_status_; |
| 459 | } |
[email protected] | ffb6973 | 2012-02-08 17:33:23 | [diff] [blame] | 460 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 461 | if (context_->zygote_) { |
[email protected] | c2c68b1f | 2012-02-25 00:29:15 | [diff] [blame] | 462 | context_->termination_status_ = ZygoteHostImpl::GetInstance()-> |
[email protected] | c7691de | 2012-12-06 08:31:51 | [diff] [blame] | 463 | GetTerminationStatus(handle, known_dead, &context_->exit_code_); |
[email protected] | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 464 | } else |
| 465 | #endif |
| 466 | { |
[email protected] | f3c1d3c | 2011-07-25 18:50:48 | [diff] [blame] | 467 | context_->termination_status_ = |
| 468 | base::GetTerminationStatus(handle, &context_->exit_code_); |
[email protected] | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 469 | } |
| 470 | |
[email protected] | f3c1d3c | 2011-07-25 18:50:48 | [diff] [blame] | 471 | if (exit_code) |
| 472 | *exit_code = context_->exit_code_; |
| 473 | |
[email protected] | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 474 | // POSIX: If the process crashed, then the kernel closed the socket |
| 475 | // for it and so the child has already died by the time we get |
| 476 | // here. Since GetTerminationStatus called waitpid with WNOHANG, |
| 477 | // it'll reap the process. However, if GetTerminationStatus didn't |
| 478 | // reap the child (because it was still running), we'll need to |
[email protected] | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 479 | // Terminate via ProcessWatcher. So we can't close the handle here. |
[email protected] | f3c1d3c | 2011-07-25 18:50:48 | [diff] [blame] | 480 | if (context_->termination_status_ != base::TERMINATION_STATUS_STILL_RUNNING) |
[email protected] | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 481 | context_->process_.Close(); |
| 482 | |
[email protected] | f3c1d3c | 2011-07-25 18:50:48 | [diff] [blame] | 483 | return context_->termination_status_; |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | void ChildProcessLauncher::SetProcessBackgrounded(bool background) { |
[email protected] | 3e55e21 | 2011-03-24 19:45:02 | [diff] [blame] | 487 | BrowserThread::PostTask( |
| 488 | BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
[email protected] | 037edb5 | 2011-11-15 21:14:06 | [diff] [blame] | 489 | base::Bind( |
[email protected] | 3e55e21 | 2011-03-24 19:45:02 | [diff] [blame] | 490 | &ChildProcessLauncher::Context::SetProcessBackgrounded, |
[email protected] | e202478 | 2011-09-07 20:20:51 | [diff] [blame] | 491 | GetHandle(), background)); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 492 | } |
[email protected] | 358cb8e | 2011-05-25 02:12:45 | [diff] [blame] | 493 | |
| 494 | void ChildProcessLauncher::SetTerminateChildOnShutdown( |
| 495 | bool terminate_on_shutdown) { |
| 496 | if (context_) |
| 497 | context_->set_terminate_child_on_shutdown(terminate_on_shutdown); |
| 498 | } |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 499 | |
| 500 | } // namespace content |