James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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), |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 21 | mapped_size_(0), |
| 22 | memory_(NULL), |
| 23 | read_only_(false), |
| 24 | requested_size_(0) { |
| 25 | } |
| 26 | |
| 27 | SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) |
| 28 | : mapped_file_(handle.fd), |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 29 | mapped_size_(0), |
| 30 | memory_(NULL), |
| 31 | read_only_(read_only), |
| 32 | requested_size_(0) { |
| 33 | } |
| 34 | |
| 35 | SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only, |
| 36 | ProcessHandle process) |
| 37 | : mapped_file_(handle.fd), |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 38 | mapped_size_(0), |
| 39 | memory_(NULL), |
| 40 | read_only_(read_only), |
| 41 | requested_size_(0) { |
| 42 | NOTREACHED(); |
| 43 | } |
| 44 | |
| 45 | SharedMemory::~SharedMemory() { |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 46 | Unmap(); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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 | |
Viet-Trung Luu | 235cf3d | 2015-06-11 10:01:25 -0700 | [diff] [blame] | 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 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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() { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 135 | if (mapped_file_ > 0) { |
| 136 | if (close(mapped_file_) < 0) |
| 137 | DPLOG(ERROR) << "close"; |
| 138 | mapped_file_ = -1; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void SharedMemory::LockDeprecated() { |
| 143 | NOTIMPLEMENTED(); |
| 144 | } |
| 145 | |
| 146 | void SharedMemory::UnlockDeprecated() { |
| 147 | NOTIMPLEMENTED(); |
| 148 | } |
| 149 | |
| 150 | bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
| 151 | SharedMemoryHandle *new_handle, |
| 152 | bool close_self, |
| 153 | ShareMode share_mode) { |
| 154 | if (share_mode == SHARE_READONLY) { |
| 155 | // Untrusted code can't create descriptors or handles, which is needed to |
| 156 | // drop permissions. |
| 157 | return false; |
| 158 | } |
| 159 | const int new_fd = dup(mapped_file_); |
| 160 | if (new_fd < 0) { |
| 161 | DPLOG(ERROR) << "dup() failed."; |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | new_handle->fd = new_fd; |
| 166 | new_handle->auto_close = true; |
| 167 | |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 168 | if (close_self) { |
| 169 | Unmap(); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 170 | Close(); |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 171 | } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 172 | return true; |
| 173 | } |
| 174 | |
| 175 | } // namespace base |