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