OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/child_process_launcher.h" | 5 #include "content/browser/child_process_launcher.h" |
6 | 6 |
7 #include <utility> // For std::pair. | 7 #include <utility> // For std::pair. |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 #elif defined(OS_POSIX) | 31 #elif defined(OS_POSIX) |
32 #include "base/memory/singleton.h" | 32 #include "base/memory/singleton.h" |
33 #include "content/browser/renderer_host/render_sandbox_host_linux.h" | 33 #include "content/browser/renderer_host/render_sandbox_host_linux.h" |
34 #include "content/browser/zygote_host/zygote_host_impl_linux.h" | 34 #include "content/browser/zygote_host/zygote_host_impl_linux.h" |
35 #endif | 35 #endif |
36 | 36 |
37 #if defined(OS_POSIX) | 37 #if defined(OS_POSIX) |
38 #include "base/global_descriptors_posix.h" | 38 #include "base/global_descriptors_posix.h" |
39 #endif | 39 #endif |
40 | 40 |
41 using content::BrowserThread; | 41 namespace content { |
42 | 42 |
43 // Having the functionality of ChildProcessLauncher be in an internal | 43 // Having the functionality of ChildProcessLauncher be in an internal |
44 // ref counted object allows us to automatically terminate the process when the | 44 // ref counted object allows us to automatically terminate the process when the |
45 // parent class destructs, while still holding on to state that we need. | 45 // parent class destructs, while still holding on to state that we need. |
46 class ChildProcessLauncher::Context | 46 class ChildProcessLauncher::Context |
47 : public base::RefCountedThreadSafe<ChildProcessLauncher::Context> { | 47 : public base::RefCountedThreadSafe<ChildProcessLauncher::Context> { |
48 public: | 48 public: |
49 Context() | 49 Context() |
50 : client_(NULL), | 50 : client_(NULL), |
51 client_thread_id_(BrowserThread::UI), | 51 client_thread_id_(BrowserThread::UI), |
52 termination_status_(base::TERMINATION_STATUS_NORMAL_TERMINATION), | 52 termination_status_(base::TERMINATION_STATUS_NORMAL_TERMINATION), |
53 exit_code_(content::RESULT_CODE_NORMAL_EXIT), | 53 exit_code_(RESULT_CODE_NORMAL_EXIT), |
54 starting_(true) | 54 starting_(true) |
55 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) | 55 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
56 , zygote_(false) | 56 , zygote_(false) |
57 #endif | 57 #endif |
58 { | 58 { |
59 #if defined(OS_POSIX) | 59 #if defined(OS_POSIX) |
60 terminate_child_on_shutdown_ = !CommandLine::ForCurrentProcess()-> | 60 terminate_child_on_shutdown_ = !CommandLine::ForCurrentProcess()-> |
61 HasSwitch(switches::kChildCleanExit); | 61 HasSwitch(switches::kChildCleanExit); |
62 #else | 62 #else |
63 terminate_child_on_shutdown_ = true; | 63 terminate_child_on_shutdown_ = true; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 #endif | 159 #endif |
160 CommandLine* cmd_line) { | 160 CommandLine* cmd_line) { |
161 scoped_ptr<CommandLine> cmd_line_deleter(cmd_line); | 161 scoped_ptr<CommandLine> cmd_line_deleter(cmd_line); |
162 | 162 |
163 #if defined(OS_WIN) | 163 #if defined(OS_WIN) |
164 base::ProcessHandle handle = sandbox::StartProcessWithAccess( | 164 base::ProcessHandle handle = sandbox::StartProcessWithAccess( |
165 cmd_line, exposed_dir); | 165 cmd_line, exposed_dir); |
166 #elif defined(OS_ANDROID) | 166 #elif defined(OS_ANDROID) |
167 std::string process_type = | 167 std::string process_type = |
168 cmd_line->GetSwitchValueASCII(switches::kProcessType); | 168 cmd_line->GetSwitchValueASCII(switches::kProcessType); |
169 std::vector<content::FileDescriptorInfo> files_to_register; | 169 std::vector<FileDescriptorInfo> files_to_register; |
170 files_to_register.push_back( | 170 files_to_register.push_back( |
171 content::FileDescriptorInfo(kPrimaryIPCChannel, | 171 FileDescriptorInfo(kPrimaryIPCChannel, |
172 base::FileDescriptor(ipcfd, false))); | 172 base::FileDescriptor(ipcfd, false))); |
173 | 173 |
174 content::GetContentClient()->browser()-> | 174 GetContentClient()->browser()-> |
175 GetAdditionalMappedFilesForChildProcess(*cmd_line, &files_to_register); | 175 GetAdditionalMappedFilesForChildProcess(*cmd_line, &files_to_register); |
176 | 176 |
177 content::StartSandboxedProcess(cmd_line->argv(), files_to_register, | 177 StartSandboxedProcess(cmd_line->argv(), files_to_register, |
178 base::Bind(&ChildProcessLauncher::Context::OnSandboxedProcessStarted, | 178 base::Bind(&ChildProcessLauncher::Context::OnSandboxedProcessStarted, |
179 this_object, client_thread_id)); | 179 this_object, client_thread_id)); |
180 | 180 |
181 #elif defined(OS_POSIX) | 181 #elif defined(OS_POSIX) |
182 base::ProcessHandle handle = base::kNullProcessHandle; | 182 base::ProcessHandle handle = base::kNullProcessHandle; |
183 // We need to close the client end of the IPC channel to reliably detect | 183 // We need to close the client end of the IPC channel to reliably detect |
184 // child termination. | 184 // child termination. |
185 file_util::ScopedFD ipcfd_closer(&ipcfd); | 185 file_util::ScopedFD ipcfd_closer(&ipcfd); |
186 | 186 |
187 std::string process_type = | 187 std::string process_type = |
188 cmd_line->GetSwitchValueASCII(switches::kProcessType); | 188 cmd_line->GetSwitchValueASCII(switches::kProcessType); |
189 std::vector<content::FileDescriptorInfo> files_to_register; | 189 std::vector<FileDescriptorInfo> files_to_register; |
190 files_to_register.push_back( | 190 files_to_register.push_back( |
191 content::FileDescriptorInfo(kPrimaryIPCChannel, | 191 FileDescriptorInfo(kPrimaryIPCChannel, |
192 base::FileDescriptor(ipcfd, false))); | 192 base::FileDescriptor(ipcfd, false))); |
193 | 193 |
194 #if !defined(OS_MACOSX) | 194 #if !defined(OS_MACOSX) |
195 content::GetContentClient()->browser()-> | 195 GetContentClient()->browser()-> |
196 GetAdditionalMappedFilesForChildProcess(*cmd_line, &files_to_register); | 196 GetAdditionalMappedFilesForChildProcess(*cmd_line, &files_to_register); |
197 if (use_zygote) { | 197 if (use_zygote) { |
198 handle = ZygoteHostImpl::GetInstance()->ForkRequest(cmd_line->argv(), | 198 handle = ZygoteHostImpl::GetInstance()->ForkRequest(cmd_line->argv(), |
199 files_to_register, | 199 files_to_register, |
200 process_type); | 200 process_type); |
201 } else | 201 } else |
202 // Fall through to the normal posix case below when we're not zygoting. | 202 // Fall through to the normal posix case below when we're not zygoting. |
203 #endif // !defined(OS_MACOSX) | 203 #endif // !defined(OS_MACOSX) |
204 { | 204 { |
205 // Convert FD mapping to FileHandleMappingVector | 205 // Convert FD mapping to FileHandleMappingVector |
206 base::FileHandleMappingVector fds_to_map; | 206 base::FileHandleMappingVector fds_to_map; |
207 for (std::vector<content::FileDescriptorInfo>::const_iterator | 207 for (std::vector<FileDescriptorInfo>::const_iterator |
208 i = files_to_register.begin(); i != files_to_register.end(); ++i) { | 208 i = files_to_register.begin(); i != files_to_register.end(); ++i) { |
209 const content::FileDescriptorInfo& fd_info = *i; | 209 const FileDescriptorInfo& fd_info = *i; |
210 fds_to_map.push_back(std::make_pair( | 210 fds_to_map.push_back(std::make_pair( |
211 fd_info.fd.fd, | 211 fd_info.fd.fd, |
212 fd_info.id + base::GlobalDescriptors::kBaseDescriptor)); | 212 fd_info.id + base::GlobalDescriptors::kBaseDescriptor)); |
213 } | 213 } |
214 | 214 |
215 #if !defined(OS_MACOSX) | 215 #if !defined(OS_MACOSX) |
216 if (process_type == switches::kRendererProcess) { | 216 if (process_type == switches::kRendererProcess) { |
217 const int sandbox_fd = | 217 const int sandbox_fd = |
218 RenderSandboxHostLinux::GetInstance()->GetRendererSocket(); | 218 RenderSandboxHostLinux::GetInstance()->GetRendererSocket(); |
219 fds_to_map.push_back(std::make_pair( | 219 fds_to_map.push_back(std::make_pair( |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 process.SetProcessBackgrounded(background); | 321 process.SetProcessBackgrounded(background); |
322 } | 322 } |
323 | 323 |
324 static void TerminateInternal( | 324 static void TerminateInternal( |
325 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) | 325 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
326 bool zygote, | 326 bool zygote, |
327 #endif | 327 #endif |
328 base::ProcessHandle handle) { | 328 base::ProcessHandle handle) { |
329 #if defined(OS_ANDROID) | 329 #if defined(OS_ANDROID) |
330 LOG(INFO) << "ChromeProcess: Stopping process with handle " << handle; | 330 LOG(INFO) << "ChromeProcess: Stopping process with handle " << handle; |
331 content::StopSandboxedProcess(handle); | 331 StopSandboxedProcess(handle); |
332 #else | 332 #else |
333 base::Process process(handle); | 333 base::Process process(handle); |
334 // Client has gone away, so just kill the process. Using exit code 0 | 334 // Client has gone away, so just kill the process. Using exit code 0 |
335 // means that UMA won't treat this as a crash. | 335 // means that UMA won't treat this as a crash. |
336 process.Terminate(content::RESULT_CODE_NORMAL_EXIT); | 336 process.Terminate(RESULT_CODE_NORMAL_EXIT); |
337 // On POSIX, we must additionally reap the child. | 337 // On POSIX, we must additionally reap the child. |
338 #if defined(OS_POSIX) | 338 #if defined(OS_POSIX) |
339 #if !defined(OS_MACOSX) | 339 #if !defined(OS_MACOSX) |
340 if (zygote) { | 340 if (zygote) { |
341 // If the renderer was created via a zygote, we have to proxy the reaping | 341 // If the renderer was created via a zygote, we have to proxy the reaping |
342 // through the zygote process. | 342 // through the zygote process. |
343 ZygoteHostImpl::GetInstance()->EnsureProcessTerminated(handle); | 343 ZygoteHostImpl::GetInstance()->EnsureProcessTerminated(handle); |
344 } else | 344 } else |
345 #endif // !OS_MACOSX | 345 #endif // !OS_MACOSX |
346 { | 346 { |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 base::Bind( | 448 base::Bind( |
449 &ChildProcessLauncher::Context::SetProcessBackgrounded, | 449 &ChildProcessLauncher::Context::SetProcessBackgrounded, |
450 GetHandle(), background)); | 450 GetHandle(), background)); |
451 } | 451 } |
452 | 452 |
453 void ChildProcessLauncher::SetTerminateChildOnShutdown( | 453 void ChildProcessLauncher::SetTerminateChildOnShutdown( |
454 bool terminate_on_shutdown) { | 454 bool terminate_on_shutdown) { |
455 if (context_) | 455 if (context_) |
456 context_->set_terminate_child_on_shutdown(terminate_on_shutdown); | 456 context_->set_terminate_child_on_shutdown(terminate_on_shutdown); |
457 } | 457 } |
| 458 |
| 459 } // namespace content |
OLD | NEW |