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 3// found in the LICENSE file. 4 5#include "content/common/sandbox_util.h" 6 7#if defined(OS_POSIX) 8#include <unistd.h> 9#endif 10 11#include "content/public/common/sandbox_init.h" 12 13namespace content { 14 15IPC::PlatformFileForTransit BrokerGetFileHandleForProcess( 16 base::PlatformFile handle, 17 base::ProcessId target_process_id, 18 bool should_close_source) { 19 IPC::PlatformFileForTransit out_handle; 20#if defined(OS_WIN) 21 DWORD options = DUPLICATE_SAME_ACCESS; 22 if (should_close_source) 23 options |= DUPLICATE_CLOSE_SOURCE; 24 if (!content::BrokerDuplicateHandle(handle, target_process_id, &out_handle, 25 0, options)) { 26 out_handle = IPC::InvalidPlatformFileForTransit(); 27 } 28#elif defined(OS_POSIX) 29 // If asked to close the source, we can simply re-use the source fd instead of 30 // dup()ing and close()ing. 31 // When we're not closing the source, we need to duplicate the handle and take 32 // ownership of that. The reason is that this function is often used to 33 // generate IPC messages, and the handle must remain valid until it's sent to 34 // the other process from the I/O thread. Without the dup, calling code might 35 // close the source handle before the message is sent, creating a race 36 // condition. 37 int fd = should_close_source ? handle : ::dup(handle); 38 out_handle = base::FileDescriptor(fd, true); 39#else 40 #error Not implemented. 41#endif 42 return out_handle; 43} 44 45} // namespace content 46 47