| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_handle.h" | 5 #include "base/memory/shared_memory_handle.h" |
| 6 | 6 |
| 7 #include <unistd.h> | 7 #include <unistd.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/posix/eintr_wrapper.h" | 10 #include "base/posix/eintr_wrapper.h" |
| 11 #include "base/unguessable_token.h" |
| 11 | 12 |
| 12 namespace base { | 13 namespace base { |
| 13 | 14 |
| 14 SharedMemoryHandle::SharedMemoryHandle() = default; | 15 SharedMemoryHandle::SharedMemoryHandle() = default; |
| 15 SharedMemoryHandle::SharedMemoryHandle(const SharedMemoryHandle& handle) = | |
| 16 default; | |
| 17 SharedMemoryHandle& SharedMemoryHandle::operator=( | |
| 18 const SharedMemoryHandle& handle) = default; | |
| 19 | 16 |
| 20 SharedMemoryHandle::SharedMemoryHandle( | 17 SharedMemoryHandle::SharedMemoryHandle( |
| 21 const base::FileDescriptor& file_descriptor) | 18 const base::FileDescriptor& file_descriptor, |
| 22 : file_descriptor_(file_descriptor) {} | 19 const base::UnguessableToken& guid) |
| 20 : file_descriptor_(file_descriptor), guid_(guid) {} |
| 23 | 21 |
| 24 // static | 22 // static |
| 25 SharedMemoryHandle SharedMemoryHandle::ImportHandle(int fd) { | 23 SharedMemoryHandle SharedMemoryHandle::ImportHandle(int fd) { |
| 26 SharedMemoryHandle handle; | 24 SharedMemoryHandle handle; |
| 27 handle.file_descriptor_.fd = fd; | 25 handle.file_descriptor_.fd = fd; |
| 28 handle.file_descriptor_.auto_close = false; | 26 handle.file_descriptor_.auto_close = false; |
| 27 handle.guid_ = UnguessableToken::Create(); |
| 29 return handle; | 28 return handle; |
| 30 } | 29 } |
| 31 | 30 |
| 32 int SharedMemoryHandle::GetHandle() const { | 31 int SharedMemoryHandle::GetHandle() const { |
| 33 return file_descriptor_.fd; | 32 return file_descriptor_.fd; |
| 34 } | 33 } |
| 35 | 34 |
| 36 void SharedMemoryHandle::SetHandle(int handle) { | 35 void SharedMemoryHandle::SetHandle(int handle) { |
| 37 file_descriptor_.fd = handle; | 36 file_descriptor_.fd = handle; |
| 38 } | 37 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 52 return old_fd; | 51 return old_fd; |
| 53 } | 52 } |
| 54 | 53 |
| 55 SharedMemoryHandle SharedMemoryHandle::Duplicate() const { | 54 SharedMemoryHandle SharedMemoryHandle::Duplicate() const { |
| 56 if (!IsValid()) | 55 if (!IsValid()) |
| 57 return SharedMemoryHandle(); | 56 return SharedMemoryHandle(); |
| 58 | 57 |
| 59 int duped_handle = HANDLE_EINTR(dup(file_descriptor_.fd)); | 58 int duped_handle = HANDLE_EINTR(dup(file_descriptor_.fd)); |
| 60 if (duped_handle < 0) | 59 if (duped_handle < 0) |
| 61 return SharedMemoryHandle(); | 60 return SharedMemoryHandle(); |
| 62 return SharedMemoryHandle(FileDescriptor(duped_handle, true)); | 61 return SharedMemoryHandle(FileDescriptor(duped_handle, true), GetGUID()); |
| 63 } | 62 } |
| 64 | 63 |
| 65 void SharedMemoryHandle::SetOwnershipPassesToIPC(bool ownership_passes) { | 64 void SharedMemoryHandle::SetOwnershipPassesToIPC(bool ownership_passes) { |
| 66 file_descriptor_.auto_close = ownership_passes; | 65 file_descriptor_.auto_close = ownership_passes; |
| 67 } | 66 } |
| 68 | 67 |
| 69 bool SharedMemoryHandle::OwnershipPassesToIPC() const { | 68 bool SharedMemoryHandle::OwnershipPassesToIPC() const { |
| 70 return file_descriptor_.auto_close; | 69 return file_descriptor_.auto_close; |
| 71 } | 70 } |
| 72 | 71 |
| 73 } // namespace base | 72 } // namespace base |
| OLD | NEW |