LocalServer.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/LocalServer.h>
  7. #include <LibCore/Notifier.h>
  8. #include <LibCore/SessionManagement.h>
  9. #include <LibCore/Socket.h>
  10. #include <LibCore/System.h>
  11. #include <LibCore/SystemServerTakeover.h>
  12. #include <fcntl.h>
  13. #include <stdio.h>
  14. #include <sys/socket.h>
  15. #include <sys/stat.h>
  16. #include <unistd.h>
  17. #ifndef SOCK_NONBLOCK
  18. # include <sys/ioctl.h>
  19. #endif
  20. namespace Core {
  21. LocalServer::LocalServer(EventReceiver* parent)
  22. : EventReceiver(parent)
  23. {
  24. }
  25. LocalServer::~LocalServer()
  26. {
  27. if (m_fd >= 0)
  28. ::close(m_fd);
  29. }
  30. ErrorOr<void> LocalServer::take_over_from_system_server(ByteString const& socket_path)
  31. {
  32. if (m_listening)
  33. return Error::from_string_literal("Core::LocalServer: Can't perform socket takeover when already listening");
  34. auto const parsed_path = TRY(Core::SessionManagement::parse_path_with_sid(socket_path));
  35. auto socket = TRY(take_over_socket_from_system_server(parsed_path));
  36. m_fd = TRY(socket->release_fd());
  37. m_listening = true;
  38. setup_notifier();
  39. return {};
  40. }
  41. ErrorOr<void> LocalServer::take_over_fd(int socket_fd)
  42. {
  43. if (m_listening)
  44. return Error::from_string_literal("Core::LocalServer: Can't perform socket takeover when already listening");
  45. m_fd = socket_fd;
  46. m_listening = true;
  47. setup_notifier();
  48. return {};
  49. }
  50. void LocalServer::setup_notifier()
  51. {
  52. m_notifier = Notifier::construct(m_fd, Notifier::Type::Read, this);
  53. m_notifier->on_activation = [this] {
  54. if (on_accept) {
  55. auto maybe_client_socket = accept();
  56. if (maybe_client_socket.is_error()) {
  57. dbgln("LocalServer::on_ready_to_read: Error accepting a connection: {}", maybe_client_socket.error());
  58. if (on_accept_error)
  59. on_accept_error(maybe_client_socket.release_error());
  60. return;
  61. }
  62. on_accept(maybe_client_socket.release_value());
  63. }
  64. };
  65. }
  66. bool LocalServer::listen(ByteString const& address)
  67. {
  68. if (m_listening)
  69. return false;
  70. int rc;
  71. #ifdef SOCK_NONBLOCK
  72. m_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  73. #else
  74. m_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  75. int option = 1;
  76. ioctl(m_fd, FIONBIO, &option);
  77. fcntl(m_fd, F_SETFD, FD_CLOEXEC);
  78. #endif
  79. VERIFY(m_fd >= 0);
  80. #if !defined(AK_OS_MACOS) && !defined(AK_OS_IOS)
  81. rc = fchmod(m_fd, 0600);
  82. if (rc < 0) {
  83. perror("fchmod");
  84. VERIFY_NOT_REACHED();
  85. }
  86. #endif
  87. auto socket_address = SocketAddress::local(address);
  88. auto un_optional = socket_address.to_sockaddr_un();
  89. if (!un_optional.has_value()) {
  90. perror("bind");
  91. return false;
  92. }
  93. auto un = un_optional.value();
  94. rc = ::bind(m_fd, (sockaddr const*)&un, sizeof(un));
  95. if (rc < 0) {
  96. perror("bind");
  97. return false;
  98. }
  99. rc = ::listen(m_fd, 5);
  100. if (rc < 0) {
  101. perror("listen");
  102. return false;
  103. }
  104. m_listening = true;
  105. setup_notifier();
  106. return true;
  107. }
  108. ErrorOr<NonnullOwnPtr<LocalSocket>> LocalServer::accept()
  109. {
  110. VERIFY(m_listening);
  111. sockaddr_un un;
  112. socklen_t un_size = sizeof(un);
  113. #if !defined(AK_OS_MACOS) && !defined(AK_OS_IOS) && !defined(AK_OS_HAIKU)
  114. int accepted_fd = ::accept4(m_fd, (sockaddr*)&un, &un_size, SOCK_NONBLOCK | SOCK_CLOEXEC);
  115. #else
  116. int accepted_fd = ::accept(m_fd, (sockaddr*)&un, &un_size);
  117. #endif
  118. if (accepted_fd < 0) {
  119. return Error::from_syscall("accept"sv, -errno);
  120. }
  121. #if defined(AK_OS_MACOS) || defined(AK_OS_IOS) || defined(AK_OS_HAIKU)
  122. int option = 1;
  123. ioctl(m_fd, FIONBIO, &option);
  124. (void)fcntl(accepted_fd, F_SETFD, FD_CLOEXEC);
  125. #endif
  126. return LocalSocket::adopt_fd(accepted_fd);
  127. }
  128. }