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