Index: base/memory/shared_memory_mac.cc |
diff --git a/base/memory/shared_memory_posix.cc b/base/memory/shared_memory_mac.cc |
similarity index 79% |
copy from base/memory/shared_memory_posix.cc |
copy to base/memory/shared_memory_mac.cc |
index 35d746e5394e0e042bbaac33421d94c8406e65e2..8d8a164f463e365b61347d1b9bfefcb20f613ec0 100644 |
--- a/base/memory/shared_memory_posix.cc |
+++ b/base/memory/shared_memory_mac.cc |
@@ -4,16 +4,13 @@ |
#include "base/memory/shared_memory.h" |
-#include <errno.h> |
#include <fcntl.h> |
#include <sys/mman.h> |
#include <sys/stat.h> |
-#include <sys/types.h> |
#include <unistd.h> |
#include "base/files/file_util.h" |
#include "base/files/scoped_file.h" |
-#include "base/lazy_instance.h" |
#include "base/logging.h" |
#include "base/posix/eintr_wrapper.h" |
#include "base/posix/safe_strerror.h" |
@@ -21,25 +18,15 @@ |
#include "base/profiler/scoped_tracker.h" |
#include "base/scoped_generic.h" |
#include "base/strings/utf_string_conversions.h" |
-#include "base/synchronization/lock.h" |
-#include "base/threading/platform_thread.h" |
-#include "base/threading/thread_restrictions.h" |
#if defined(OS_MACOSX) |
#include "base/mac/foundation_util.h" |
#endif // OS_MACOSX |
-#if defined(OS_ANDROID) |
-#include "base/os_compat_android.h" |
-#include "third_party/ashmem/ashmem.h" |
-#endif |
- |
namespace base { |
namespace { |
-LazyInstance<Lock>::Leaky g_thread_lock_ = LAZY_INSTANCE_INITIALIZER; |
- |
struct ScopedPathUnlinkerTraits { |
static FilePath* InvalidValue() { return nullptr; } |
@@ -57,7 +44,6 @@ struct ScopedPathUnlinkerTraits { |
// Unlinks the FilePath when the object is destroyed. |
typedef ScopedGeneric<FilePath*, ScopedPathUnlinkerTraits> ScopedPathUnlinker; |
-#if !defined(OS_ANDROID) |
// Makes a temporary file, fdopens it, and then unlinks it. |fp| is populated |
// with the fdopened FILE. |readonly_fd| is populated with the opened fd if |
// options.share_read_only is true. |path| is populated with the location of |
@@ -106,7 +92,6 @@ bool CreateAnonymousSharedMemory(const SharedMemoryCreateOptions& options, |
} |
return true; |
} |
-#endif // !defined(OS_ANDROID) |
} |
SharedMemory::SharedMemory() |
@@ -115,21 +100,20 @@ SharedMemory::SharedMemory() |
mapped_size_(0), |
memory_(NULL), |
read_only_(false), |
- requested_size_(0) { |
-} |
+ requested_size_(0) {} |
-SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) |
- : mapped_file_(handle.fd), |
+SharedMemory::SharedMemory(const SharedMemoryHandle& handle, bool read_only) |
+ : mapped_file_(GetFdFromSharedMemoryHandle(handle)), |
readonly_mapped_file_(-1), |
mapped_size_(0), |
memory_(NULL), |
read_only_(read_only), |
- requested_size_(0) { |
-} |
+ requested_size_(0) {} |
-SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only, |
+SharedMemory::SharedMemory(const SharedMemoryHandle& handle, |
+ bool read_only, |
ProcessHandle process) |
- : mapped_file_(handle.fd), |
+ : mapped_file_(GetFdFromSharedMemoryHandle(handle)), |
readonly_mapped_file_(-1), |
mapped_size_(0), |
memory_(NULL), |
@@ -147,7 +131,7 @@ SharedMemory::~SharedMemory() { |
// static |
bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { |
- return handle.fd >= 0; |
+ return handle.IsValid(); |
} |
// static |
@@ -157,8 +141,8 @@ SharedMemoryHandle SharedMemory::NULLHandle() { |
// static |
void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { |
- DCHECK_GE(handle.fd, 0); |
- if (close(handle.fd) < 0) |
+ DCHECK_GE(GetFdFromSharedMemoryHandle(handle), 0); |
+ if (close(GetFdFromSharedMemoryHandle(handle)) < 0) |
DPLOG(ERROR) << "close"; |
} |
@@ -170,28 +154,24 @@ size_t SharedMemory::GetHandleLimit() { |
// static |
SharedMemoryHandle SharedMemory::DuplicateHandle( |
const SharedMemoryHandle& handle) { |
- int duped_handle = HANDLE_EINTR(dup(handle.fd)); |
- if (duped_handle < 0) |
- return base::SharedMemory::NULLHandle(); |
- return base::FileDescriptor(duped_handle, true); |
+ return handle.Duplicate(); |
} |
// static |
int SharedMemory::GetFdFromSharedMemoryHandle( |
const SharedMemoryHandle& handle) { |
- return handle.fd; |
+ return handle.GetFileDescriptor().fd; |
} |
bool SharedMemory::CreateAndMapAnonymous(size_t size) { |
return CreateAnonymous(size) && Map(size); |
} |
-#if !defined(OS_ANDROID) |
// static |
int SharedMemory::GetSizeFromSharedMemoryHandle( |
const SharedMemoryHandle& handle) { |
struct stat st; |
- if (fstat(handle.fd, &st) != 0) |
+ if (fstat(GetFdFromSharedMemoryHandle(handle), &st) != 0) |
return -1; |
return st.st_size; |
} |
@@ -206,10 +186,10 @@ bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
// TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466437 |
// is fixed. |
tracked_objects::ScopedTracker tracking_profile1( |
- FROM_HERE_WITH_EXPLICIT_FUNCTION( |
- "466437 SharedMemory::Create::Start")); |
+ FROM_HERE_WITH_EXPLICIT_FUNCTION("466437 SharedMemory::Create::Start")); |
DCHECK_EQ(-1, mapped_file_); |
- if (options.size == 0) return false; |
+ if (options.size == 0) |
+ return false; |
if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) |
return false; |
@@ -257,11 +237,9 @@ bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
const uid_t real_uid = getuid(); |
const uid_t effective_uid = geteuid(); |
struct stat sb; |
- if (fd >= 0 && |
- (fstat(fd, &sb) != 0 || sb.st_uid != real_uid || |
- sb.st_uid != effective_uid)) { |
- LOG(ERROR) << |
- "Invalid owner when opening existing shared memory file."; |
+ if (fd >= 0 && (fstat(fd, &sb) != 0 || sb.st_uid != real_uid || |
+ sb.st_uid != effective_uid)) { |
+ LOG(ERROR) << "Invalid owner when opening existing shared memory file."; |
close(fd); |
return false; |
} |
@@ -298,19 +276,7 @@ bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
requested_size_ = options.size; |
} |
if (fp == NULL) { |
-#if !defined(OS_MACOSX) |
PLOG(ERROR) << "Creating shared memory in " << path.value() << " failed"; |
- FilePath dir = path.DirName(); |
- if (access(dir.value().c_str(), W_OK | X_OK) < 0) { |
- PLOG(ERROR) << "Unable to access(W_OK|X_OK) " << dir.value(); |
- if (dir.value() == "/dev/shm") { |
- LOG(FATAL) << "This is frequently caused by incorrect permissions on " |
- << "/dev/shm. Try 'sudo chmod 1777 /dev/shm' to fix."; |
- } |
- } |
-#else |
- PLOG(ERROR) << "Creating shared memory in " << path.value() << " failed"; |
-#endif |
return false; |
} |
@@ -339,7 +305,7 @@ bool SharedMemory::Open(const std::string& name, bool read_only) { |
read_only_ = read_only; |
- const char *mode = read_only ? "r" : "r+"; |
+ const char* mode = read_only ? "r" : "r+"; |
ScopedFILE fp(base::OpenFile(path, mode)); |
ScopedFD readonly_fd(HANDLE_EINTR(open(path.value().c_str(), O_RDONLY))); |
if (!readonly_fd.is_valid()) { |
@@ -348,7 +314,6 @@ bool SharedMemory::Open(const std::string& name, bool read_only) { |
} |
return PrepareMapFile(fp.Pass(), readonly_fd.Pass()); |
} |
-#endif // !defined(OS_ANDROID) |
bool SharedMemory::MapAt(off_t offset, size_t bytes) { |
if (mapped_file_ == -1) |
@@ -360,18 +325,6 @@ bool SharedMemory::MapAt(off_t offset, size_t bytes) { |
if (memory_) |
return false; |
-#if defined(OS_ANDROID) |
- // On Android, Map can be called with a size and offset of zero to use the |
- // ashmem-determined size. |
- if (bytes == 0) { |
- DCHECK_EQ(0, offset); |
- int ashmem_bytes = ashmem_get_size_region(mapped_file_); |
- if (ashmem_bytes < 0) |
- return false; |
- bytes = ashmem_bytes; |
- } |
-#endif |
- |
memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), |
MAP_SHARED, mapped_file_, offset); |
@@ -379,7 +332,7 @@ bool SharedMemory::MapAt(off_t offset, size_t bytes) { |
if (mmap_succeeded) { |
mapped_size_ = bytes; |
DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & |
- (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
+ (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
} else { |
memory_ = NULL; |
} |
@@ -398,7 +351,7 @@ bool SharedMemory::Unmap() { |
} |
SharedMemoryHandle SharedMemory::handle() const { |
- return FileDescriptor(mapped_file_, false); |
+ return SharedMemoryHandle(mapped_file_, false); |
} |
void SharedMemory::Close() { |
@@ -414,17 +367,6 @@ void SharedMemory::Close() { |
} |
} |
-void SharedMemory::LockDeprecated() { |
- g_thread_lock_.Get().Acquire(); |
- LockOrUnlockCommon(F_LOCK); |
-} |
- |
-void SharedMemory::UnlockDeprecated() { |
- LockOrUnlockCommon(F_ULOCK); |
- g_thread_lock_.Get().Release(); |
-} |
- |
-#if !defined(OS_ANDROID) |
bool SharedMemory::PrepareMapFile(ScopedFILE fp, ScopedFD readonly_fd) { |
DCHECK_EQ(-1, mapped_file_); |
DCHECK_EQ(-1, readonly_mapped_file_); |
@@ -477,45 +419,17 @@ bool SharedMemory::FilePathForMemoryName(const std::string& mem_name, |
if (!GetShmemTempDir(false, &temp_dir)) |
return false; |
-#if !defined(OS_MACOSX) |
-#if defined(GOOGLE_CHROME_BUILD) |
- std::string name_base = std::string("com.google.Chrome"); |
-#else |
- std::string name_base = std::string("org.chromium.Chromium"); |
-#endif |
-#else // OS_MACOSX |
std::string name_base = std::string(base::mac::BaseBundleID()); |
-#endif // OS_MACOSX |
*path = temp_dir.AppendASCII(name_base + ".shmem." + mem_name); |
return true; |
} |
-#endif // !defined(OS_ANDROID) |
- |
-void SharedMemory::LockOrUnlockCommon(int function) { |
- DCHECK_GE(mapped_file_, 0); |
- while (lockf(mapped_file_, function, 0) < 0) { |
- if (errno == EINTR) { |
- continue; |
- } else if (errno == ENOLCK) { |
- // temporary kernel resource exaustion |
- base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(500)); |
- continue; |
- } else { |
- NOTREACHED() << "lockf() failed." |
- << " function:" << function |
- << " fd:" << mapped_file_ |
- << " errno:" << errno |
- << " msg:" << base::safe_strerror(errno); |
- } |
- } |
-} |
bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
SharedMemoryHandle* new_handle, |
bool close_self, |
ShareMode share_mode) { |
int handle_to_dup = -1; |
- switch(share_mode) { |
+ switch (share_mode) { |
case SHARE_CURRENT_MODE: |
handle_to_dup = mapped_file_; |
break; |
@@ -533,8 +447,7 @@ bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
return false; |
} |
- new_handle->fd = new_fd; |
- new_handle->auto_close = true; |
+ new_handle->SetFileHandle(new_fd, true); |
if (close_self) { |
Unmap(); |