| 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 <unistd.h> | 11 #include <unistd.h> |
| 12 | 12 |
| 13 #include <limits> | 13 #include <limits> |
| 14 | 14 |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 | 16 |
| 17 namespace base { | 17 namespace base { |
| 18 | 18 |
| 19 SharedMemory::SharedMemory() | 19 SharedMemory::SharedMemory() |
| 20 : mapped_file_(-1), | 20 : mapped_file_(-1), |
| 21 inode_(0), | |
| 22 mapped_size_(0), | 21 mapped_size_(0), |
| 23 memory_(NULL), | 22 memory_(NULL), |
| 24 read_only_(false), | 23 read_only_(false), |
| 25 requested_size_(0) { | 24 requested_size_(0) { |
| 26 } | 25 } |
| 27 | 26 |
| 28 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) | 27 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) |
| 29 : mapped_file_(handle.fd), | 28 : mapped_file_(handle.fd), |
| 30 inode_(0), | |
| 31 mapped_size_(0), | 29 mapped_size_(0), |
| 32 memory_(NULL), | 30 memory_(NULL), |
| 33 read_only_(read_only), | 31 read_only_(read_only), |
| 34 requested_size_(0) { | 32 requested_size_(0) { |
| 35 } | 33 } |
| 36 | 34 |
| 37 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only, | 35 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only, |
| 38 ProcessHandle process) | 36 ProcessHandle process) |
| 39 : mapped_file_(handle.fd), | 37 : mapped_file_(handle.fd), |
| 40 inode_(0), | |
| 41 mapped_size_(0), | 38 mapped_size_(0), |
| 42 memory_(NULL), | 39 memory_(NULL), |
| 43 read_only_(read_only), | 40 read_only_(read_only), |
| 44 requested_size_(0) { | 41 requested_size_(0) { |
| 45 NOTREACHED(); | 42 NOTREACHED(); |
| 46 } | 43 } |
| 47 | 44 |
| 48 SharedMemory::~SharedMemory() { | 45 SharedMemory::~SharedMemory() { |
| 49 Unmap(); | 46 Unmap(); |
| 50 Close(); | 47 Close(); |
| 51 } | 48 } |
| 52 | 49 |
| 53 // static | 50 // static |
| 54 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { | 51 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { |
| 55 return handle.fd >= 0; | 52 return handle.fd >= 0; |
| 56 } | 53 } |
| 57 | 54 |
| 58 // static | 55 // static |
| 59 SharedMemoryHandle SharedMemory::NULLHandle() { | 56 SharedMemoryHandle SharedMemory::NULLHandle() { |
| 60 return SharedMemoryHandle(); | 57 return SharedMemoryHandle(); |
| 61 } | 58 } |
| 62 | 59 |
| 63 // static | 60 // static |
| 64 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { | 61 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { |
| 65 DCHECK_GE(handle.fd, 0); | 62 DCHECK_GE(handle.fd, 0); |
| 66 if (close(handle.fd) < 0) | 63 if (close(handle.fd) < 0) |
| 67 DPLOG(ERROR) << "close"; | 64 DPLOG(ERROR) << "close"; |
| 68 } | 65 } |
| 69 | 66 |
| 67 // static |
| 68 SharedMemoryHandle SharedMemory::DuplicateHandle( |
| 69 const SharedMemoryHandle& handle) { |
| 70 int duped_handle = HANDLE_EINTR(dup(handle.fd)); |
| 71 if (duped_handle < 0) |
| 72 return base::SharedMemory::NULLHandle(); |
| 73 return base::FileDescriptor(duped_handle, true); |
| 74 } |
| 75 |
| 70 bool SharedMemory::CreateAndMapAnonymous(size_t size) { | 76 bool SharedMemory::CreateAndMapAnonymous(size_t size) { |
| 71 // Untrusted code can't create descriptors or handles. | 77 // Untrusted code can't create descriptors or handles. |
| 72 return false; | 78 return false; |
| 73 } | 79 } |
| 74 | 80 |
| 75 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { | 81 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
| 76 // Untrusted code can't create descriptors or handles. | 82 // Untrusted code can't create descriptors or handles. |
| 77 return false; | 83 return false; |
| 78 } | 84 } |
| 79 | 85 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 new_handle->auto_close = true; | 166 new_handle->auto_close = true; |
| 161 | 167 |
| 162 if (close_self) { | 168 if (close_self) { |
| 163 Unmap(); | 169 Unmap(); |
| 164 Close(); | 170 Close(); |
| 165 } | 171 } |
| 166 return true; | 172 return true; |
| 167 } | 173 } |
| 168 | 174 |
| 169 } // namespace base | 175 } // namespace base |
| OLD | NEW |