shenhan@google.com | 8a6e499 | 2012-06-05 10:54:46 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
jcivelli@chromium.org | 3bab103 | 2011-06-15 04:35:10 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 5 | #include "build/build_config.h" |
jcivelli@chromium.org | 3bab103 | 2011-06-15 04:35:10 +0900 | [diff] [blame] | 6 | #include "ipc/ipc_platform_file.h" |
| 7 | |
shenhan@google.com | 8a6e499 | 2012-06-05 10:54:46 +0900 | [diff] [blame] | 8 | #if defined(OS_POSIX) |
jrg@chromium.org | 2eb192e | 2011-11-04 09:14:16 +0900 | [diff] [blame] | 9 | #include <unistd.h> |
Dale Curtis | 56733a0 | 2017-08-30 06:53:23 +0900 | [diff] [blame^] | 10 | |
| 11 | #include "base/posix/eintr_wrapper.h" |
jrg@chromium.org | 2eb192e | 2011-11-04 09:14:16 +0900 | [diff] [blame] | 12 | #endif |
| 13 | |
jcivelli@chromium.org | 3bab103 | 2011-06-15 04:35:10 +0900 | [diff] [blame] | 14 | namespace IPC { |
| 15 | |
erikchen | 9a454c1 | 2017-04-29 11:24:36 +0900 | [diff] [blame] | 16 | #if defined(OS_WIN) |
| 17 | PlatformFileForTransit::PlatformFileForTransit() : handle_(nullptr) {} |
| 18 | |
| 19 | PlatformFileForTransit::PlatformFileForTransit(HANDLE handle) |
| 20 | : handle_(handle) {} |
| 21 | |
| 22 | bool PlatformFileForTransit::operator==( |
| 23 | const PlatformFileForTransit& platform_file) const { |
| 24 | return handle_ == platform_file.handle_; |
| 25 | } |
| 26 | |
| 27 | bool PlatformFileForTransit::operator!=( |
| 28 | const PlatformFileForTransit& platform_file) const { |
| 29 | return !(*this == platform_file); |
| 30 | } |
| 31 | |
| 32 | HANDLE PlatformFileForTransit::GetHandle() const { |
| 33 | return handle_; |
| 34 | } |
| 35 | |
| 36 | bool PlatformFileForTransit::IsValid() const { |
| 37 | return handle_ != nullptr; |
| 38 | } |
| 39 | |
| 40 | #endif // defined(OS_WIN) |
| 41 | |
erikchen | 556548b | 2016-04-07 03:51:55 +0900 | [diff] [blame] | 42 | PlatformFileForTransit GetPlatformFileForTransit(base::PlatformFile handle, |
| 43 | bool close_source_handle) { |
jcivelli@chromium.org | 3bab103 | 2011-06-15 04:35:10 +0900 | [diff] [blame] | 44 | #if defined(OS_WIN) |
erikchen | b23e3a4 | 2016-03-30 07:26:42 +0900 | [diff] [blame] | 45 | HANDLE raw_handle = INVALID_HANDLE_VALUE; |
jcivelli@chromium.org | 3bab103 | 2011-06-15 04:35:10 +0900 | [diff] [blame] | 46 | DWORD options = DUPLICATE_SAME_ACCESS; |
| 47 | if (close_source_handle) |
| 48 | options |= DUPLICATE_CLOSE_SOURCE; |
jschuh@chromium.org | 9e1076f | 2013-05-25 07:36:01 +0900 | [diff] [blame] | 49 | if (handle == INVALID_HANDLE_VALUE || |
erikchen | de36fd3 | 2016-04-06 03:32:48 +0900 | [diff] [blame] | 50 | !::DuplicateHandle(::GetCurrentProcess(), handle, ::GetCurrentProcess(), |
| 51 | &raw_handle, 0, FALSE, options)) { |
| 52 | return IPC::InvalidPlatformFileForTransit(); |
jcivelli@chromium.org | 3bab103 | 2011-06-15 04:35:10 +0900 | [diff] [blame] | 53 | } |
erikchen | de36fd3 | 2016-04-06 03:32:48 +0900 | [diff] [blame] | 54 | |
erikchen | 9a454c1 | 2017-04-29 11:24:36 +0900 | [diff] [blame] | 55 | return IPC::PlatformFileForTransit(raw_handle); |
jcivelli@chromium.org | 3bab103 | 2011-06-15 04:35:10 +0900 | [diff] [blame] | 56 | #elif defined(OS_POSIX) |
| 57 | // If asked to close the source, we can simply re-use the source fd instead of |
| 58 | // dup()ing and close()ing. |
| 59 | // When we're not closing the source, we need to duplicate the handle and take |
| 60 | // ownership of that. The reason is that this function is often used to |
| 61 | // generate IPC messages, and the handle must remain valid until it's sent to |
| 62 | // the other process from the I/O thread. Without the dup, calling code might |
| 63 | // close the source handle before the message is sent, creating a race |
| 64 | // condition. |
Dale Curtis | 56733a0 | 2017-08-30 06:53:23 +0900 | [diff] [blame^] | 65 | int fd = close_source_handle ? handle : HANDLE_EINTR(::dup(handle)); |
erikchen | de36fd3 | 2016-04-06 03:32:48 +0900 | [diff] [blame] | 66 | return base::FileDescriptor(fd, true); |
jcivelli@chromium.org | 3bab103 | 2011-06-15 04:35:10 +0900 | [diff] [blame] | 67 | #else |
| 68 | #error Not implemented. |
| 69 | #endif |
jcivelli@chromium.org | 3bab103 | 2011-06-15 04:35:10 +0900 | [diff] [blame] | 70 | } |
| 71 | |
erikchen | 33f45fa | 2016-04-08 01:35:11 +0900 | [diff] [blame] | 72 | PlatformFileForTransit TakePlatformFileForTransit(base::File file) { |
erikchen | 556548b | 2016-04-07 03:51:55 +0900 | [diff] [blame] | 73 | return GetPlatformFileForTransit(file.TakePlatformFile(), true); |
rvargas@chromium.org | f40fe38 | 2014-03-06 05:13:49 +0900 | [diff] [blame] | 74 | } |
| 75 | |
jcivelli@chromium.org | 3bab103 | 2011-06-15 04:35:10 +0900 | [diff] [blame] | 76 | } // namespace IPC |