1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_ipc_Transport_win_h
#define mozilla_ipc_Transport_win_h 1
#include <string>
#include "base/process.h"
#include "ipc/IPCMessageUtils.h"
#include "nsWindowsHelpers.h"
namespace mozilla {
namespace ipc {
struct TransportDescriptor
{
std::wstring mPipeName;
HANDLE mServerPipeHandle;
base::ProcessId mDestinationProcessId;
};
HANDLE
TransferHandleToProcess(HANDLE source, base::ProcessId pid);
} // namespace ipc
} // namespace mozilla
namespace IPC {
template<>
struct ParamTraits<mozilla::ipc::TransportDescriptor>
{
typedef mozilla::ipc::TransportDescriptor paramType;
static void Write(Message* aMsg, const paramType& aParam)
{
HANDLE pipe = mozilla::ipc::TransferHandleToProcess(aParam.mServerPipeHandle,
aParam.mDestinationProcessId);
DWORD duplicateFromProcessId = 0;
if (!pipe) {
if (XRE_IsParentProcess()) {
// If we are the parent and failed to transfer then there is no hope,
// just close the handle.
::CloseHandle(aParam.mServerPipeHandle);
} else {
// We are probably sending to parent so it should be able to duplicate.
pipe = aParam.mServerPipeHandle;
duplicateFromProcessId = ::GetCurrentProcessId();
}
}
WriteParam(aMsg, aParam.mPipeName);
WriteParam(aMsg, pipe);
WriteParam(aMsg, duplicateFromProcessId);
WriteParam(aMsg, aParam.mDestinationProcessId);
}
static bool Read(const Message* aMsg, PickleIterator* aIter, paramType* aResult)
{
DWORD duplicateFromProcessId;
bool r = (ReadParam(aMsg, aIter, &aResult->mPipeName) &&
ReadParam(aMsg, aIter, &aResult->mServerPipeHandle) &&
ReadParam(aMsg, aIter, &duplicateFromProcessId) &&
ReadParam(aMsg, aIter, &aResult->mDestinationProcessId));
if (!r) {
return r;
}
MOZ_RELEASE_ASSERT(aResult->mServerPipeHandle,
"Main process failed to duplicate pipe handle to child.");
// If this is a not the "server" side descriptor, we have finished.
if (aResult->mServerPipeHandle == INVALID_HANDLE_VALUE) {
return true;
}
MOZ_RELEASE_ASSERT(aResult->mDestinationProcessId == base::GetCurrentProcId());
// If the pipe has already been duplicated to us, we have finished.
if (!duplicateFromProcessId) {
return true;
}
// Otherwise duplicate the handle to us.
nsAutoHandle sourceProcess(::OpenProcess(PROCESS_DUP_HANDLE, FALSE,
duplicateFromProcessId));
if (!sourceProcess) {
return false;
}
HANDLE ourHandle;
BOOL duped = ::DuplicateHandle(sourceProcess, aResult->mServerPipeHandle,
::GetCurrentProcess(), &ourHandle, 0, FALSE,
DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
if (!duped) {
aResult->mServerPipeHandle = INVALID_HANDLE_VALUE;
return false;
}
aResult->mServerPipeHandle = ourHandle;
return true;
}
};
} // namespace IPC
#endif // mozilla_ipc_Transport_win_h
|