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