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