OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/common/child_process_host.h" | 5 #include "content/common/child_process_host.h" |
6 | 6 |
| 7 #include <limits> |
| 8 |
| 9 #include "base/atomicops.h" |
7 #include "base/command_line.h" | 10 #include "base/command_line.h" |
8 #include "base/file_path.h" | 11 #include "base/file_path.h" |
9 #include "base/logging.h" | 12 #include "base/logging.h" |
10 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
11 #include "base/path_service.h" | 14 #include "base/path_service.h" |
12 #include "base/process_util.h" | 15 #include "base/process_util.h" |
| 16 #include "base/rand_util.h" |
| 17 #include "base/stringprintf.h" |
13 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 18 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
14 #include "content/common/child_process_info.h" | |
15 #include "content/common/child_process_messages.h" | 19 #include "content/common/child_process_messages.h" |
16 #include "content/public/common/content_paths.h" | 20 #include "content/public/common/content_paths.h" |
17 #include "content/public/common/content_switches.h" | 21 #include "content/public/common/content_switches.h" |
18 #include "ipc/ipc_logging.h" | 22 #include "ipc/ipc_logging.h" |
19 | 23 |
20 #if defined(OS_LINUX) | 24 #if defined(OS_LINUX) |
21 #include "base/linux_util.h" | 25 #include "base/linux_util.h" |
22 #elif defined(OS_WIN) | 26 #elif defined(OS_WIN) |
23 #include "content/common/font_cache_dispatcher_win.h" | 27 #include "content/common/font_cache_dispatcher_win.h" |
24 #endif // OS_LINUX | 28 #endif // OS_LINUX |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 #endif | 138 #endif |
135 | 139 |
136 return child_path; | 140 return child_path; |
137 } | 141 } |
138 | 142 |
139 void ChildProcessHost::ForceShutdown() { | 143 void ChildProcessHost::ForceShutdown() { |
140 Send(new ChildProcessMsg_Shutdown()); | 144 Send(new ChildProcessMsg_Shutdown()); |
141 } | 145 } |
142 | 146 |
143 bool ChildProcessHost::CreateChannel() { | 147 bool ChildProcessHost::CreateChannel() { |
144 channel_id_ = ChildProcessInfo::GenerateRandomChannelID(this); | 148 channel_id_ = GenerateRandomChannelID(this); |
145 channel_.reset(new IPC::Channel( | 149 channel_.reset(new IPC::Channel( |
146 channel_id_, IPC::Channel::MODE_SERVER, &listener_)); | 150 channel_id_, IPC::Channel::MODE_SERVER, &listener_)); |
147 if (!channel_->Connect()) | 151 if (!channel_->Connect()) |
148 return false; | 152 return false; |
149 | 153 |
150 for (size_t i = 0; i < filters_.size(); ++i) | 154 for (size_t i = 0; i < filters_.size(); ++i) |
151 filters_[i]->OnFilterAdded(channel_.get()); | 155 filters_[i]->OnFilterAdded(channel_.get()); |
152 | 156 |
153 // Make sure these messages get sent first. | 157 // Make sure these messages get sent first. |
154 #if defined(IPC_MESSAGE_LOG_ENABLED) | 158 #if defined(IPC_MESSAGE_LOG_ENABLED) |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 base::SharedMemoryHandle* shared_memory_handle) { | 190 base::SharedMemoryHandle* shared_memory_handle) { |
187 base::SharedMemory shared_buf; | 191 base::SharedMemory shared_buf; |
188 if (!shared_buf.CreateAndMapAnonymous(buffer_size)) { | 192 if (!shared_buf.CreateAndMapAnonymous(buffer_size)) { |
189 *shared_memory_handle = base::SharedMemory::NULLHandle(); | 193 *shared_memory_handle = base::SharedMemory::NULLHandle(); |
190 NOTREACHED() << "Cannot map shared memory buffer"; | 194 NOTREACHED() << "Cannot map shared memory buffer"; |
191 return; | 195 return; |
192 } | 196 } |
193 shared_buf.GiveToProcess(child_process_handle, shared_memory_handle); | 197 shared_buf.GiveToProcess(child_process_handle, shared_memory_handle); |
194 } | 198 } |
195 | 199 |
| 200 std::string ChildProcessHost::GenerateRandomChannelID(void* instance) { |
| 201 // Note: the string must start with the current process id, this is how |
| 202 // child processes determine the pid of the parent. |
| 203 // Build the channel ID. This is composed of a unique identifier for the |
| 204 // parent browser process, an identifier for the child instance, and a random |
| 205 // component. We use a random component so that a hacked child process can't |
| 206 // cause denial of service by causing future named pipe creation to fail. |
| 207 return base::StringPrintf("%d.%p.%d", |
| 208 base::GetCurrentProcId(), instance, |
| 209 base::RandInt(0, std::numeric_limits<int>::max())); |
| 210 } |
| 211 |
| 212 int ChildProcessHost::GenerateChildProcessUniqueId() { |
| 213 // This function must be threadsafe. |
| 214 static base::subtle::Atomic32 last_unique_child_id = 0; |
| 215 return base::subtle::NoBarrier_AtomicIncrement(&last_unique_child_id, 1); |
| 216 } |
| 217 |
| 218 |
196 void ChildProcessHost::OnChildDied() { | 219 void ChildProcessHost::OnChildDied() { |
197 delete this; | 220 delete this; |
198 } | 221 } |
199 | 222 |
200 void ChildProcessHost::OnChildDisconnected() { | 223 void ChildProcessHost::OnChildDisconnected() { |
201 OnChildDied(); | 224 OnChildDied(); |
202 } | 225 } |
203 | 226 |
204 void ChildProcessHost::ShutdownStarted() { | 227 void ChildProcessHost::ShutdownStarted() { |
205 } | 228 } |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 uint32 buffer_size, | 318 uint32 buffer_size, |
296 base::SharedMemoryHandle* handle) { | 319 base::SharedMemoryHandle* handle) { |
297 ChildProcessHost::OnAllocateSharedMemory( | 320 ChildProcessHost::OnAllocateSharedMemory( |
298 buffer_size, peer_handle_, handle); | 321 buffer_size, peer_handle_, handle); |
299 } | 322 } |
300 | 323 |
301 void ChildProcessHost::ListenerHook::OnShutdownRequest() { | 324 void ChildProcessHost::ListenerHook::OnShutdownRequest() { |
302 if (host_->CanShutdown()) | 325 if (host_->CanShutdown()) |
303 host_->Send(new ChildProcessMsg_Shutdown()); | 326 host_->Send(new ChildProcessMsg_Shutdown()); |
304 } | 327 } |
OLD | NEW |