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 "ipc/ipc_channel_nacl.h" | 5 #include "ipc/ipc_channel_nacl.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
11 | 11 |
12 #include <algorithm> | 12 #include <algorithm> |
13 | 13 |
14 #include "base/bind.h" | 14 #include "base/bind.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
18 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
19 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
20 #include "base/task_runner_util.h" | 20 #include "base/task_runner_util.h" |
21 #include "base/threading/simple_thread.h" | 21 #include "base/threading/simple_thread.h" |
22 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
23 #include "ipc/ipc_listener.h" | 23 #include "ipc/ipc_listener.h" |
24 #include "ipc/ipc_logging.h" | 24 #include "ipc/ipc_logging.h" |
25 #include "ipc/ipc_message_attachment_set.h" | 25 #include "ipc/ipc_message_attachment_set.h" |
| 26 #include "ipc/ipc_platform_file_attachment_posix.h" |
26 #include "native_client/src/public/imc_syscalls.h" | 27 #include "native_client/src/public/imc_syscalls.h" |
27 #include "native_client/src/public/imc_types.h" | 28 #include "native_client/src/public/imc_types.h" |
28 | 29 |
29 namespace IPC { | 30 namespace IPC { |
30 | 31 |
31 struct MessageContents { | 32 struct MessageContents { |
32 std::vector<char> data; | 33 std::vector<char> data; |
33 std::vector<int> fds; | 34 std::vector<int> fds; |
34 }; | 35 }; |
35 | 36 |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 void ChannelNacl::DidRecvMsg(std::unique_ptr<MessageContents> contents) { | 217 void ChannelNacl::DidRecvMsg(std::unique_ptr<MessageContents> contents) { |
217 // Close sets the pipe to -1. It's possible we'll get a buffer sent to us from | 218 // Close sets the pipe to -1. It's possible we'll get a buffer sent to us from |
218 // the reader thread after Close is called. If so, we ignore it. | 219 // the reader thread after Close is called. If so, we ignore it. |
219 if (pipe_ == -1) | 220 if (pipe_ == -1) |
220 return; | 221 return; |
221 | 222 |
222 linked_ptr<std::vector<char> > data(new std::vector<char>); | 223 linked_ptr<std::vector<char> > data(new std::vector<char>); |
223 data->swap(contents->data); | 224 data->swap(contents->data); |
224 read_queue_.push_back(data); | 225 read_queue_.push_back(data); |
225 | 226 |
226 input_fds_.insert(input_fds_.end(), | 227 input_attachments_.reserve(contents->fds.size()); |
227 contents->fds.begin(), contents->fds.end()); | 228 for (int fd : contents->fds) { |
| 229 input_attachments_.push_back( |
| 230 new internal::PlatformFileAttachment(base::ScopedFD(fd))); |
| 231 } |
228 contents->fds.clear(); | 232 contents->fds.clear(); |
229 | 233 |
230 // In POSIX, we would be told when there are bytes to read by implementing | 234 // In POSIX, we would be told when there are bytes to read by implementing |
231 // OnFileCanReadWithoutBlocking in MessageLoopForIO::Watcher. In NaCl, we | 235 // OnFileCanReadWithoutBlocking in MessageLoopForIO::Watcher. In NaCl, we |
232 // instead know at this point because the reader thread posted some data to | 236 // instead know at this point because the reader thread posted some data to |
233 // us. | 237 // us. |
234 ProcessIncomingMessages(); | 238 ProcessIncomingMessages(); |
235 } | 239 } |
236 | 240 |
237 void ChannelNacl::ReadDidFail() { | 241 void ChannelNacl::ReadDidFail() { |
(...skipping 27 matching lines...) Expand all Loading... |
265 | 269 |
266 if (pipe_ == -1) | 270 if (pipe_ == -1) |
267 return false; | 271 return false; |
268 | 272 |
269 // Write out all the messages. The trusted implementation is guaranteed to not | 273 // Write out all the messages. The trusted implementation is guaranteed to not |
270 // block. See NaClIPCAdapter::Send for the implementation of imc_sendmsg. | 274 // block. See NaClIPCAdapter::Send for the implementation of imc_sendmsg. |
271 while (!output_queue_.empty()) { | 275 while (!output_queue_.empty()) { |
272 linked_ptr<Message> msg = output_queue_.front(); | 276 linked_ptr<Message> msg = output_queue_.front(); |
273 output_queue_.pop_front(); | 277 output_queue_.pop_front(); |
274 | 278 |
275 int fds[MessageAttachmentSet::kMaxDescriptorsPerMessage]; | 279 const size_t num_fds = msg->attachment_set()->size(); |
276 const size_t num_fds = | |
277 msg->attachment_set()->num_non_brokerable_attachments(); | |
278 DCHECK(num_fds <= MessageAttachmentSet::kMaxDescriptorsPerMessage); | 280 DCHECK(num_fds <= MessageAttachmentSet::kMaxDescriptorsPerMessage); |
279 msg->attachment_set()->PeekDescriptors(fds); | 281 std::vector<int> fds; |
| 282 fds.reserve(num_fds); |
| 283 for (size_t i = 0; i < num_fds; i++) { |
| 284 scoped_refptr<MessageAttachment> attachment = |
| 285 msg->attachment_set()->GetAttachmentAt(i); |
| 286 DCHECK_EQ(MessageAttachment::Type::PLATFORM_FILE, attachment->GetType()); |
| 287 fds.push_back(static_cast<internal::PlatformFileAttachment&>(*attachment) |
| 288 .TakePlatformFile()); |
| 289 } |
280 | 290 |
281 NaClAbiNaClImcMsgIoVec iov = { | 291 NaClAbiNaClImcMsgIoVec iov = { |
282 const_cast<void*>(msg->data()), msg->size() | 292 const_cast<void*>(msg->data()), msg->size() |
283 }; | 293 }; |
284 NaClAbiNaClImcMsgHdr msgh = { &iov, 1, fds, num_fds }; | 294 NaClAbiNaClImcMsgHdr msgh = {&iov, 1, fds.data(), num_fds}; |
285 ssize_t bytes_written = imc_sendmsg(pipe_, &msgh, 0); | 295 ssize_t bytes_written = imc_sendmsg(pipe_, &msgh, 0); |
286 | 296 |
287 DCHECK(bytes_written); // The trusted side shouldn't return 0. | 297 DCHECK(bytes_written); // The trusted side shouldn't return 0. |
288 if (bytes_written < 0) { | 298 if (bytes_written < 0) { |
289 // The trusted side should only ever give us an error of EPIPE. We | 299 // The trusted side should only ever give us an error of EPIPE. We |
290 // should never be interrupted, nor should we get EAGAIN. | 300 // should never be interrupted, nor should we get EAGAIN. |
291 DCHECK(errno == EPIPE); | 301 DCHECK(errno == EPIPE); |
292 Close(); | 302 Close(); |
293 PLOG(ERROR) << "pipe_ error on " | 303 PLOG(ERROR) << "pipe_ error on " |
294 << pipe_ | 304 << pipe_ |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 *bytes_read += bytes_to_read; | 348 *bytes_read += bytes_to_read; |
339 } | 349 } |
340 } | 350 } |
341 return READ_SUCCEEDED; | 351 return READ_SUCCEEDED; |
342 } | 352 } |
343 | 353 |
344 bool ChannelNacl::ShouldDispatchInputMessage(Message* msg) { | 354 bool ChannelNacl::ShouldDispatchInputMessage(Message* msg) { |
345 return true; | 355 return true; |
346 } | 356 } |
347 | 357 |
348 bool ChannelNacl::GetNonBrokeredAttachments(Message* msg) { | 358 bool ChannelNacl::GetAttachments(Message* msg) { |
349 uint16_t header_fds = msg->header()->num_fds; | 359 uint16_t header_fds = msg->header()->num_fds; |
350 CHECK(header_fds == input_fds_.size()); | 360 CHECK(header_fds == input_attachments_.size()); |
351 if (header_fds == 0) | 361 if (header_fds == 0) |
352 return true; // Nothing to do. | 362 return true; // Nothing to do. |
353 | 363 |
354 // The shenaniganery below with &foo.front() requires input_fds_ to have | 364 for (auto& attachment : input_attachments_) { |
355 // contiguous underlying storage (such as a simple array or a std::vector). | 365 msg->attachment_set()->AddAttachment(std::move(attachment)); |
356 // This is why the header warns not to make input_fds_ a deque<>. | 366 } |
357 msg->attachment_set()->AddDescriptorsToOwn(&input_fds_.front(), header_fds); | 367 input_attachments_.clear(); |
358 input_fds_.clear(); | |
359 return true; | 368 return true; |
360 } | 369 } |
361 | 370 |
362 bool ChannelNacl::DidEmptyInputBuffers() { | 371 bool ChannelNacl::DidEmptyInputBuffers() { |
363 // When the input data buffer is empty, the fds should be too. | 372 // When the input data buffer is empty, the attachments should be too. |
364 return input_fds_.empty(); | 373 return input_attachments_.empty(); |
365 } | 374 } |
366 | 375 |
367 void ChannelNacl::HandleInternalMessage(const Message& msg) { | 376 void ChannelNacl::HandleInternalMessage(const Message& msg) { |
368 // The trusted side IPC::Channel should handle the "hello" handshake; we | 377 // The trusted side IPC::Channel should handle the "hello" handshake; we |
369 // should not receive the "Hello" message. | 378 // should not receive the "Hello" message. |
370 NOTREACHED(); | 379 NOTREACHED(); |
371 } | 380 } |
372 | 381 |
373 // Channel's methods | 382 // Channel's methods |
374 | 383 |
375 // static | 384 // static |
376 std::unique_ptr<Channel> Channel::Create( | 385 std::unique_ptr<Channel> Channel::Create( |
377 const IPC::ChannelHandle& channel_handle, | 386 const IPC::ChannelHandle& channel_handle, |
378 Mode mode, | 387 Mode mode, |
379 Listener* listener) { | 388 Listener* listener) { |
380 return base::WrapUnique(new ChannelNacl(channel_handle, mode, listener)); | 389 return base::WrapUnique(new ChannelNacl(channel_handle, mode, listener)); |
381 } | 390 } |
382 | 391 |
383 } // namespace IPC | 392 } // namespace IPC |
OLD | NEW |