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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
|
/*
* Copyright (C) 2010 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "Connection.h"
#include "DataReference.h"
#include "ImportanceAssertion.h"
#include "MachPort.h"
#include "MachUtilities.h"
#include <WebCore/AXObjectCache.h>
#include <mach/mach_error.h>
#include <mach/vm_map.h>
#include <sys/mman.h>
#include <wtf/RunLoop.h>
#include <wtf/spi/darwin/XPCSPI.h>
#if PLATFORM(IOS)
#include "ProcessAssertion.h"
#include <UIKit/UIAccessibility.h>
#if USE(APPLE_INTERNAL_SDK)
#include <AXRuntime/AXDefines.h>
#include <AXRuntime/AXNotificationConstants.h>
#else
#define kAXPidStatusChangedNotification 0
#endif
#endif
#if PLATFORM(MAC)
#if USE(APPLE_INTERNAL_SDK)
#include <HIServices/AccessibilityPriv.h>
#else
typedef enum {
AXSuspendStatusRunning = 0,
AXSuspendStatusSuspended,
} AXSuspendStatus;
#endif
extern "C" AXError _AXUIElementNotifyProcessSuspendStatus(AXSuspendStatus);
#endif // PLATFORM(MAC)
namespace IPC {
static const size_t inlineMessageMaxSize = 4096;
// Message flags.
enum {
MessageBodyIsOutOfLine = 1 << 0
};
// ConnectionTerminationWatchdog does two things:
// 1) It sets a watchdog timer to kill the peered process.
// 2) On iOS, make the process runnable for the duration of the watchdog
// to ensure it has a chance to terminate cleanly.
class ConnectionTerminationWatchdog {
public:
static void createConnectionTerminationWatchdog(OSObjectPtr<xpc_connection_t>& xpcConnection, double intervalInSeconds)
{
new ConnectionTerminationWatchdog(xpcConnection, intervalInSeconds);
}
private:
ConnectionTerminationWatchdog(OSObjectPtr<xpc_connection_t>& xpcConnection, double intervalInSeconds)
: m_xpcConnection(xpcConnection)
, m_watchdogTimer(RunLoop::main(), this, &ConnectionTerminationWatchdog::watchdogTimerFired)
#if PLATFORM(IOS)
, m_assertion(std::make_unique<WebKit::ProcessAndUIAssertion>(xpc_connection_get_pid(m_xpcConnection.get()), WebKit::AssertionState::Background))
#endif
{
m_watchdogTimer.startOneShot(intervalInSeconds);
}
void watchdogTimerFired()
{
xpc_connection_kill(m_xpcConnection.get(), SIGKILL);
delete this;
}
OSObjectPtr<xpc_connection_t> m_xpcConnection;
RunLoop::Timer<ConnectionTerminationWatchdog> m_watchdogTimer;
#if PLATFORM(IOS)
std::unique_ptr<WebKit::ProcessAndUIAssertion> m_assertion;
#endif
};
void Connection::platformInvalidate()
{
if (!m_isConnected) {
if (m_sendPort) {
mach_port_deallocate(mach_task_self(), m_sendPort);
m_sendPort = MACH_PORT_NULL;
}
if (m_receivePort) {
mach_port_mod_refs(mach_task_self(), m_receivePort, MACH_PORT_RIGHT_RECEIVE, -1);
m_receivePort = MACH_PORT_NULL;
}
return;
}
m_isConnected = false;
ASSERT(m_sendPort);
ASSERT(m_receivePort);
// Unregister our ports.
dispatch_source_cancel(m_deadNameSource);
dispatch_release(m_deadNameSource);
m_deadNameSource = 0;
m_sendPort = MACH_PORT_NULL;
dispatch_source_cancel(m_receivePortDataAvailableSource);
dispatch_release(m_receivePortDataAvailableSource);
m_receivePortDataAvailableSource = 0;
m_receivePort = MACH_PORT_NULL;
#if (PLATFORM(MAC) || (PLATFORM(QT) && USE(MACH_PORTS))) && __MAC_OS_X_VERSION_MIN_REQUIRED <= 101000
if (m_exceptionPort) {
dispatch_source_cancel(m_exceptionPortDataAvailableSource);
dispatch_release(m_exceptionPortDataAvailableSource);
m_exceptionPortDataAvailableSource = 0;
m_exceptionPort = MACH_PORT_NULL;
}
#endif
}
void Connection::terminateSoon(double intervalInSeconds)
{
if (m_xpcConnection)
ConnectionTerminationWatchdog::createConnectionTerminationWatchdog(m_xpcConnection, intervalInSeconds);
}
void Connection::platformInitialize(Identifier identifier)
{
#if (PLATFORM(MAC) || (PLATFORM(QT) && USE(MACH_PORTS))) && __MAC_OS_X_VERSION_MIN_REQUIRED <= 101000
m_exceptionPort = MACH_PORT_NULL;
m_exceptionPortDataAvailableSource = nullptr;
#endif
if (m_isServer) {
m_receivePort = identifier.port;
m_sendPort = MACH_PORT_NULL;
} else {
m_receivePort = MACH_PORT_NULL;
m_sendPort = identifier.port;
}
m_deadNameSource = nullptr;
m_receivePortDataAvailableSource = nullptr;
m_xpcConnection = identifier.xpcConnection;
}
template<typename Function>
static dispatch_source_t createDataAvailableSource(mach_port_t receivePort, WorkQueue& workQueue, Function&& function)
{
dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_RECV, receivePort, 0, workQueue.dispatchQueue());
dispatch_source_set_event_handler(source, function);
dispatch_source_set_cancel_handler(source, ^{
mach_port_mod_refs(mach_task_self(), receivePort, MACH_PORT_RIGHT_RECEIVE, -1);
});
return source;
}
bool Connection::open()
{
if (m_isServer) {
ASSERT(m_receivePort);
ASSERT(!m_sendPort);
} else {
ASSERT(!m_receivePort);
ASSERT(m_sendPort);
// Create the receive port.
mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &m_receivePort);
#if PLATFORM(MAC)
mach_port_set_attributes(mach_task_self(), m_receivePort, MACH_PORT_DENAP_RECEIVER, (mach_port_info_t)0, 0);
#endif
m_isConnected = true;
// Send the initialize message, which contains a send right for the server to use.
auto encoder = std::make_unique<MessageEncoder>("IPC", "InitializeConnection", 0);
encoder->encode(MachPort(m_receivePort, MACH_MSG_TYPE_MAKE_SEND));
sendMessage(WTFMove(encoder));
initializeDeadNameSource();
}
// Change the message queue length for the receive port.
setMachPortQueueLength(m_receivePort, MACH_PORT_QLIMIT_LARGE);
// Register the data available handler.
RefPtr<Connection> connection(this);
m_receivePortDataAvailableSource = createDataAvailableSource(m_receivePort, m_connectionQueue, [connection] {
connection->receiveSourceEventHandler();
});
#if (PLATFORM(MAC) || (PLATFORM(QT) && USE(MACH_PORTS))) && __MAC_OS_X_VERSION_MIN_REQUIRED <= 101000
if (m_exceptionPort) {
m_exceptionPortDataAvailableSource = createDataAvailableSource(m_exceptionPort, m_connectionQueue, [connection] {
connection->exceptionSourceEventHandler();
});
auto encoder = std::make_unique<MessageEncoder>("IPC", "SetExceptionPort", 0);
encoder->encode(MachPort(m_exceptionPort, MACH_MSG_TYPE_MAKE_SEND));
sendMessage(WTFMove(encoder));
}
#endif
ref();
dispatch_async(m_connectionQueue->dispatchQueue(), ^{
dispatch_resume(m_receivePortDataAvailableSource);
if (m_deadNameSource)
dispatch_resume(m_deadNameSource);
#if (PLATFORM(MAC) || (PLATFORM(QT) && USE(MACH_PORTS))) && __MAC_OS_X_VERSION_MIN_REQUIRED <= 101000
if (m_exceptionPortDataAvailableSource)
dispatch_resume(m_exceptionPortDataAvailableSource);
#endif
deref();
});
return true;
}
static inline size_t machMessageSize(size_t bodySize, size_t numberOfPortDescriptors = 0, size_t numberOfOOLMemoryDescriptors = 0)
{
size_t size = sizeof(mach_msg_header_t) + bodySize;
if (numberOfPortDescriptors || numberOfOOLMemoryDescriptors) {
size += sizeof(mach_msg_body_t);
if (numberOfPortDescriptors)
size += (numberOfPortDescriptors * sizeof(mach_msg_port_descriptor_t));
if (numberOfOOLMemoryDescriptors)
size += (numberOfOOLMemoryDescriptors * sizeof(mach_msg_ool_descriptor_t));
}
return round_msg(size);
}
bool Connection::platformCanSendOutgoingMessages() const
{
return true;
}
bool Connection::sendOutgoingMessage(std::unique_ptr<MessageEncoder> encoder)
{
Vector<Attachment> attachments = encoder->releaseAttachments();
size_t numberOfPortDescriptors = 0;
size_t numberOfOOLMemoryDescriptors = 0;
for (size_t i = 0; i < attachments.size(); ++i) {
Attachment::Type type = attachments[i].type();
if (type == Attachment::MachPortType)
numberOfPortDescriptors++;
}
size_t messageSize = machMessageSize(encoder->bufferSize(), numberOfPortDescriptors, numberOfOOLMemoryDescriptors);
bool messageBodyIsOOL = false;
if (messageSize > inlineMessageMaxSize) {
messageBodyIsOOL = true;
numberOfOOLMemoryDescriptors++;
messageSize = machMessageSize(0, numberOfPortDescriptors, numberOfOOLMemoryDescriptors);
}
char stackBuffer[inlineMessageMaxSize];
char* buffer = &stackBuffer[0];
if (messageSize > inlineMessageMaxSize) {
buffer = (char*)mmap(0, messageSize, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
if (buffer == MAP_FAILED)
return false;
}
bool isComplex = (numberOfPortDescriptors + numberOfOOLMemoryDescriptors > 0);
mach_msg_header_t* header = reinterpret_cast<mach_msg_header_t*>(buffer);
header->msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0);
header->msgh_size = messageSize;
header->msgh_remote_port = m_sendPort;
header->msgh_local_port = MACH_PORT_NULL;
header->msgh_id = 0;
if (messageBodyIsOOL)
header->msgh_id |= MessageBodyIsOutOfLine;
uint8_t* messageData;
if (isComplex) {
header->msgh_bits |= MACH_MSGH_BITS_COMPLEX;
mach_msg_body_t* body = reinterpret_cast<mach_msg_body_t*>(header + 1);
body->msgh_descriptor_count = numberOfPortDescriptors + numberOfOOLMemoryDescriptors;
uint8_t* descriptorData = reinterpret_cast<uint8_t*>(body + 1);
for (size_t i = 0; i < attachments.size(); ++i) {
Attachment attachment = attachments[i];
mach_msg_descriptor_t* descriptor = reinterpret_cast<mach_msg_descriptor_t*>(descriptorData);
switch (attachment.type()) {
case Attachment::MachPortType:
descriptor->port.name = attachment.port();
descriptor->port.disposition = attachment.disposition();
descriptor->port.type = MACH_MSG_PORT_DESCRIPTOR;
descriptorData += sizeof(mach_msg_port_descriptor_t);
break;
default:
ASSERT_NOT_REACHED();
}
}
if (messageBodyIsOOL) {
mach_msg_descriptor_t* descriptor = reinterpret_cast<mach_msg_descriptor_t*>(descriptorData);
descriptor->out_of_line.address = encoder->buffer();
descriptor->out_of_line.size = encoder->bufferSize();
descriptor->out_of_line.copy = MACH_MSG_VIRTUAL_COPY;
descriptor->out_of_line.deallocate = false;
descriptor->out_of_line.type = MACH_MSG_OOL_DESCRIPTOR;
descriptorData += sizeof(mach_msg_ool_descriptor_t);
}
messageData = descriptorData;
} else
messageData = (uint8_t*)(header + 1);
// Copy the data if it is not being sent out-of-line.
if (!messageBodyIsOOL)
memcpy(messageData, encoder->buffer(), encoder->bufferSize());
ASSERT(m_sendPort);
// Send the message.
kern_return_t kr = mach_msg(header, MACH_SEND_MSG, messageSize, 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
if (kr != KERN_SUCCESS) {
// FIXME: What should we do here?
}
if (buffer != &stackBuffer[0])
munmap(buffer, messageSize);
return true;
}
void Connection::initializeDeadNameSource()
{
m_deadNameSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_SEND, m_sendPort, 0, m_connectionQueue->dispatchQueue());
RefPtr<Connection> connection(this);
dispatch_source_set_event_handler(m_deadNameSource, [connection] {
connection->connectionDidClose();
});
mach_port_t sendPort = m_sendPort;
dispatch_source_set_cancel_handler(m_deadNameSource, ^{
// Release our send right.
mach_port_deallocate(mach_task_self(), sendPort);
});
}
static std::unique_ptr<MessageDecoder> createMessageDecoder(mach_msg_header_t* header)
{
if (!(header->msgh_bits & MACH_MSGH_BITS_COMPLEX)) {
// We have a simple message.
uint8_t* body = reinterpret_cast<uint8_t*>(header + 1);
size_t bodySize = header->msgh_size - sizeof(mach_msg_header_t);
return std::make_unique<MessageDecoder>(DataReference(body, bodySize), Vector<Attachment>());
}
bool messageBodyIsOOL = header->msgh_id & MessageBodyIsOutOfLine;
mach_msg_body_t* body = reinterpret_cast<mach_msg_body_t*>(header + 1);
mach_msg_size_t numDescriptors = body->msgh_descriptor_count;
ASSERT(numDescriptors);
uint8_t* descriptorData = reinterpret_cast<uint8_t*>(body + 1);
// If the message body was sent out-of-line, don't treat the last descriptor
// as an attachment, since it is really the message body.
if (messageBodyIsOOL)
--numDescriptors;
// Build attachment list
Vector<Attachment> attachments(numDescriptors);
for (mach_msg_size_t i = 0; i < numDescriptors; ++i) {
mach_msg_descriptor_t* descriptor = reinterpret_cast<mach_msg_descriptor_t*>(descriptorData);
switch (descriptor->type.type) {
case MACH_MSG_PORT_DESCRIPTOR:
attachments[numDescriptors - i - 1] = Attachment(descriptor->port.name, descriptor->port.disposition);
descriptorData += sizeof(mach_msg_port_descriptor_t);
break;
default:
ASSERT(false && "Unhandled descriptor type");
}
}
if (messageBodyIsOOL) {
mach_msg_descriptor_t* descriptor = reinterpret_cast<mach_msg_descriptor_t*>(descriptorData);
ASSERT(descriptor->type.type == MACH_MSG_OOL_DESCRIPTOR);
uint8_t* messageBody = static_cast<uint8_t*>(descriptor->out_of_line.address);
size_t messageBodySize = descriptor->out_of_line.size;
auto decoder = std::make_unique<MessageDecoder>(DataReference(messageBody, messageBodySize), WTFMove(attachments));
vm_deallocate(mach_task_self(), reinterpret_cast<vm_address_t>(descriptor->out_of_line.address), descriptor->out_of_line.size);
return decoder;
}
uint8_t* messageBody = descriptorData;
size_t messageBodySize = header->msgh_size - (descriptorData - reinterpret_cast<uint8_t*>(header));
return std::make_unique<MessageDecoder>(DataReference(messageBody, messageBodySize), attachments);
}
// The receive buffer size should always include the maximum trailer size.
static const size_t receiveBufferSize = inlineMessageMaxSize + MAX_TRAILER_SIZE;
typedef Vector<char, receiveBufferSize> ReceiveBuffer;
static mach_msg_header_t* readFromMachPort(mach_port_t machPort, ReceiveBuffer& buffer)
{
buffer.resize(receiveBufferSize);
mach_msg_header_t* header = reinterpret_cast<mach_msg_header_t*>(buffer.data());
kern_return_t kr = mach_msg(header, MACH_RCV_MSG | MACH_RCV_LARGE | MACH_RCV_TIMEOUT, 0, buffer.size(), machPort, 0, MACH_PORT_NULL);
if (kr == MACH_RCV_TIMED_OUT)
return 0;
if (kr == MACH_RCV_TOO_LARGE) {
// The message was too large, resize the buffer and try again.
buffer.resize(header->msgh_size + MAX_TRAILER_SIZE);
header = reinterpret_cast<mach_msg_header_t*>(buffer.data());
kr = mach_msg(header, MACH_RCV_MSG | MACH_RCV_LARGE | MACH_RCV_TIMEOUT, 0, buffer.size(), machPort, 0, MACH_PORT_NULL);
ASSERT(kr != MACH_RCV_TOO_LARGE);
}
if (kr != MACH_MSG_SUCCESS) {
ASSERT_NOT_REACHED();
return 0;
}
return header;
}
void Connection::receiveSourceEventHandler()
{
ReceiveBuffer buffer;
mach_msg_header_t* header = readFromMachPort(m_receivePort, buffer);
if (!header)
return;
std::unique_ptr<MessageDecoder> decoder = createMessageDecoder(header);
ASSERT(decoder);
#if PLATFORM(MAC)
decoder->setImportanceAssertion(std::make_unique<ImportanceAssertion>(header));
#endif
if (decoder->messageReceiverName() == "IPC" && decoder->messageName() == "InitializeConnection") {
ASSERT(m_isServer);
ASSERT(!m_isConnected);
ASSERT(!m_sendPort);
MachPort port;
if (!decoder->decode(port)) {
// FIXME: Disconnect.
return;
}
m_sendPort = port.port();
if (m_sendPort) {
initializeDeadNameSource();
dispatch_resume(m_deadNameSource);
}
m_isConnected = true;
// Send any pending outgoing messages.
sendOutgoingMessages();
return;
}
#if !PLATFORM(IOS)
if (decoder->messageReceiverName() == "IPC" && decoder->messageName() == "SetExceptionPort") {
if (m_isServer) {
// Server connections aren't supposed to have their exception ports overriden. Treat this as an invalid message.
RefPtr<Connection> protectedThis(this);
StringReference messageReceiverName = decoder->messageReceiverName();
StringCapture capturedMessageReceiverName(String(messageReceiverName.data(), messageReceiverName.size()));
StringReference messageName = decoder->messageName();
StringCapture capturedMessageName(String(messageName.data(), messageName.size()));
RunLoop::main().dispatch([protectedThis, capturedMessageReceiverName, capturedMessageName] {
protectedThis->dispatchDidReceiveInvalidMessage(capturedMessageReceiverName.string().utf8(), capturedMessageName.string().utf8());
});
return;
}
MachPort exceptionPort;
if (!decoder->decode(exceptionPort))
return;
setMachExceptionPort(exceptionPort.port());
return;
}
#endif
processIncomingMessage(WTFMove(decoder));
}
#if (PLATFORM(MAC) || (PLATFORM(QT) && USE(MACH_PORTS))) && __MAC_OS_X_VERSION_MIN_REQUIRED <= 101000
void Connection::exceptionSourceEventHandler()
{
ReceiveBuffer buffer;
mach_msg_header_t* header = readFromMachPort(m_exceptionPort, buffer);
if (!header)
return;
// We've read the exception message. Now send it on to the real exception port.
// The remote port should have a send once right.
ASSERT(MACH_MSGH_BITS_REMOTE(header->msgh_bits) == MACH_MSG_TYPE_MOVE_SEND_ONCE);
// Now get the real exception port.
mach_port_t exceptionPort = machExceptionPort();
// First, get the complex bit from the source message.
mach_msg_bits_t messageBits = header->msgh_bits & MACH_MSGH_BITS_COMPLEX;
messageBits |= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MOVE_SEND_ONCE);
header->msgh_bits = messageBits;
header->msgh_local_port = header->msgh_remote_port;
header->msgh_remote_port = exceptionPort;
// Now send along the message.
kern_return_t kr = mach_msg(header, MACH_SEND_MSG, header->msgh_size, 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
if (kr != KERN_SUCCESS)
LOG_ERROR("Failed to send message to real exception port. %s (%x)", mach_error_string(kr), kr);
connectionDidClose();
}
void Connection::setShouldCloseConnectionOnMachExceptions()
{
ASSERT(m_exceptionPort == MACH_PORT_NULL);
if (mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &m_exceptionPort) != KERN_SUCCESS)
ASSERT_NOT_REACHED();
if (mach_port_insert_right(mach_task_self(), m_exceptionPort, m_exceptionPort, MACH_MSG_TYPE_MAKE_SEND) != KERN_SUCCESS)
ASSERT_NOT_REACHED();
}
#endif
IPC::Connection::Identifier Connection::identifier() const
{
return Identifier(m_isServer ? m_receivePort : m_sendPort, m_xpcConnection);
}
bool Connection::getAuditToken(audit_token_t& auditToken)
{
if (!m_xpcConnection)
return false;
xpc_connection_get_audit_token(m_xpcConnection.get(), &auditToken);
return true;
}
bool Connection::kill()
{
if (m_xpcConnection) {
xpc_connection_kill(m_xpcConnection.get(), SIGKILL);
return true;
}
return false;
}
static void AccessibilityProcessSuspendedNotification(bool suspended)
{
#if PLATFORM(MAC)
_AXUIElementNotifyProcessSuspendStatus(suspended ? AXSuspendStatusSuspended : AXSuspendStatusRunning);
#elif PLATFORM(IOS)
UIAccessibilityPostNotification(kAXPidStatusChangedNotification, @{ @"pid" : @(getpid()), @"suspended" : @(suspended) });
#else
UNUSED_PARAM(suspended);
#endif
}
void Connection::willSendSyncMessage(unsigned flags)
{
if ((flags & InformPlatformProcessWillSuspend) && WebCore::AXObjectCache::accessibilityEnabled())
AccessibilityProcessSuspendedNotification(true);
}
void Connection::didReceiveSyncReply(unsigned flags)
{
if ((flags & InformPlatformProcessWillSuspend) && WebCore::AXObjectCache::accessibilityEnabled())
AccessibilityProcessSuspendedNotification(false);
}
pid_t Connection::remoteProcessID() const
{
if (!m_xpcConnection)
return 0;
return xpc_connection_get_pid(m_xpcConnection.get());
}
} // namespace IPC
|