This source file includes following definitions.
- requested_size_
- requested_size_
- requested_size_
- IsHandleValid
- NULLHandle
- CloseHandle
- GetHandleLimit
- CreateAndMapAnonymous
- Create
- Delete
- Open
- MapAt
- Unmap
- handle
- Close
- LockDeprecated
- UnlockDeprecated
- PrepareMapFile
- FilePathForMemoryName
- LockOrUnlockCommon
- ShareToProcessCommon
#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/file_util.h"
#include "base/files/scoped_file.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/process/process_metrics.h"
#include "base/safe_strerror_posix.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
#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;
}
SharedMemory::SharedMemory()
: mapped_file_(-1),
readonly_mapped_file_(-1),
inode_(0),
mapped_size_(0),
memory_(NULL),
read_only_(false),
requested_size_(0) {
}
SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only)
: mapped_file_(handle.fd),
readonly_mapped_file_(-1),
inode_(0),
mapped_size_(0),
memory_(NULL),
read_only_(read_only),
requested_size_(0) {
struct stat st;
if (fstat(handle.fd, &st) == 0) {
inode_ = st.st_ino;
}
}
SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only,
ProcessHandle process)
: mapped_file_(handle.fd),
readonly_mapped_file_(-1),
inode_(0),
mapped_size_(0),
memory_(NULL),
read_only_(read_only),
requested_size_(0) {
NOTREACHED();
}
SharedMemory::~SharedMemory() {
Close();
}
bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) {
return handle.fd >= 0;
}
SharedMemoryHandle SharedMemory::NULLHandle() {
return SharedMemoryHandle();
}
void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) {
DCHECK_GE(handle.fd, 0);
if (close(handle.fd) < 0)
DPLOG(ERROR) << "close";
}
size_t SharedMemory::GetHandleLimit() {
return base::GetMaxFds();
}
bool SharedMemory::CreateAndMapAnonymous(size_t size) {
return CreateAnonymous(size) && Map(size);
}
#if !defined(OS_ANDROID)
bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
DCHECK_EQ(-1, mapped_file_);
if (options.size == 0) return false;
if (options.size > static_cast<size_t>(std::numeric_limits<int>::max()))
return false;
base::ThreadRestrictions::ScopedAllowIO allow_io;
ScopedFILE fp;
bool fix_size = true;
ScopedFD readonly_fd;
FilePath path;
if (options.name_deprecated == NULL || options.name_deprecated->empty()) {
DCHECK(!options.open_existing_deprecated);
FilePath directory;
if (GetShmemTempDir(options.executable, &directory))
fp.reset(CreateAndOpenTemporaryFileInDir(directory, &path));
if (fp) {
readonly_fd.reset(HANDLE_EINTR(open(path.value().c_str(), O_RDONLY)));
if (!readonly_fd.is_valid()) {
DPLOG(ERROR) << "open(\"" << path.value() << "\", O_RDONLY) failed";
fp.reset();
}
if (unlink(path.value().c_str()))
PLOG(WARNING) << "unlink";
}
} else {
if (!FilePathForMemoryName(*options.name_deprecated, &path))
return false;
const mode_t kOwnerOnly = S_IRUSR | S_IWUSR;
int fd = HANDLE_EINTR(
open(path.value().c_str(), O_RDWR | O_CREAT | O_EXCL, kOwnerOnly));
if (fd == -1 && options.open_existing_deprecated) {
fd = HANDLE_EINTR(
open(path.value().c_str(), O_RDWR | O_APPEND | O_NOFOLLOW));
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.";
close(fd);
return false;
}
fix_size = false;
}
readonly_fd.reset(HANDLE_EINTR(open(path.value().c_str(), O_RDONLY)));
if (!readonly_fd.is_valid()) {
DPLOG(ERROR) << "open(\"" << path.value() << "\", O_RDONLY) failed";
close(fd);
fd = -1;
}
if (fd >= 0) {
fp.reset(fdopen(fd, "a+"));
}
}
if (fp && fix_size) {
struct stat stat;
if (fstat(fileno(fp.get()), &stat) != 0)
return false;
const size_t current_size = stat.st_size;
if (current_size != options.size) {
if (HANDLE_EINTR(ftruncate(fileno(fp.get()), options.size)) != 0)
return false;
}
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;
}
return PrepareMapFile(fp.Pass(), readonly_fd.Pass());
}
bool SharedMemory::Delete(const std::string& name) {
FilePath path;
if (!FilePathForMemoryName(name, &path))
return false;
if (PathExists(path))
return base::DeleteFile(path, false);
return true;
}
bool SharedMemory::Open(const std::string& name, bool read_only) {
FilePath path;
if (!FilePathForMemoryName(name, &path))
return false;
read_only_ = read_only;
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()) {
DPLOG(ERROR) << "open(\"" << path.value() << "\", O_RDONLY) failed";
}
return PrepareMapFile(fp.Pass(), readonly_fd.Pass());
}
#endif
bool SharedMemory::MapAt(off_t offset, size_t bytes) {
if (mapped_file_ == -1)
return false;
if (bytes > static_cast<size_t>(std::numeric_limits<int>::max()))
return false;
if (memory_)
return false;
#if defined(OS_ANDROID)
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);
bool mmap_succeeded = memory_ != (void*)-1 && memory_ != NULL;
if (mmap_succeeded) {
mapped_size_ = bytes;
DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) &
(SharedMemory::MAP_MINIMUM_ALIGNMENT - 1));
} else {
memory_ = NULL;
}
return mmap_succeeded;
}
bool SharedMemory::Unmap() {
if (memory_ == NULL)
return false;
munmap(memory_, mapped_size_);
memory_ = NULL;
mapped_size_ = 0;
return true;
}
SharedMemoryHandle SharedMemory::handle() const {
return FileDescriptor(mapped_file_, false);
}
void SharedMemory::Close() {
Unmap();
if (mapped_file_ > 0) {
if (close(mapped_file_) < 0)
PLOG(ERROR) << "close";
mapped_file_ = -1;
}
if (readonly_mapped_file_ > 0) {
if (close(readonly_mapped_file_) < 0)
PLOG(ERROR) << "close";
readonly_mapped_file_ = -1;
}
}
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_);
if (fp == NULL || !readonly_fd.is_valid()) return false;
base::ThreadRestrictions::ScopedAllowIO allow_io;
struct stat st = {};
struct stat readonly_st = {};
if (fstat(fileno(fp.get()), &st))
NOTREACHED();
if (fstat(readonly_fd.get(), &readonly_st))
NOTREACHED();
if (st.st_dev != readonly_st.st_dev || st.st_ino != readonly_st.st_ino) {
LOG(ERROR) << "writable and read-only inodes don't match; bailing";
return false;
}
mapped_file_ = dup(fileno(fp.get()));
if (mapped_file_ == -1) {
if (errno == EMFILE) {
LOG(WARNING) << "Shared memory creation failed; out of file descriptors";
return false;
} else {
NOTREACHED() << "Call to dup failed, errno=" << errno;
}
}
inode_ = st.st_ino;
readonly_mapped_file_ = readonly_fd.release();
return true;
}
bool SharedMemory::FilePathForMemoryName(const std::string& mem_name,
FilePath* path) {
DCHECK_EQ(std::string::npos, mem_name.find('/'));
DCHECK_EQ(std::string::npos, mem_name.find('\0'));
FilePath temp_dir;
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
std::string name_base = std::string(base::mac::BaseBundleID());
#endif
*path = temp_dir.AppendASCII(name_base + ".shmem." + mem_name);
return true;
}
#endif
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) {
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(500));
continue;
} else {
NOTREACHED() << "lockf() failed."
<< " function:" << function
<< " fd:" << mapped_file_
<< " errno:" << errno
<< " msg:" << 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) {
case SHARE_CURRENT_MODE:
handle_to_dup = mapped_file_;
break;
case SHARE_READONLY:
CHECK(readonly_mapped_file_ >= 0);
handle_to_dup = readonly_mapped_file_;
break;
}
const int new_fd = dup(handle_to_dup);
if (new_fd < 0) {
DPLOG(ERROR) << "dup() failed.";
return false;
}
new_handle->fd = new_fd;
new_handle->auto_close = true;
if (close_self)
Close();
return true;
}
}