ipc/glue/ProtocolUtils.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * vim: sw=2 ts=8 et :
michael@0 3 */
michael@0 4 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 5 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 7
michael@0 8 #include "base/process_util.h"
michael@0 9
michael@0 10 #include "mozilla/ipc/MessageChannel.h"
michael@0 11 #include "mozilla/ipc/ProtocolUtils.h"
michael@0 12 #include "mozilla/ipc/Transport.h"
michael@0 13
michael@0 14 using namespace base;
michael@0 15 using namespace IPC;
michael@0 16
michael@0 17 namespace mozilla {
michael@0 18 namespace ipc {
michael@0 19
michael@0 20 IToplevelProtocol::~IToplevelProtocol()
michael@0 21 {
michael@0 22 mOpenActors.clear();
michael@0 23 }
michael@0 24
michael@0 25 void IToplevelProtocol::AddOpenedActor(IToplevelProtocol* aActor)
michael@0 26 {
michael@0 27 #ifdef DEBUG
michael@0 28 for (const IToplevelProtocol* actor = mOpenActors.getFirst();
michael@0 29 actor;
michael@0 30 actor = actor->getNext()) {
michael@0 31 NS_ASSERTION(actor != aActor,
michael@0 32 "Open the same protocol for more than one time");
michael@0 33 }
michael@0 34 #endif
michael@0 35
michael@0 36 mOpenActors.insertBack(aActor);
michael@0 37 }
michael@0 38
michael@0 39 IToplevelProtocol*
michael@0 40 IToplevelProtocol::CloneToplevel(const InfallibleTArray<ProtocolFdMapping>& aFds,
michael@0 41 base::ProcessHandle aPeerProcess,
michael@0 42 ProtocolCloneContext* aCtx)
michael@0 43 {
michael@0 44 NS_NOTREACHED("Clone() for this protocol actor is not implemented");
michael@0 45 return nullptr;
michael@0 46 }
michael@0 47
michael@0 48 void
michael@0 49 IToplevelProtocol::CloneOpenedToplevels(IToplevelProtocol* aTemplate,
michael@0 50 const InfallibleTArray<ProtocolFdMapping>& aFds,
michael@0 51 base::ProcessHandle aPeerProcess,
michael@0 52 ProtocolCloneContext* aCtx)
michael@0 53 {
michael@0 54 for (IToplevelProtocol* actor = aTemplate->GetFirstOpenedActors();
michael@0 55 actor;
michael@0 56 actor = actor->getNext()) {
michael@0 57 IToplevelProtocol* newactor = actor->CloneToplevel(aFds, aPeerProcess, aCtx);
michael@0 58 AddOpenedActor(newactor);
michael@0 59 }
michael@0 60 }
michael@0 61
michael@0 62 class ChannelOpened : public IPC::Message
michael@0 63 {
michael@0 64 public:
michael@0 65 ChannelOpened(TransportDescriptor aDescriptor,
michael@0 66 ProcessId aOtherProcess,
michael@0 67 ProtocolId aProtocol)
michael@0 68 : IPC::Message(MSG_ROUTING_CONTROL, // these only go to top-level actors
michael@0 69 CHANNEL_OPENED_MESSAGE_TYPE,
michael@0 70 PRIORITY_NORMAL)
michael@0 71 {
michael@0 72 IPC::WriteParam(this, aDescriptor);
michael@0 73 IPC::WriteParam(this, aOtherProcess);
michael@0 74 IPC::WriteParam(this, static_cast<uint32_t>(aProtocol));
michael@0 75 }
michael@0 76
michael@0 77 static bool Read(const IPC::Message& aMsg,
michael@0 78 TransportDescriptor* aDescriptor,
michael@0 79 ProcessId* aOtherProcess,
michael@0 80 ProtocolId* aProtocol)
michael@0 81 {
michael@0 82 void* iter = nullptr;
michael@0 83 if (!IPC::ReadParam(&aMsg, &iter, aDescriptor) ||
michael@0 84 !IPC::ReadParam(&aMsg, &iter, aOtherProcess) ||
michael@0 85 !IPC::ReadParam(&aMsg, &iter, reinterpret_cast<uint32_t*>(aProtocol))) {
michael@0 86 return false;
michael@0 87 }
michael@0 88 aMsg.EndRead(iter);
michael@0 89 return true;
michael@0 90 }
michael@0 91 };
michael@0 92
michael@0 93 bool
michael@0 94 Bridge(const PrivateIPDLInterface&,
michael@0 95 MessageChannel* aParentChannel, ProcessHandle aParentProcess,
michael@0 96 MessageChannel* aChildChannel, ProcessHandle aChildProcess,
michael@0 97 ProtocolId aProtocol, ProtocolId aChildProtocol)
michael@0 98 {
michael@0 99 ProcessId parentId = GetProcId(aParentProcess);
michael@0 100 ProcessId childId = GetProcId(aChildProcess);
michael@0 101 if (!parentId || !childId) {
michael@0 102 return false;
michael@0 103 }
michael@0 104
michael@0 105 TransportDescriptor parentSide, childSide;
michael@0 106 if (!CreateTransport(aParentProcess, aChildProcess,
michael@0 107 &parentSide, &childSide)) {
michael@0 108 return false;
michael@0 109 }
michael@0 110
michael@0 111 if (!aParentChannel->Send(new ChannelOpened(parentSide,
michael@0 112 childId,
michael@0 113 aProtocol)) ||
michael@0 114 !aChildChannel->Send(new ChannelOpened(childSide,
michael@0 115 parentId,
michael@0 116 aChildProtocol))) {
michael@0 117 CloseDescriptor(parentSide);
michael@0 118 CloseDescriptor(childSide);
michael@0 119 return false;
michael@0 120 }
michael@0 121 return true;
michael@0 122 }
michael@0 123
michael@0 124 bool
michael@0 125 Open(const PrivateIPDLInterface&,
michael@0 126 MessageChannel* aOpenerChannel, ProcessHandle aOtherProcess,
michael@0 127 Transport::Mode aOpenerMode,
michael@0 128 ProtocolId aProtocol, ProtocolId aChildProtocol)
michael@0 129 {
michael@0 130 bool isParent = (Transport::MODE_SERVER == aOpenerMode);
michael@0 131 ProcessHandle thisHandle = GetCurrentProcessHandle();
michael@0 132 ProcessHandle parentHandle = isParent ? thisHandle : aOtherProcess;
michael@0 133 ProcessHandle childHandle = !isParent ? thisHandle : aOtherProcess;
michael@0 134 ProcessId parentId = GetProcId(parentHandle);
michael@0 135 ProcessId childId = GetProcId(childHandle);
michael@0 136 if (!parentId || !childId) {
michael@0 137 return false;
michael@0 138 }
michael@0 139
michael@0 140 TransportDescriptor parentSide, childSide;
michael@0 141 if (!CreateTransport(parentHandle, childHandle,
michael@0 142 &parentSide, &childSide)) {
michael@0 143 return false;
michael@0 144 }
michael@0 145
michael@0 146 Message* parentMsg = new ChannelOpened(parentSide, childId, aProtocol);
michael@0 147 Message* childMsg = new ChannelOpened(childSide, parentId, aChildProtocol);
michael@0 148 nsAutoPtr<Message> messageForUs(isParent ? parentMsg : childMsg);
michael@0 149 nsAutoPtr<Message> messageForOtherSide(!isParent ? parentMsg : childMsg);
michael@0 150 if (!aOpenerChannel->Echo(messageForUs.forget()) ||
michael@0 151 !aOpenerChannel->Send(messageForOtherSide.forget())) {
michael@0 152 CloseDescriptor(parentSide);
michael@0 153 CloseDescriptor(childSide);
michael@0 154 return false;
michael@0 155 }
michael@0 156 return true;
michael@0 157 }
michael@0 158
michael@0 159 bool
michael@0 160 UnpackChannelOpened(const PrivateIPDLInterface&,
michael@0 161 const Message& aMsg,
michael@0 162 TransportDescriptor* aTransport,
michael@0 163 ProcessId* aOtherProcess,
michael@0 164 ProtocolId* aProtocol)
michael@0 165 {
michael@0 166 return ChannelOpened::Read(aMsg, aTransport, aOtherProcess, aProtocol);
michael@0 167 }
michael@0 168
michael@0 169 void
michael@0 170 ProtocolErrorBreakpoint(const char* aMsg)
michael@0 171 {
michael@0 172 // Bugs that generate these error messages can be tough to
michael@0 173 // reproduce. Log always in the hope that someone finds the error
michael@0 174 // message.
michael@0 175 printf_stderr("IPDL protocol error: %s\n", aMsg);
michael@0 176 }
michael@0 177
michael@0 178 void
michael@0 179 FatalError(const char* aProtocolName, const char* aMsg,
michael@0 180 ProcessHandle aHandle, bool aIsParent)
michael@0 181 {
michael@0 182 ProtocolErrorBreakpoint(aMsg);
michael@0 183
michael@0 184 nsAutoCString formattedMessage("IPDL error [");
michael@0 185 formattedMessage.AppendASCII(aProtocolName);
michael@0 186 formattedMessage.AppendLiteral("]: \"");
michael@0 187 formattedMessage.AppendASCII(aMsg);
michael@0 188 if (aIsParent) {
michael@0 189 formattedMessage.AppendLiteral("\". Killing child side as a result.");
michael@0 190 NS_ERROR(formattedMessage.get());
michael@0 191
michael@0 192 if (aHandle != kInvalidProcessHandle &&
michael@0 193 !base::KillProcess(aHandle, base::PROCESS_END_KILLED_BY_USER, false)) {
michael@0 194 NS_ERROR("May have failed to kill child!");
michael@0 195 }
michael@0 196 } else {
michael@0 197 formattedMessage.AppendLiteral("\". abort()ing as a result.");
michael@0 198 NS_RUNTIMEABORT(formattedMessage.get());
michael@0 199 }
michael@0 200 }
michael@0 201
michael@0 202 } // namespace ipc
michael@0 203 } // namespace mozilla

mercurial