Files
ladybird/Libraries/LibCore/MachPort.h
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

91 lines
2.5 KiB
C++

/*
* Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Platform.h>
#ifndef AK_OS_MACH
# error "MachPort is only available on Mach platforms"
#endif
#include <AK/Error.h>
#include <AK/Noncopyable.h>
#include <LibCore/Export.h>
#if defined(AK_OS_MACOS) || defined(AK_OS_IOS)
# include <mach/mach.h>
#else
extern "C" {
# include <mach/mach.h>
}
#endif
namespace Core {
// https://www.gnu.org/software/hurd/gnumach-doc/Major-Concepts.html#Major-Concepts
class CORE_API MachPort {
AK_MAKE_NONCOPYABLE(MachPort);
public:
// https://www.gnu.org/software/hurd/gnumach-doc/Exchanging-Port-Rights.html#Exchanging-Port-Rights
enum class PortRight : mach_port_right_t {
Send = MACH_PORT_RIGHT_SEND,
Receive = MACH_PORT_RIGHT_RECEIVE,
SendOnce = MACH_PORT_RIGHT_SEND_ONCE,
PortSet = MACH_PORT_RIGHT_PORT_SET,
DeadName = MACH_PORT_RIGHT_DEAD_NAME,
};
enum class MessageRight : mach_msg_type_name_t {
MoveReceive = MACH_MSG_TYPE_MOVE_RECEIVE,
MoveSend = MACH_MSG_TYPE_MOVE_SEND,
MoveSendOnce = MACH_MSG_TYPE_MOVE_SEND_ONCE,
CopySend = MACH_MSG_TYPE_COPY_SEND,
MakeSend = MACH_MSG_TYPE_MAKE_SEND,
MakeSendOnce = MACH_MSG_TYPE_MAKE_SEND_ONCE,
#if defined(AK_OS_MACOS)
CopyReceive = MACH_MSG_TYPE_COPY_RECEIVE,
DisposeReceive = MACH_MSG_TYPE_DISPOSE_RECEIVE,
DisposeSend = MACH_MSG_TYPE_DISPOSE_SEND,
DisposeSendOnce = MACH_MSG_TYPE_DISPOSE_SEND_ONCE,
#endif
};
MachPort() = default;
MachPort(MachPort&& other);
MachPort& operator=(MachPort&& other);
~MachPort();
mach_port_t release();
static ErrorOr<MachPort> create_with_right(PortRight);
static MachPort adopt_right(mach_port_t, PortRight);
ErrorOr<MachPort> insert_right(MessageRight);
#if defined(AK_OS_MACOS)
// https://opensource.apple.com/source/launchd/launchd-842.92.1/liblaunch/bootstrap.h.auto.html
static ErrorOr<MachPort> look_up_from_bootstrap_server(ByteString const& service_name);
ErrorOr<void> register_with_bootstrap_server(ByteString const& service_name);
#endif
// FIXME: mach_msg wrapper? For now just let the owner poke into the internals
mach_port_t port() const { return m_port; }
private:
MachPort(PortRight, mach_port_t);
void unref_port();
PortRight m_right { PortRight::DeadName };
mach_port_t m_port { MACH_PORT_NULL };
};
CORE_API Error mach_error_to_error(kern_return_t error);
}