OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/shared_memory.h" | 5 #include "base/memory/shared_memory.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <stddef.h> | 9 #include <stddef.h> |
10 #include <sys/mman.h> | 10 #include <sys/mman.h> |
11 #include <sys/stat.h> | 11 #include <sys/stat.h> |
12 #include <unistd.h> | 12 #include <unistd.h> |
13 | 13 |
14 #include <limits> | 14 #include <limits> |
15 | 15 |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 | 17 |
18 namespace base { | 18 namespace base { |
19 | 19 |
20 SharedMemory::SharedMemory() | 20 SharedMemory::SharedMemory() |
21 : mapped_file_(-1), | 21 : mapped_size_(0), memory_(NULL), read_only_(false), requested_size_(0) {} |
22 mapped_size_(0), | |
23 memory_(NULL), | |
24 read_only_(false), | |
25 requested_size_(0) { | |
26 } | |
27 | 22 |
28 SharedMemory::SharedMemory(const SharedMemoryHandle& handle, bool read_only) | 23 SharedMemory::SharedMemory(const SharedMemoryHandle& handle, bool read_only) |
29 : mapped_file_(handle.fd), | 24 : shm_(handle), |
30 mapped_size_(0), | 25 mapped_size_(0), |
31 memory_(NULL), | 26 memory_(NULL), |
32 read_only_(read_only), | 27 read_only_(read_only), |
33 requested_size_(0) { | 28 requested_size_(0) {} |
34 } | |
35 | 29 |
36 SharedMemory::~SharedMemory() { | 30 SharedMemory::~SharedMemory() { |
37 Unmap(); | 31 Unmap(); |
38 Close(); | 32 Close(); |
39 } | 33 } |
40 | 34 |
41 // static | 35 // static |
42 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { | 36 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { |
43 return handle.fd >= 0; | 37 return handle.IsValid(); |
44 } | 38 } |
45 | 39 |
46 // static | 40 // static |
47 SharedMemoryHandle SharedMemory::NULLHandle() { | 41 SharedMemoryHandle SharedMemory::NULLHandle() { |
48 return SharedMemoryHandle(); | 42 return SharedMemoryHandle(); |
49 } | 43 } |
50 | 44 |
51 // static | 45 // static |
52 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { | 46 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { |
53 DCHECK_GE(handle.fd, 0); | 47 DCHECK(handle.IsValid()); |
54 if (close(handle.fd) < 0) | 48 handle.Close(); |
55 DPLOG(ERROR) << "close"; | |
56 } | 49 } |
57 | 50 |
58 // static | 51 // static |
59 SharedMemoryHandle SharedMemory::DuplicateHandle( | 52 SharedMemoryHandle SharedMemory::DuplicateHandle( |
60 const SharedMemoryHandle& handle) { | 53 const SharedMemoryHandle& handle) { |
61 int duped_handle = HANDLE_EINTR(dup(handle.fd)); | 54 return handle.Duplicate(); |
62 if (duped_handle < 0) | |
63 return base::SharedMemory::NULLHandle(); | |
64 return base::FileDescriptor(duped_handle, true); | |
65 } | 55 } |
66 | 56 |
67 bool SharedMemory::CreateAndMapAnonymous(size_t size) { | 57 bool SharedMemory::CreateAndMapAnonymous(size_t size) { |
68 // Untrusted code can't create descriptors or handles. | 58 // Untrusted code can't create descriptors or handles. |
69 return false; | 59 return false; |
70 } | 60 } |
71 | 61 |
72 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { | 62 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
73 // Untrusted code can't create descriptors or handles. | 63 // Untrusted code can't create descriptors or handles. |
74 return false; | 64 return false; |
75 } | 65 } |
76 | 66 |
77 bool SharedMemory::Delete(const std::string& name) { | 67 bool SharedMemory::Delete(const std::string& name) { |
78 return false; | 68 return false; |
79 } | 69 } |
80 | 70 |
81 bool SharedMemory::Open(const std::string& name, bool read_only) { | 71 bool SharedMemory::Open(const std::string& name, bool read_only) { |
82 return false; | 72 return false; |
83 } | 73 } |
84 | 74 |
85 bool SharedMemory::MapAt(off_t offset, size_t bytes) { | 75 bool SharedMemory::MapAt(off_t offset, size_t bytes) { |
86 if (mapped_file_ == -1) | 76 if (!shm_.IsValid()) |
87 return false; | 77 return false; |
88 | 78 |
89 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) | 79 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) |
90 return false; | 80 return false; |
91 | 81 |
92 if (memory_) | 82 if (memory_) |
93 return false; | 83 return false; |
94 | 84 |
95 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), | 85 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), |
96 MAP_SHARED, mapped_file_, offset); | 86 MAP_SHARED, shm_.GetHandle(), offset); |
97 | 87 |
98 bool mmap_succeeded = memory_ != MAP_FAILED && memory_ != NULL; | 88 bool mmap_succeeded = memory_ != MAP_FAILED && memory_ != NULL; |
99 if (mmap_succeeded) { | 89 if (mmap_succeeded) { |
100 mapped_size_ = bytes; | 90 mapped_size_ = bytes; |
101 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & | 91 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & |
102 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); | 92 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
103 } else { | 93 } else { |
104 memory_ = NULL; | 94 memory_ = NULL; |
105 } | 95 } |
106 | 96 |
107 return mmap_succeeded; | 97 return mmap_succeeded; |
108 } | 98 } |
109 | 99 |
110 bool SharedMemory::Unmap() { | 100 bool SharedMemory::Unmap() { |
111 if (memory_ == NULL) | 101 if (memory_ == NULL) |
112 return false; | 102 return false; |
113 | 103 |
114 if (munmap(memory_, mapped_size_) < 0) | 104 if (munmap(memory_, mapped_size_) < 0) |
115 DPLOG(ERROR) << "munmap"; | 105 DPLOG(ERROR) << "munmap"; |
116 memory_ = NULL; | 106 memory_ = NULL; |
117 mapped_size_ = 0; | 107 mapped_size_ = 0; |
118 return true; | 108 return true; |
119 } | 109 } |
120 | 110 |
121 SharedMemoryHandle SharedMemory::handle() const { | 111 SharedMemoryHandle SharedMemory::handle() const { |
122 return FileDescriptor(mapped_file_, false); | 112 SharedMemoryHandle handle_copy = shm_; |
| 113 handle_copy.SetOwnershipPassesToIPC(false); |
| 114 return handle_copy; |
123 } | 115 } |
124 | 116 |
125 SharedMemoryHandle SharedMemory::TakeHandle() { | 117 SharedMemoryHandle SharedMemory::TakeHandle() { |
126 FileDescriptor handle(mapped_file_, true); | 118 SharedMemoryHandle handle_copy = shm_; |
127 mapped_file_ = -1; | 119 handle_copy.SetOwnershipPassesToIPC(true); |
| 120 shm_ = SharedMemoryHandle(); |
128 memory_ = nullptr; | 121 memory_ = nullptr; |
129 mapped_size_ = 0; | 122 mapped_size_ = 0; |
130 return handle; | 123 return handle_copy; |
131 } | 124 } |
132 | 125 |
133 void SharedMemory::Close() { | 126 void SharedMemory::Close() { |
134 if (mapped_file_ > 0) { | 127 if (shm_.IsValid()) { |
135 if (close(mapped_file_) < 0) | 128 shm_.Close(); |
136 DPLOG(ERROR) << "close"; | 129 shm_ = SharedMemoryHandle(); |
137 mapped_file_ = -1; | |
138 } | 130 } |
139 } | 131 } |
140 | 132 |
141 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, | 133 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
142 SharedMemoryHandle *new_handle, | 134 SharedMemoryHandle *new_handle, |
143 bool close_self, | 135 bool close_self, |
144 ShareMode share_mode) { | 136 ShareMode share_mode) { |
145 if (share_mode == SHARE_READONLY) { | 137 if (share_mode == SHARE_READONLY) { |
146 // Untrusted code can't create descriptors or handles, which is needed to | 138 // Untrusted code can't create descriptors or handles, which is needed to |
147 // drop permissions. | 139 // drop permissions. |
148 return false; | 140 return false; |
149 } | 141 } |
150 const int new_fd = dup(mapped_file_); | |
151 if (new_fd < 0) { | |
152 DPLOG(ERROR) << "dup() failed."; | |
153 return false; | |
154 } | |
155 | 142 |
156 new_handle->fd = new_fd; | 143 *new_handle = shm_.Duplicate(); |
157 new_handle->auto_close = true; | |
158 | 144 |
159 if (close_self) { | 145 if (close_self) { |
160 Unmap(); | 146 Unmap(); |
161 Close(); | 147 Close(); |
162 } | 148 } |
163 return true; | 149 return new_handle->IsValid(); |
164 } | 150 } |
165 | 151 |
166 } // namespace base | 152 } // namespace base |
OLD | NEW |