2016-05-27 21:54:31 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* 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/. */
|
2011-06-03 18:33:56 +00:00
|
|
|
|
|
|
|
#include "base/message_loop.h"
|
|
|
|
|
|
|
|
#include "mozilla/ipc/Transport.h"
|
2015-04-01 08:40:35 +00:00
|
|
|
#include "mozilla/ipc/ProtocolUtils.h"
|
2011-06-03 18:33:56 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2015-02-11 20:01:26 +00:00
|
|
|
using base::ProcessHandle;
|
|
|
|
|
2011-06-03 18:33:56 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
2015-04-01 08:40:35 +00:00
|
|
|
nsresult CreateTransport(base::ProcessId aProcIdOne, TransportDescriptor* aOne,
|
2015-07-10 00:07:49 +00:00
|
|
|
TransportDescriptor* aTwo) {
|
2015-04-23 10:09:04 +00:00
|
|
|
wstring id = IPC::Channel::GenerateVerifiedChannelID(std::wstring());
|
2011-06-03 18:33:56 +00:00
|
|
|
// Use MODE_SERVER to force creation of the pipe
|
2012-07-30 14:20:58 +00:00
|
|
|
Transport t(id, Transport::MODE_SERVER, nullptr);
|
2011-06-03 18:33:56 +00:00
|
|
|
HANDLE serverPipe = t.GetServerPipeHandle();
|
|
|
|
if (!serverPipe) {
|
2015-07-10 00:07:49 +00:00
|
|
|
return NS_ERROR_TRANSPORT_INIT;
|
2011-06-03 18:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NB: we create the server pipe immediately, instead of just
|
|
|
|
// grabbing an ID, on purpose. In the current setup, the client
|
|
|
|
// needs to connect to an existing server pipe, so to prevent race
|
2015-11-01 03:02:08 +00:00
|
|
|
// conditions, we create the server side here. When we send the pipe
|
|
|
|
// to the server, we DuplicateHandle it to the server process to give it
|
|
|
|
// access.
|
2011-06-03 18:33:56 +00:00
|
|
|
HANDLE serverDup;
|
|
|
|
DWORD access = 0;
|
|
|
|
DWORD options = DUPLICATE_SAME_ACCESS;
|
2015-11-01 03:02:08 +00:00
|
|
|
if (!DuplicateHandle(serverPipe, base::GetCurrentProcId(), &serverDup, access,
|
|
|
|
options)) {
|
2015-07-10 00:07:49 +00:00
|
|
|
return NS_ERROR_DUPLICATE_HANDLE;
|
2011-06-03 18:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
aOne->mPipeName = aTwo->mPipeName = id;
|
2015-11-01 03:02:08 +00:00
|
|
|
aOne->mServerPipeHandle = serverDup;
|
|
|
|
aOne->mDestinationProcessId = aProcIdOne;
|
|
|
|
aTwo->mServerPipeHandle = INVALID_HANDLE_VALUE;
|
|
|
|
aTwo->mDestinationProcessId = 0;
|
2015-07-10 00:07:49 +00:00
|
|
|
return NS_OK;
|
2011-06-03 18:33:56 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 03:02:08 +00:00
|
|
|
HANDLE
|
|
|
|
TransferHandleToProcess(HANDLE source, base::ProcessId pid) {
|
|
|
|
// At this point we're sending the handle to another process.
|
|
|
|
|
|
|
|
if (source == INVALID_HANDLE_VALUE) {
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
HANDLE handleDup;
|
|
|
|
DWORD access = 0;
|
|
|
|
DWORD options = DUPLICATE_SAME_ACCESS;
|
|
|
|
bool ok = DuplicateHandle(source, pid, &handleDup, access, options);
|
2016-04-01 06:33:52 +00:00
|
|
|
if (!ok) {
|
2016-05-21 15:16:33 +00:00
|
|
|
return nullptr;
|
2016-04-01 06:33:52 +00:00
|
|
|
}
|
2015-11-01 03:02:08 +00:00
|
|
|
|
|
|
|
// Now close our own copy of the handle (we're supposed to be transferring,
|
|
|
|
// not copying).
|
|
|
|
CloseHandle(source);
|
|
|
|
|
|
|
|
return handleDup;
|
|
|
|
}
|
|
|
|
|
2011-06-03 18:33:56 +00:00
|
|
|
UniquePtr<Transport> OpenDescriptor(const TransportDescriptor& aTd,
|
|
|
|
Transport::Mode aMode) {
|
2015-11-01 03:02:08 +00:00
|
|
|
if (aTd.mServerPipeHandle != INVALID_HANDLE_VALUE) {
|
|
|
|
MOZ_RELEASE_ASSERT(aTd.mDestinationProcessId == base::GetCurrentProcId());
|
|
|
|
}
|
2016-07-07 01:51:20 +00:00
|
|
|
return MakeUnique<Transport>(aTd.mPipeName, aTd.mServerPipeHandle, aMode,
|
|
|
|
nullptr);
|
2011-06-03 18:33:56 +00:00
|
|
|
}
|
|
|
|
|
2013-06-03 10:14:37 +00:00
|
|
|
UniquePtr<Transport> OpenDescriptor(const FileDescriptor& aFd,
|
|
|
|
Transport::Mode aMode) {
|
2018-06-18 05:43:11 +00:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Not implemented!");
|
2013-06-03 10:14:37 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-11-01 03:02:08 +00:00
|
|
|
TransportDescriptor DuplicateDescriptor(const TransportDescriptor& aTd) {
|
|
|
|
// We're duplicating this handle in our own process for bookkeeping purposes.
|
|
|
|
|
|
|
|
if (aTd.mServerPipeHandle == INVALID_HANDLE_VALUE) {
|
|
|
|
return aTd;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE serverDup;
|
|
|
|
DWORD access = 0;
|
|
|
|
DWORD options = DUPLICATE_SAME_ACCESS;
|
|
|
|
bool ok = DuplicateHandle(aTd.mServerPipeHandle, base::GetCurrentProcId(),
|
|
|
|
&serverDup, access, options);
|
2016-04-01 06:33:52 +00:00
|
|
|
if (!ok) {
|
|
|
|
AnnotateSystemError();
|
|
|
|
}
|
2015-11-01 03:02:08 +00:00
|
|
|
MOZ_RELEASE_ASSERT(ok);
|
|
|
|
|
|
|
|
TransportDescriptor desc = aTd;
|
|
|
|
desc.mServerPipeHandle = serverDup;
|
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
|
2011-06-03 18:33:56 +00:00
|
|
|
void CloseDescriptor(const TransportDescriptor& aTd) {
|
2015-11-01 03:02:08 +00:00
|
|
|
// We're closing our own local copy of the pipe.
|
|
|
|
|
|
|
|
CloseHandle(aTd.mServerPipeHandle);
|
2011-06-03 18:33:56 +00:00
|
|
|
}
|
|
|
|
|
2011-06-03 18:33:56 +00:00
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|