| 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 <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <sys/mman.h> | 8 #include <sys/mman.h> |
| 9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
| 10 #include <unistd.h> | 10 #include <unistd.h> |
| 11 | 11 |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/files/scoped_file.h" | 13 #include "base/files/scoped_file.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/posix/eintr_wrapper.h" | 15 #include "base/posix/eintr_wrapper.h" |
| 16 #include "base/posix/safe_strerror.h" | 16 #include "base/posix/safe_strerror.h" |
| 17 #include "base/process/process_metrics.h" | 17 #include "base/process/process_metrics.h" |
| 18 #include "base/profiler/scoped_tracker.h" | 18 #include "base/profiler/scoped_tracker.h" |
| 19 #include "base/scoped_generic.h" | 19 #include "base/scoped_generic.h" |
| 20 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 21 | 21 |
| 22 #if defined(OS_MACOSX) |
| 23 #include "base/mac/foundation_util.h" |
| 24 #endif // OS_MACOSX |
| 25 |
| 22 #if defined(OS_ANDROID) | 26 #if defined(OS_ANDROID) |
| 23 #include "base/os_compat_android.h" | 27 #include "base/os_compat_android.h" |
| 24 #include "third_party/ashmem/ashmem.h" | 28 #include "third_party/ashmem/ashmem.h" |
| 25 #endif | 29 #endif |
| 26 | 30 |
| 27 namespace base { | 31 namespace base { |
| 28 | 32 |
| 29 namespace { | 33 namespace { |
| 30 | 34 |
| 31 struct ScopedPathUnlinkerTraits { | 35 struct ScopedPathUnlinkerTraits { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 | 103 |
| 100 SharedMemory::SharedMemory() | 104 SharedMemory::SharedMemory() |
| 101 : mapped_file_(-1), | 105 : mapped_file_(-1), |
| 102 readonly_mapped_file_(-1), | 106 readonly_mapped_file_(-1), |
| 103 mapped_size_(0), | 107 mapped_size_(0), |
| 104 memory_(NULL), | 108 memory_(NULL), |
| 105 read_only_(false), | 109 read_only_(false), |
| 106 requested_size_(0) { | 110 requested_size_(0) { |
| 107 } | 111 } |
| 108 | 112 |
| 109 SharedMemory::SharedMemory(const SharedMemoryHandle& handle, bool read_only) | 113 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) |
| 110 : mapped_file_(handle.fd), | 114 : mapped_file_(handle.fd), |
| 111 readonly_mapped_file_(-1), | 115 readonly_mapped_file_(-1), |
| 112 mapped_size_(0), | 116 mapped_size_(0), |
| 113 memory_(NULL), | 117 memory_(NULL), |
| 114 read_only_(read_only), | 118 read_only_(read_only), |
| 115 requested_size_(0) { | 119 requested_size_(0) { |
| 116 } | 120 } |
| 117 | 121 |
| 118 SharedMemory::SharedMemory(const SharedMemoryHandle& handle, | 122 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only, |
| 119 bool read_only, | |
| 120 ProcessHandle process) | 123 ProcessHandle process) |
| 121 : mapped_file_(handle.fd), | 124 : mapped_file_(handle.fd), |
| 122 readonly_mapped_file_(-1), | 125 readonly_mapped_file_(-1), |
| 123 mapped_size_(0), | 126 mapped_size_(0), |
| 124 memory_(NULL), | 127 memory_(NULL), |
| 125 read_only_(read_only), | 128 read_only_(read_only), |
| 126 requested_size_(0) { | 129 requested_size_(0) { |
| 127 // We don't handle this case yet (note the ignored parameter); let's die if | 130 // We don't handle this case yet (note the ignored parameter); let's die if |
| 128 // someone comes calling. | 131 // someone comes calling. |
| 129 NOTREACHED(); | 132 NOTREACHED(); |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 if (fstat(fileno(fp.get()), &stat) != 0) | 283 if (fstat(fileno(fp.get()), &stat) != 0) |
| 281 return false; | 284 return false; |
| 282 const size_t current_size = stat.st_size; | 285 const size_t current_size = stat.st_size; |
| 283 if (current_size != options.size) { | 286 if (current_size != options.size) { |
| 284 if (HANDLE_EINTR(ftruncate(fileno(fp.get()), options.size)) != 0) | 287 if (HANDLE_EINTR(ftruncate(fileno(fp.get()), options.size)) != 0) |
| 285 return false; | 288 return false; |
| 286 } | 289 } |
| 287 requested_size_ = options.size; | 290 requested_size_ = options.size; |
| 288 } | 291 } |
| 289 if (fp == NULL) { | 292 if (fp == NULL) { |
| 293 #if !defined(OS_MACOSX) |
| 290 PLOG(ERROR) << "Creating shared memory in " << path.value() << " failed"; | 294 PLOG(ERROR) << "Creating shared memory in " << path.value() << " failed"; |
| 291 FilePath dir = path.DirName(); | 295 FilePath dir = path.DirName(); |
| 292 if (access(dir.value().c_str(), W_OK | X_OK) < 0) { | 296 if (access(dir.value().c_str(), W_OK | X_OK) < 0) { |
| 293 PLOG(ERROR) << "Unable to access(W_OK|X_OK) " << dir.value(); | 297 PLOG(ERROR) << "Unable to access(W_OK|X_OK) " << dir.value(); |
| 294 if (dir.value() == "/dev/shm") { | 298 if (dir.value() == "/dev/shm") { |
| 295 LOG(FATAL) << "This is frequently caused by incorrect permissions on " | 299 LOG(FATAL) << "This is frequently caused by incorrect permissions on " |
| 296 << "/dev/shm. Try 'sudo chmod 1777 /dev/shm' to fix."; | 300 << "/dev/shm. Try 'sudo chmod 1777 /dev/shm' to fix."; |
| 297 } | 301 } |
| 298 } | 302 } |
| 303 #else |
| 304 PLOG(ERROR) << "Creating shared memory in " << path.value() << " failed"; |
| 305 #endif |
| 299 return false; | 306 return false; |
| 300 } | 307 } |
| 301 | 308 |
| 302 return PrepareMapFile(fp.Pass(), readonly_fd.Pass()); | 309 return PrepareMapFile(fp.Pass(), readonly_fd.Pass()); |
| 303 } | 310 } |
| 304 | 311 |
| 305 // Our current implementation of shmem is with mmap()ing of files. | 312 // Our current implementation of shmem is with mmap()ing of files. |
| 306 // These files need to be deleted explicitly. | 313 // These files need to be deleted explicitly. |
| 307 // In practice this call is only needed for unit tests. | 314 // In practice this call is only needed for unit tests. |
| 308 bool SharedMemory::Delete(const std::string& name) { | 315 bool SharedMemory::Delete(const std::string& name) { |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 FilePath* path) { | 452 FilePath* path) { |
| 446 // mem_name will be used for a filename; make sure it doesn't | 453 // mem_name will be used for a filename; make sure it doesn't |
| 447 // contain anything which will confuse us. | 454 // contain anything which will confuse us. |
| 448 DCHECK_EQ(std::string::npos, mem_name.find('/')); | 455 DCHECK_EQ(std::string::npos, mem_name.find('/')); |
| 449 DCHECK_EQ(std::string::npos, mem_name.find('\0')); | 456 DCHECK_EQ(std::string::npos, mem_name.find('\0')); |
| 450 | 457 |
| 451 FilePath temp_dir; | 458 FilePath temp_dir; |
| 452 if (!GetShmemTempDir(false, &temp_dir)) | 459 if (!GetShmemTempDir(false, &temp_dir)) |
| 453 return false; | 460 return false; |
| 454 | 461 |
| 462 #if !defined(OS_MACOSX) |
| 455 #if defined(GOOGLE_CHROME_BUILD) | 463 #if defined(GOOGLE_CHROME_BUILD) |
| 456 std::string name_base = std::string("com.google.Chrome"); | 464 std::string name_base = std::string("com.google.Chrome"); |
| 457 #else | 465 #else |
| 458 std::string name_base = std::string("org.chromium.Chromium"); | 466 std::string name_base = std::string("org.chromium.Chromium"); |
| 459 #endif | 467 #endif |
| 468 #else // OS_MACOSX |
| 469 std::string name_base = std::string(base::mac::BaseBundleID()); |
| 470 #endif // OS_MACOSX |
| 460 *path = temp_dir.AppendASCII(name_base + ".shmem." + mem_name); | 471 *path = temp_dir.AppendASCII(name_base + ".shmem." + mem_name); |
| 461 return true; | 472 return true; |
| 462 } | 473 } |
| 463 #endif // !defined(OS_ANDROID) | 474 #endif // !defined(OS_ANDROID) |
| 464 | 475 |
| 465 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, | 476 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
| 466 SharedMemoryHandle* new_handle, | 477 SharedMemoryHandle* new_handle, |
| 467 bool close_self, | 478 bool close_self, |
| 468 ShareMode share_mode) { | 479 ShareMode share_mode) { |
| 469 int handle_to_dup = -1; | 480 int handle_to_dup = -1; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 490 | 501 |
| 491 if (close_self) { | 502 if (close_self) { |
| 492 Unmap(); | 503 Unmap(); |
| 493 Close(); | 504 Close(); |
| 494 } | 505 } |
| 495 | 506 |
| 496 return true; | 507 return true; |
| 497 } | 508 } |
| 498 | 509 |
| 499 } // namespace base | 510 } // namespace base |
| OLD | NEW |