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 <sys/mman.h> | 9 #include <sys/mman.h> |
10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
11 #include <sys/types.h> | 11 #include <sys/types.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/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/process/process_metrics.h" | 18 #include "base/process/process_metrics.h" |
| 19 #include "base/profiler/scoped_tracker.h" |
19 #include "base/safe_strerror_posix.h" | 20 #include "base/safe_strerror_posix.h" |
20 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
21 #include "base/synchronization/lock.h" | 22 #include "base/synchronization/lock.h" |
22 #include "base/threading/platform_thread.h" | 23 #include "base/threading/platform_thread.h" |
23 #include "base/threading/thread_restrictions.h" | 24 #include "base/threading/thread_restrictions.h" |
24 | 25 |
25 #if defined(OS_MACOSX) | 26 #if defined(OS_MACOSX) |
26 #include "base/mac/foundation_util.h" | 27 #include "base/mac/foundation_util.h" |
27 #endif // OS_MACOSX | 28 #endif // OS_MACOSX |
28 | 29 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 } | 112 } |
112 | 113 |
113 #if !defined(OS_ANDROID) | 114 #if !defined(OS_ANDROID) |
114 // Chromium mostly only uses the unique/private shmem as specified by | 115 // Chromium mostly only uses the unique/private shmem as specified by |
115 // "name == L"". The exception is in the StatsTable. | 116 // "name == L"". The exception is in the StatsTable. |
116 // TODO(jrg): there is no way to "clean up" all unused named shmem if | 117 // TODO(jrg): there is no way to "clean up" all unused named shmem if |
117 // we restart from a crash. (That isn't a new problem, but it is a problem.) | 118 // we restart from a crash. (That isn't a new problem, but it is a problem.) |
118 // In case we want to delete it later, it may be useful to save the value | 119 // In case we want to delete it later, it may be useful to save the value |
119 // of mem_filename after FilePathForMemoryName(). | 120 // of mem_filename after FilePathForMemoryName(). |
120 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { | 121 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
| 122 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466437 |
| 123 // is fixed. |
| 124 tracked_objects::ScopedTracker tracking_profile1( |
| 125 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 126 "466437 SharedMemory::Create::Start")); |
121 DCHECK_EQ(-1, mapped_file_); | 127 DCHECK_EQ(-1, mapped_file_); |
122 if (options.size == 0) return false; | 128 if (options.size == 0) return false; |
123 | 129 |
124 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) | 130 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) |
125 return false; | 131 return false; |
126 | 132 |
127 // This function theoretically can block on the disk, but realistically | 133 // This function theoretically can block on the disk, but realistically |
128 // the temporary files we create will just go into the buffer cache | 134 // the temporary files we create will just go into the buffer cache |
129 // and be deleted before they ever make it out to disk. | 135 // and be deleted before they ever make it out to disk. |
130 base::ThreadRestrictions::ScopedAllowIO allow_io; | 136 base::ThreadRestrictions::ScopedAllowIO allow_io; |
131 | 137 |
132 ScopedFILE fp; | 138 ScopedFILE fp; |
133 bool fix_size = true; | 139 bool fix_size = true; |
134 ScopedFD readonly_fd; | 140 ScopedFD readonly_fd; |
135 | 141 |
136 FilePath path; | 142 FilePath path; |
137 if (options.name_deprecated == NULL || options.name_deprecated->empty()) { | 143 if (options.name_deprecated == NULL || options.name_deprecated->empty()) { |
138 // It doesn't make sense to have a open-existing private piece of shmem | 144 // It doesn't make sense to have a open-existing private piece of shmem |
139 DCHECK(!options.open_existing_deprecated); | 145 DCHECK(!options.open_existing_deprecated); |
140 // Q: Why not use the shm_open() etc. APIs? | 146 // Q: Why not use the shm_open() etc. APIs? |
141 // A: Because they're limited to 4mb on OS X. FFFFFFFUUUUUUUUUUU | 147 // A: Because they're limited to 4mb on OS X. FFFFFFFUUUUUUUUUUU |
142 FilePath directory; | 148 FilePath directory; |
143 if (GetShmemTempDir(options.executable, &directory)) | 149 if (GetShmemTempDir(options.executable, &directory)) { |
| 150 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466437 |
| 151 // is fixed. |
| 152 tracked_objects::ScopedTracker tracking_profile2( |
| 153 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 154 "466437 SharedMemory::Create::OpenTemporaryFile")); |
144 fp.reset(CreateAndOpenTemporaryFileInDir(directory, &path)); | 155 fp.reset(CreateAndOpenTemporaryFileInDir(directory, &path)); |
| 156 } |
145 | 157 |
146 if (fp) { | 158 if (fp) { |
147 if (options.share_read_only) { | 159 if (options.share_read_only) { |
| 160 // TODO(erikchen): Remove ScopedTracker below once |
| 161 // http://crbug.com/466437 is fixed. |
| 162 tracked_objects::ScopedTracker tracking_profile3( |
| 163 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 164 "466437 SharedMemory::Create::OpenReadonly")); |
148 // Also open as readonly so that we can ShareReadOnlyToProcess. | 165 // Also open as readonly so that we can ShareReadOnlyToProcess. |
149 readonly_fd.reset(HANDLE_EINTR(open(path.value().c_str(), O_RDONLY))); | 166 readonly_fd.reset(HANDLE_EINTR(open(path.value().c_str(), O_RDONLY))); |
150 if (!readonly_fd.is_valid()) { | 167 if (!readonly_fd.is_valid()) { |
151 DPLOG(ERROR) << "open(\"" << path.value() << "\", O_RDONLY) failed"; | 168 DPLOG(ERROR) << "open(\"" << path.value() << "\", O_RDONLY) failed"; |
152 fp.reset(); | 169 fp.reset(); |
153 return false; | 170 return false; |
154 } | 171 } |
155 } | 172 } |
| 173 |
| 174 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466437 |
| 175 // is fixed. |
| 176 tracked_objects::ScopedTracker tracking_profile4( |
| 177 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 178 "466437 SharedMemory::Create::Unlink")); |
156 // Deleting the file prevents anyone else from mapping it in (making it | 179 // Deleting the file prevents anyone else from mapping it in (making it |
157 // private), and prevents the need for cleanup (once the last fd is | 180 // private), and prevents the need for cleanup (once the last fd is |
158 // closed, it is truly freed). | 181 // closed, it is truly freed). |
159 if (unlink(path.value().c_str())) | 182 if (unlink(path.value().c_str())) |
160 PLOG(WARNING) << "unlink"; | 183 PLOG(WARNING) << "unlink"; |
161 } | 184 } |
162 } else { | 185 } else { |
163 if (!FilePathForMemoryName(*options.name_deprecated, &path)) | 186 if (!FilePathForMemoryName(*options.name_deprecated, &path)) |
164 return false; | 187 return false; |
165 | 188 |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 bool close_self, | 469 bool close_self, |
447 ShareMode share_mode) { | 470 ShareMode share_mode) { |
448 int handle_to_dup = -1; | 471 int handle_to_dup = -1; |
449 switch(share_mode) { | 472 switch(share_mode) { |
450 case SHARE_CURRENT_MODE: | 473 case SHARE_CURRENT_MODE: |
451 handle_to_dup = mapped_file_; | 474 handle_to_dup = mapped_file_; |
452 break; | 475 break; |
453 case SHARE_READONLY: | 476 case SHARE_READONLY: |
454 // We could imagine re-opening the file from /dev/fd, but that can't make | 477 // We could imagine re-opening the file from /dev/fd, but that can't make |
455 // it readonly on Mac: https://codereview.chromium.org/27265002/#msg10 | 478 // it readonly on Mac: https://codereview.chromium.org/27265002/#msg10 |
456 CHECK(readonly_mapped_file_ >= 0); | 479 CHECK_GE(readonly_mapped_file_, 0); |
457 handle_to_dup = readonly_mapped_file_; | 480 handle_to_dup = readonly_mapped_file_; |
458 break; | 481 break; |
459 } | 482 } |
460 | 483 |
461 const int new_fd = dup(handle_to_dup); | 484 const int new_fd = dup(handle_to_dup); |
462 if (new_fd < 0) { | 485 if (new_fd < 0) { |
463 DPLOG(ERROR) << "dup() failed."; | 486 DPLOG(ERROR) << "dup() failed."; |
464 return false; | 487 return false; |
465 } | 488 } |
466 | 489 |
467 new_handle->fd = new_fd; | 490 new_handle->fd = new_fd; |
468 new_handle->auto_close = true; | 491 new_handle->auto_close = true; |
469 | 492 |
470 if (close_self) { | 493 if (close_self) { |
471 Unmap(); | 494 Unmap(); |
472 Close(); | 495 Close(); |
473 } | 496 } |
474 | 497 |
475 return true; | 498 return true; |
476 } | 499 } |
477 | 500 |
478 } // namespace base | 501 } // namespace base |
OLD | NEW |