2016-05-28 00:54:31 +03: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 15:12:37 +04: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 22:33:56 +04:00
|
|
|
|
|
|
|
#include "base/message_loop.h"
|
|
|
|
|
|
|
|
#include "mozilla/ipc/Transport.h"
|
2015-04-01 11:40:35 +03:00
|
|
|
#include "mozilla/ipc/ProtocolUtils.h"
|
2011-06-03 22:33:56 +04:00
|
|
|
|
2015-02-11 23:01:26 +03:00
|
|
|
using base::ProcessHandle;
|
|
|
|
|
2011-06-03 22:33:56 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
2015-04-01 11:40:35 +03:00
|
|
|
nsresult CreateTransport(base::ProcessId aProcIdOne, TransportDescriptor* aOne,
|
2015-07-10 03:07:49 +03:00
|
|
|
TransportDescriptor* aTwo) {
|
Bug 1632687 - Part 3: Introduce an OS-dependent ChannelId type to reflect that Unix doesn't use channel IDs. r=mccr8
The Chromium-derived IPC code was, as far as I can tell, originally
designed for Windows and assumed that channels would be named pipes,
managed and connected to via `std::wstring` paths. The port to Unix,
however, used unnamed `socketpair()` and passed them directly from
process to process, so it has no use for these channel IDs... but it
still computes and propagates them, even though they're not used, using
deprecated wide-string APIs.
This patch introduces a typedef for an abstract channel ID, which is a
`wstring` on Windows and an empty struct on Unix, to allow removing the
string code where it's not needed without needing to completely redesign
the channel abstraction.
Differential Revision: https://phabricator.services.mozilla.com/D72260
2020-07-22 22:04:48 +03:00
|
|
|
auto id = IPC::Channel::GenerateVerifiedChannelID();
|
2011-06-03 22:33:56 +04:00
|
|
|
// Use MODE_SERVER to force creation of the pipe
|
2012-07-30 18:20:58 +04:00
|
|
|
Transport t(id, Transport::MODE_SERVER, nullptr);
|
2011-06-03 22:33:56 +04:00
|
|
|
HANDLE serverPipe = t.GetServerPipeHandle();
|
|
|
|
if (!serverPipe) {
|
2015-07-10 03:07:49 +03:00
|
|
|
return NS_ERROR_TRANSPORT_INIT;
|
2011-06-03 22:33:56 +04: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 06:02:08 +03: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 22:33:56 +04:00
|
|
|
HANDLE serverDup;
|
|
|
|
DWORD access = 0;
|
|
|
|
DWORD options = DUPLICATE_SAME_ACCESS;
|
2015-11-01 06:02:08 +03:00
|
|
|
if (!DuplicateHandle(serverPipe, base::GetCurrentProcId(), &serverDup, access,
|
|
|
|
options)) {
|
2015-07-10 03:07:49 +03:00
|
|
|
return NS_ERROR_DUPLICATE_HANDLE;
|
2011-06-03 22:33:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
aOne->mPipeName = aTwo->mPipeName = id;
|
2015-11-01 06:02:08 +03:00
|
|
|
aOne->mServerPipeHandle = serverDup;
|
|
|
|
aOne->mDestinationProcessId = aProcIdOne;
|
|
|
|
aTwo->mServerPipeHandle = INVALID_HANDLE_VALUE;
|
|
|
|
aTwo->mDestinationProcessId = 0;
|
2015-07-10 03:07:49 +03:00
|
|
|
return NS_OK;
|
2011-06-03 22:33:56 +04:00
|
|
|
}
|
|
|
|
|
2015-11-01 06:02:08 +03: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 09:33:52 +03:00
|
|
|
if (!ok) {
|
2016-05-21 18:16:33 +03:00
|
|
|
return nullptr;
|
2016-04-01 09:33:52 +03:00
|
|
|
}
|
2015-11-01 06:02:08 +03:00
|
|
|
|
|
|
|
// Now close our own copy of the handle (we're supposed to be transferring,
|
|
|
|
// not copying).
|
|
|
|
CloseHandle(source);
|
|
|
|
|
|
|
|
return handleDup;
|
|
|
|
}
|
|
|
|
|
2011-06-03 22:33:56 +04:00
|
|
|
UniquePtr<Transport> OpenDescriptor(const TransportDescriptor& aTd,
|
|
|
|
Transport::Mode aMode) {
|
2015-11-01 06:02:08 +03:00
|
|
|
if (aTd.mServerPipeHandle != INVALID_HANDLE_VALUE) {
|
|
|
|
MOZ_RELEASE_ASSERT(aTd.mDestinationProcessId == base::GetCurrentProcId());
|
|
|
|
}
|
2016-07-07 04:51:20 +03:00
|
|
|
return MakeUnique<Transport>(aTd.mPipeName, aTd.mServerPipeHandle, aMode,
|
|
|
|
nullptr);
|
2011-06-03 22:33:56 +04:00
|
|
|
}
|
|
|
|
|
2013-06-03 14:14:37 +04:00
|
|
|
UniquePtr<Transport> OpenDescriptor(const FileDescriptor& aFd,
|
|
|
|
Transport::Mode aMode) {
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Not implemented!");
|
2013-06-03 14:14:37 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-11-01 06:02:08 +03: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 09:33:52 +03:00
|
|
|
if (!ok) {
|
|
|
|
AnnotateSystemError();
|
|
|
|
}
|
2015-11-01 06:02:08 +03:00
|
|
|
MOZ_RELEASE_ASSERT(ok);
|
|
|
|
|
|
|
|
TransportDescriptor desc = aTd;
|
|
|
|
desc.mServerPipeHandle = serverDup;
|
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
|
2011-06-03 22:33:56 +04:00
|
|
|
void CloseDescriptor(const TransportDescriptor& aTd) {
|
2015-11-01 06:02:08 +03:00
|
|
|
// We're closing our own local copy of the pipe.
|
|
|
|
|
|
|
|
CloseHandle(aTd.mServerPipeHandle);
|
2011-06-03 22:33:56 +04:00
|
|
|
}
|
|
|
|
|
2011-06-03 22:33:56 +04:00
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|