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> |
(...skipping 21 matching lines...) Expand all Loading... |
32 MAP_MEM_NAMED_CREATE | VM_PROT_READ | VM_PROT_WRITE, | 32 MAP_MEM_NAMED_CREATE | VM_PROT_READ | VM_PROT_WRITE, |
33 &named_right, | 33 &named_right, |
34 MACH_PORT_NULL); // Parent handle. | 34 MACH_PORT_NULL); // Parent handle. |
35 if (kr != KERN_SUCCESS) { | 35 if (kr != KERN_SUCCESS) { |
36 memory_object_ = MACH_PORT_NULL; | 36 memory_object_ = MACH_PORT_NULL; |
37 return; | 37 return; |
38 } | 38 } |
39 | 39 |
40 memory_object_ = named_right; | 40 memory_object_ = named_right; |
41 size_ = size; | 41 size_ = size; |
42 pid_ = GetCurrentProcId(); | |
43 ownership_passes_to_ipc_ = false; | 42 ownership_passes_to_ipc_ = false; |
44 } | 43 } |
45 | 44 |
46 SharedMemoryHandle::SharedMemoryHandle(mach_port_t memory_object, | 45 SharedMemoryHandle::SharedMemoryHandle(mach_port_t memory_object, |
47 mach_vm_size_t size, | 46 mach_vm_size_t size) |
48 base::ProcessId pid) | |
49 : type_(MACH), | 47 : type_(MACH), |
50 memory_object_(memory_object), | 48 memory_object_(memory_object), |
51 size_(size), | 49 size_(size), |
52 pid_(pid), | |
53 ownership_passes_to_ipc_(false) {} | 50 ownership_passes_to_ipc_(false) {} |
54 | 51 |
55 SharedMemoryHandle::SharedMemoryHandle(const SharedMemoryHandle& handle) { | 52 SharedMemoryHandle::SharedMemoryHandle(const SharedMemoryHandle& handle) { |
56 CopyRelevantData(handle); | 53 CopyRelevantData(handle); |
57 } | 54 } |
58 | 55 |
59 SharedMemoryHandle& SharedMemoryHandle::operator=( | 56 SharedMemoryHandle& SharedMemoryHandle::operator=( |
60 const SharedMemoryHandle& handle) { | 57 const SharedMemoryHandle& handle) { |
61 if (this == &handle) | 58 if (this == &handle) |
62 return *this; | 59 return *this; |
63 | 60 |
64 type_ = handle.type_; | 61 type_ = handle.type_; |
65 CopyRelevantData(handle); | 62 CopyRelevantData(handle); |
66 return *this; | 63 return *this; |
67 } | 64 } |
68 | 65 |
69 SharedMemoryHandle SharedMemoryHandle::Duplicate() const { | 66 SharedMemoryHandle SharedMemoryHandle::Duplicate() const { |
70 switch (type_) { | 67 switch (type_) { |
71 case POSIX: { | 68 case POSIX: { |
72 if (!IsValid()) | 69 if (!IsValid()) |
73 return SharedMemoryHandle(); | 70 return SharedMemoryHandle(); |
74 int duped_fd = HANDLE_EINTR(dup(file_descriptor_.fd)); | 71 int duped_fd = HANDLE_EINTR(dup(file_descriptor_.fd)); |
75 if (duped_fd < 0) | 72 if (duped_fd < 0) |
76 return SharedMemoryHandle(); | 73 return SharedMemoryHandle(); |
77 return SharedMemoryHandle(FileDescriptor(duped_fd, true)); | 74 return SharedMemoryHandle(FileDescriptor(duped_fd, true)); |
78 } | 75 } |
79 case MACH: { | 76 case MACH: { |
80 if (!IsValid()) | 77 if (!IsValid()) |
81 return SharedMemoryHandle(MACH_PORT_NULL, 0, 0); | 78 return SharedMemoryHandle(); |
82 | 79 |
83 // Increment the ref count. | 80 // Increment the ref count. |
84 kern_return_t kr = mach_port_mod_refs(mach_task_self(), memory_object_, | 81 kern_return_t kr = mach_port_mod_refs(mach_task_self(), memory_object_, |
85 MACH_PORT_RIGHT_SEND, 1); | 82 MACH_PORT_RIGHT_SEND, 1); |
86 DCHECK_EQ(kr, KERN_SUCCESS); | 83 DCHECK_EQ(kr, KERN_SUCCESS); |
87 SharedMemoryHandle handle(*this); | 84 SharedMemoryHandle handle(*this); |
88 handle.SetOwnershipPassesToIPC(true); | 85 handle.SetOwnershipPassesToIPC(true); |
89 return handle; | 86 return handle; |
90 } | 87 } |
91 } | 88 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 size_t bytes, | 127 size_t bytes, |
131 void** memory, | 128 void** memory, |
132 bool read_only) { | 129 bool read_only) { |
133 DCHECK(IsValid()); | 130 DCHECK(IsValid()); |
134 switch (type_) { | 131 switch (type_) { |
135 case SharedMemoryHandle::POSIX: | 132 case SharedMemoryHandle::POSIX: |
136 *memory = mmap(nullptr, bytes, PROT_READ | (read_only ? 0 : PROT_WRITE), | 133 *memory = mmap(nullptr, bytes, PROT_READ | (read_only ? 0 : PROT_WRITE), |
137 MAP_SHARED, file_descriptor_.fd, offset); | 134 MAP_SHARED, file_descriptor_.fd, offset); |
138 return *memory != MAP_FAILED; | 135 return *memory != MAP_FAILED; |
139 case SharedMemoryHandle::MACH: | 136 case SharedMemoryHandle::MACH: |
140 DCHECK_EQ(pid_, GetCurrentProcId()); | |
141 kern_return_t kr = mach_vm_map( | 137 kern_return_t kr = mach_vm_map( |
142 mach_task_self(), | 138 mach_task_self(), |
143 reinterpret_cast<mach_vm_address_t*>(memory), // Output parameter | 139 reinterpret_cast<mach_vm_address_t*>(memory), // Output parameter |
144 bytes, | 140 bytes, |
145 0, // Alignment mask | 141 0, // Alignment mask |
146 VM_FLAGS_ANYWHERE, | 142 VM_FLAGS_ANYWHERE, |
147 memory_object_, | 143 memory_object_, |
148 offset, | 144 offset, |
149 FALSE, // Copy | 145 FALSE, // Copy |
150 VM_PROT_READ | (read_only ? 0 : VM_PROT_WRITE), // Current protection | 146 VM_PROT_READ | (read_only ? 0 : VM_PROT_WRITE), // Current protection |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 | 179 |
184 void SharedMemoryHandle::CopyRelevantData(const SharedMemoryHandle& handle) { | 180 void SharedMemoryHandle::CopyRelevantData(const SharedMemoryHandle& handle) { |
185 type_ = handle.type_; | 181 type_ = handle.type_; |
186 switch (type_) { | 182 switch (type_) { |
187 case POSIX: | 183 case POSIX: |
188 file_descriptor_ = handle.file_descriptor_; | 184 file_descriptor_ = handle.file_descriptor_; |
189 break; | 185 break; |
190 case MACH: | 186 case MACH: |
191 memory_object_ = handle.memory_object_; | 187 memory_object_ = handle.memory_object_; |
192 size_ = handle.size_; | 188 size_ = handle.size_; |
193 pid_ = handle.pid_; | |
194 ownership_passes_to_ipc_ = handle.ownership_passes_to_ipc_; | 189 ownership_passes_to_ipc_ = handle.ownership_passes_to_ipc_; |
195 break; | 190 break; |
196 } | 191 } |
197 } | 192 } |
198 | 193 |
199 } // namespace base | 194 } // namespace base |
OLD | NEW |