| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/memory/shared_memory.h" | |
| 6 | |
| 7 #include <errno.h> | |
| 8 #include <fcntl.h> | |
| 9 #include <sys/mman.h> | |
| 10 #include <sys/stat.h> | |
| 11 #include <unistd.h> | |
| 12 | |
| 13 #include <limits> | |
| 14 | |
| 15 #include "base/logging.h" | |
| 16 | |
| 17 namespace base { | |
| 18 | |
| 19 SharedMemory::SharedMemory() | |
| 20 : mapped_file_(-1), | |
| 21 mapped_size_(0), | |
| 22 memory_(NULL), | |
| 23 read_only_(false), | |
| 24 requested_size_(0) { | |
| 25 } | |
| 26 | |
| 27 SharedMemory::SharedMemory(const SharedMemoryHandle& handle, bool read_only) | |
| 28 : mapped_file_(handle.fd), | |
| 29 mapped_size_(0), | |
| 30 memory_(NULL), | |
| 31 read_only_(read_only), | |
| 32 requested_size_(0) {} | |
| 33 | |
| 34 SharedMemory::SharedMemory(const SharedMemoryHandle& handle, | |
| 35 bool read_only, | |
| 36 ProcessHandle process) | |
| 37 : mapped_file_(handle.fd), | |
| 38 mapped_size_(0), | |
| 39 memory_(NULL), | |
| 40 read_only_(read_only), | |
| 41 requested_size_(0) { | |
| 42 NOTREACHED(); | |
| 43 } | |
| 44 | |
| 45 SharedMemory::~SharedMemory() { | |
| 46 Unmap(); | |
| 47 Close(); | |
| 48 } | |
| 49 | |
| 50 // static | |
| 51 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { | |
| 52 return handle.fd >= 0; | |
| 53 } | |
| 54 | |
| 55 // static | |
| 56 SharedMemoryHandle SharedMemory::NULLHandle() { | |
| 57 return SharedMemoryHandle(); | |
| 58 } | |
| 59 | |
| 60 // static | |
| 61 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { | |
| 62 DCHECK_GE(handle.fd, 0); | |
| 63 if (close(handle.fd) < 0) | |
| 64 DPLOG(ERROR) << "close"; | |
| 65 } | |
| 66 | |
| 67 // static | |
| 68 SharedMemoryHandle SharedMemory::DuplicateHandle( | |
| 69 const SharedMemoryHandle& handle) { | |
| 70 int duped_handle = HANDLE_EINTR(dup(handle.fd)); | |
| 71 if (duped_handle < 0) | |
| 72 return base::SharedMemory::NULLHandle(); | |
| 73 return base::FileDescriptor(duped_handle, true); | |
| 74 } | |
| 75 | |
| 76 bool SharedMemory::CreateAndMapAnonymous(size_t size) { | |
| 77 // Untrusted code can't create descriptors or handles. | |
| 78 return false; | |
| 79 } | |
| 80 | |
| 81 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { | |
| 82 // Untrusted code can't create descriptors or handles. | |
| 83 return false; | |
| 84 } | |
| 85 | |
| 86 bool SharedMemory::Delete(const std::string& name) { | |
| 87 return false; | |
| 88 } | |
| 89 | |
| 90 bool SharedMemory::Open(const std::string& name, bool read_only) { | |
| 91 return false; | |
| 92 } | |
| 93 | |
| 94 bool SharedMemory::MapAt(off_t offset, size_t bytes) { | |
| 95 if (mapped_file_ == -1) | |
| 96 return false; | |
| 97 | |
| 98 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) | |
| 99 return false; | |
| 100 | |
| 101 if (memory_) | |
| 102 return false; | |
| 103 | |
| 104 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), | |
| 105 MAP_SHARED, mapped_file_, offset); | |
| 106 | |
| 107 bool mmap_succeeded = memory_ != MAP_FAILED && memory_ != NULL; | |
| 108 if (mmap_succeeded) { | |
| 109 mapped_size_ = bytes; | |
| 110 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & | |
| 111 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); | |
| 112 } else { | |
| 113 memory_ = NULL; | |
| 114 } | |
| 115 | |
| 116 return mmap_succeeded; | |
| 117 } | |
| 118 | |
| 119 bool SharedMemory::Unmap() { | |
| 120 if (memory_ == NULL) | |
| 121 return false; | |
| 122 | |
| 123 if (munmap(memory_, mapped_size_) < 0) | |
| 124 DPLOG(ERROR) << "munmap"; | |
| 125 memory_ = NULL; | |
| 126 mapped_size_ = 0; | |
| 127 return true; | |
| 128 } | |
| 129 | |
| 130 SharedMemoryHandle SharedMemory::handle() const { | |
| 131 return FileDescriptor(mapped_file_, false); | |
| 132 } | |
| 133 | |
| 134 void SharedMemory::Close() { | |
| 135 if (mapped_file_ > 0) { | |
| 136 if (close(mapped_file_) < 0) | |
| 137 DPLOG(ERROR) << "close"; | |
| 138 mapped_file_ = -1; | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, | |
| 143 SharedMemoryHandle *new_handle, | |
| 144 bool close_self, | |
| 145 ShareMode share_mode) { | |
| 146 if (share_mode == SHARE_READONLY) { | |
| 147 // Untrusted code can't create descriptors or handles, which is needed to | |
| 148 // drop permissions. | |
| 149 return false; | |
| 150 } | |
| 151 const int new_fd = dup(mapped_file_); | |
| 152 if (new_fd < 0) { | |
| 153 DPLOG(ERROR) << "dup() failed."; | |
| 154 return false; | |
| 155 } | |
| 156 | |
| 157 new_handle->fd = new_fd; | |
| 158 new_handle->auto_close = true; | |
| 159 | |
| 160 if (close_self) { | |
| 161 Unmap(); | |
| 162 Close(); | |
| 163 } | |
| 164 return true; | |
| 165 } | |
| 166 | |
| 167 } // namespace base | |
| OLD | NEW |