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

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

Issue 1421933002: Revert of mac: Add auto-close and share-read-only functionality to SharedMemory. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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"
17 #include "base/posix/eintr_wrapper.h" 16 #include "base/posix/eintr_wrapper.h"
18 #include "base/posix/safe_strerror.h" 17 #include "base/posix/safe_strerror.h"
19 #include "base/process/process_metrics.h" 18 #include "base/process/process_metrics.h"
20 #include "base/profiler/scoped_tracker.h" 19 #include "base/profiler/scoped_tracker.h"
21 #include "base/scoped_generic.h" 20 #include "base/scoped_generic.h"
22 #include "base/strings/utf_string_conversions.h" 21 #include "base/strings/utf_string_conversions.h"
23 22
24 #if defined(OS_MACOSX) 23 #if defined(OS_MACOSX)
25 #include "base/mac/foundation_util.h" 24 #include "base/mac/foundation_util.h"
26 #endif // OS_MACOSX 25 #endif // OS_MACOSX
27 26
28 namespace base { 27 namespace base {
29 28
30 namespace { 29 namespace {
31 30
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
75 struct ScopedPathUnlinkerTraits { 31 struct ScopedPathUnlinkerTraits {
76 static FilePath* InvalidValue() { return nullptr; } 32 static FilePath* InvalidValue() { return nullptr; }
77 33
78 static void Free(FilePath* path) { 34 static void Free(FilePath* path) {
79 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466437 35 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466437
80 // is fixed. 36 // is fixed.
81 tracked_objects::ScopedTracker tracking_profile( 37 tracked_objects::ScopedTracker tracking_profile(
82 FROM_HERE_WITH_EXPLICIT_FUNCTION( 38 FROM_HERE_WITH_EXPLICIT_FUNCTION(
83 "466437 SharedMemory::Create::Unlink")); 39 "466437 SharedMemory::Create::Unlink"));
84 if (unlink(path->value().c_str())) 40 if (unlink(path->value().c_str()))
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 shm_ = SharedMemoryHandle(mapped_file, false); 315 shm_ = SharedMemoryHandle(mapped_file, false);
360 readonly_mapped_file_ = readonly_fd.release(); 316 readonly_mapped_file_ = readonly_fd.release();
361 317
362 return true; 318 return true;
363 } 319 }
364 320
365 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, 321 bool SharedMemory::ShareToProcessCommon(ProcessHandle process,
366 SharedMemoryHandle* new_handle, 322 SharedMemoryHandle* new_handle,
367 bool close_self, 323 bool close_self,
368 ShareMode share_mode) { 324 ShareMode share_mode) {
369 if (shm_.GetType() == SharedMemoryHandle::MACH) { 325 DCHECK_NE(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
394 int handle_to_dup = -1; 326 int handle_to_dup = -1;
395 switch (share_mode) { 327 switch (share_mode) {
396 case SHARE_CURRENT_MODE: 328 case SHARE_CURRENT_MODE:
397 handle_to_dup = shm_.GetFileDescriptor().fd; 329 handle_to_dup = shm_.GetFileDescriptor().fd;
398 break; 330 break;
399 case SHARE_READONLY: 331 case SHARE_READONLY:
400 // We could imagine re-opening the file from /dev/fd, but that can't make 332 // We could imagine re-opening the file from /dev/fd, but that can't make
401 // it readonly on Mac: https://codereview.chromium.org/27265002/#msg10 333 // it readonly on Mac: https://codereview.chromium.org/27265002/#msg10
402 CHECK_GE(readonly_mapped_file_, 0); 334 CHECK_GE(readonly_mapped_file_, 0);
403 handle_to_dup = readonly_mapped_file_; 335 handle_to_dup = readonly_mapped_file_;
(...skipping 10 matching lines...) Expand all
414 346
415 if (close_self) { 347 if (close_self) {
416 Unmap(); 348 Unmap();
417 Close(); 349 Close();
418 } 350 }
419 351
420 return true; 352 return true;
421 } 353 }
422 354
423 } // namespace base 355 } // 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