OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef BASE_MEMORY_SHARED_MEMORY_H_ | 5 #ifndef BASE_MEMORY_SHARED_MEMORY_H_ |
6 #define BASE_MEMORY_SHARED_MEMORY_H_ | 6 #define BASE_MEMORY_SHARED_MEMORY_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <string> | 10 #include <string> |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 // Similar to the default constructor, except that this allows for | 73 // Similar to the default constructor, except that this allows for |
74 // calling LockDeprecated() to acquire the named mutex before either Create or | 74 // calling LockDeprecated() to acquire the named mutex before either Create or |
75 // Open are called on Windows. | 75 // Open are called on Windows. |
76 explicit SharedMemory(const std::wstring& name); | 76 explicit SharedMemory(const std::wstring& name); |
77 #endif | 77 #endif |
78 | 78 |
79 // Create a new SharedMemory object from an existing, open | 79 // Create a new SharedMemory object from an existing, open |
80 // shared memory file. | 80 // shared memory file. |
81 // | 81 // |
82 // WARNING: This does not reduce the OS-level permissions on the handle; it | 82 // WARNING: This does not reduce the OS-level permissions on the handle; it |
83 // only affects how the SharedMemory will be mmapped. Use | 83 // only affects how the SharedMemory will be mmapped. Use |
84 // ShareReadOnlyToProcess to drop permissions. TODO(jln,jyasskin): DCHECK | 84 // GetReadOnlyHandle to drop permissions. TODO(jln,jyasskin): DCHECK |
85 // that |read_only| matches the permissions of the handle. | 85 // that |read_only| matches the permissions of the handle. |
86 SharedMemory(const SharedMemoryHandle& handle, bool read_only); | 86 SharedMemory(const SharedMemoryHandle& handle, bool read_only); |
87 | 87 |
88 // Closes any open files. | 88 // Closes any open files. |
89 ~SharedMemory(); | 89 ~SharedMemory(); |
90 | 90 |
91 // Return true iff the given handle is valid (i.e. not the distingished | 91 // Return true iff the given handle is valid (i.e. not the distingished |
92 // invalid value; NULL for a HANDLE and -1 for a file descriptor) | 92 // invalid value; NULL for a HANDLE and -1 for a file descriptor) |
93 static bool IsHandleValid(const SharedMemoryHandle& handle); | 93 static bool IsHandleValid(const SharedMemoryHandle& handle); |
94 | 94 |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 // SharedMemoryHandle dup = DuplicateHandle(handle()); | 199 // SharedMemoryHandle dup = DuplicateHandle(handle()); |
200 // Close(); | 200 // Close(); |
201 // return dup; | 201 // return dup; |
202 SharedMemoryHandle TakeHandle(); | 202 SharedMemoryHandle TakeHandle(); |
203 | 203 |
204 // Closes the open shared memory segment. The memory will remain mapped if | 204 // Closes the open shared memory segment. The memory will remain mapped if |
205 // it was previously mapped. | 205 // it was previously mapped. |
206 // It is safe to call Close repeatedly. | 206 // It is safe to call Close repeatedly. |
207 void Close(); | 207 void Close(); |
208 | 208 |
209 // Shares the shared memory to another process. Attempts to create a | 209 // Returns a read-only handle to this shared memory region. The caller takes |
210 // platform-specific new_handle which can be used in a remote process to read | 210 // ownership of the handle. For POSIX handles, CHECK-fails if the region |
211 // the shared memory file. new_handle is an output parameter to receive the | 211 // wasn't Created or Opened with share_read_only=true, which is required to |
212 // handle for use in the remote process. | 212 // make the handle read-only. When the handle is passed to the IPC subsystem, |
213 // | 213 // that takes ownership of the handle. As such, it's not valid to pass the |
214 // |*this| must have been initialized using one of the Create*() or Open() | 214 // sample handle to the IPC subsystem twice. Returns an invalid handle on |
215 // methods with share_read_only=true. If it was constructed from a | 215 // failure. |
216 // SharedMemoryHandle, this call will CHECK-fail. | 216 SharedMemoryHandle GetReadOnlyHandle(); |
217 // | |
218 // Returns true on success, false otherwise. | |
219 bool ShareReadOnlyToProcess(ProcessHandle process, | |
220 SharedMemoryHandle* new_handle) { | |
221 return ShareToProcessCommon(process, new_handle, false, SHARE_READONLY); | |
222 } | |
223 | |
224 // Logically equivalent to: | |
225 // bool ok = ShareReadOnlyToProcess(process, new_handle); | |
226 // Close(); | |
227 // return ok; | |
228 // Note that the memory is unmapped by calling this method, regardless of the | |
229 // return value. | |
230 bool GiveReadOnlyToProcess(ProcessHandle process, | |
231 SharedMemoryHandle* new_handle) { | |
232 return ShareToProcessCommon(process, new_handle, true, SHARE_READONLY); | |
233 } | |
234 | 217 |
235 // Shares the shared memory to another process. Attempts | 218 // Shares the shared memory to another process. Attempts |
236 // to create a platform-specific new_handle which can be | 219 // to create a platform-specific new_handle which can be |
237 // used in a remote process to access the shared memory | 220 // used in a remote process to access the shared memory |
238 // file. new_handle is an output parameter to receive | 221 // file. new_handle is an output parameter to receive |
239 // the handle for use in the remote process. | 222 // the handle for use in the remote process. |
240 // Returns true on success, false otherwise. | 223 // Returns true on success, false otherwise. |
241 bool ShareToProcess(ProcessHandle process, | 224 bool ShareToProcess(ProcessHandle process, |
242 SharedMemoryHandle* new_handle) { | 225 SharedMemoryHandle* new_handle) { |
243 return ShareToProcessCommon(process, new_handle, false, SHARE_CURRENT_MODE); | 226 return ShareToProcessCommon(process, new_handle, false); |
244 } | 227 } |
245 | 228 |
246 // Logically equivalent to: | 229 // Logically equivalent to: |
247 // bool ok = ShareToProcess(process, new_handle); | 230 // bool ok = ShareToProcess(process, new_handle); |
248 // Close(); | 231 // Close(); |
249 // return ok; | 232 // return ok; |
250 // Note that the memory is unmapped by calling this method, regardless of the | 233 // Note that the memory is unmapped by calling this method, regardless of the |
251 // return value. | 234 // return value. |
252 bool GiveToProcess(ProcessHandle process, | 235 bool GiveToProcess(ProcessHandle process, |
253 SharedMemoryHandle* new_handle) { | 236 SharedMemoryHandle* new_handle) { |
254 return ShareToProcessCommon(process, new_handle, true, SHARE_CURRENT_MODE); | 237 return ShareToProcessCommon(process, new_handle, true); |
255 } | 238 } |
256 | 239 |
257 #if defined(OS_POSIX) && (!defined(OS_MACOSX) || defined(OS_IOS)) && \ | 240 #if defined(OS_POSIX) && (!defined(OS_MACOSX) || defined(OS_IOS)) && \ |
258 !defined(OS_NACL) | 241 !defined(OS_NACL) |
259 using UniqueId = std::pair<dev_t, ino_t>; | 242 using UniqueId = std::pair<dev_t, ino_t>; |
260 | 243 |
261 struct UniqueIdHash { | 244 struct UniqueIdHash { |
262 size_t operator()(const UniqueId& id) const { | 245 size_t operator()(const UniqueId& id) const { |
263 return HashInts(id.first, id.second); | 246 return HashInts(id.first, id.second); |
264 } | 247 } |
265 }; | 248 }; |
266 | 249 |
267 // Returns a unique ID for this shared memory's handle. Note this function may | 250 // Returns a unique ID for this shared memory's handle. Note this function may |
268 // access file system and be slow. | 251 // access file system and be slow. |
269 bool GetUniqueId(UniqueId* id) const; | 252 bool GetUniqueId(UniqueId* id) const; |
270 #endif | 253 #endif |
271 | 254 |
272 private: | 255 private: |
273 #if defined(OS_POSIX) && !defined(OS_NACL) && !defined(OS_ANDROID) && \ | 256 #if defined(OS_POSIX) && !defined(OS_NACL) && !defined(OS_ANDROID) && \ |
274 (!defined(OS_MACOSX) || defined(OS_IOS)) | 257 (!defined(OS_MACOSX) || defined(OS_IOS)) |
275 bool FilePathForMemoryName(const std::string& mem_name, FilePath* path); | 258 bool FilePathForMemoryName(const std::string& mem_name, FilePath* path); |
276 #endif | 259 #endif |
277 | 260 |
278 enum ShareMode { | |
279 SHARE_READONLY, | |
280 SHARE_CURRENT_MODE, | |
281 }; | |
282 | |
283 #if defined(OS_MACOSX) | 261 #if defined(OS_MACOSX) |
284 bool Share(SharedMemoryHandle* new_handle, ShareMode share_mode); | 262 bool Share(SharedMemoryHandle* new_handle); |
285 #endif | 263 #endif |
286 | 264 |
287 bool ShareToProcessCommon(ProcessHandle process, | 265 bool ShareToProcessCommon(ProcessHandle process, |
288 SharedMemoryHandle* new_handle, | 266 SharedMemoryHandle* new_handle, |
289 bool close_self, | 267 bool close_self); |
290 ShareMode); | |
291 | 268 |
292 #if defined(OS_WIN) | 269 #if defined(OS_WIN) |
293 // If true indicates this came from an external source so needs extra checks | 270 // If true indicates this came from an external source so needs extra checks |
294 // before being mapped. | 271 // before being mapped. |
295 bool external_section_; | 272 bool external_section_; |
296 std::wstring name_; | 273 std::wstring name_; |
297 win::ScopedHandle mapped_file_; | 274 win::ScopedHandle mapped_file_; |
298 #elif defined(OS_MACOSX) && !defined(OS_IOS) | 275 #else |
299 // The OS primitive that backs the shared memory region. | 276 // The OS primitive that backs the shared memory region. |
300 SharedMemoryHandle shm_; | 277 SharedMemoryHandle shm_; |
301 | 278 |
| 279 // If valid, points to the same memory region as shm_, but with readonly |
| 280 // permissions. |
| 281 SharedMemoryHandle readonly_shm_; |
| 282 #endif |
| 283 |
| 284 #if defined(OS_MACOSX) && !defined(OS_IOS) |
302 // The mechanism by which the memory is mapped. Only valid if |memory_| is not | 285 // The mechanism by which the memory is mapped. Only valid if |memory_| is not |
303 // |nullptr|. | 286 // |nullptr|. |
304 SharedMemoryHandle::Type mapped_memory_mechanism_; | 287 SharedMemoryHandle::Type mapped_memory_mechanism_; |
| 288 #endif |
305 | 289 |
306 int readonly_mapped_file_; | |
307 #elif defined(OS_POSIX) | |
308 // The OS primitive that backs the shared memory region. | |
309 SharedMemoryHandle shm_; | |
310 int readonly_mapped_file_; | |
311 #endif | |
312 size_t mapped_size_; | 290 size_t mapped_size_; |
313 void* memory_; | 291 void* memory_; |
314 bool read_only_; | 292 bool read_only_; |
315 size_t requested_size_; | 293 size_t requested_size_; |
316 | 294 |
317 DISALLOW_COPY_AND_ASSIGN(SharedMemory); | 295 DISALLOW_COPY_AND_ASSIGN(SharedMemory); |
318 }; | 296 }; |
319 | 297 |
320 } // namespace base | 298 } // namespace base |
321 | 299 |
322 #endif // BASE_MEMORY_SHARED_MEMORY_H_ | 300 #endif // BASE_MEMORY_SHARED_MEMORY_H_ |
OLD | NEW |