MachPort.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Platform.h>
  8. #ifndef AK_OS_MACH
  9. # error "MachPort is only available on Mach platforms"
  10. #endif
  11. #include <AK/Error.h>
  12. #include <AK/Noncopyable.h>
  13. extern "C" {
  14. #include <mach/mach.h>
  15. }
  16. namespace Core {
  17. // https://www.gnu.org/software/hurd/gnumach-doc/Major-Concepts.html#Major-Concepts
  18. class MachPort {
  19. AK_MAKE_NONCOPYABLE(MachPort);
  20. public:
  21. // https://www.gnu.org/software/hurd/gnumach-doc/Exchanging-Port-Rights.html#Exchanging-Port-Rights
  22. enum class PortRight : mach_port_right_t {
  23. Send = MACH_PORT_RIGHT_SEND,
  24. Receive = MACH_PORT_RIGHT_RECEIVE,
  25. SendOnce = MACH_PORT_RIGHT_SEND_ONCE,
  26. PortSet = MACH_PORT_RIGHT_PORT_SET,
  27. DeadName = MACH_PORT_RIGHT_DEAD_NAME,
  28. };
  29. enum class MessageRight : mach_msg_type_name_t {
  30. MoveReceive = MACH_MSG_TYPE_MOVE_RECEIVE,
  31. MoveSend = MACH_MSG_TYPE_MOVE_SEND,
  32. MoveSendOnce = MACH_MSG_TYPE_MOVE_SEND_ONCE,
  33. CopySend = MACH_MSG_TYPE_COPY_SEND,
  34. MakeSend = MACH_MSG_TYPE_MAKE_SEND,
  35. MakeSendOnce = MACH_MSG_TYPE_MAKE_SEND_ONCE,
  36. #if defined(AK_OS_MACOS)
  37. CopyReceive = MACH_MSG_TYPE_COPY_RECEIVE,
  38. DisposeReceive = MACH_MSG_TYPE_DISPOSE_RECEIVE,
  39. DisposeSend = MACH_MSG_TYPE_DISPOSE_SEND,
  40. DisposeSendOnce = MACH_MSG_TYPE_DISPOSE_SEND_ONCE,
  41. #endif
  42. };
  43. MachPort() = default;
  44. MachPort(MachPort&& other);
  45. MachPort& operator=(MachPort&& other);
  46. ~MachPort();
  47. mach_port_t release();
  48. static ErrorOr<MachPort> create_with_right(PortRight);
  49. static MachPort adopt_right(mach_port_t, PortRight);
  50. ErrorOr<MachPort> insert_right(MessageRight);
  51. #if defined(AK_OS_MACOS)
  52. // https://opensource.apple.com/source/launchd/launchd-842.92.1/liblaunch/bootstrap.h.auto.html
  53. static ErrorOr<MachPort> look_up_from_bootstrap_server(ByteString const& service_name);
  54. ErrorOr<void> register_with_bootstrap_server(ByteString const& service_name);
  55. #endif
  56. // FIXME: mach_msg wrapper? For now just let the owner poke into the internals
  57. mach_port_t port() const { return m_port; }
  58. private:
  59. MachPort(PortRight, mach_port_t);
  60. void unref_port();
  61. PortRight m_right { PortRight::DeadName };
  62. mach_port_t m_port { MACH_PORT_NULL };
  63. };
  64. Error mach_error_to_error(kern_return_t error);
  65. }