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 "content/common/sandbox_util.h" | 5 #include "content/common/sandbox_util.h" |
6 | 6 |
7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
8 #include "content/public/common/sandbox_init.h" | 8 #include "content/public/common/sandbox_init.h" |
9 | 9 |
10 #if defined(OS_POSIX) | 10 #if defined(OS_POSIX) |
11 #include <unistd.h> | 11 #include <unistd.h> |
12 #endif | 12 #endif |
13 | 13 |
14 namespace content { | 14 namespace content { |
15 | 15 |
16 IPC::PlatformFileForTransit BrokerGetFileHandleForProcess( | 16 IPC::PlatformFileForTransit BrokerGetFileHandleForProcess( |
17 base::PlatformFile handle, | 17 base::PlatformFile handle, |
18 base::ProcessId target_process_id, | 18 base::ProcessId target_process_id, |
19 bool should_close_source) { | 19 bool should_close_source) { |
20 IPC::PlatformFileForTransit out_handle; | 20 IPC::PlatformFileForTransit out_handle; |
21 #if defined(OS_WIN) | 21 #if defined(OS_WIN) |
22 DWORD options = DUPLICATE_SAME_ACCESS; | 22 DWORD options = DUPLICATE_SAME_ACCESS; |
23 if (should_close_source) | 23 if (should_close_source) |
24 options |= DUPLICATE_CLOSE_SOURCE; | 24 options |= DUPLICATE_CLOSE_SOURCE; |
25 if (!content::BrokerDuplicateHandle(handle, target_process_id, &out_handle, | 25 HANDLE raw_handle = INVALID_HANDLE_VALUE; |
26 0, options)) { | 26 if (content::BrokerDuplicateHandle(handle, target_process_id, &raw_handle, 0, |
| 27 options)) { |
| 28 out_handle = IPC::PlatformFileForTransit(raw_handle, target_process_id); |
| 29 } else { |
27 out_handle = IPC::InvalidPlatformFileForTransit(); | 30 out_handle = IPC::InvalidPlatformFileForTransit(); |
28 } | 31 } |
29 #elif defined(OS_POSIX) | 32 #elif defined(OS_POSIX) |
30 // 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 |
31 // dup()ing and close()ing. | 34 // dup()ing and close()ing. |
32 // 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 |
33 // 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 |
34 // 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 |
35 // 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 |
36 // 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 |
37 // condition. | 40 // condition. |
38 int fd = should_close_source ? handle : ::dup(handle); | 41 int fd = should_close_source ? handle : ::dup(handle); |
39 out_handle = base::FileDescriptor(fd, true); | 42 out_handle = base::FileDescriptor(fd, true); |
40 #else | 43 #else |
41 #error Not implemented. | 44 #error Not implemented. |
42 #endif | 45 #endif |
43 return out_handle; | 46 return out_handle; |
44 } | 47 } |
45 | 48 |
46 } // namespace content | 49 } // namespace content |
47 | 50 |
OLD | NEW |