LocalServer.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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(DeprecatedString 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. void LocalServer::setup_notifier()
  42. {
  43. m_notifier = Notifier::construct(m_fd, Notifier::Type::Read, this);
  44. m_notifier->on_activation = [this] {
  45. if (on_accept) {
  46. auto maybe_client_socket = accept();
  47. if (maybe_client_socket.is_error()) {
  48. dbgln("LocalServer::on_ready_to_read: Error accepting a connection: {}", maybe_client_socket.error());
  49. if (on_accept_error)
  50. on_accept_error(maybe_client_socket.release_error());
  51. return;
  52. }
  53. on_accept(maybe_client_socket.release_value());
  54. }
  55. };
  56. }
  57. bool LocalServer::listen(DeprecatedString const& address)
  58. {
  59. if (m_listening)
  60. return false;
  61. int rc;
  62. #ifdef SOCK_NONBLOCK
  63. m_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  64. #else
  65. m_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  66. int option = 1;
  67. ioctl(m_fd, FIONBIO, &option);
  68. fcntl(m_fd, F_SETFD, FD_CLOEXEC);
  69. #endif
  70. VERIFY(m_fd >= 0);
  71. #ifndef AK_OS_MACOS
  72. rc = fchmod(m_fd, 0600);
  73. if (rc < 0) {
  74. perror("fchmod");
  75. VERIFY_NOT_REACHED();
  76. }
  77. #endif
  78. auto socket_address = SocketAddress::local(address);
  79. auto un_optional = socket_address.to_sockaddr_un();
  80. if (!un_optional.has_value()) {
  81. perror("bind");
  82. return false;
  83. }
  84. auto un = un_optional.value();
  85. rc = ::bind(m_fd, (sockaddr const*)&un, sizeof(un));
  86. if (rc < 0) {
  87. perror("bind");
  88. return false;
  89. }
  90. rc = ::listen(m_fd, 5);
  91. if (rc < 0) {
  92. perror("listen");
  93. return false;
  94. }
  95. m_listening = true;
  96. setup_notifier();
  97. return true;
  98. }
  99. ErrorOr<NonnullOwnPtr<LocalSocket>> LocalServer::accept()
  100. {
  101. VERIFY(m_listening);
  102. sockaddr_un un;
  103. socklen_t un_size = sizeof(un);
  104. #ifndef AK_OS_MACOS
  105. int accepted_fd = ::accept4(m_fd, (sockaddr*)&un, &un_size, SOCK_NONBLOCK | SOCK_CLOEXEC);
  106. #else
  107. int accepted_fd = ::accept(m_fd, (sockaddr*)&un, &un_size);
  108. #endif
  109. if (accepted_fd < 0) {
  110. return Error::from_syscall("accept"sv, -errno);
  111. }
  112. #ifdef AK_OS_MACOS
  113. int option = 1;
  114. ioctl(m_fd, FIONBIO, &option);
  115. (void)fcntl(accepted_fd, F_SETFD, FD_CLOEXEC);
  116. #endif
  117. return LocalSocket::adopt_fd(accepted_fd, Socket::PreventSIGPIPE::Yes);
  118. }
  119. }