Remove chromeos==0 blacklist for test_isolation_mode.
[chromium-blink-merge.git] / base / memory / shared_memory_nacl.cc
blob41416a1d7f8303d9070b3b675ca26f82343df68c
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(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) {
35 SharedMemory::SharedMemory(const SharedMemoryHandle& handle,
36 bool read_only,
37 ProcessHandle process)
38 : mapped_file_(handle.fd),
39 mapped_size_(0),
40 memory_(NULL),
41 read_only_(read_only),
42 requested_size_(0) {
43 NOTREACHED();
46 SharedMemory::~SharedMemory() {
47 Unmap();
48 Close();
51 // static
52 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) {
53 return handle.fd >= 0;
56 // static
57 SharedMemoryHandle SharedMemory::NULLHandle() {
58 return SharedMemoryHandle();
61 // static
62 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) {
63 DCHECK_GE(handle.fd, 0);
64 if (close(handle.fd) < 0)
65 DPLOG(ERROR) << "close";
68 // static
69 SharedMemoryHandle SharedMemory::DuplicateHandle(
70 const SharedMemoryHandle& handle) {
71 int duped_handle = HANDLE_EINTR(dup(handle.fd));
72 if (duped_handle < 0)
73 return base::SharedMemory::NULLHandle();
74 return base::FileDescriptor(duped_handle, true);
77 bool SharedMemory::CreateAndMapAnonymous(size_t size) {
78 // Untrusted code can't create descriptors or handles.
79 return false;
82 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
83 // Untrusted code can't create descriptors or handles.
84 return false;
87 bool SharedMemory::Delete(const std::string& name) {
88 return false;
91 bool SharedMemory::Open(const std::string& name, bool read_only) {
92 return false;
95 bool SharedMemory::MapAt(off_t offset, size_t bytes) {
96 if (mapped_file_ == -1)
97 return false;
99 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max()))
100 return false;
102 if (memory_)
103 return false;
105 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE),
106 MAP_SHARED, mapped_file_, offset);
108 bool mmap_succeeded = memory_ != MAP_FAILED && memory_ != NULL;
109 if (mmap_succeeded) {
110 mapped_size_ = bytes;
111 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) &
112 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1));
113 } else {
114 memory_ = NULL;
117 return mmap_succeeded;
120 bool SharedMemory::Unmap() {
121 if (memory_ == NULL)
122 return false;
124 if (munmap(memory_, mapped_size_) < 0)
125 DPLOG(ERROR) << "munmap";
126 memory_ = NULL;
127 mapped_size_ = 0;
128 return true;
131 SharedMemoryHandle SharedMemory::handle() const {
132 return FileDescriptor(mapped_file_, false);
135 void SharedMemory::Close() {
136 if (mapped_file_ > 0) {
137 if (close(mapped_file_) < 0)
138 DPLOG(ERROR) << "close";
139 mapped_file_ = -1;
143 bool SharedMemory::ShareToProcessCommon(ProcessHandle process,
144 SharedMemoryHandle *new_handle,
145 bool close_self,
146 ShareMode share_mode) {
147 if (share_mode == SHARE_READONLY) {
148 // Untrusted code can't create descriptors or handles, which is needed to
149 // drop permissions.
150 return false;
152 const int new_fd = dup(mapped_file_);
153 if (new_fd < 0) {
154 DPLOG(ERROR) << "dup() failed.";
155 return false;
158 new_handle->fd = new_fd;
159 new_handle->auto_close = true;
161 if (close_self) {
162 Unmap();
163 Close();
165 return true;
168 } // namespace base