OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "mojo/edk/system/mach_port_relay.h" |
| 6 |
| 7 #include <mach/mach.h> |
| 8 |
| 9 #include <utility> |
| 10 |
| 11 #include "base/logging.h" |
| 12 #include "base/mac/mach_port_util.h" |
| 13 #include "base/mac/scoped_mach_port.h" |
| 14 #include "base/process/process.h" |
| 15 #include "mojo/edk/embedder/platform_handle_vector.h" |
| 16 |
| 17 namespace mojo { |
| 18 namespace edk { |
| 19 |
| 20 // static |
| 21 bool MachPortRelay::ReceivePorts(PlatformHandleVector* handles) { |
| 22 DCHECK(handles); |
| 23 |
| 24 for (size_t i = 0; i < handles->size(); i++) { |
| 25 PlatformHandle* handle = handles->data() + i; |
| 26 DCHECK(handle->type != PlatformHandle::Type::MACH); |
| 27 if (handle->type != PlatformHandle::Type::MACH_NAME) |
| 28 continue; |
| 29 |
| 30 base::mac::ScopedMachReceiveRight message_port(handle->port); |
| 31 base::mac::ScopedMachSendRight received_port( |
| 32 base::ReceiveMachPort(message_port.get())); |
| 33 if (received_port.get() == MACH_PORT_NULL) { |
| 34 handle->port = MACH_PORT_NULL; |
| 35 LOG(ERROR) << "Error receiving mach port"; |
| 36 return false; |
| 37 } |
| 38 |
| 39 handle->port = received_port.release(); |
| 40 handle->type = PlatformHandle::Type::MACH; |
| 41 } |
| 42 |
| 43 return true; |
| 44 } |
| 45 |
| 46 MachPortRelay::MachPortRelay(base::PortProvider* port_provider) |
| 47 : port_provider_(port_provider) { |
| 48 DCHECK(port_provider); |
| 49 port_provider_->AddObserver(this); |
| 50 } |
| 51 |
| 52 MachPortRelay::~MachPortRelay() { |
| 53 port_provider_->RemoveObserver(this); |
| 54 } |
| 55 |
| 56 bool MachPortRelay::SendPortsToProcess(Channel::Message* message, |
| 57 base::ProcessHandle process) { |
| 58 DCHECK(message); |
| 59 mach_port_t task_port = port_provider_->TaskForPid(process); |
| 60 if (task_port == MACH_PORT_NULL) |
| 61 return false; |
| 62 |
| 63 size_t num_sent = 0; |
| 64 bool error = false; |
| 65 ScopedPlatformHandleVectorPtr handles = message->TakeHandles(); |
| 66 // Message should have handles, otherwise there's no point in calling this |
| 67 // function. |
| 68 DCHECK(handles); |
| 69 for (size_t i = 0; i < handles->size(); i++) { |
| 70 PlatformHandle* handle = &(*handles)[i]; |
| 71 DCHECK(handle->type != PlatformHandle::Type::MACH_NAME); |
| 72 if (handle->type != PlatformHandle::Type::MACH) |
| 73 continue; |
| 74 |
| 75 mach_port_name_t intermediate_port; |
| 76 DCHECK(handle->port != MACH_PORT_NULL); |
| 77 intermediate_port = base::CreateIntermediateMachPort( |
| 78 task_port, base::mac::ScopedMachSendRight(handle->port), nullptr); |
| 79 if (intermediate_port == MACH_PORT_NULL) { |
| 80 handle->port = MACH_PORT_NULL; |
| 81 error = true; |
| 82 break; |
| 83 } |
| 84 handle->port = intermediate_port; |
| 85 handle->type = PlatformHandle::Type::MACH_NAME; |
| 86 num_sent++; |
| 87 } |
| 88 DCHECK(error || num_sent); |
| 89 message->SetHandles(std::move(handles)); |
| 90 |
| 91 return !error; |
| 92 } |
| 93 |
| 94 bool MachPortRelay::ExtractPortRights(Channel::Message* message, |
| 95 base::ProcessHandle process) { |
| 96 DCHECK(message); |
| 97 |
| 98 mach_port_t task_port = port_provider_->TaskForPid(process); |
| 99 if (task_port == MACH_PORT_NULL) |
| 100 return false; |
| 101 |
| 102 size_t num_received = 0; |
| 103 bool error = false; |
| 104 ScopedPlatformHandleVectorPtr handles = message->TakeHandles(); |
| 105 // Message should have handles, otherwise there's no point in calling this |
| 106 // function. |
| 107 DCHECK(handles); |
| 108 for (size_t i = 0; i < handles->size(); i++) { |
| 109 PlatformHandle* handle = handles->data() + i; |
| 110 DCHECK(handle->type != PlatformHandle::Type::MACH); |
| 111 if (handle->type != PlatformHandle::Type::MACH_NAME) |
| 112 continue; |
| 113 |
| 114 mach_port_t extracted_right = MACH_PORT_NULL; |
| 115 mach_msg_type_name_t extracted_right_type; |
| 116 kern_return_t kr = |
| 117 mach_port_extract_right(task_port, handle->port, |
| 118 MACH_MSG_TYPE_MOVE_SEND, |
| 119 &extracted_right, &extracted_right_type); |
| 120 if (kr != KERN_SUCCESS) { |
| 121 error = true; |
| 122 break; |
| 123 } |
| 124 |
| 125 DCHECK_EQ(static_cast<mach_msg_type_name_t>(MACH_MSG_TYPE_PORT_SEND), |
| 126 extracted_right_type); |
| 127 handle->port = extracted_right; |
| 128 handle->type = PlatformHandle::Type::MACH; |
| 129 num_received++; |
| 130 } |
| 131 DCHECK(error || num_received); |
| 132 message->SetHandles(std::move(handles)); |
| 133 |
| 134 return !error; |
| 135 } |
| 136 |
| 137 void MachPortRelay::AddObserver(Observer* observer) { |
| 138 base::AutoLock locker(observers_lock_); |
| 139 bool inserted = observers_.insert(observer).second; |
| 140 DCHECK(inserted); |
| 141 } |
| 142 |
| 143 void MachPortRelay::RemoveObserver(Observer* observer) { |
| 144 base::AutoLock locker(observers_lock_); |
| 145 observers_.erase(observer); |
| 146 } |
| 147 |
| 148 void MachPortRelay::OnReceivedTaskPort(base::ProcessHandle process) { |
| 149 base::AutoLock locker(observers_lock_); |
| 150 for (const auto observer : observers_) |
| 151 observer->OnProcessReady(process); |
| 152 } |
| 153 |
| 154 } // namespace edk |
| 155 } // namespace mojo |
OLD | NEW |