2026-03-19 15:42:05 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2026, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/HashMap.h>
|
|
|
|
|
#include <AK/Platform.h>
|
|
|
|
|
#include <AK/StringView.h>
|
|
|
|
|
#include <AK/Variant.h>
|
|
|
|
|
|
|
|
|
|
#if !defined(AK_OS_MACH)
|
|
|
|
|
# error "TransportBootstrapMach is only available on Mach platforms"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <LibCore/MachPort.h>
|
2025-10-30 09:29:29 -03:00
|
|
|
#include <LibSync/Mutex.h>
|
2026-03-19 15:42:05 -03:00
|
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
|
|
|
|
|
struct TransportBootstrapMachPorts {
|
|
|
|
|
Core::MachPort receive_right;
|
|
|
|
|
Core::MachPort send_right;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ErrorOr<TransportBootstrapMachPorts> bootstrap_transport_from_mach_server(StringView server_name);
|
|
|
|
|
ErrorOr<TransportBootstrapMachPorts> bootstrap_transport_from_server_port(Core::MachPort const& server_port);
|
|
|
|
|
|
|
|
|
|
class TransportBootstrapMachServer {
|
|
|
|
|
AK_MAKE_NONCOPYABLE(TransportBootstrapMachServer);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
TransportBootstrapMachServer() = default;
|
|
|
|
|
|
2026-03-24 01:42:42 -03:00
|
|
|
struct ChildTransportHandled {
|
2026-03-22 15:59:08 -03:00
|
|
|
};
|
|
|
|
|
struct OnDemandTransport {
|
|
|
|
|
TransportBootstrapMachPorts ports;
|
|
|
|
|
};
|
2026-03-24 01:42:42 -03:00
|
|
|
using BootstrapRequestResult = Variant<ChildTransportHandled, OnDemandTransport>;
|
2026-03-22 15:59:08 -03:00
|
|
|
|
2026-03-24 01:42:42 -03:00
|
|
|
// Hold this lock across process spawn and child transport registration so a
|
|
|
|
|
// child bootstrap request cannot observe an unregistered pid.
|
2025-10-30 09:29:29 -03:00
|
|
|
Sync::Mutex& child_registration_lock() { return m_child_registration_mutex; }
|
2026-03-19 15:42:05 -03:00
|
|
|
|
2026-03-24 01:42:42 -03:00
|
|
|
// Must be called while holding child_registration_lock().
|
|
|
|
|
void register_child_transport(pid_t, TransportBootstrapMachPorts);
|
|
|
|
|
ErrorOr<BootstrapRequestResult> handle_bootstrap_request(pid_t, Core::MachPort reply_port);
|
2026-03-19 15:42:05 -03:00
|
|
|
|
2026-03-24 01:42:42 -03:00
|
|
|
private:
|
2026-03-19 15:42:05 -03:00
|
|
|
static void send_transport_ports_to_child(Core::MachPort reply_port, TransportBootstrapMachPorts ports);
|
2026-03-22 15:59:08 -03:00
|
|
|
static ErrorOr<TransportBootstrapMachPorts> create_on_demand_local_transport(Core::MachPort reply_port);
|
2026-03-19 15:42:05 -03:00
|
|
|
|
2025-10-30 09:29:29 -03:00
|
|
|
Sync::Mutex m_child_registration_mutex;
|
2026-03-24 01:42:42 -03:00
|
|
|
HashMap<pid_t, TransportBootstrapMachPorts> m_child_transports;
|
2026-03-19 15:42:05 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|