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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 // the temporary files we create will just go into the buffer cache | 106 // the temporary files we create will just go into the buffer cache |
107 // and be deleted before they ever make it out to disk. | 107 // and be deleted before they ever make it out to disk. |
108 base::ThreadRestrictions::ScopedAllowIO allow_io; | 108 base::ThreadRestrictions::ScopedAllowIO allow_io; |
109 | 109 |
110 ScopedFILE fp; | 110 ScopedFILE fp; |
111 bool fix_size = true; | 111 bool fix_size = true; |
112 ScopedFD readonly_fd; | 112 ScopedFD readonly_fd; |
113 | 113 |
114 FilePath path; | 114 FilePath path; |
115 if (options.name_deprecated == NULL || options.name_deprecated->empty()) { | 115 if (options.name_deprecated == NULL || options.name_deprecated->empty()) { |
116 bool result = | 116 bool result = CreateAnonymousSharedMemory(options, &fp, &readonly_fd, &path, |
117 CreateAnonymousSharedMemory(options, &fp, &readonly_fd, &path); | 117 &last_error_); |
118 if (!result) | 118 if (!result) |
119 return false; | 119 return false; |
120 } else { | 120 } else { |
121 if (!FilePathForMemoryName(*options.name_deprecated, &path)) | 121 if (!FilePathForMemoryName(*options.name_deprecated, &path)) |
122 return false; | 122 return false; |
123 | 123 |
124 // Make sure that the file is opened without any permission | 124 // Make sure that the file is opened without any permission |
125 // to other users on the system. | 125 // to other users on the system. |
126 const mode_t kOwnerOnly = S_IRUSR | S_IWUSR; | 126 const mode_t kOwnerOnly = S_IRUSR | S_IWUSR; |
127 | 127 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 if (dir.value() == "/dev/shm") { | 198 if (dir.value() == "/dev/shm") { |
199 LOG(FATAL) << "This is frequently caused by incorrect permissions on " | 199 LOG(FATAL) << "This is frequently caused by incorrect permissions on " |
200 << "/dev/shm. Try 'sudo chmod 1777 /dev/shm' to fix."; | 200 << "/dev/shm. Try 'sudo chmod 1777 /dev/shm' to fix."; |
201 } | 201 } |
202 } | 202 } |
203 return false; | 203 return false; |
204 } | 204 } |
205 | 205 |
206 int mapped_file = -1; | 206 int mapped_file = -1; |
207 int readonly_mapped_file = -1; | 207 int readonly_mapped_file = -1; |
208 bool result = PrepareMapFile(std::move(fp), std::move(readonly_fd), | 208 bool result = |
209 &mapped_file, &readonly_mapped_file); | 209 PrepareMapFile(std::move(fp), std::move(readonly_fd), &mapped_file, |
| 210 &readonly_mapped_file, &last_error_); |
210 shm_ = SharedMemoryHandle(base::FileDescriptor(mapped_file, false), | 211 shm_ = SharedMemoryHandle(base::FileDescriptor(mapped_file, false), |
211 UnguessableToken::Create()); | 212 UnguessableToken::Create()); |
212 readonly_shm_ = SharedMemoryHandle( | 213 readonly_shm_ = SharedMemoryHandle( |
213 base::FileDescriptor(readonly_mapped_file, false), shm_.GetGUID()); | 214 base::FileDescriptor(readonly_mapped_file, false), shm_.GetGUID()); |
214 return result; | 215 return result; |
215 } | 216 } |
216 | 217 |
217 // Our current implementation of shmem is with mmap()ing of files. | 218 // Our current implementation of shmem is with mmap()ing of files. |
218 // These files need to be deleted explicitly. | 219 // These files need to be deleted explicitly. |
219 // In practice this call is only needed for unit tests. | 220 // In practice this call is only needed for unit tests. |
(...skipping 18 matching lines...) Expand all Loading... |
238 | 239 |
239 const char *mode = read_only ? "r" : "r+"; | 240 const char *mode = read_only ? "r" : "r+"; |
240 ScopedFILE fp(base::OpenFile(path, mode)); | 241 ScopedFILE fp(base::OpenFile(path, mode)); |
241 ScopedFD readonly_fd(HANDLE_EINTR(open(path.value().c_str(), O_RDONLY))); | 242 ScopedFD readonly_fd(HANDLE_EINTR(open(path.value().c_str(), O_RDONLY))); |
242 if (!readonly_fd.is_valid()) { | 243 if (!readonly_fd.is_valid()) { |
243 DPLOG(ERROR) << "open(\"" << path.value() << "\", O_RDONLY) failed"; | 244 DPLOG(ERROR) << "open(\"" << path.value() << "\", O_RDONLY) failed"; |
244 return false; | 245 return false; |
245 } | 246 } |
246 int mapped_file = -1; | 247 int mapped_file = -1; |
247 int readonly_mapped_file = -1; | 248 int readonly_mapped_file = -1; |
248 bool result = PrepareMapFile(std::move(fp), std::move(readonly_fd), | 249 bool result = |
249 &mapped_file, &readonly_mapped_file); | 250 PrepareMapFile(std::move(fp), std::move(readonly_fd), &mapped_file, |
| 251 &readonly_mapped_file, &last_error_); |
250 // This form of sharing shared memory is deprecated. https://crbug.com/345734. | 252 // This form of sharing shared memory is deprecated. https://crbug.com/345734. |
251 // However, we can't get rid of it without a significant refactor because its | 253 // However, we can't get rid of it without a significant refactor because its |
252 // used to communicate between two versions of the same service process, very | 254 // used to communicate between two versions of the same service process, very |
253 // early in the life cycle. | 255 // early in the life cycle. |
254 // Technically, we should also pass the GUID from the original shared memory | 256 // Technically, we should also pass the GUID from the original shared memory |
255 // region. We don't do that - this means that we will overcount this memory, | 257 // region. We don't do that - this means that we will overcount this memory, |
256 // which thankfully isn't relevant since Chrome only communicates with a | 258 // which thankfully isn't relevant since Chrome only communicates with a |
257 // single version of the service process. | 259 // single version of the service process. |
258 shm_ = SharedMemoryHandle(base::FileDescriptor(mapped_file, false), | 260 shm_ = SharedMemoryHandle(base::FileDescriptor(mapped_file, false), |
259 UnguessableToken::Create()); | 261 UnguessableToken::Create()); |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 struct stat file_stat; | 382 struct stat file_stat; |
381 if (HANDLE_EINTR( | 383 if (HANDLE_EINTR( |
382 ::fstat(static_cast<int>(handle().GetHandle()), &file_stat)) != 0) | 384 ::fstat(static_cast<int>(handle().GetHandle()), &file_stat)) != 0) |
383 return false; | 385 return false; |
384 id->first = file_stat.st_dev; | 386 id->first = file_stat.st_dev; |
385 id->second = file_stat.st_ino; | 387 id->second = file_stat.st_ino; |
386 return true; | 388 return true; |
387 } | 389 } |
388 | 390 |
389 } // namespace base | 391 } // namespace base |
OLD | NEW |