LocalServer.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/LocalSocket.h>
  8. #include <LibCore/Notifier.h>
  9. #include <LibCore/System.h>
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12. #include <sys/socket.h>
  13. #include <sys/stat.h>
  14. #include <unistd.h>
  15. #ifndef SOCK_NONBLOCK
  16. # include <sys/ioctl.h>
  17. #endif
  18. namespace Core {
  19. LocalServer::LocalServer(Object* parent)
  20. : Object(parent)
  21. {
  22. }
  23. LocalServer::~LocalServer()
  24. {
  25. if (m_fd >= 0)
  26. ::close(m_fd);
  27. }
  28. ErrorOr<void> LocalServer::take_over_from_system_server(String const& socket_path)
  29. {
  30. if (m_listening)
  31. return Error::from_string_literal("Core::LocalServer: Can't perform socket takeover when already listening"sv);
  32. if (!LocalSocket::s_overtaken_sockets_parsed)
  33. LocalSocket::parse_sockets_from_system_server();
  34. int fd = -1;
  35. if (socket_path.is_null()) {
  36. // We want the first (and only) socket.
  37. if (LocalSocket::s_overtaken_sockets.size() == 1) {
  38. fd = LocalSocket::s_overtaken_sockets.begin()->value;
  39. }
  40. } else {
  41. auto it = LocalSocket::s_overtaken_sockets.find(socket_path);
  42. if (it != LocalSocket::s_overtaken_sockets.end()) {
  43. fd = it->value;
  44. }
  45. }
  46. if (fd < 0)
  47. return Error::from_string_literal("Core::LocalServer: No file descriptor for socket takeover"sv);
  48. // Sanity check: it has to be a socket.
  49. auto stat = TRY(Core::System::fstat(fd));
  50. if (!S_ISSOCK(stat.st_mode))
  51. return Error::from_string_literal("Core::LocalServer: Attempted socket takeover with non-socket file descriptor"sv);
  52. // It had to be !CLOEXEC for obvious reasons, but we
  53. // don't need it to be !CLOEXEC anymore, so set the
  54. // CLOEXEC flag now.
  55. TRY(Core::System::fcntl(fd, F_SETFD, FD_CLOEXEC));
  56. // The SystemServer has passed us the socket, so use that instead of
  57. // creating our own.
  58. m_fd = fd;
  59. m_listening = true;
  60. setup_notifier();
  61. return {};
  62. }
  63. void LocalServer::setup_notifier()
  64. {
  65. m_notifier = Notifier::construct(m_fd, Notifier::Event::Read, this);
  66. m_notifier->on_ready_to_read = [this] {
  67. if (on_accept) {
  68. if (auto client_socket = accept())
  69. on_accept(client_socket.release_nonnull());
  70. }
  71. };
  72. }
  73. bool LocalServer::listen(const String& address)
  74. {
  75. if (m_listening)
  76. return false;
  77. int rc;
  78. #ifdef SOCK_NONBLOCK
  79. m_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  80. #else
  81. m_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  82. int option = 1;
  83. ioctl(m_fd, FIONBIO, &option);
  84. fcntl(m_fd, F_SETFD, FD_CLOEXEC);
  85. #endif
  86. VERIFY(m_fd >= 0);
  87. #ifndef AK_OS_MACOS
  88. rc = fchmod(m_fd, 0600);
  89. if (rc < 0) {
  90. perror("fchmod");
  91. VERIFY_NOT_REACHED();
  92. }
  93. #endif
  94. auto socket_address = SocketAddress::local(address);
  95. auto un_optional = socket_address.to_sockaddr_un();
  96. if (!un_optional.has_value()) {
  97. perror("bind");
  98. return false;
  99. }
  100. auto un = un_optional.value();
  101. rc = ::bind(m_fd, (const sockaddr*)&un, sizeof(un));
  102. if (rc < 0) {
  103. perror("bind");
  104. return false;
  105. }
  106. rc = ::listen(m_fd, 5);
  107. if (rc < 0) {
  108. perror("listen");
  109. return false;
  110. }
  111. m_listening = true;
  112. setup_notifier();
  113. return true;
  114. }
  115. RefPtr<LocalSocket> LocalServer::accept()
  116. {
  117. VERIFY(m_listening);
  118. sockaddr_un un;
  119. socklen_t un_size = sizeof(un);
  120. #ifndef AK_OS_MACOS
  121. int accepted_fd = ::accept4(m_fd, (sockaddr*)&un, &un_size, SOCK_NONBLOCK | SOCK_CLOEXEC);
  122. #else
  123. int accepted_fd = ::accept(m_fd, (sockaddr*)&un, &un_size);
  124. #endif
  125. if (accepted_fd < 0) {
  126. perror("accept");
  127. return nullptr;
  128. }
  129. #ifdef AK_OS_MACOS
  130. int option = 1;
  131. ioctl(m_fd, FIONBIO, &option);
  132. (void)fcntl(accepted_fd, F_SETFD, FD_CLOEXEC);
  133. #endif
  134. return LocalSocket::construct(accepted_fd);
  135. }
  136. }