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