Create OWNERS file for renderer/resources.
[chromium-blink-merge.git] / base / memory / shared_memory_nacl.cc
blob26dd4a380d6b02660e51c43a2af85ca6f5e23cec
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.
5 #include "base/memory/shared_memory.h"
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <sys/mman.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
13 #include <limits>
15 #include "base/logging.h"
17 namespace base {
19 SharedMemory::SharedMemory()
20 : mapped_file_(-1),
21 mapped_size_(0),
22 memory_(NULL),
23 read_only_(false),
24 requested_size_(0) {
27 SharedMemory::SharedMemory(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) {
35 SharedMemory::SharedMemory(SharedMemoryHandle handle, 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();
45 SharedMemory::~SharedMemory() {
46 Unmap();
47 Close();
50 // static
51 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) {
52 return handle.fd >= 0;
55 // static
56 SharedMemoryHandle SharedMemory::NULLHandle() {
57 return SharedMemoryHandle();
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";
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);
76 bool SharedMemory::CreateAndMapAnonymous(size_t size) {
77 // Untrusted code can't create descriptors or handles.
78 return false;
81 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
82 // Untrusted code can't create descriptors or handles.
83 return false;
86 bool SharedMemory::Delete(const std::string& name) {
87 return false;
90 bool SharedMemory::Open(const std::string& name, bool read_only) {
91 return false;
94 bool SharedMemory::MapAt(off_t offset, size_t bytes) {
95 if (mapped_file_ == -1)
96 return false;
98 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max()))
99 return false;
101 if (memory_)
102 return false;
104 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE),
105 MAP_SHARED, mapped_file_, offset);
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;
116 return mmap_succeeded;
119 bool SharedMemory::Unmap() {
120 if (memory_ == NULL)
121 return false;
123 if (munmap(memory_, mapped_size_) < 0)
124 DPLOG(ERROR) << "munmap";
125 memory_ = NULL;
126 mapped_size_ = 0;
127 return true;
130 SharedMemoryHandle SharedMemory::handle() const {
131 return FileDescriptor(mapped_file_, false);
134 void SharedMemory::Close() {
135 if (mapped_file_ > 0) {
136 if (close(mapped_file_) < 0)
137 DPLOG(ERROR) << "close";
138 mapped_file_ = -1;
142 void SharedMemory::LockDeprecated() {
143 NOTIMPLEMENTED();
146 void SharedMemory::UnlockDeprecated() {
147 NOTIMPLEMENTED();
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;
159 const int new_fd = dup(mapped_file_);
160 if (new_fd < 0) {
161 DPLOG(ERROR) << "dup() failed.";
162 return false;
165 new_handle->fd = new_fd;
166 new_handle->auto_close = true;
168 if (close_self) {
169 Unmap();
170 Close();
172 return true;
175 } // namespace base