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 <stddef.h> | 8 #include <stddef.h> |
9 #include <sys/mman.h> | 9 #include <sys/mman.h> |
10 #include <unistd.h> | 10 #include <unistd.h> |
11 | 11 |
12 #include "base/mac/mac_util.h" | 12 #include "base/mac/mac_util.h" |
13 #include "base/mac/mach_logging.h" | 13 #include "base/mac/mach_logging.h" |
14 #include "base/posix/eintr_wrapper.h" | 14 #include "base/posix/eintr_wrapper.h" |
| 15 #include "base/unguessable_token.h" |
15 | 16 |
16 namespace base { | 17 namespace base { |
17 | 18 |
18 SharedMemoryHandle::SharedMemoryHandle() | 19 SharedMemoryHandle::SharedMemoryHandle() |
19 : type_(MACH), memory_object_(MACH_PORT_NULL) {} | 20 : type_(MACH), memory_object_(MACH_PORT_NULL) {} |
20 | 21 |
21 SharedMemoryHandle::SharedMemoryHandle( | 22 SharedMemoryHandle::SharedMemoryHandle( |
22 const base::FileDescriptor& file_descriptor) | 23 const base::FileDescriptor& file_descriptor, |
23 : type_(POSIX), file_descriptor_(file_descriptor) {} | 24 const base::UnguessableToken& guid) |
| 25 : type_(POSIX), file_descriptor_(file_descriptor), guid_(guid) {} |
24 | 26 |
25 SharedMemoryHandle::SharedMemoryHandle(mach_vm_size_t size) { | 27 SharedMemoryHandle::SharedMemoryHandle(mach_vm_size_t size, |
| 28 const base::UnguessableToken& guid) { |
26 type_ = MACH; | 29 type_ = MACH; |
27 mach_port_t named_right; | 30 mach_port_t named_right; |
28 kern_return_t kr = mach_make_memory_entry_64( | 31 kern_return_t kr = mach_make_memory_entry_64( |
29 mach_task_self(), | 32 mach_task_self(), |
30 &size, | 33 &size, |
31 0, // Address. | 34 0, // Address. |
32 MAP_MEM_NAMED_CREATE | VM_PROT_READ | VM_PROT_WRITE, | 35 MAP_MEM_NAMED_CREATE | VM_PROT_READ | VM_PROT_WRITE, |
33 &named_right, | 36 &named_right, |
34 MACH_PORT_NULL); // Parent handle. | 37 MACH_PORT_NULL); // Parent handle. |
35 if (kr != KERN_SUCCESS) { | 38 if (kr != KERN_SUCCESS) { |
36 memory_object_ = MACH_PORT_NULL; | 39 memory_object_ = MACH_PORT_NULL; |
37 return; | 40 return; |
38 } | 41 } |
39 | 42 |
40 memory_object_ = named_right; | 43 memory_object_ = named_right; |
41 size_ = size; | 44 size_ = size; |
42 ownership_passes_to_ipc_ = false; | 45 ownership_passes_to_ipc_ = false; |
| 46 guid_ = guid; |
43 } | 47 } |
44 | 48 |
45 SharedMemoryHandle::SharedMemoryHandle(mach_port_t memory_object, | 49 SharedMemoryHandle::SharedMemoryHandle(mach_port_t memory_object, |
46 mach_vm_size_t size) | 50 mach_vm_size_t size, |
| 51 const base::UnguessableToken& guid) |
47 : type_(MACH), | 52 : type_(MACH), |
48 memory_object_(memory_object), | 53 memory_object_(memory_object), |
49 size_(size), | 54 size_(size), |
50 ownership_passes_to_ipc_(false) {} | 55 ownership_passes_to_ipc_(false), |
51 | 56 guid_(guid) {} |
52 SharedMemoryHandle::SharedMemoryHandle(const SharedMemoryHandle& handle) { | |
53 CopyRelevantData(handle); | |
54 } | |
55 | |
56 SharedMemoryHandle& SharedMemoryHandle::operator=( | |
57 const SharedMemoryHandle& handle) { | |
58 if (this == &handle) | |
59 return *this; | |
60 | |
61 type_ = handle.type_; | |
62 CopyRelevantData(handle); | |
63 return *this; | |
64 } | |
65 | 57 |
66 SharedMemoryHandle SharedMemoryHandle::Duplicate() const { | 58 SharedMemoryHandle SharedMemoryHandle::Duplicate() const { |
67 switch (type_) { | 59 switch (type_) { |
68 case POSIX: { | 60 case POSIX: { |
69 if (!IsValid()) | 61 if (!IsValid()) |
70 return SharedMemoryHandle(); | 62 return SharedMemoryHandle(); |
71 int duped_fd = HANDLE_EINTR(dup(file_descriptor_.fd)); | 63 int duped_fd = HANDLE_EINTR(dup(file_descriptor_.fd)); |
72 if (duped_fd < 0) | 64 if (duped_fd < 0) |
73 return SharedMemoryHandle(); | 65 return SharedMemoryHandle(); |
74 return SharedMemoryHandle(FileDescriptor(duped_fd, true)); | 66 return SharedMemoryHandle(FileDescriptor(duped_fd, true), guid_); |
75 } | 67 } |
76 case MACH: { | 68 case MACH: { |
77 if (!IsValid()) | 69 if (!IsValid()) |
78 return SharedMemoryHandle(); | 70 return SharedMemoryHandle(); |
79 | 71 |
80 // Increment the ref count. | 72 // Increment the ref count. |
81 kern_return_t kr = mach_port_mod_refs(mach_task_self(), memory_object_, | 73 kern_return_t kr = mach_port_mod_refs(mach_task_self(), memory_object_, |
82 MACH_PORT_RIGHT_SEND, 1); | 74 MACH_PORT_RIGHT_SEND, 1); |
83 DCHECK_EQ(kr, KERN_SUCCESS); | 75 DCHECK_EQ(kr, KERN_SUCCESS); |
84 SharedMemoryHandle handle(*this); | 76 SharedMemoryHandle handle(*this); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 void SharedMemoryHandle::SetOwnershipPassesToIPC(bool ownership_passes) { | 162 void SharedMemoryHandle::SetOwnershipPassesToIPC(bool ownership_passes) { |
171 DCHECK_EQ(type_, MACH); | 163 DCHECK_EQ(type_, MACH); |
172 ownership_passes_to_ipc_ = ownership_passes; | 164 ownership_passes_to_ipc_ = ownership_passes; |
173 } | 165 } |
174 | 166 |
175 bool SharedMemoryHandle::OwnershipPassesToIPC() const { | 167 bool SharedMemoryHandle::OwnershipPassesToIPC() const { |
176 DCHECK_EQ(type_, MACH); | 168 DCHECK_EQ(type_, MACH); |
177 return ownership_passes_to_ipc_; | 169 return ownership_passes_to_ipc_; |
178 } | 170 } |
179 | 171 |
180 void SharedMemoryHandle::CopyRelevantData(const SharedMemoryHandle& handle) { | |
181 type_ = handle.type_; | |
182 switch (type_) { | |
183 case POSIX: | |
184 file_descriptor_ = handle.file_descriptor_; | |
185 break; | |
186 case MACH: | |
187 memory_object_ = handle.memory_object_; | |
188 size_ = handle.size_; | |
189 ownership_passes_to_ipc_ = handle.ownership_passes_to_ipc_; | |
190 break; | |
191 } | |
192 } | |
193 | |
194 } // namespace base | 172 } // namespace base |
OLD | NEW |