Roll src/third_party/skia 35f5576:fbcf0bd
[chromium-blink-merge.git] / base / memory / shared_memory_nacl.cc
blob8435b2ba295c0134b0161968052a4f12685f3cc3
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 inode_(0),
22 mapped_size_(0),
23 memory_(NULL),
24 read_only_(false),
25 requested_size_(0) {
28 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only)
29 : mapped_file_(handle.fd),
30 inode_(0),
31 mapped_size_(0),
32 memory_(NULL),
33 read_only_(read_only),
34 requested_size_(0) {
37 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only,
38 ProcessHandle process)
39 : mapped_file_(handle.fd),
40 inode_(0),
41 mapped_size_(0),
42 memory_(NULL),
43 read_only_(read_only),
44 requested_size_(0) {
45 NOTREACHED();
48 SharedMemory::~SharedMemory() {
49 Unmap();
50 Close();
53 // static
54 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) {
55 return handle.fd >= 0;
58 // static
59 SharedMemoryHandle SharedMemory::NULLHandle() {
60 return SharedMemoryHandle();
63 // static
64 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) {
65 DCHECK_GE(handle.fd, 0);
66 if (close(handle.fd) < 0)
67 DPLOG(ERROR) << "close";
70 bool SharedMemory::CreateAndMapAnonymous(size_t size) {
71 // Untrusted code can't create descriptors or handles.
72 return false;
75 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
76 // Untrusted code can't create descriptors or handles.
77 return false;
80 bool SharedMemory::Delete(const std::string& name) {
81 return false;
84 bool SharedMemory::Open(const std::string& name, bool read_only) {
85 return false;
88 bool SharedMemory::MapAt(off_t offset, size_t bytes) {
89 if (mapped_file_ == -1)
90 return false;
92 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max()))
93 return false;
95 if (memory_)
96 return false;
98 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE),
99 MAP_SHARED, mapped_file_, offset);
101 bool mmap_succeeded = memory_ != MAP_FAILED && memory_ != NULL;
102 if (mmap_succeeded) {
103 mapped_size_ = bytes;
104 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) &
105 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1));
106 } else {
107 memory_ = NULL;
110 return mmap_succeeded;
113 bool SharedMemory::Unmap() {
114 if (memory_ == NULL)
115 return false;
117 if (munmap(memory_, mapped_size_) < 0)
118 DPLOG(ERROR) << "munmap";
119 memory_ = NULL;
120 mapped_size_ = 0;
121 return true;
124 SharedMemoryHandle SharedMemory::handle() const {
125 return FileDescriptor(mapped_file_, false);
128 void SharedMemory::Close() {
129 if (mapped_file_ > 0) {
130 if (close(mapped_file_) < 0)
131 DPLOG(ERROR) << "close";
132 mapped_file_ = -1;
136 void SharedMemory::LockDeprecated() {
137 NOTIMPLEMENTED();
140 void SharedMemory::UnlockDeprecated() {
141 NOTIMPLEMENTED();
144 bool SharedMemory::ShareToProcessCommon(ProcessHandle process,
145 SharedMemoryHandle *new_handle,
146 bool close_self,
147 ShareMode share_mode) {
148 if (share_mode == SHARE_READONLY) {
149 // Untrusted code can't create descriptors or handles, which is needed to
150 // drop permissions.
151 return false;
153 const int new_fd = dup(mapped_file_);
154 if (new_fd < 0) {
155 DPLOG(ERROR) << "dup() failed.";
156 return false;
159 new_handle->fd = new_fd;
160 new_handle->auto_close = true;
162 if (close_self) {
163 Unmap();
164 Close();
166 return true;
169 } // namespace base