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 #ifndef BASE_MEMORY_SHARED_MEMORY_H_ | 5 #ifndef BASE_MEMORY_SHARED_MEMORY_H_ |
6 #define BASE_MEMORY_SHARED_MEMORY_H_ | 6 #define BASE_MEMORY_SHARED_MEMORY_H_ |
7 | 7 |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 | 9 |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #if defined(OS_POSIX) | 12 #if defined(OS_POSIX) |
13 #include <stdio.h> | 13 #include <stdio.h> |
14 #include <sys/types.h> | 14 #include <sys/types.h> |
15 #include <semaphore.h> | 15 #include <semaphore.h> |
16 #endif | 16 #endif |
17 | 17 |
18 #include "base/base_export.h" | 18 #include "base/base_export.h" |
19 #include "base/basictypes.h" | 19 #include "base/basictypes.h" |
20 #include "base/process/process_handle.h" | 20 #include "base/process/process_handle.h" |
21 | 21 |
22 #if defined(OS_POSIX) | 22 #if defined(OS_POSIX) |
23 #include "base/file_descriptor_posix.h" | 23 #include "base/file_descriptor_posix.h" |
| 24 #include "base/file_util.h" |
24 #endif | 25 #endif |
25 | 26 |
26 namespace base { | 27 namespace base { |
27 | 28 |
28 class FilePath; | 29 class FilePath; |
29 | 30 |
30 // SharedMemoryHandle is a platform specific type which represents | 31 // SharedMemoryHandle is a platform specific type which represents |
31 // the underlying OS handle to a shared memory segment. | 32 // the underlying OS handle to a shared memory segment. |
32 #if defined(OS_WIN) | 33 #if defined(OS_WIN) |
33 typedef HANDLE SharedMemoryHandle; | 34 typedef HANDLE SharedMemoryHandle; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 | 74 |
74 #if defined(OS_WIN) | 75 #if defined(OS_WIN) |
75 // Similar to the default constructor, except that this allows for | 76 // Similar to the default constructor, except that this allows for |
76 // calling Lock() to acquire the named mutex before either Create or Open | 77 // calling Lock() to acquire the named mutex before either Create or Open |
77 // are called on Windows. | 78 // are called on Windows. |
78 explicit SharedMemory(const std::wstring& name); | 79 explicit SharedMemory(const std::wstring& name); |
79 #endif | 80 #endif |
80 | 81 |
81 // Create a new SharedMemory object from an existing, open | 82 // Create a new SharedMemory object from an existing, open |
82 // shared memory file. | 83 // shared memory file. |
| 84 // |
| 85 // WARNING: This does not reduce the OS-level permissions on the handle; it |
| 86 // only affects how the SharedMemory will be mmapped. Use |
| 87 // ShareReadOnlyToProcess to drop permissions. TODO(jln,jyasskin): DCHECK |
| 88 // that |read_only| matches the permissions of the handle. |
83 SharedMemory(SharedMemoryHandle handle, bool read_only); | 89 SharedMemory(SharedMemoryHandle handle, bool read_only); |
84 | 90 |
85 // Create a new SharedMemory object from an existing, open | 91 // Create a new SharedMemory object from an existing, open |
86 // shared memory file that was created by a remote process and not shared | 92 // shared memory file that was created by a remote process and not shared |
87 // to the current process. | 93 // to the current process. |
88 SharedMemory(SharedMemoryHandle handle, bool read_only, | 94 SharedMemory(SharedMemoryHandle handle, bool read_only, |
89 ProcessHandle process); | 95 ProcessHandle process); |
90 | 96 |
91 // Closes any open files. | 97 // Closes any open files. |
92 ~SharedMemory(); | 98 ~SharedMemory(); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 // are technically only unique to a single filesystem. However, we always | 188 // are technically only unique to a single filesystem. However, we always |
183 // allocate shared memory backing files from the same directory, so will end | 189 // allocate shared memory backing files from the same directory, so will end |
184 // up on the same filesystem. | 190 // up on the same filesystem. |
185 SharedMemoryId id() const { return inode_; } | 191 SharedMemoryId id() const { return inode_; } |
186 #endif | 192 #endif |
187 | 193 |
188 // Closes the open shared memory segment. | 194 // Closes the open shared memory segment. |
189 // It is safe to call Close repeatedly. | 195 // It is safe to call Close repeatedly. |
190 void Close(); | 196 void Close(); |
191 | 197 |
| 198 // Shares the shared memory to another process. Attempts to create a |
| 199 // platform-specific new_handle which can be used in a remote process to read |
| 200 // the shared memory file. new_handle is an output parameter to receive the |
| 201 // handle for use in the remote process. |
| 202 // |
| 203 // |*this| must have been initialized using one of the Create*() or Open() |
| 204 // methods. If it was constructed from a SharedMemoryHandle, this call will |
| 205 // CHECK-fail. |
| 206 // |
| 207 // Returns true on success, false otherwise. |
| 208 bool ShareReadOnlyToProcess(ProcessHandle process, |
| 209 SharedMemoryHandle* new_handle) { |
| 210 return ShareToProcessCommon(process, new_handle, false, SHARE_READONLY); |
| 211 } |
| 212 |
| 213 // Logically equivalent to: |
| 214 // bool ok = ShareReadOnlyToProcess(process, new_handle); |
| 215 // Close(); |
| 216 // return ok; |
| 217 // Note that the memory is unmapped by calling this method, regardless of the |
| 218 // return value. |
| 219 bool GiveReadOnlyToProcess(ProcessHandle process, |
| 220 SharedMemoryHandle* new_handle) { |
| 221 return ShareToProcessCommon(process, new_handle, true, SHARE_READONLY); |
| 222 } |
| 223 |
192 // Shares the shared memory to another process. Attempts | 224 // Shares the shared memory to another process. Attempts |
193 // to create a platform-specific new_handle which can be | 225 // to create a platform-specific new_handle which can be |
194 // used in a remote process to access the shared memory | 226 // used in a remote process to access the shared memory |
195 // file. new_handle is an ouput parameter to receive | 227 // file. new_handle is an output parameter to receive |
196 // the handle for use in the remote process. | 228 // the handle for use in the remote process. |
197 // Returns true on success, false otherwise. | 229 // Returns true on success, false otherwise. |
198 bool ShareToProcess(ProcessHandle process, | 230 bool ShareToProcess(ProcessHandle process, |
199 SharedMemoryHandle* new_handle) { | 231 SharedMemoryHandle* new_handle) { |
200 return ShareToProcessCommon(process, new_handle, false); | 232 return ShareToProcessCommon(process, new_handle, false, SHARE_CURRENT_MODE); |
201 } | 233 } |
202 | 234 |
203 // Logically equivalent to: | 235 // Logically equivalent to: |
204 // bool ok = ShareToProcess(process, new_handle); | 236 // bool ok = ShareToProcess(process, new_handle); |
205 // Close(); | 237 // Close(); |
206 // return ok; | 238 // return ok; |
207 // Note that the memory is unmapped by calling this method, regardless of the | 239 // Note that the memory is unmapped by calling this method, regardless of the |
208 // return value. | 240 // return value. |
209 bool GiveToProcess(ProcessHandle process, | 241 bool GiveToProcess(ProcessHandle process, |
210 SharedMemoryHandle* new_handle) { | 242 SharedMemoryHandle* new_handle) { |
211 return ShareToProcessCommon(process, new_handle, true); | 243 return ShareToProcessCommon(process, new_handle, true, SHARE_CURRENT_MODE); |
212 } | 244 } |
213 | 245 |
214 // Locks the shared memory. | 246 // Locks the shared memory. |
215 // | 247 // |
216 // WARNING: on POSIX the memory locking primitive only works across | 248 // WARNING: on POSIX the memory locking primitive only works across |
217 // processes, not across threads. The Lock method is not currently | 249 // processes, not across threads. The Lock method is not currently |
218 // used in inner loops, so we protect against multiple threads in a | 250 // used in inner loops, so we protect against multiple threads in a |
219 // critical section using a class global lock. | 251 // critical section using a class global lock. |
220 void Lock(); | 252 void Lock(); |
221 | 253 |
222 #if defined(OS_WIN) | 254 #if defined(OS_WIN) |
223 // A Lock() implementation with a timeout that also allows setting | 255 // A Lock() implementation with a timeout that also allows setting |
224 // security attributes on the mutex. sec_attr may be NULL. | 256 // security attributes on the mutex. sec_attr may be NULL. |
225 // Returns true if the Lock() has been acquired, false if the timeout was | 257 // Returns true if the Lock() has been acquired, false if the timeout was |
226 // reached. | 258 // reached. |
227 bool Lock(uint32 timeout_ms, SECURITY_ATTRIBUTES* sec_attr); | 259 bool Lock(uint32 timeout_ms, SECURITY_ATTRIBUTES* sec_attr); |
228 #endif | 260 #endif |
229 | 261 |
230 // Releases the shared memory lock. | 262 // Releases the shared memory lock. |
231 void Unlock(); | 263 void Unlock(); |
232 | 264 |
233 private: | 265 private: |
234 #if defined(OS_POSIX) && !defined(OS_NACL) | 266 #if defined(OS_POSIX) && !defined(OS_NACL) |
235 bool PrepareMapFile(FILE *fp); | 267 bool PrepareMapFile(file_util::ScopedFILE fp, file_util::ScopedFD readonly); |
236 bool FilePathForMemoryName(const std::string& mem_name, FilePath* path); | 268 bool FilePathForMemoryName(const std::string& mem_name, FilePath* path); |
237 void LockOrUnlockCommon(int function); | 269 void LockOrUnlockCommon(int function); |
238 #endif | 270 #endif |
| 271 enum ShareMode { |
| 272 SHARE_READONLY, |
| 273 SHARE_CURRENT_MODE, |
| 274 }; |
239 bool ShareToProcessCommon(ProcessHandle process, | 275 bool ShareToProcessCommon(ProcessHandle process, |
240 SharedMemoryHandle* new_handle, | 276 SharedMemoryHandle* new_handle, |
241 bool close_self); | 277 bool close_self, |
| 278 ShareMode); |
242 | 279 |
243 #if defined(OS_WIN) | 280 #if defined(OS_WIN) |
244 std::wstring name_; | 281 std::wstring name_; |
245 HANDLE mapped_file_; | 282 HANDLE mapped_file_; |
246 #elif defined(OS_POSIX) | 283 #elif defined(OS_POSIX) |
247 int mapped_file_; | 284 int mapped_file_; |
| 285 int readonly_mapped_file_; |
248 ino_t inode_; | 286 ino_t inode_; |
249 #endif | 287 #endif |
250 size_t mapped_size_; | 288 size_t mapped_size_; |
251 void* memory_; | 289 void* memory_; |
252 bool read_only_; | 290 bool read_only_; |
253 size_t requested_size_; | 291 size_t requested_size_; |
254 #if !defined(OS_POSIX) | 292 #if !defined(OS_POSIX) |
255 SharedMemoryLock lock_; | 293 SharedMemoryLock lock_; |
256 #endif | 294 #endif |
257 | 295 |
(...skipping 14 matching lines...) Expand all Loading... |
272 } | 310 } |
273 | 311 |
274 private: | 312 private: |
275 SharedMemory* shared_memory_; | 313 SharedMemory* shared_memory_; |
276 DISALLOW_COPY_AND_ASSIGN(SharedMemoryAutoLock); | 314 DISALLOW_COPY_AND_ASSIGN(SharedMemoryAutoLock); |
277 }; | 315 }; |
278 | 316 |
279 } // namespace base | 317 } // namespace base |
280 | 318 |
281 #endif // BASE_MEMORY_SHARED_MEMORY_H_ | 319 #endif // BASE_MEMORY_SHARED_MEMORY_H_ |
OLD | NEW |