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> |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 // the temporary files we create will just go into the buffer cache | 93 // the temporary files we create will just go into the buffer cache |
94 // and be deleted before they ever make it out to disk. | 94 // and be deleted before they ever make it out to disk. |
95 base::ThreadRestrictions::ScopedAllowIO allow_io; | 95 base::ThreadRestrictions::ScopedAllowIO allow_io; |
96 | 96 |
97 ScopedFILE fp; | 97 ScopedFILE fp; |
98 bool fix_size = true; | 98 bool fix_size = true; |
99 ScopedFD readonly_fd; | 99 ScopedFD readonly_fd; |
100 | 100 |
101 FilePath path; | 101 FilePath path; |
102 if (options.name_deprecated == NULL || options.name_deprecated->empty()) { | 102 if (options.name_deprecated == NULL || options.name_deprecated->empty()) { |
103 bool result = CreateAnonymousSharedMemory(options, &fp, &readonly_fd, &path, | 103 bool result = |
104 &last_error_); | 104 CreateAnonymousSharedMemory(options, &fp, &readonly_fd, &path); |
105 if (!result) | 105 if (!result) |
106 return false; | 106 return false; |
107 } else { | 107 } else { |
108 if (!FilePathForMemoryName(*options.name_deprecated, &path)) | 108 if (!FilePathForMemoryName(*options.name_deprecated, &path)) |
109 return false; | 109 return false; |
110 | 110 |
111 // Make sure that the file is opened without any permission | 111 // Make sure that the file is opened without any permission |
112 // to other users on the system. | 112 // to other users on the system. |
113 const mode_t kOwnerOnly = S_IRUSR | S_IWUSR; | 113 const mode_t kOwnerOnly = S_IRUSR | S_IWUSR; |
114 | 114 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 if (dir.value() == "/dev/shm") { | 185 if (dir.value() == "/dev/shm") { |
186 LOG(FATAL) << "This is frequently caused by incorrect permissions on " | 186 LOG(FATAL) << "This is frequently caused by incorrect permissions on " |
187 << "/dev/shm. Try 'sudo chmod 1777 /dev/shm' to fix."; | 187 << "/dev/shm. Try 'sudo chmod 1777 /dev/shm' to fix."; |
188 } | 188 } |
189 } | 189 } |
190 return false; | 190 return false; |
191 } | 191 } |
192 | 192 |
193 int mapped_file = -1; | 193 int mapped_file = -1; |
194 int readonly_mapped_file = -1; | 194 int readonly_mapped_file = -1; |
195 bool result = | 195 bool result = PrepareMapFile(std::move(fp), std::move(readonly_fd), |
196 PrepareMapFile(std::move(fp), std::move(readonly_fd), &mapped_file, | 196 &mapped_file, &readonly_mapped_file); |
197 &readonly_mapped_file, &last_error_); | |
198 shm_ = SharedMemoryHandle(base::FileDescriptor(mapped_file, false), | 197 shm_ = SharedMemoryHandle(base::FileDescriptor(mapped_file, false), |
199 options.size, UnguessableToken::Create()); | 198 options.size, UnguessableToken::Create()); |
200 readonly_shm_ = | 199 readonly_shm_ = |
201 SharedMemoryHandle(base::FileDescriptor(readonly_mapped_file, false), | 200 SharedMemoryHandle(base::FileDescriptor(readonly_mapped_file, false), |
202 options.size, shm_.GetGUID()); | 201 options.size, shm_.GetGUID()); |
203 return result; | 202 return result; |
204 } | 203 } |
205 | 204 |
206 // Our current implementation of shmem is with mmap()ing of files. | 205 // Our current implementation of shmem is with mmap()ing of files. |
207 // These files need to be deleted explicitly. | 206 // These files need to be deleted explicitly. |
(...skipping 19 matching lines...) Expand all Loading... |
227 | 226 |
228 const char *mode = read_only ? "r" : "r+"; | 227 const char *mode = read_only ? "r" : "r+"; |
229 ScopedFILE fp(base::OpenFile(path, mode)); | 228 ScopedFILE fp(base::OpenFile(path, mode)); |
230 ScopedFD readonly_fd(HANDLE_EINTR(open(path.value().c_str(), O_RDONLY))); | 229 ScopedFD readonly_fd(HANDLE_EINTR(open(path.value().c_str(), O_RDONLY))); |
231 if (!readonly_fd.is_valid()) { | 230 if (!readonly_fd.is_valid()) { |
232 DPLOG(ERROR) << "open(\"" << path.value() << "\", O_RDONLY) failed"; | 231 DPLOG(ERROR) << "open(\"" << path.value() << "\", O_RDONLY) failed"; |
233 return false; | 232 return false; |
234 } | 233 } |
235 int mapped_file = -1; | 234 int mapped_file = -1; |
236 int readonly_mapped_file = -1; | 235 int readonly_mapped_file = -1; |
237 bool result = | 236 bool result = PrepareMapFile(std::move(fp), std::move(readonly_fd), |
238 PrepareMapFile(std::move(fp), std::move(readonly_fd), &mapped_file, | 237 &mapped_file, &readonly_mapped_file); |
239 &readonly_mapped_file, &last_error_); | |
240 // This form of sharing shared memory is deprecated. https://crbug.com/345734. | 238 // This form of sharing shared memory is deprecated. https://crbug.com/345734. |
241 // However, we can't get rid of it without a significant refactor because its | 239 // However, we can't get rid of it without a significant refactor because its |
242 // used to communicate between two versions of the same service process, very | 240 // used to communicate between two versions of the same service process, very |
243 // early in the life cycle. | 241 // early in the life cycle. |
244 // Technically, we should also pass the GUID from the original shared memory | 242 // Technically, we should also pass the GUID from the original shared memory |
245 // region. We don't do that - this means that we will overcount this memory, | 243 // region. We don't do that - this means that we will overcount this memory, |
246 // which thankfully isn't relevant since Chrome only communicates with a | 244 // which thankfully isn't relevant since Chrome only communicates with a |
247 // single version of the service process. | 245 // single version of the service process. |
248 // We pass the size |0|, which is a dummy size and wrong, but otherwise | 246 // We pass the size |0|, which is a dummy size and wrong, but otherwise |
249 // harmless. | 247 // harmless. |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 return true; | 351 return true; |
354 } | 352 } |
355 #endif // !defined(OS_ANDROID) | 353 #endif // !defined(OS_ANDROID) |
356 | 354 |
357 SharedMemoryHandle SharedMemory::GetReadOnlyHandle() { | 355 SharedMemoryHandle SharedMemory::GetReadOnlyHandle() { |
358 CHECK(readonly_shm_.IsValid()); | 356 CHECK(readonly_shm_.IsValid()); |
359 return readonly_shm_.Duplicate(); | 357 return readonly_shm_.Duplicate(); |
360 } | 358 } |
361 | 359 |
362 } // namespace base | 360 } // namespace base |
OLD | NEW |