Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: base/memory/shared_memory_mac.cc

Issue 1422873002: Reland 1: "mac: Add auto-close and share-read-only functionality to SharedMemory." (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix error. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/memory/shared_memory_handle_mac.cc ('k') | base/memory/shared_memory_mac_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 shm_ = SharedMemoryHandle(mapped_file, false); 382 shm_ = SharedMemoryHandle(mapped_file, false);
339 readonly_mapped_file_ = readonly_fd.release(); 383 readonly_mapped_file_ = readonly_fd.release();
340 384
341 return true; 385 return true;
342 } 386 }
343 387
344 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, 388 bool SharedMemory::ShareToProcessCommon(ProcessHandle process,
345 SharedMemoryHandle* new_handle, 389 SharedMemoryHandle* new_handle,
346 bool close_self, 390 bool close_self,
347 ShareMode share_mode) { 391 ShareMode share_mode) {
348 DCHECK_NE(shm_.GetType(), SharedMemoryHandle::MACH); 392 if (shm_.GetType() == SharedMemoryHandle::MACH) {
393 DCHECK(shm_.IsValid());
394
395 bool success = false;
396 switch (share_mode) {
397 case SHARE_CURRENT_MODE:
398 *new_handle = shm_.Duplicate();
399 success = true;
400 break;
401 case SHARE_READONLY:
402 success = MakeMachSharedMemoryHandleReadOnly(new_handle, shm_, memory_);
403 break;
404 }
405
406 if (success)
407 new_handle->SetOwnershipPassesToIPC(true);
408
409 if (close_self) {
410 Unmap();
411 Close();
412 }
413
414 return success;
415 }
416
349 int handle_to_dup = -1; 417 int handle_to_dup = -1;
350 switch (share_mode) { 418 switch (share_mode) {
351 case SHARE_CURRENT_MODE: 419 case SHARE_CURRENT_MODE:
352 handle_to_dup = shm_.GetFileDescriptor().fd; 420 handle_to_dup = shm_.GetFileDescriptor().fd;
353 break; 421 break;
354 case SHARE_READONLY: 422 case SHARE_READONLY:
355 // We could imagine re-opening the file from /dev/fd, but that can't make 423 // We could imagine re-opening the file from /dev/fd, but that can't make
356 // it readonly on Mac: https://codereview.chromium.org/27265002/#msg10 424 // it readonly on Mac: https://codereview.chromium.org/27265002/#msg10
357 CHECK_GE(readonly_mapped_file_, 0); 425 CHECK_GE(readonly_mapped_file_, 0);
358 handle_to_dup = readonly_mapped_file_; 426 handle_to_dup = readonly_mapped_file_;
(...skipping 14 matching lines...) Expand all
373 441
374 if (close_self) { 442 if (close_self) {
375 Unmap(); 443 Unmap();
376 Close(); 444 Close();
377 } 445 }
378 446
379 return true; 447 return true;
380 } 448 }
381 449
382 } // namespace base 450 } // namespace base
OLDNEW
« no previous file with comments | « base/memory/shared_memory_handle_mac.cc ('k') | base/memory/shared_memory_mac_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698