Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "FileDescriptor.h" |
michael@0 | 6 | |
michael@0 | 7 | #include "mozilla/Assertions.h" |
michael@0 | 8 | #include "nsDebug.h" |
michael@0 | 9 | |
michael@0 | 10 | #ifdef XP_WIN |
michael@0 | 11 | |
michael@0 | 12 | #include <windows.h> |
michael@0 | 13 | #define INVALID_HANDLE INVALID_HANDLE_VALUE |
michael@0 | 14 | |
michael@0 | 15 | #else // XP_WIN |
michael@0 | 16 | |
michael@0 | 17 | #include <unistd.h> |
michael@0 | 18 | |
michael@0 | 19 | #ifndef OS_POSIX |
michael@0 | 20 | #define OS_POSIX |
michael@0 | 21 | #endif |
michael@0 | 22 | |
michael@0 | 23 | #include "base/eintr_wrapper.h" |
michael@0 | 24 | #define INVALID_HANDLE -1 |
michael@0 | 25 | |
michael@0 | 26 | #endif // XP_WIN |
michael@0 | 27 | |
michael@0 | 28 | using mozilla::ipc::FileDescriptor; |
michael@0 | 29 | |
michael@0 | 30 | FileDescriptor::FileDescriptor() |
michael@0 | 31 | : mHandle(INVALID_HANDLE), mHandleCreatedByOtherProcess(false), |
michael@0 | 32 | mHandleCreatedByOtherProcessWasUsed(false) |
michael@0 | 33 | { } |
michael@0 | 34 | |
michael@0 | 35 | FileDescriptor::FileDescriptor(PlatformHandleType aHandle) |
michael@0 | 36 | : mHandle(INVALID_HANDLE), mHandleCreatedByOtherProcess(false), |
michael@0 | 37 | mHandleCreatedByOtherProcessWasUsed(false) |
michael@0 | 38 | { |
michael@0 | 39 | DuplicateInCurrentProcess(aHandle); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | void |
michael@0 | 43 | FileDescriptor::DuplicateInCurrentProcess(PlatformHandleType aHandle) |
michael@0 | 44 | { |
michael@0 | 45 | MOZ_ASSERT_IF(mHandleCreatedByOtherProcess, |
michael@0 | 46 | mHandleCreatedByOtherProcessWasUsed); |
michael@0 | 47 | |
michael@0 | 48 | if (IsValid(aHandle)) { |
michael@0 | 49 | PlatformHandleType newHandle; |
michael@0 | 50 | #ifdef XP_WIN |
michael@0 | 51 | if (DuplicateHandle(GetCurrentProcess(), aHandle, GetCurrentProcess(), |
michael@0 | 52 | &newHandle, 0, FALSE, DUPLICATE_SAME_ACCESS)) { |
michael@0 | 53 | #else // XP_WIN |
michael@0 | 54 | if ((newHandle = dup(aHandle)) != INVALID_HANDLE) { |
michael@0 | 55 | #endif |
michael@0 | 56 | mHandle = newHandle; |
michael@0 | 57 | return; |
michael@0 | 58 | } |
michael@0 | 59 | NS_WARNING("Failed to duplicate file handle for current process!"); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | mHandle = INVALID_HANDLE; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | void |
michael@0 | 66 | FileDescriptor::CloseCurrentProcessHandle() |
michael@0 | 67 | { |
michael@0 | 68 | MOZ_ASSERT_IF(mHandleCreatedByOtherProcess, |
michael@0 | 69 | mHandleCreatedByOtherProcessWasUsed); |
michael@0 | 70 | |
michael@0 | 71 | // Don't actually close handles created by another process. |
michael@0 | 72 | if (mHandleCreatedByOtherProcess) { |
michael@0 | 73 | return; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | if (IsValid()) { |
michael@0 | 77 | #ifdef XP_WIN |
michael@0 | 78 | if (!CloseHandle(mHandle)) { |
michael@0 | 79 | NS_WARNING("Failed to close file handle for current process!"); |
michael@0 | 80 | } |
michael@0 | 81 | #else // XP_WIN |
michael@0 | 82 | HANDLE_EINTR(close(mHandle)); |
michael@0 | 83 | #endif |
michael@0 | 84 | mHandle = INVALID_HANDLE; |
michael@0 | 85 | } |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | FileDescriptor::PickleType |
michael@0 | 89 | FileDescriptor::ShareTo(const FileDescriptor::IPDLPrivate&, |
michael@0 | 90 | FileDescriptor::ProcessHandle aOtherProcess) const |
michael@0 | 91 | { |
michael@0 | 92 | PlatformHandleType newHandle; |
michael@0 | 93 | #ifdef XP_WIN |
michael@0 | 94 | if (IsValid()) { |
michael@0 | 95 | if (DuplicateHandle(GetCurrentProcess(), mHandle, aOtherProcess, |
michael@0 | 96 | &newHandle, 0, FALSE, DUPLICATE_SAME_ACCESS)) { |
michael@0 | 97 | return newHandle; |
michael@0 | 98 | } |
michael@0 | 99 | NS_WARNING("Failed to duplicate file handle for other process!"); |
michael@0 | 100 | } |
michael@0 | 101 | return INVALID_HANDLE; |
michael@0 | 102 | #else // XP_WIN |
michael@0 | 103 | if (IsValid()) { |
michael@0 | 104 | newHandle = dup(mHandle); |
michael@0 | 105 | if (IsValid(newHandle)) { |
michael@0 | 106 | return base::FileDescriptor(newHandle, /* auto_close */ true); |
michael@0 | 107 | } |
michael@0 | 108 | NS_WARNING("Failed to duplicate file handle for other process!"); |
michael@0 | 109 | } |
michael@0 | 110 | return base::FileDescriptor(); |
michael@0 | 111 | #endif |
michael@0 | 112 | |
michael@0 | 113 | MOZ_CRASH("Must not get here!"); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | // static |
michael@0 | 117 | bool |
michael@0 | 118 | FileDescriptor::IsValid(PlatformHandleType aHandle) |
michael@0 | 119 | { |
michael@0 | 120 | return aHandle != INVALID_HANDLE; |
michael@0 | 121 | } |