OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "mojo/edk/system/channel.h" | 5 #include "mojo/edk/system/channel.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <sys/uio.h> | 8 #include <sys/uio.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 } | 125 } |
126 } | 126 } |
127 if (write_error) { | 127 if (write_error) { |
128 // Do not synchronously invoke OnError(). Write() may have been called by | 128 // Do not synchronously invoke OnError(). Write() may have been called by |
129 // the delegate and we don't want to re-enter it. | 129 // the delegate and we don't want to re-enter it. |
130 io_task_runner_->PostTask(FROM_HERE, | 130 io_task_runner_->PostTask(FROM_HERE, |
131 base::Bind(&ChannelPosix::OnError, this)); | 131 base::Bind(&ChannelPosix::OnError, this)); |
132 } | 132 } |
133 } | 133 } |
134 | 134 |
135 ScopedPlatformHandleVectorPtr GetReadPlatformHandles( | 135 bool GetReadPlatformHandles( |
136 size_t num_handles, | 136 size_t num_handles, |
137 const void* extra_header, | 137 const void* extra_header, |
138 size_t extra_header_size) override { | 138 size_t extra_header_size, |
| 139 ScopedPlatformHandleVectorPtr* handles) override { |
139 #if defined(OS_MACOSX) && !defined(OS_IOS) | 140 #if defined(OS_MACOSX) && !defined(OS_IOS) |
140 // On OSX, we can have mach ports which are located in the extra header | 141 // On OSX, we can have mach ports which are located in the extra header |
141 // section. | 142 // section. |
142 using MachPortsEntry = Channel::Message::MachPortsEntry; | 143 using MachPortsEntry = Channel::Message::MachPortsEntry; |
143 using MachPortsExtraHeader = Channel::Message::MachPortsExtraHeader; | 144 using MachPortsExtraHeader = Channel::Message::MachPortsExtraHeader; |
144 CHECK(extra_header_size >= | 145 CHECK(extra_header_size >= |
145 sizeof(MachPortsExtraHeader) + num_handles * sizeof(MachPortsEntry)); | 146 sizeof(MachPortsExtraHeader) + num_handles * sizeof(MachPortsEntry)); |
146 const MachPortsExtraHeader* mach_ports_header = | 147 const MachPortsExtraHeader* mach_ports_header = |
147 reinterpret_cast<const MachPortsExtraHeader*>(extra_header); | 148 reinterpret_cast<const MachPortsExtraHeader*>(extra_header); |
148 size_t num_mach_ports = mach_ports_header->num_ports; | 149 size_t num_mach_ports = mach_ports_header->num_ports; |
149 CHECK(num_mach_ports <= num_handles); | 150 CHECK(num_mach_ports <= num_handles); |
150 if (incoming_platform_handles_.size() + num_mach_ports < num_handles) | 151 if (incoming_platform_handles_.size() + num_mach_ports < num_handles) { |
151 return nullptr; | 152 handles->reset(); |
| 153 return true; |
| 154 } |
152 | 155 |
153 ScopedPlatformHandleVectorPtr handles( | 156 handles->reset(new PlatformHandleVector(num_handles)); |
154 new PlatformHandleVector(num_handles)); | |
155 const MachPortsEntry* mach_ports = mach_ports_header->entries; | 157 const MachPortsEntry* mach_ports = mach_ports_header->entries; |
156 for (size_t i = 0, mach_port_index = 0; i < num_handles; ++i) { | 158 for (size_t i = 0, mach_port_index = 0; i < num_handles; ++i) { |
157 if (mach_port_index < num_mach_ports && | 159 if (mach_port_index < num_mach_ports && |
158 mach_ports[mach_port_index].index == i) { | 160 mach_ports[mach_port_index].index == i) { |
159 (*handles)[i] = PlatformHandle( | 161 (*handles)->at(i) = PlatformHandle( |
160 static_cast<mach_port_t>(mach_ports[mach_port_index].mach_port)); | 162 static_cast<mach_port_t>(mach_ports[mach_port_index].mach_port)); |
161 CHECK((*handles)[i].type == PlatformHandle::Type::MACH); | 163 CHECK((*handles)->at(i).type == PlatformHandle::Type::MACH); |
162 // These are actually just Mach port names until they're resolved from | 164 // These are actually just Mach port names until they're resolved from |
163 // the remote process. | 165 // the remote process. |
164 (*handles)[i].type = PlatformHandle::Type::MACH_NAME; | 166 (*handles)->at(i).type = PlatformHandle::Type::MACH_NAME; |
165 mach_port_index++; | 167 mach_port_index++; |
166 } else { | 168 } else { |
167 CHECK(!incoming_platform_handles_.empty()); | 169 CHECK(!incoming_platform_handles_.empty()); |
168 (*handles)[i] = incoming_platform_handles_.front(); | 170 (*handles)->at(i) = incoming_platform_handles_.front(); |
169 incoming_platform_handles_.pop_front(); | 171 incoming_platform_handles_.pop_front(); |
170 } | 172 } |
171 } | 173 } |
172 #else | 174 #else |
173 if (incoming_platform_handles_.size() < num_handles) | 175 if (incoming_platform_handles_.size() < num_handles) { |
174 return nullptr; | 176 handles->reset(); |
| 177 return true; |
| 178 } |
175 | 179 |
176 ScopedPlatformHandleVectorPtr handles( | 180 handles->reset(new PlatformHandleVector(num_handles)); |
177 new PlatformHandleVector(num_handles)); | |
178 for (size_t i = 0; i < num_handles; ++i) { | 181 for (size_t i = 0; i < num_handles; ++i) { |
179 (*handles)[i] = incoming_platform_handles_.front(); | 182 (*handles)->at(i) = incoming_platform_handles_.front(); |
180 incoming_platform_handles_.pop_front(); | 183 incoming_platform_handles_.pop_front(); |
181 } | 184 } |
182 #endif | 185 #endif |
183 | 186 |
184 return handles; | 187 return true; |
185 } | 188 } |
186 | 189 |
187 private: | 190 private: |
188 ~ChannelPosix() override { | 191 ~ChannelPosix() override { |
189 DCHECK(!read_watcher_); | 192 DCHECK(!read_watcher_); |
190 DCHECK(!write_watcher_); | 193 DCHECK(!write_watcher_); |
191 for (auto handle : incoming_platform_handles_) | 194 for (auto handle : incoming_platform_handles_) |
192 handle.CloseIfNecessary(); | 195 handle.CloseIfNecessary(); |
193 } | 196 } |
194 | 197 |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 messages.pop_back(); | 391 messages.pop_back(); |
389 } | 392 } |
390 return true; | 393 return true; |
391 } | 394 } |
392 } | 395 } |
393 | 396 |
394 return true; | 397 return true; |
395 } | 398 } |
396 | 399 |
397 #if defined(OS_MACOSX) | 400 #if defined(OS_MACOSX) |
398 void OnControlMessage(Message::Header::MessageType message_type, | 401 bool OnControlMessage(Message::Header::MessageType message_type, |
399 const void* payload, | 402 const void* payload, |
400 size_t payload_size, | 403 size_t payload_size, |
401 ScopedPlatformHandleVectorPtr handles) override { | 404 ScopedPlatformHandleVectorPtr handles) override { |
402 switch (message_type) { | 405 switch (message_type) { |
403 case Message::Header::MessageType::HANDLES_SENT: { | 406 case Message::Header::MessageType::HANDLES_SENT: { |
| 407 if (payload_size == 0) |
| 408 break; |
404 MessagePtr message(new Channel::Message( | 409 MessagePtr message(new Channel::Message( |
405 payload_size, 0, Message::Header::MessageType::HANDLES_SENT_ACK)); | 410 payload_size, 0, Message::Header::MessageType::HANDLES_SENT_ACK)); |
406 memcpy(message->mutable_payload(), payload, payload_size); | 411 memcpy(message->mutable_payload(), payload, payload_size); |
407 Write(std::move(message)); | 412 Write(std::move(message)); |
| 413 return true; |
| 414 } |
| 415 |
| 416 case Message::Header::MessageType::HANDLES_SENT_ACK: { |
| 417 size_t num_fds = payload_size / sizeof(int); |
| 418 if (num_fds == 0 || payload_size % sizeof(int) != 0) |
| 419 break; |
| 420 |
| 421 const int* fds = reinterpret_cast<const int*>(payload); |
| 422 if (!CloseHandles(fds, num_fds)) |
| 423 break; |
| 424 return true; |
| 425 } |
| 426 |
| 427 default: |
408 break; | 428 break; |
409 } | |
410 case Message::Header::MessageType::HANDLES_SENT_ACK: { | |
411 const int* fds = reinterpret_cast<const int*>(payload); | |
412 size_t num_fds = payload_size / sizeof(*fds); | |
413 if (payload_size % sizeof(*fds) != 0 || !CloseHandles(fds, num_fds)) { | |
414 io_task_runner_->PostTask(FROM_HERE, | |
415 base::Bind(&ChannelPosix::OnError, this)); | |
416 } | |
417 break; | |
418 } | |
419 default: | |
420 NOTREACHED(); | |
421 } | 429 } |
| 430 |
| 431 return false; |
422 } | 432 } |
423 | 433 |
424 // Closes handles referenced by |fds|. Returns false if |num_fds| is 0, or if | 434 // Closes handles referenced by |fds|. Returns false if |num_fds| is 0, or if |
425 // |fds| does not match a sequence of handles in |handles_to_close_|. | 435 // |fds| does not match a sequence of handles in |handles_to_close_|. |
426 bool CloseHandles(const int* fds, size_t num_fds) { | 436 bool CloseHandles(const int* fds, size_t num_fds) { |
427 base::AutoLock l(handles_to_close_lock_); | 437 base::AutoLock l(handles_to_close_lock_); |
428 if (!num_fds) | 438 if (!num_fds) |
429 return false; | 439 return false; |
430 | 440 |
431 auto start = | 441 auto start = |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 // static | 495 // static |
486 scoped_refptr<Channel> Channel::Create( | 496 scoped_refptr<Channel> Channel::Create( |
487 Delegate* delegate, | 497 Delegate* delegate, |
488 ScopedPlatformHandle platform_handle, | 498 ScopedPlatformHandle platform_handle, |
489 scoped_refptr<base::TaskRunner> io_task_runner) { | 499 scoped_refptr<base::TaskRunner> io_task_runner) { |
490 return new ChannelPosix(delegate, std::move(platform_handle), io_task_runner); | 500 return new ChannelPosix(delegate, std::move(platform_handle), io_task_runner); |
491 } | 501 } |
492 | 502 |
493 } // namespace edk | 503 } // namespace edk |
494 } // namespace mojo | 504 } // namespace mojo |
OLD | NEW |