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; | 17 IPC::PlatformFileForTransit out_handle; |
18 #if defined(OS_WIN) | 18 #if defined(OS_WIN) |
| 19 HANDLE raw_handle = INVALID_HANDLE_VALUE; |
19 DWORD options = DUPLICATE_SAME_ACCESS; | 20 DWORD options = DUPLICATE_SAME_ACCESS; |
20 if (close_source_handle) | 21 if (close_source_handle) |
21 options |= DUPLICATE_CLOSE_SOURCE; | 22 options |= DUPLICATE_CLOSE_SOURCE; |
22 if (handle == INVALID_HANDLE_VALUE || | 23 if (handle == INVALID_HANDLE_VALUE || |
23 !::DuplicateHandle(::GetCurrentProcess(), | 24 !::DuplicateHandle(::GetCurrentProcess(), handle, process, &raw_handle, 0, |
24 handle, | 25 FALSE, options)) { |
25 process, | |
26 &out_handle, | |
27 0, | |
28 FALSE, | |
29 options)) { | |
30 out_handle = IPC::InvalidPlatformFileForTransit(); | 26 out_handle = IPC::InvalidPlatformFileForTransit(); |
| 27 } else { |
| 28 out_handle = |
| 29 IPC::PlatformFileForTransit(raw_handle, base::GetProcId(process)); |
| 30 out_handle.SetOwnershipPassesToIPC(true); |
31 } | 31 } |
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 out_handle = base::FileDescriptor(fd, true); |
43 #else | 43 #else |
44 #error Not implemented. | 44 #error Not implemented. |
45 #endif | 45 #endif |
46 return out_handle; | 46 return out_handle; |
47 } | 47 } |
48 | 48 |
49 PlatformFileForTransit TakeFileHandleForProcess(base::File file, | 49 PlatformFileForTransit TakeFileHandleForProcess(base::File file, |
50 base::ProcessHandle process) { | 50 base::ProcessHandle process) { |
51 return GetFileHandleForProcess(file.TakePlatformFile(), process, true); | 51 return GetFileHandleForProcess(file.TakePlatformFile(), process, true); |
52 } | 52 } |
53 | 53 |
54 } // namespace IPC | 54 } // namespace IPC |
OLD | NEW |