Connection.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibIPC/Connection.h>
  7. #include <LibIPC/Stub.h>
  8. #include <sys/select.h>
  9. namespace IPC {
  10. ConnectionBase::ConnectionBase(IPC::Stub& local_stub, NonnullRefPtr<Core::LocalSocket> socket)
  11. : m_local_stub(local_stub)
  12. , m_socket(move(socket))
  13. , m_notifier(Core::Notifier::construct(m_socket->fd(), Core::Notifier::Read, this))
  14. {
  15. m_responsiveness_timer = Core::Timer::create_single_shot(3000, [this] { may_have_become_unresponsive(); });
  16. }
  17. ConnectionBase::~ConnectionBase()
  18. {
  19. }
  20. void ConnectionBase::post_message(Message const& message)
  21. {
  22. post_message(message.encode());
  23. }
  24. void ConnectionBase::post_message(MessageBuffer buffer)
  25. {
  26. // NOTE: If this connection is being shut down, but has not yet been destroyed,
  27. // the socket will be closed. Don't try to send more messages.
  28. if (!m_socket->is_open())
  29. return;
  30. // Prepend the message size.
  31. uint32_t message_size = buffer.data.size();
  32. buffer.data.prepend(reinterpret_cast<const u8*>(&message_size), sizeof(message_size));
  33. #ifdef __serenity__
  34. for (auto& fd : buffer.fds) {
  35. auto rc = sendfd(m_socket->fd(), fd->value());
  36. if (rc < 0) {
  37. perror("sendfd");
  38. shutdown();
  39. }
  40. }
  41. #else
  42. if (!buffer.fds.is_empty())
  43. warnln("fd passing is not supported on this platform, sorry :(");
  44. #endif
  45. size_t total_nwritten = 0;
  46. while (total_nwritten < buffer.data.size()) {
  47. auto nwritten = write(m_socket->fd(), buffer.data.data() + total_nwritten, buffer.data.size() - total_nwritten);
  48. if (nwritten < 0) {
  49. switch (errno) {
  50. case EPIPE:
  51. dbgln("{}::post_message: Disconnected from peer", static_cast<Core::Object const&>(*this));
  52. shutdown();
  53. return;
  54. case EAGAIN:
  55. dbgln("{}::post_message: Peer buffer overflowed", static_cast<Core::Object const&>(*this));
  56. shutdown();
  57. return;
  58. default:
  59. perror("Connection::post_message write");
  60. shutdown();
  61. return;
  62. }
  63. }
  64. total_nwritten += nwritten;
  65. }
  66. m_responsiveness_timer->start();
  67. }
  68. void ConnectionBase::shutdown()
  69. {
  70. m_notifier->close();
  71. m_socket->close();
  72. die();
  73. }
  74. void ConnectionBase::handle_messages(u32 local_endpoint_magic)
  75. {
  76. auto messages = move(m_unprocessed_messages);
  77. for (auto& message : messages) {
  78. if (message.endpoint_magic() == local_endpoint_magic)
  79. if (auto response = m_local_stub.handle(message))
  80. post_message(*response);
  81. }
  82. }
  83. void ConnectionBase::wait_for_socket_to_become_readable()
  84. {
  85. fd_set read_fds;
  86. FD_ZERO(&read_fds);
  87. FD_SET(m_socket->fd(), &read_fds);
  88. for (;;) {
  89. if (auto rc = select(m_socket->fd() + 1, &read_fds, nullptr, nullptr, nullptr); rc < 0) {
  90. if (errno == EINTR)
  91. continue;
  92. perror("wait_for_specific_endpoint_message: select");
  93. VERIFY_NOT_REACHED();
  94. } else {
  95. VERIFY(rc > 0);
  96. VERIFY(FD_ISSET(m_socket->fd(), &read_fds));
  97. break;
  98. }
  99. }
  100. }
  101. Result<Vector<u8>, bool> ConnectionBase::read_as_much_as_possible_from_socket_without_blocking()
  102. {
  103. Vector<u8> bytes;
  104. if (!m_unprocessed_bytes.is_empty()) {
  105. bytes.append(m_unprocessed_bytes.data(), m_unprocessed_bytes.size());
  106. m_unprocessed_bytes.clear();
  107. }
  108. while (m_socket->is_open()) {
  109. u8 buffer[4096];
  110. ssize_t nread = recv(m_socket->fd(), buffer, sizeof(buffer), MSG_DONTWAIT);
  111. if (nread < 0) {
  112. if (errno == EAGAIN)
  113. break;
  114. perror("recv");
  115. exit(1);
  116. return false;
  117. }
  118. if (nread == 0) {
  119. if (bytes.is_empty()) {
  120. deferred_invoke([this] { shutdown(); });
  121. return false;
  122. }
  123. break;
  124. }
  125. bytes.append(buffer, nread);
  126. }
  127. if (!bytes.is_empty()) {
  128. m_responsiveness_timer->stop();
  129. did_become_responsive();
  130. }
  131. return bytes;
  132. }
  133. bool ConnectionBase::drain_messages_from_peer(u32 local_endpoint_magic)
  134. {
  135. auto bytes = TRY(read_as_much_as_possible_from_socket_without_blocking());
  136. size_t index = 0;
  137. try_parse_messages(bytes, index);
  138. if (index < bytes.size()) {
  139. // Sometimes we might receive a partial message. That's okay, just stash away
  140. // the unprocessed bytes and we'll prepend them to the next incoming message
  141. // in the next run of this function.
  142. auto remaining_bytes_result = ByteBuffer::copy(bytes.span().slice(index));
  143. if (!remaining_bytes_result.has_value()) {
  144. dbgln("{}::drain_messages_from_peer: Failed to allocate buffer", static_cast<Core::Object const&>(*this));
  145. return false;
  146. }
  147. if (!m_unprocessed_bytes.is_empty()) {
  148. dbgln("{}::drain_messages_from_peer: Already have unprocessed bytes", static_cast<Core::Object const&>(*this));
  149. shutdown();
  150. return false;
  151. }
  152. m_unprocessed_bytes = remaining_bytes_result.release_value();
  153. }
  154. if (!m_unprocessed_messages.is_empty()) {
  155. deferred_invoke([this, local_endpoint_magic] {
  156. handle_messages(local_endpoint_magic);
  157. });
  158. }
  159. return true;
  160. }
  161. }