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 #include "base/memory/shared_memory.h" | 5 #include "base/memory/shared_memory.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <mach/mach_vm.h> | 8 #include <mach/mach_vm.h> |
9 #include <sys/mman.h> | 9 #include <sys/mman.h> |
10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
11 #include <unistd.h> | 11 #include <unistd.h> |
12 | 12 |
13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
14 #include "base/files/scoped_file.h" | 14 #include "base/files/scoped_file.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/mac/scoped_mach_vm.h" | |
16 #include "base/posix/eintr_wrapper.h" | 17 #include "base/posix/eintr_wrapper.h" |
17 #include "base/posix/safe_strerror.h" | 18 #include "base/posix/safe_strerror.h" |
18 #include "base/process/process_metrics.h" | 19 #include "base/process/process_metrics.h" |
19 #include "base/profiler/scoped_tracker.h" | 20 #include "base/profiler/scoped_tracker.h" |
20 #include "base/scoped_generic.h" | 21 #include "base/scoped_generic.h" |
21 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
22 | 23 |
23 #if defined(OS_MACOSX) | 24 #if defined(OS_MACOSX) |
24 #include "base/mac/foundation_util.h" | 25 #include "base/mac/foundation_util.h" |
25 #endif // OS_MACOSX | 26 #endif // OS_MACOSX |
26 | 27 |
27 namespace base { | 28 namespace base { |
28 | 29 |
29 namespace { | 30 namespace { |
30 | 31 |
32 // Returns whether the operation succeeded. | |
33 // |new_handle| is an output variable, populated on success. The caller takes | |
34 // ownership of the underlying memory object. | |
35 // |handle| is the handle to copy. | |
36 // If |handle| is already mapped, |mapped_addr| is its mapped location. | |
37 // Otherwise, |mapped_addr| should be |nullptr|. | |
38 bool MakeMachSharedMemoryHandleReadOnly(SharedMemoryHandle* new_handle, | |
39 SharedMemoryHandle handle, | |
40 void* mapped_addr) { | |
41 if (!handle.IsValid()) | |
42 return false; | |
43 | |
44 size_t size; | |
45 CHECK(handle.GetSize(&size)); | |
46 | |
47 // Map if necessary. | |
48 void* temp_addr = mapped_addr; | |
49 base::mac::ScopedMachVM scoper; | |
50 if (!temp_addr) { | |
51 // Intentionally lower current prot and max prot to |VM_PROT_READ|. | |
52 kern_return_t kr = mach_vm_map( | |
53 mach_task_self(), reinterpret_cast<mach_vm_address_t*>(&temp_addr), | |
54 size, 0, VM_FLAGS_ANYWHERE, handle.GetMemoryObject(), 0, FALSE, | |
55 VM_PROT_READ, VM_PROT_READ, VM_INHERIT_NONE); | |
56 if (kr != KERN_SUCCESS) | |
57 return false; | |
58 scoper.reset(reinterpret_cast<vm_address_t>(temp_addr), | |
59 mach_vm_round_page(size)); | |
60 } | |
61 | |
62 // Make new memory object. | |
63 mach_port_t named_right; | |
64 kern_return_t kr = mach_make_memory_entry_64( | |
65 mach_task_self(), reinterpret_cast<memory_object_size_t*>(&size), | |
66 reinterpret_cast<memory_object_offset_t>(temp_addr), VM_PROT_READ, | |
67 &named_right, MACH_PORT_NULL); | |
68 if (kr != KERN_SUCCESS) | |
69 return false; | |
70 | |
71 *new_handle = SharedMemoryHandle(named_right, size, base::GetCurrentProcId()); | |
72 return true; | |
73 } | |
74 | |
31 struct ScopedPathUnlinkerTraits { | 75 struct ScopedPathUnlinkerTraits { |
32 static FilePath* InvalidValue() { return nullptr; } | 76 static FilePath* InvalidValue() { return nullptr; } |
33 | 77 |
34 static void Free(FilePath* path) { | 78 static void Free(FilePath* path) { |
35 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466437 | 79 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466437 |
36 // is fixed. | 80 // is fixed. |
37 tracked_objects::ScopedTracker tracking_profile( | 81 tracked_objects::ScopedTracker tracking_profile( |
38 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 82 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
39 "466437 SharedMemory::Create::Unlink")); | 83 "466437 SharedMemory::Create::Unlink")); |
40 if (unlink(path->value().c_str())) | 84 if (unlink(path->value().c_str())) |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
315 shm_ = SharedMemoryHandle(mapped_file, false); | 359 shm_ = SharedMemoryHandle(mapped_file, false); |
316 readonly_mapped_file_ = readonly_fd.release(); | 360 readonly_mapped_file_ = readonly_fd.release(); |
317 | 361 |
318 return true; | 362 return true; |
319 } | 363 } |
320 | 364 |
321 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, | 365 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
322 SharedMemoryHandle* new_handle, | 366 SharedMemoryHandle* new_handle, |
323 bool close_self, | 367 bool close_self, |
324 ShareMode share_mode) { | 368 ShareMode share_mode) { |
325 DCHECK_NE(shm_.GetType(), SharedMemoryHandle::MACH); | 369 if (shm_.GetType() == SharedMemoryHandle::MACH) { |
370 DCHECK(shm_.IsValid()); | |
371 | |
372 bool success = false; | |
373 switch (share_mode) { | |
374 case SHARE_CURRENT_MODE: | |
375 *new_handle = shm_.Duplicate(); | |
376 success = true; | |
377 break; | |
378 case SHARE_READONLY: | |
379 success = MakeMachSharedMemoryHandleReadOnly(new_handle, shm_, memory_); | |
380 break; | |
381 } | |
382 | |
383 if (success) | |
384 new_handle->SetOwnershipPassesToIPC(true); | |
385 | |
386 if (close_self) { | |
387 Unmap(); | |
388 Close(); | |
389 } | |
390 | |
391 return success; | |
392 } | |
393 | |
326 int handle_to_dup = -1; | 394 int handle_to_dup = -1; |
327 switch (share_mode) { | 395 switch (share_mode) { |
328 case SHARE_CURRENT_MODE: | 396 case SHARE_CURRENT_MODE: |
329 handle_to_dup = shm_.GetFileDescriptor().fd; | 397 handle_to_dup = shm_.GetFileDescriptor().fd; |
330 break; | 398 break; |
331 case SHARE_READONLY: | 399 case SHARE_READONLY: |
332 // We could imagine re-opening the file from /dev/fd, but that can't make | 400 // We could imagine re-opening the file from /dev/fd, but that can't make |
333 // it readonly on Mac: https://codereview.chromium.org/27265002/#msg10 | 401 // it readonly on Mac: https://codereview.chromium.org/27265002/#msg10 |
334 CHECK_GE(readonly_mapped_file_, 0); | 402 CHECK_GE(readonly_mapped_file_, 0); |
335 handle_to_dup = readonly_mapped_file_; | 403 handle_to_dup = readonly_mapped_file_; |
336 break; | 404 break; |
337 } | 405 } |
338 | 406 |
339 const int new_fd = HANDLE_EINTR(dup(handle_to_dup)); | 407 const int new_fd = HANDLE_EINTR(dup(handle_to_dup)); |
340 if (new_fd < 0) { | 408 if (new_fd < 0) { |
409 // TODO(erikchen): If |close_self| is |true|, this should probably call | |
410 // Unmap() and Close(). | |
Mark Mentovai
2015/10/23 14:24:03
So do it?
erikchen
2015/10/23 17:25:56
Will do so in a separate CL. removing the comment.
| |
341 DPLOG(ERROR) << "dup() failed."; | 411 DPLOG(ERROR) << "dup() failed."; |
342 return false; | 412 return false; |
343 } | 413 } |
344 | 414 |
345 new_handle->SetFileHandle(new_fd, true); | 415 new_handle->SetFileHandle(new_fd, true); |
346 | 416 |
347 if (close_self) { | 417 if (close_self) { |
348 Unmap(); | 418 Unmap(); |
349 Close(); | 419 Close(); |
350 } | 420 } |
351 | 421 |
352 return true; | 422 return true; |
353 } | 423 } |
354 | 424 |
355 } // namespace base | 425 } // namespace base |
OLD | NEW |