ladybird/Libraries/LibIPC/AttachmentMachPort.cpp
Aliaksandr Kalenik 4ea4d63008 Everywhere: Replace Unix socket IPC transport with Mach ports on macOS
On macOS, use Mach port messaging instead of Unix domain sockets for
all IPC transport. This makes the transport capable of carrying Mach
port rights as message attachments, which is a prerequisite for sending
IOSurface handles over the main IPC channel (currently sent via a
separate out-of-band path). It also avoids the need for the FD
acknowledgement protocol that TransportSocket requires, since Mach port
right transfers are atomic in the kernel.

Three connection establishment patterns:

- Spawned helper processes (WebContent, RequestServer, etc.) use the
  existing MachPortServer: the child sends its task port with a reply
  port, and the parent responds with a pre-created port pair.

- Socket-bootstrapped connections (WebDriver, BrowserProcess) exchange
  Mach port names over the socket, then drop the socket.

- Pre-created pairs for IPC tests and in-message transport transfer.

Attachment on macOS now wraps a MachPort instead of a file descriptor,
converting between the two via fileport_makeport()/fileport_makefd().

The LibIPC socket transport tests are disabled on macOS since they are
socket-specific.
2026-03-23 18:50:48 +01:00

74 lines
2 KiB
C++

/*
* Copyright (c) 2026, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Format.h>
#include <LibCore/MachPort.h>
#include <LibCore/System.h>
#include <LibIPC/Attachment.h>
// fileport_makeport() and fileport_makefd() are private macOS APIs that convert
// between file descriptors and Mach port rights. Since Mach messages can only
// carry port rights (not file descriptors), we convert FDs to "file ports" for
// transmission and convert them back on the receiving side. These APIs are stable
// and used by Apple's own frameworks (including XPC).
extern "C" {
int fileport_makeport(int fd, mach_port_t* port);
int fileport_makefd(mach_port_t port);
}
namespace IPC {
Attachment::Attachment(Attachment&& other)
: m_port(move(other.m_port))
, m_message_right(other.m_message_right)
{
}
Attachment& Attachment::operator=(Attachment&& other)
{
if (this != &other) {
m_port = move(other.m_port);
m_message_right = other.m_message_right;
}
return *this;
}
Attachment::~Attachment() = default;
Attachment Attachment::from_fd(int fd)
{
mach_port_t port = MACH_PORT_NULL;
auto result = fileport_makeport(fd, &port);
(void)Core::System::close(fd);
VERIFY(result == 0);
return from_mach_port(Core::MachPort::adopt_right(port, Core::MachPort::PortRight::Send), Core::MachPort::MessageRight::MoveSend);
}
int Attachment::to_fd()
{
VERIFY(MACH_PORT_VALID(m_port.port()));
int fd = fileport_makefd(m_port.port());
VERIFY(fd >= 0);
mach_port_deallocate(mach_task_self(), m_port.release());
return fd;
}
Attachment Attachment::from_mach_port(Core::MachPort port, Core::MachPort::MessageRight right)
{
VERIFY(MACH_PORT_VALID(port.port()));
Attachment attachment;
attachment.m_port = move(port);
attachment.m_message_right = right;
return attachment;
}
Core::MachPort Attachment::release_mach_port()
{
VERIFY(MACH_PORT_VALID(m_port.port()));
return move(m_port);
}
}