[email protected] | 3e55e21 | 2011-03-24 19:45:02 | [diff] [blame] | 1 | // Copyright (c) 2011 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] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 9 | #include "base/command_line.h" |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 11 | #include "base/memory/scoped_ptr.h" |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 12 | #include "base/synchronization/lock.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 13 | #include "base/threading/thread.h" |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 14 | #include "content/browser/browser_thread.h" |
[email protected] | b80f6843 | 2011-05-02 17:22:30 | [diff] [blame] | 15 | #include "content/browser/content_browser_client.h" |
[email protected] | acb9472 | 2011-03-18 01:33:34 | [diff] [blame] | 16 | #include "content/common/chrome_descriptors.h" |
[email protected] | 25fe7fc5 | 2011-05-27 21:48:41 | [diff] [blame] | 17 | #include "content/common/content_switches.h" |
[email protected] | 4dd5793 | 2011-03-17 06:06:12 | [diff] [blame] | 18 | #include "content/common/process_watcher.h" |
| 19 | #include "content/common/result_codes.h" |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 20 | |
| 21 | #if defined(OS_WIN) |
[email protected] | 864b136 | 2010-08-19 03:49:38 | [diff] [blame] | 22 | #include "base/file_path.h" |
[email protected] | 7c863570 | 2011-06-09 22:35:33 | [diff] [blame] | 23 | #include "content/browser/handle_enumerator_win.h" |
[email protected] | cd5fa1a | 2011-05-28 18:21:47 | [diff] [blame] | 24 | #include "content/common/sandbox_policy.h" |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 25 | #elif defined(OS_MACOSX) |
[email protected] | ed0fbe6 | 2011-06-23 18:32:11 | [diff] [blame] | 26 | #include "content/browser/mach_broker_mac.h" |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 27 | #elif defined(OS_POSIX) |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 28 | #include "base/memory/singleton.h" |
[email protected] | a01efd2 | 2011-03-01 00:38:32 | [diff] [blame] | 29 | #include "content/browser/zygote_host_linux.h" |
| 30 | #include "content/browser/renderer_host/render_sandbox_host_linux.h" |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 31 | #endif |
| 32 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 33 | #if defined(OS_POSIX) |
| 34 | #include "base/global_descriptors_posix.h" |
| 35 | #endif |
| 36 | |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 37 | // Having the functionality of ChildProcessLauncher be in an internal |
| 38 | // ref counted object allows us to automatically terminate the process when the |
| 39 | // parent class destructs, while still holding on to state that we need. |
| 40 | class ChildProcessLauncher::Context |
| 41 | : public base::RefCountedThreadSafe<ChildProcessLauncher::Context> { |
| 42 | public: |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 43 | Context() |
[email protected] | 18cbea3 | 2010-10-13 19:56:00 | [diff] [blame] | 44 | : client_(NULL), |
| 45 | client_thread_id_(BrowserThread::UI), |
[email protected] | f3c1d3c | 2011-07-25 18:50:48 | [diff] [blame] | 46 | termination_status_(base::TERMINATION_STATUS_NORMAL_TERMINATION), |
| 47 | exit_code_(content::RESULT_CODE_NORMAL_EXIT), |
[email protected] | 358cb8e | 2011-05-25 02:12:45 | [diff] [blame] | 48 | starting_(true), |
| 49 | terminate_child_on_shutdown_(true) |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 50 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 51 | , zygote_(false) |
| 52 | #endif |
| 53 | { |
| 54 | } |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 55 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 56 | void Launch( |
| 57 | #if defined(OS_WIN) |
| 58 | const FilePath& exposed_dir, |
| 59 | #elif defined(OS_POSIX) |
[email protected] | 7c4ea14 | 2010-01-26 05:15:42 | [diff] [blame] | 60 | bool use_zygote, |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 61 | const base::environment_vector& environ, |
| 62 | int ipcfd, |
| 63 | #endif |
| 64 | CommandLine* cmd_line, |
| 65 | Client* client) { |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 66 | client_ = client; |
| 67 | |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 68 | CHECK(BrowserThread::GetCurrentThreadIdentifier(&client_thread_id_)); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 69 | |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 70 | BrowserThread::PostTask( |
| 71 | BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 72 | NewRunnableMethod( |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 73 | this, |
| 74 | &Context::LaunchInternal, |
| 75 | #if defined(OS_WIN) |
| 76 | exposed_dir, |
[email protected] | eb1bd83 | 2010-05-18 20:39:58 | [diff] [blame] | 77 | #elif defined(OS_POSIX) |
[email protected] | 7c4ea14 | 2010-01-26 05:15:42 | [diff] [blame] | 78 | use_zygote, |
[email protected] | 3d2217d | 2009-11-23 21:26:47 | [diff] [blame] | 79 | environ, |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 80 | ipcfd, |
| 81 | #endif |
| 82 | cmd_line)); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void ResetClient() { |
| 86 | // No need for locking as this function gets called on the same thread that |
| 87 | // client_ would be used. |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 88 | CHECK(BrowserThread::CurrentlyOn(client_thread_id_)); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 89 | client_ = NULL; |
| 90 | } |
| 91 | |
[email protected] | 358cb8e | 2011-05-25 02:12:45 | [diff] [blame] | 92 | void set_terminate_child_on_shutdown(bool terminate_on_shutdown) { |
| 93 | terminate_child_on_shutdown_ = terminate_on_shutdown; |
| 94 | } |
| 95 | |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 96 | private: |
| 97 | friend class base::RefCountedThreadSafe<ChildProcessLauncher::Context>; |
| 98 | friend class ChildProcessLauncher; |
| 99 | |
| 100 | ~Context() { |
| 101 | Terminate(); |
| 102 | } |
| 103 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 104 | void LaunchInternal( |
| 105 | #if defined(OS_WIN) |
| 106 | const FilePath& exposed_dir, |
| 107 | #elif defined(OS_POSIX) |
[email protected] | 7c4ea14 | 2010-01-26 05:15:42 | [diff] [blame] | 108 | bool use_zygote, |
[email protected] | 3d2217d | 2009-11-23 21:26:47 | [diff] [blame] | 109 | const base::environment_vector& env, |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 110 | int ipcfd, |
| 111 | #endif |
| 112 | CommandLine* cmd_line) { |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 113 | scoped_ptr<CommandLine> cmd_line_deleter(cmd_line); |
| 114 | base::ProcessHandle handle = base::kNullProcessHandle; |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 115 | #if defined(OS_WIN) |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 116 | handle = sandbox::StartProcessWithAccess(cmd_line, exposed_dir); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 117 | #elif defined(OS_POSIX) |
| 118 | |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 119 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | 54457f3 | 2011-04-15 22:05:29 | [diff] [blame] | 120 | // On Linux, we need to add some extra file descriptors for crash handling. |
| 121 | std::string process_type = |
| 122 | cmd_line->GetSwitchValueASCII(switches::kProcessType); |
[email protected] | b80f6843 | 2011-05-02 17:22:30 | [diff] [blame] | 123 | int crash_signal_fd = |
| 124 | content::GetContentClient()->browser()->GetCrashSignalFD(process_type); |
[email protected] | 7c4ea14 | 2010-01-26 05:15:42 | [diff] [blame] | 125 | if (use_zygote) { |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 126 | base::GlobalDescriptors::Mapping mapping; |
| 127 | mapping.push_back(std::pair<uint32_t, int>(kPrimaryIPCChannel, ipcfd)); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 128 | if (crash_signal_fd >= 0) { |
| 129 | mapping.push_back(std::pair<uint32_t, int>(kCrashDumpSignal, |
| 130 | crash_signal_fd)); |
| 131 | } |
[email protected] | ce2ae44 | 2011-07-18 16:13:41 | [diff] [blame] | 132 | handle = ZygoteHost::GetInstance()->ForkRequest(cmd_line->argv(), |
| 133 | mapping, |
| 134 | process_type); |
[email protected] | 7c4ea14 | 2010-01-26 05:15:42 | [diff] [blame] | 135 | } else |
| 136 | // Fall through to the normal posix case below when we're not zygoting. |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 137 | #endif |
| 138 | { |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 139 | base::file_handle_mapping_vector fds_to_map; |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 140 | fds_to_map.push_back(std::make_pair( |
| 141 | ipcfd, |
| 142 | kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor)); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 143 | |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 144 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | 54457f3 | 2011-04-15 22:05:29 | [diff] [blame] | 145 | if (crash_signal_fd >= 0) { |
| 146 | fds_to_map.push_back(std::make_pair( |
| 147 | crash_signal_fd, |
| 148 | kCrashDumpSignal + base::GlobalDescriptors::kBaseDescriptor)); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 149 | } |
[email protected] | b80f6843 | 2011-05-02 17:22:30 | [diff] [blame] | 150 | if (process_type == switches::kRendererProcess) { |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 151 | const int sandbox_fd = |
[email protected] | d3c6c0d7 | 2010-12-09 08:15:04 | [diff] [blame] | 152 | RenderSandboxHostLinux::GetInstance()->GetRendererSocket(); |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 153 | fds_to_map.push_back(std::make_pair( |
| 154 | sandbox_fd, |
| 155 | kSandboxIPCChannel + base::GlobalDescriptors::kBaseDescriptor)); |
| 156 | } |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 157 | #endif // defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 158 | |
[email protected] | b88a749 | 2010-09-17 12:28:32 | [diff] [blame] | 159 | bool launched = false; |
[email protected] | c002879 | 2010-01-12 00:39:15 | [diff] [blame] | 160 | #if defined(OS_MACOSX) |
[email protected] | b88a749 | 2010-09-17 12:28:32 | [diff] [blame] | 161 | // It is possible for the child process to die immediately after |
| 162 | // launching. To prevent leaking MachBroker map entries in this case, |
[email protected] | 1e41c38 | 2011-07-12 18:09:46 | [diff] [blame] | 163 | // lock around all of LaunchProcess(). If the child dies, the death |
[email protected] | b88a749 | 2010-09-17 12:28:32 | [diff] [blame] | 164 | // notification will be processed by the MachBroker after the call to |
| 165 | // AddPlaceholderForPid(), enabling proper cleanup. |
| 166 | { // begin scope for AutoLock |
[email protected] | dc8caba | 2010-12-13 16:52:35 | [diff] [blame] | 167 | MachBroker* broker = MachBroker::GetInstance(); |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 168 | base::AutoLock lock(broker->GetLock()); |
[email protected] | b88a749 | 2010-09-17 12:28:32 | [diff] [blame] | 169 | |
| 170 | // This call to |PrepareForFork()| will start the MachBroker listener |
| 171 | // thread, if it is not already running. Therefore the browser process |
[email protected] | 1e41c38 | 2011-07-12 18:09:46 | [diff] [blame] | 172 | // will be listening for Mach IPC before LaunchProcess() is called. |
[email protected] | b88a749 | 2010-09-17 12:28:32 | [diff] [blame] | 173 | broker->PrepareForFork(); |
| 174 | #endif |
[email protected] | 1e41c38 | 2011-07-12 18:09:46 | [diff] [blame] | 175 | |
[email protected] | b88a749 | 2010-09-17 12:28:32 | [diff] [blame] | 176 | // Actually launch the app. |
[email protected] | 1e41c38 | 2011-07-12 18:09:46 | [diff] [blame] | 177 | base::LaunchOptions options; |
| 178 | options.environ = &env; |
| 179 | options.fds_to_remap = &fds_to_map; |
[email protected] | e599218 | 2011-07-15 16:47:02 | [diff] [blame] | 180 | launched = base::LaunchProcess(*cmd_line, options, &handle); |
[email protected] | 1e41c38 | 2011-07-12 18:09:46 | [diff] [blame] | 181 | |
[email protected] | b88a749 | 2010-09-17 12:28:32 | [diff] [blame] | 182 | #if defined(OS_MACOSX) |
| 183 | if (launched) |
| 184 | broker->AddPlaceholderForPid(handle); |
| 185 | } // end scope for AutoLock |
[email protected] | c002879 | 2010-01-12 00:39:15 | [diff] [blame] | 186 | #endif |
| 187 | if (!launched) |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 188 | handle = base::kNullProcessHandle; |
| 189 | } |
[email protected] | c002879 | 2010-01-12 00:39:15 | [diff] [blame] | 190 | #endif // else defined(OS_POSIX) |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 191 | |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 192 | BrowserThread::PostTask( |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 193 | client_thread_id_, FROM_HERE, |
| 194 | NewRunnableMethod( |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 195 | this, |
[email protected] | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 196 | &ChildProcessLauncher::Context::Notify, |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 197 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | 7c4ea14 | 2010-01-26 05:15:42 | [diff] [blame] | 198 | use_zygote, |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 199 | #endif |
| 200 | handle)); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 201 | } |
| 202 | |
[email protected] | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 203 | void Notify( |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 204 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 205 | bool zygote, |
| 206 | #endif |
| 207 | base::ProcessHandle handle) { |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 208 | starting_ = false; |
| 209 | process_.set_handle(handle); |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 210 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 211 | zygote_ = zygote; |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 212 | #endif |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 213 | if (client_) { |
| 214 | client_->OnProcessLaunched(); |
| 215 | } else { |
| 216 | Terminate(); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | void Terminate() { |
| 221 | if (!process_.handle()) |
| 222 | return; |
| 223 | |
[email protected] | 358cb8e | 2011-05-25 02:12:45 | [diff] [blame] | 224 | if (!terminate_child_on_shutdown_) |
| 225 | return; |
| 226 | |
[email protected] | 7c863570 | 2011-06-09 22:35:33 | [diff] [blame] | 227 | #if defined(OS_WIN) |
| 228 | const CommandLine& browser_command_line = |
| 229 | *CommandLine::ForCurrentProcess(); |
| 230 | if (browser_command_line.HasSwitch(switches::kAuditHandles) || |
| 231 | browser_command_line.HasSwitch(switches::kAuditAllHandles)) { |
| 232 | scoped_refptr<content::HandleEnumerator> handle_enum( |
| 233 | new content::HandleEnumerator(process_.handle(), |
| 234 | browser_command_line.HasSwitch(switches::kAuditAllHandles))); |
| 235 | handle_enum->RunHandleEnumeration(); |
| 236 | process_.set_handle(base::kNullProcessHandle); |
| 237 | return; |
| 238 | } |
| 239 | #endif |
| 240 | |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 241 | // On Posix, EnsureProcessTerminated can lead to 2 seconds of sleep! So |
| 242 | // don't this on the UI/IO threads. |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 243 | BrowserThread::PostTask( |
| 244 | BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 245 | NewRunnableFunction( |
| 246 | &ChildProcessLauncher::Context::TerminateInternal, |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 247 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 248 | zygote_, |
| 249 | #endif |
| 250 | process_.handle())); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 251 | process_.set_handle(base::kNullProcessHandle); |
| 252 | } |
| 253 | |
[email protected] | 3e55e21 | 2011-03-24 19:45:02 | [diff] [blame] | 254 | void SetProcessBackgrounded(bool background) { |
| 255 | DCHECK(!starting_); |
| 256 | process_.SetProcessBackgrounded(background); |
| 257 | } |
| 258 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 259 | static void TerminateInternal( |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 260 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 261 | bool zygote, |
| 262 | #endif |
| 263 | base::ProcessHandle handle) { |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 264 | base::Process process(handle); |
| 265 | // Client has gone away, so just kill the process. Using exit code 0 |
| 266 | // means that UMA won't treat this as a crash. |
[email protected] | 1fcfb20 | 2011-07-19 19:53:14 | [diff] [blame] | 267 | process.Terminate(content::RESULT_CODE_NORMAL_EXIT); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 268 | // On POSIX, we must additionally reap the child. |
| 269 | #if defined(OS_POSIX) |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 270 | #if !defined(OS_MACOSX) |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 271 | if (zygote) { |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 272 | // If the renderer was created via a zygote, we have to proxy the reaping |
| 273 | // through the zygote process. |
[email protected] | d3c6c0d7 | 2010-12-09 08:15:04 | [diff] [blame] | 274 | ZygoteHost::GetInstance()->EnsureProcessTerminated(handle); |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 275 | } else |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 276 | #endif // !OS_MACOSX |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 277 | { |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 278 | ProcessWatcher::EnsureProcessTerminated(handle); |
| 279 | } |
[email protected] | e36e86f | 2009-12-15 11:55:08 | [diff] [blame] | 280 | #endif // OS_POSIX |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 281 | process.Close(); |
| 282 | } |
| 283 | |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 284 | Client* client_; |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 285 | BrowserThread::ID client_thread_id_; |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 286 | base::Process process_; |
[email protected] | f3c1d3c | 2011-07-25 18:50:48 | [diff] [blame] | 287 | base::TerminationStatus termination_status_; |
| 288 | int exit_code_; |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 289 | bool starting_; |
[email protected] | 358cb8e | 2011-05-25 02:12:45 | [diff] [blame] | 290 | // Controls whether the child process should be terminated on browser |
| 291 | // shutdown. Default behavior is to terminate the child. |
| 292 | bool terminate_child_on_shutdown_; |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 293 | |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 294 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 295 | bool zygote_; |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 296 | #endif |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 300 | ChildProcessLauncher::ChildProcessLauncher( |
| 301 | #if defined(OS_WIN) |
| 302 | const FilePath& exposed_dir, |
| 303 | #elif defined(OS_POSIX) |
[email protected] | 7c4ea14 | 2010-01-26 05:15:42 | [diff] [blame] | 304 | bool use_zygote, |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 305 | const base::environment_vector& environ, |
| 306 | int ipcfd, |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 307 | #endif |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 308 | CommandLine* cmd_line, |
| 309 | Client* client) { |
| 310 | context_ = new Context(); |
| 311 | context_->Launch( |
| 312 | #if defined(OS_WIN) |
| 313 | exposed_dir, |
| 314 | #elif defined(OS_POSIX) |
[email protected] | 7c4ea14 | 2010-01-26 05:15:42 | [diff] [blame] | 315 | use_zygote, |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 316 | environ, |
| 317 | ipcfd, |
| 318 | #endif |
| 319 | cmd_line, |
| 320 | client); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | ChildProcessLauncher::~ChildProcessLauncher() { |
| 324 | context_->ResetClient(); |
| 325 | } |
| 326 | |
| 327 | bool ChildProcessLauncher::IsStarting() { |
| 328 | return context_->starting_; |
| 329 | } |
| 330 | |
| 331 | base::ProcessHandle ChildProcessLauncher::GetHandle() { |
| 332 | DCHECK(!context_->starting_); |
| 333 | return context_->process_.handle(); |
| 334 | } |
| 335 | |
[email protected] | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 336 | base::TerminationStatus ChildProcessLauncher::GetChildTerminationStatus( |
| 337 | int* exit_code) { |
[email protected] | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 338 | base::ProcessHandle handle = context_->process_.handle(); |
[email protected] | f3c1d3c | 2011-07-25 18:50:48 | [diff] [blame] | 339 | if (handle == base::kNullProcessHandle) { |
| 340 | // Process is already gone, so return the cached termination status. |
| 341 | if (exit_code) |
| 342 | *exit_code = context_->exit_code_; |
| 343 | return context_->termination_status_; |
| 344 | } |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 345 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 346 | if (context_->zygote_) { |
[email protected] | f3c1d3c | 2011-07-25 18:50:48 | [diff] [blame] | 347 | context_->termination_status_ = ZygoteHost::GetInstance()-> |
| 348 | GetTerminationStatus(handle, &context_->exit_code_); |
[email protected] | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 349 | } else |
| 350 | #endif |
| 351 | { |
[email protected] | f3c1d3c | 2011-07-25 18:50:48 | [diff] [blame] | 352 | context_->termination_status_ = |
| 353 | base::GetTerminationStatus(handle, &context_->exit_code_); |
[email protected] | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 354 | } |
| 355 | |
[email protected] | f3c1d3c | 2011-07-25 18:50:48 | [diff] [blame] | 356 | if (exit_code) |
| 357 | *exit_code = context_->exit_code_; |
| 358 | |
[email protected] | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 359 | // POSIX: If the process crashed, then the kernel closed the socket |
| 360 | // for it and so the child has already died by the time we get |
| 361 | // here. Since GetTerminationStatus called waitpid with WNOHANG, |
| 362 | // it'll reap the process. However, if GetTerminationStatus didn't |
| 363 | // reap the child (because it was still running), we'll need to |
[email protected] | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 364 | // Terminate via ProcessWatcher. So we can't close the handle here. |
[email protected] | f3c1d3c | 2011-07-25 18:50:48 | [diff] [blame] | 365 | if (context_->termination_status_ != base::TERMINATION_STATUS_STILL_RUNNING) |
[email protected] | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 366 | context_->process_.Close(); |
| 367 | |
[email protected] | f3c1d3c | 2011-07-25 18:50:48 | [diff] [blame] | 368 | return context_->termination_status_; |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | void ChildProcessLauncher::SetProcessBackgrounded(bool background) { |
[email protected] | 3e55e21 | 2011-03-24 19:45:02 | [diff] [blame] | 372 | BrowserThread::PostTask( |
| 373 | BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
| 374 | NewRunnableMethod( |
| 375 | context_.get(), |
| 376 | &ChildProcessLauncher::Context::SetProcessBackgrounded, |
| 377 | background)); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 378 | } |
[email protected] | 358cb8e | 2011-05-25 02:12:45 | [diff] [blame] | 379 | |
| 380 | void ChildProcessLauncher::SetTerminateChildOnShutdown( |
| 381 | bool terminate_on_shutdown) { |
| 382 | if (context_) |
| 383 | context_->set_terminate_child_on_shutdown(terminate_on_shutdown); |
| 384 | } |