1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/glue/FileDescriptor.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,121 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "FileDescriptor.h" 1.9 + 1.10 +#include "mozilla/Assertions.h" 1.11 +#include "nsDebug.h" 1.12 + 1.13 +#ifdef XP_WIN 1.14 + 1.15 +#include <windows.h> 1.16 +#define INVALID_HANDLE INVALID_HANDLE_VALUE 1.17 + 1.18 +#else // XP_WIN 1.19 + 1.20 +#include <unistd.h> 1.21 + 1.22 +#ifndef OS_POSIX 1.23 +#define OS_POSIX 1.24 +#endif 1.25 + 1.26 +#include "base/eintr_wrapper.h" 1.27 +#define INVALID_HANDLE -1 1.28 + 1.29 +#endif // XP_WIN 1.30 + 1.31 +using mozilla::ipc::FileDescriptor; 1.32 + 1.33 +FileDescriptor::FileDescriptor() 1.34 +: mHandle(INVALID_HANDLE), mHandleCreatedByOtherProcess(false), 1.35 + mHandleCreatedByOtherProcessWasUsed(false) 1.36 +{ } 1.37 + 1.38 +FileDescriptor::FileDescriptor(PlatformHandleType aHandle) 1.39 +: mHandle(INVALID_HANDLE), mHandleCreatedByOtherProcess(false), 1.40 + mHandleCreatedByOtherProcessWasUsed(false) 1.41 +{ 1.42 + DuplicateInCurrentProcess(aHandle); 1.43 +} 1.44 + 1.45 +void 1.46 +FileDescriptor::DuplicateInCurrentProcess(PlatformHandleType aHandle) 1.47 +{ 1.48 + MOZ_ASSERT_IF(mHandleCreatedByOtherProcess, 1.49 + mHandleCreatedByOtherProcessWasUsed); 1.50 + 1.51 + if (IsValid(aHandle)) { 1.52 + PlatformHandleType newHandle; 1.53 +#ifdef XP_WIN 1.54 + if (DuplicateHandle(GetCurrentProcess(), aHandle, GetCurrentProcess(), 1.55 + &newHandle, 0, FALSE, DUPLICATE_SAME_ACCESS)) { 1.56 +#else // XP_WIN 1.57 + if ((newHandle = dup(aHandle)) != INVALID_HANDLE) { 1.58 +#endif 1.59 + mHandle = newHandle; 1.60 + return; 1.61 + } 1.62 + NS_WARNING("Failed to duplicate file handle for current process!"); 1.63 + } 1.64 + 1.65 + mHandle = INVALID_HANDLE; 1.66 +} 1.67 + 1.68 +void 1.69 +FileDescriptor::CloseCurrentProcessHandle() 1.70 +{ 1.71 + MOZ_ASSERT_IF(mHandleCreatedByOtherProcess, 1.72 + mHandleCreatedByOtherProcessWasUsed); 1.73 + 1.74 + // Don't actually close handles created by another process. 1.75 + if (mHandleCreatedByOtherProcess) { 1.76 + return; 1.77 + } 1.78 + 1.79 + if (IsValid()) { 1.80 +#ifdef XP_WIN 1.81 + if (!CloseHandle(mHandle)) { 1.82 + NS_WARNING("Failed to close file handle for current process!"); 1.83 + } 1.84 +#else // XP_WIN 1.85 + HANDLE_EINTR(close(mHandle)); 1.86 +#endif 1.87 + mHandle = INVALID_HANDLE; 1.88 + } 1.89 +} 1.90 + 1.91 +FileDescriptor::PickleType 1.92 +FileDescriptor::ShareTo(const FileDescriptor::IPDLPrivate&, 1.93 + FileDescriptor::ProcessHandle aOtherProcess) const 1.94 +{ 1.95 + PlatformHandleType newHandle; 1.96 +#ifdef XP_WIN 1.97 + if (IsValid()) { 1.98 + if (DuplicateHandle(GetCurrentProcess(), mHandle, aOtherProcess, 1.99 + &newHandle, 0, FALSE, DUPLICATE_SAME_ACCESS)) { 1.100 + return newHandle; 1.101 + } 1.102 + NS_WARNING("Failed to duplicate file handle for other process!"); 1.103 + } 1.104 + return INVALID_HANDLE; 1.105 +#else // XP_WIN 1.106 + if (IsValid()) { 1.107 + newHandle = dup(mHandle); 1.108 + if (IsValid(newHandle)) { 1.109 + return base::FileDescriptor(newHandle, /* auto_close */ true); 1.110 + } 1.111 + NS_WARNING("Failed to duplicate file handle for other process!"); 1.112 + } 1.113 + return base::FileDescriptor(); 1.114 +#endif 1.115 + 1.116 + MOZ_CRASH("Must not get here!"); 1.117 +} 1.118 + 1.119 +// static 1.120 +bool 1.121 +FileDescriptor::IsValid(PlatformHandleType aHandle) 1.122 +{ 1.123 + return aHandle != INVALID_HANDLE; 1.124 +}