| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/memory/shared_memory_handle.h" | 5 #include "base/memory/shared_memory_handle.h" |
| 6 | 6 |
| 7 #include <mach/mach_vm.h> | 7 #include <mach/mach_vm.h> |
| 8 #include <sys/mman.h> | 8 #include <sys/mman.h> |
| 9 #include <unistd.h> | 9 #include <unistd.h> |
| 10 | 10 |
| 11 #include "base/mac/mac_util.h" | |
| 12 #include "base/posix/eintr_wrapper.h" | 11 #include "base/posix/eintr_wrapper.h" |
| 13 | 12 |
| 14 namespace base { | 13 namespace base { |
| 15 | 14 |
| 16 static_assert(sizeof(SharedMemoryHandle::Type) <= | 15 static_assert(sizeof(SharedMemoryHandle::Type) <= |
| 17 sizeof(SharedMemoryHandle::TypeWireFormat), | 16 sizeof(SharedMemoryHandle::TypeWireFormat), |
| 18 "Size of enum SharedMemoryHandle::Type exceeds size of type " | 17 "Size of enum SharedMemoryHandle::Type exceeds size of type " |
| 19 "transmitted over wire."); | 18 "transmitted over wire."); |
| 20 | 19 |
| 21 SharedMemoryHandle::SharedMemoryHandle() : type_(POSIX), file_descriptor_() {} | 20 SharedMemoryHandle::SharedMemoryHandle() : type_(POSIX), file_descriptor_() {} |
| (...skipping 16 matching lines...) Expand all Loading... |
| 38 &named_right, | 37 &named_right, |
| 39 MACH_PORT_NULL); // Parent handle. | 38 MACH_PORT_NULL); // Parent handle. |
| 40 if (kr != KERN_SUCCESS) { | 39 if (kr != KERN_SUCCESS) { |
| 41 memory_object_ = MACH_PORT_NULL; | 40 memory_object_ = MACH_PORT_NULL; |
| 42 return; | 41 return; |
| 43 } | 42 } |
| 44 | 43 |
| 45 memory_object_ = named_right; | 44 memory_object_ = named_right; |
| 46 size_ = size; | 45 size_ = size; |
| 47 pid_ = GetCurrentProcId(); | 46 pid_ = GetCurrentProcId(); |
| 48 ownership_passes_to_ipc_ = false; | |
| 49 } | 47 } |
| 50 | 48 |
| 51 SharedMemoryHandle::SharedMemoryHandle(mach_port_t memory_object, | 49 SharedMemoryHandle::SharedMemoryHandle(mach_port_t memory_object, |
| 52 mach_vm_size_t size, | 50 mach_vm_size_t size, |
| 53 base::ProcessId pid) | 51 base::ProcessId pid) |
| 54 : type_(MACH), | 52 : type_(MACH), memory_object_(memory_object), size_(size), pid_(pid) {} |
| 55 memory_object_(memory_object), | |
| 56 size_(size), | |
| 57 pid_(pid), | |
| 58 ownership_passes_to_ipc_(false) {} | |
| 59 | 53 |
| 60 SharedMemoryHandle::SharedMemoryHandle(const SharedMemoryHandle& handle) | 54 SharedMemoryHandle::SharedMemoryHandle(const SharedMemoryHandle& handle) |
| 61 : type_(handle.type_) { | 55 : type_(handle.type_) { |
| 62 CopyRelevantData(handle); | 56 CopyRelevantData(handle); |
| 63 } | 57 } |
| 64 | 58 |
| 65 SharedMemoryHandle& SharedMemoryHandle::operator=( | 59 SharedMemoryHandle& SharedMemoryHandle::operator=( |
| 66 const SharedMemoryHandle& handle) { | 60 const SharedMemoryHandle& handle) { |
| 67 if (this == &handle) | 61 if (this == &handle) |
| 68 return *this; | 62 return *this; |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 void** memory, | 164 void** memory, |
| 171 bool read_only) { | 165 bool read_only) { |
| 172 DCHECK(IsValid()); | 166 DCHECK(IsValid()); |
| 173 switch (type_) { | 167 switch (type_) { |
| 174 case SharedMemoryHandle::POSIX: | 168 case SharedMemoryHandle::POSIX: |
| 175 *memory = mmap(nullptr, bytes, PROT_READ | (read_only ? 0 : PROT_WRITE), | 169 *memory = mmap(nullptr, bytes, PROT_READ | (read_only ? 0 : PROT_WRITE), |
| 176 MAP_SHARED, file_descriptor_.fd, offset); | 170 MAP_SHARED, file_descriptor_.fd, offset); |
| 177 | 171 |
| 178 return *memory && *memory != reinterpret_cast<void*>(-1); | 172 return *memory && *memory != reinterpret_cast<void*>(-1); |
| 179 case SharedMemoryHandle::MACH: | 173 case SharedMemoryHandle::MACH: |
| 180 // The flag VM_PROT_IS_MASK is only supported on OSX 10.7+. | |
| 181 DCHECK(mac::IsOSLionOrLater()); | |
| 182 | |
| 183 DCHECK_EQ(pid_, GetCurrentProcId()); | 174 DCHECK_EQ(pid_, GetCurrentProcId()); |
| 184 kern_return_t kr = mach_vm_map( | 175 kern_return_t kr = mach_vm_map( |
| 185 mach_task_self(), | 176 mach_task_self(), |
| 186 reinterpret_cast<mach_vm_address_t*>(memory), // Output parameter | 177 reinterpret_cast<mach_vm_address_t*>(memory), // Output parameter |
| 187 bytes, | 178 bytes, |
| 188 0, // Alignment mask | 179 0, // Alignment mask |
| 189 VM_FLAGS_ANYWHERE, | 180 VM_FLAGS_ANYWHERE, |
| 190 memory_object_, | 181 memory_object_, |
| 191 offset, | 182 offset, |
| 192 FALSE, // Copy | 183 FALSE, // Copy |
| 193 VM_PROT_READ | (read_only ? 0 : VM_PROT_WRITE), // Current protection | 184 VM_PROT_READ | (read_only ? 0 : VM_PROT_WRITE), // Current protection |
| 194 VM_PROT_WRITE | VM_PROT_READ | VM_PROT_IS_MASK, // Maximum protection | 185 VM_PROT_READ | VM_PROT_WRITE, // Maximum protection |
| 195 VM_INHERIT_NONE); | 186 VM_INHERIT_NONE); |
| 196 return kr == KERN_SUCCESS; | 187 return kr == KERN_SUCCESS; |
| 197 } | 188 } |
| 198 } | 189 } |
| 199 | 190 |
| 200 void SharedMemoryHandle::Close() const { | 191 void SharedMemoryHandle::Close() const { |
| 201 if (!IsValid()) | 192 if (!IsValid()) |
| 202 return; | 193 return; |
| 203 | 194 |
| 204 switch (type_) { | 195 switch (type_) { |
| 205 case POSIX: | 196 case POSIX: |
| 206 if (IGNORE_EINTR(close(file_descriptor_.fd)) < 0) | 197 if (IGNORE_EINTR(close(file_descriptor_.fd)) < 0) |
| 207 DPLOG(ERROR) << "Error closing fd."; | 198 DPLOG(ERROR) << "Error closing fd."; |
| 208 break; | 199 break; |
| 209 case MACH: | 200 case MACH: |
| 210 kern_return_t kr = mach_port_deallocate(mach_task_self(), memory_object_); | 201 kern_return_t kr = mach_port_deallocate(mach_task_self(), memory_object_); |
| 211 if (kr != KERN_SUCCESS) | 202 if (kr != KERN_SUCCESS) |
| 212 DPLOG(ERROR) << "Error deallocating mach port: " << kr; | 203 DPLOG(ERROR) << "Error deallocating mach port: " << kr; |
| 213 break; | 204 break; |
| 214 } | 205 } |
| 215 } | 206 } |
| 216 | 207 |
| 217 void SharedMemoryHandle::SetOwnershipPassesToIPC(bool ownership_passes) { | |
| 218 DCHECK_EQ(type_, MACH); | |
| 219 ownership_passes_to_ipc_ = ownership_passes; | |
| 220 } | |
| 221 | |
| 222 bool SharedMemoryHandle::OwnershipPassesToIPC() const { | |
| 223 DCHECK_EQ(type_, MACH); | |
| 224 return ownership_passes_to_ipc_; | |
| 225 } | |
| 226 | |
| 227 void SharedMemoryHandle::CopyRelevantData(const SharedMemoryHandle& handle) { | 208 void SharedMemoryHandle::CopyRelevantData(const SharedMemoryHandle& handle) { |
| 228 switch (type_) { | 209 switch (type_) { |
| 229 case POSIX: | 210 case POSIX: |
| 230 file_descriptor_ = handle.file_descriptor_; | 211 file_descriptor_ = handle.file_descriptor_; |
| 231 break; | 212 break; |
| 232 case MACH: | 213 case MACH: |
| 233 memory_object_ = handle.memory_object_; | 214 memory_object_ = handle.memory_object_; |
| 234 size_ = handle.size_; | 215 size_ = handle.size_; |
| 235 pid_ = handle.pid_; | 216 pid_ = handle.pid_; |
| 236 ownership_passes_to_ipc_ = handle.ownership_passes_to_ipc_; | |
| 237 break; | 217 break; |
| 238 } | 218 } |
| 239 } | 219 } |
| 240 | 220 |
| 241 } // namespace base | 221 } // namespace base |
| OLD | NEW |