blob: 26dd4a380d6b02660e51c43a2af85ca6f5e23cec [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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
17namespace base {
18
19SharedMemory::SharedMemory()
20 : mapped_file_(-1),
James Robinson646469d2014-10-03 15:33:28 -070021 mapped_size_(0),
22 memory_(NULL),
23 read_only_(false),
24 requested_size_(0) {
25}
26
27SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only)
28 : mapped_file_(handle.fd),
James Robinson646469d2014-10-03 15:33:28 -070029 mapped_size_(0),
30 memory_(NULL),
31 read_only_(read_only),
32 requested_size_(0) {
33}
34
35SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only,
36 ProcessHandle process)
37 : mapped_file_(handle.fd),
James Robinson646469d2014-10-03 15:33:28 -070038 mapped_size_(0),
39 memory_(NULL),
40 read_only_(read_only),
41 requested_size_(0) {
42 NOTREACHED();
43}
44
45SharedMemory::~SharedMemory() {
Etienne Membrivesb1556b32014-12-16 13:56:09 +010046 Unmap();
James Robinson646469d2014-10-03 15:33:28 -070047 Close();
48}
49
50// static
51bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) {
52 return handle.fd >= 0;
53}
54
55// static
56SharedMemoryHandle SharedMemory::NULLHandle() {
57 return SharedMemoryHandle();
58}
59
60// static
61void 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 Luu235cf3d2015-06-11 10:01:25 -070067// static
68SharedMemoryHandle 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 Robinson646469d2014-10-03 15:33:28 -070076bool SharedMemory::CreateAndMapAnonymous(size_t size) {
77 // Untrusted code can't create descriptors or handles.
78 return false;
79}
80
81bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
82 // Untrusted code can't create descriptors or handles.
83 return false;
84}
85
86bool SharedMemory::Delete(const std::string& name) {
87 return false;
88}
89
90bool SharedMemory::Open(const std::string& name, bool read_only) {
91 return false;
92}
93
94bool 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
119bool 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
130SharedMemoryHandle SharedMemory::handle() const {
131 return FileDescriptor(mapped_file_, false);
132}
133
134void SharedMemory::Close() {
James Robinson646469d2014-10-03 15:33:28 -0700135 if (mapped_file_ > 0) {
136 if (close(mapped_file_) < 0)
137 DPLOG(ERROR) << "close";
138 mapped_file_ = -1;
139 }
140}
141
142void SharedMemory::LockDeprecated() {
143 NOTIMPLEMENTED();
144}
145
146void SharedMemory::UnlockDeprecated() {
147 NOTIMPLEMENTED();
148}
149
150bool 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 Membrivesb1556b32014-12-16 13:56:09 +0100168 if (close_self) {
169 Unmap();
James Robinson646469d2014-10-03 15:33:28 -0700170 Close();
Etienne Membrivesb1556b32014-12-16 13:56:09 +0100171 }
James Robinson646469d2014-10-03 15:33:28 -0700172 return true;
173}
174
175} // namespace base