| 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 "build/build_config.h" | 5 #include "build/build_config.h" |
| 6 #include "ipc/ipc_platform_file.h" | 6 #include "ipc/ipc_platform_file.h" |
| 7 | 7 |
| 8 #if defined(OS_POSIX) | 8 #if defined(OS_POSIX) |
| 9 #include <unistd.h> | 9 #include <unistd.h> |
| 10 #endif | 10 #endif |
| 11 | 11 |
| 12 namespace IPC { | 12 namespace IPC { |
| 13 | 13 |
| 14 PlatformFileForTransit GetFileHandleForProcess(base::PlatformFile handle, | 14 PlatformFileForTransit GetFileHandleForProcess(base::PlatformFile handle, |
| 15 base::ProcessHandle process, | 15 base::ProcessHandle process, |
| 16 bool close_source_handle) { | 16 bool close_source_handle) { |
| 17 IPC::PlatformFileForTransit out_handle; | |
| 18 #if defined(OS_WIN) | 17 #if defined(OS_WIN) |
| 19 HANDLE raw_handle = INVALID_HANDLE_VALUE; | 18 HANDLE raw_handle = INVALID_HANDLE_VALUE; |
| 20 DWORD options = DUPLICATE_SAME_ACCESS; | 19 DWORD options = DUPLICATE_SAME_ACCESS; |
| 21 if (close_source_handle) | 20 if (close_source_handle) |
| 22 options |= DUPLICATE_CLOSE_SOURCE; | 21 options |= DUPLICATE_CLOSE_SOURCE; |
| 23 if (handle == INVALID_HANDLE_VALUE || | 22 if (handle == INVALID_HANDLE_VALUE || |
| 24 !::DuplicateHandle(::GetCurrentProcess(), handle, process, &raw_handle, 0, | 23 !::DuplicateHandle(::GetCurrentProcess(), handle, ::GetCurrentProcess(), |
| 25 FALSE, options)) { | 24 &raw_handle, 0, FALSE, options)) { |
| 26 out_handle = IPC::InvalidPlatformFileForTransit(); | 25 return IPC::InvalidPlatformFileForTransit(); |
| 27 } else { | |
| 28 out_handle = | |
| 29 IPC::PlatformFileForTransit(raw_handle, base::GetProcId(process)); | |
| 30 out_handle.SetOwnershipPassesToIPC(true); | |
| 31 } | 26 } |
| 27 |
| 28 IPC::PlatformFileForTransit out_handle = IPC::PlatformFileForTransit( |
| 29 raw_handle, base::GetCurrentProcId()); |
| 30 out_handle.SetOwnershipPassesToIPC(true); |
| 31 return out_handle; |
| 32 #elif defined(OS_POSIX) | 32 #elif defined(OS_POSIX) |
| 33 // If asked to close the source, we can simply re-use the source fd instead of | 33 // If asked to close the source, we can simply re-use the source fd instead of |
| 34 // dup()ing and close()ing. | 34 // dup()ing and close()ing. |
| 35 // When we're not closing the source, we need to duplicate the handle and take | 35 // When we're not closing the source, we need to duplicate the handle and take |
| 36 // ownership of that. The reason is that this function is often used to | 36 // ownership of that. The reason is that this function is often used to |
| 37 // generate IPC messages, and the handle must remain valid until it's sent to | 37 // generate IPC messages, and the handle must remain valid until it's sent to |
| 38 // the other process from the I/O thread. Without the dup, calling code might | 38 // the other process from the I/O thread. Without the dup, calling code might |
| 39 // close the source handle before the message is sent, creating a race | 39 // close the source handle before the message is sent, creating a race |
| 40 // condition. | 40 // condition. |
| 41 int fd = close_source_handle ? handle : ::dup(handle); | 41 int fd = close_source_handle ? handle : ::dup(handle); |
| 42 out_handle = base::FileDescriptor(fd, true); | 42 return base::FileDescriptor(fd, true); |
| 43 #else | 43 #else |
| 44 #error Not implemented. | 44 #error Not implemented. |
| 45 #endif | 45 #endif |
| 46 return out_handle; | |
| 47 } | 46 } |
| 48 | 47 |
| 49 PlatformFileForTransit TakeFileHandleForProcess(base::File file, | 48 PlatformFileForTransit TakeFileHandleForProcess(base::File file, |
| 50 base::ProcessHandle process) { | 49 base::ProcessHandle process) { |
| 51 return GetFileHandleForProcess(file.TakePlatformFile(), process, true); | 50 return GetFileHandleForProcess(file.TakePlatformFile(), process, true); |
| 52 } | 51 } |
| 53 | 52 |
| 54 } // namespace IPC | 53 } // namespace IPC |
| OLD | NEW |