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 "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
15 #include "base/files/scoped_file.h" | 15 #include "base/files/scoped_file.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/memory/shared_memory_helper.h" | 17 #include "base/memory/shared_memory_helper.h" |
18 #include "base/memory/shared_memory_tracker.h" | 18 #include "base/memory/shared_memory_tracker.h" |
19 #include "base/posix/eintr_wrapper.h" | 19 #include "base/posix/eintr_wrapper.h" |
20 #include "base/posix/safe_strerror.h" | 20 #include "base/posix/safe_strerror.h" |
21 #include "base/process/process_metrics.h" | 21 #include "base/process/process_metrics.h" |
22 #include "base/scoped_generic.h" | 22 #include "base/scoped_generic.h" |
23 #include "base/strings/utf_string_conversions.h" | 23 #include "base/strings/utf_string_conversions.h" |
24 #include "base/threading/thread_restrictions.h" | 24 #include "base/threading/thread_restrictions.h" |
25 #include "base/trace_event/trace_event.h" | 25 #include "base/trace_event/trace_event.h" |
| 26 #include "base/unguessable_token.h" |
26 #include "build/build_config.h" | 27 #include "build/build_config.h" |
27 | 28 |
28 #if defined(OS_ANDROID) | 29 #if defined(OS_ANDROID) |
29 #include "base/os_compat_android.h" | 30 #include "base/os_compat_android.h" |
30 #include "third_party/ashmem/ashmem.h" | 31 #include "third_party/ashmem/ashmem.h" |
31 #endif | 32 #endif |
32 | 33 |
33 namespace base { | 34 namespace base { |
34 | 35 |
35 SharedMemory::SharedMemory() | 36 SharedMemory::SharedMemory() |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 << "/dev/shm. Try 'sudo chmod 1777 /dev/shm' to fix."; | 205 << "/dev/shm. Try 'sudo chmod 1777 /dev/shm' to fix."; |
205 } | 206 } |
206 } | 207 } |
207 return false; | 208 return false; |
208 } | 209 } |
209 | 210 |
210 int mapped_file = -1; | 211 int mapped_file = -1; |
211 int readonly_mapped_file = -1; | 212 int readonly_mapped_file = -1; |
212 bool result = PrepareMapFile(std::move(fp), std::move(readonly_fd), | 213 bool result = PrepareMapFile(std::move(fp), std::move(readonly_fd), |
213 &mapped_file, &readonly_mapped_file); | 214 &mapped_file, &readonly_mapped_file); |
214 shm_ = SharedMemoryHandle::ImportHandle(mapped_file); | 215 shm_ = SharedMemoryHandle(base::FileDescriptor(mapped_file, false), |
215 readonly_shm_ = SharedMemoryHandle::ImportHandle(readonly_mapped_file); | 216 UnguessableToken::Create()); |
| 217 readonly_shm_ = SharedMemoryHandle( |
| 218 base::FileDescriptor(readonly_mapped_file, false), shm_.GetGUID()); |
216 return result; | 219 return result; |
217 } | 220 } |
218 | 221 |
219 // Our current implementation of shmem is with mmap()ing of files. | 222 // Our current implementation of shmem is with mmap()ing of files. |
220 // These files need to be deleted explicitly. | 223 // These files need to be deleted explicitly. |
221 // In practice this call is only needed for unit tests. | 224 // In practice this call is only needed for unit tests. |
222 bool SharedMemory::Delete(const std::string& name) { | 225 bool SharedMemory::Delete(const std::string& name) { |
223 FilePath path; | 226 FilePath path; |
224 if (!FilePathForMemoryName(name, &path)) | 227 if (!FilePathForMemoryName(name, &path)) |
225 return false; | 228 return false; |
(...skipping 16 matching lines...) Expand all Loading... |
242 ScopedFILE fp(base::OpenFile(path, mode)); | 245 ScopedFILE fp(base::OpenFile(path, mode)); |
243 ScopedFD readonly_fd(HANDLE_EINTR(open(path.value().c_str(), O_RDONLY))); | 246 ScopedFD readonly_fd(HANDLE_EINTR(open(path.value().c_str(), O_RDONLY))); |
244 if (!readonly_fd.is_valid()) { | 247 if (!readonly_fd.is_valid()) { |
245 DPLOG(ERROR) << "open(\"" << path.value() << "\", O_RDONLY) failed"; | 248 DPLOG(ERROR) << "open(\"" << path.value() << "\", O_RDONLY) failed"; |
246 return false; | 249 return false; |
247 } | 250 } |
248 int mapped_file = -1; | 251 int mapped_file = -1; |
249 int readonly_mapped_file = -1; | 252 int readonly_mapped_file = -1; |
250 bool result = PrepareMapFile(std::move(fp), std::move(readonly_fd), | 253 bool result = PrepareMapFile(std::move(fp), std::move(readonly_fd), |
251 &mapped_file, &readonly_mapped_file); | 254 &mapped_file, &readonly_mapped_file); |
252 shm_ = SharedMemoryHandle::ImportHandle(mapped_file); | 255 // This form of sharing shared memory is deprecated. https://crbug.com/345734. |
253 readonly_shm_ = SharedMemoryHandle::ImportHandle(readonly_mapped_file); | 256 // However, we can't get rid of it without a significant refactor because its |
| 257 // used to communicate between two versions of the same service process, very |
| 258 // early in the life cycle. |
| 259 // Technically, we should also pass the GUID from the original shared memory |
| 260 // region. We don't do that - this means that we will overcount this memory, |
| 261 // which thankfully isn't relevant since Chrome only communicates with a |
| 262 // single version of the service process. |
| 263 shm_ = SharedMemoryHandle(base::FileDescriptor(mapped_file, false), |
| 264 UnguessableToken::Create()); |
| 265 readonly_shm_ = SharedMemoryHandle( |
| 266 base::FileDescriptor(readonly_mapped_file, false), shm_.GetGUID()); |
254 return result; | 267 return result; |
255 } | 268 } |
256 #endif // !defined(OS_ANDROID) | 269 #endif // !defined(OS_ANDROID) |
257 | 270 |
258 bool SharedMemory::MapAt(off_t offset, size_t bytes) { | 271 bool SharedMemory::MapAt(off_t offset, size_t bytes) { |
259 if (!shm_.IsValid()) | 272 if (!shm_.IsValid()) |
260 return false; | 273 return false; |
261 | 274 |
262 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) | 275 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) |
263 return false; | 276 return false; |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 struct stat file_stat; | 385 struct stat file_stat; |
373 if (HANDLE_EINTR( | 386 if (HANDLE_EINTR( |
374 ::fstat(static_cast<int>(handle().GetHandle()), &file_stat)) != 0) | 387 ::fstat(static_cast<int>(handle().GetHandle()), &file_stat)) != 0) |
375 return false; | 388 return false; |
376 id->first = file_stat.st_dev; | 389 id->first = file_stat.st_dev; |
377 id->second = file_stat.st_ino; | 390 id->second = file_stat.st_ino; |
378 return true; | 391 return true; |
379 } | 392 } |
380 | 393 |
381 } // namespace base | 394 } // namespace base |
OLD | NEW |