Connection.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibCore/System.h>
  8. #include <LibIPC/Connection.h>
  9. #include <LibIPC/Stub.h>
  10. #include <sys/select.h>
  11. namespace IPC {
  12. ConnectionBase::ConnectionBase(IPC::Stub& local_stub, NonnullOwnPtr<Core::Stream::LocalSocket> socket, u32 local_endpoint_magic)
  13. : m_local_stub(local_stub)
  14. , m_socket(move(socket))
  15. , m_local_endpoint_magic(local_endpoint_magic)
  16. {
  17. m_responsiveness_timer = Core::Timer::create_single_shot(3000, [this] { may_have_become_unresponsive(); });
  18. }
  19. ErrorOr<void> ConnectionBase::post_message(Message const& message)
  20. {
  21. return post_message(message.encode());
  22. }
  23. ErrorOr<void> ConnectionBase::post_message(MessageBuffer buffer)
  24. {
  25. // NOTE: If this connection is being shut down, but has not yet been destroyed,
  26. // the socket will be closed. Don't try to send more messages.
  27. if (!m_socket->is_open())
  28. return Error::from_string_literal("Trying to post_message during IPC shutdown"sv);
  29. // Prepend the message size.
  30. uint32_t message_size = buffer.data.size();
  31. TRY(buffer.data.try_prepend(reinterpret_cast<u8 const*>(&message_size), sizeof(message_size)));
  32. #ifdef __serenity__
  33. for (auto& fd : buffer.fds) {
  34. if (auto result = m_socket->send_fd(fd.value()); result.is_error()) {
  35. dbgln("{}", result.error());
  36. shutdown();
  37. return result;
  38. }
  39. }
  40. #else
  41. if (!buffer.fds.is_empty())
  42. warnln("fd passing is not supported on this platform, sorry :(");
  43. #endif
  44. ReadonlyBytes bytes_to_write { buffer.data.span() };
  45. while (!bytes_to_write.is_empty()) {
  46. auto maybe_nwritten = m_socket->write(bytes_to_write);
  47. if (maybe_nwritten.is_error()) {
  48. auto error = maybe_nwritten.release_error();
  49. if (error.is_errno()) {
  50. switch (error.code()) {
  51. case EPIPE:
  52. shutdown();
  53. return Error::from_string_literal("IPC::Connection::post_message: Disconnected from peer"sv);
  54. case EAGAIN:
  55. shutdown();
  56. return Error::from_string_literal("IPC::Connection::post_message: Peer buffer overflowed"sv);
  57. default:
  58. shutdown();
  59. return Error::from_syscall("IPC::Connection::post_message write"sv, -error.code());
  60. }
  61. } else {
  62. return error;
  63. }
  64. }
  65. bytes_to_write = bytes_to_write.slice(maybe_nwritten.value());
  66. }
  67. m_responsiveness_timer->start();
  68. return {};
  69. }
  70. void ConnectionBase::shutdown()
  71. {
  72. m_socket->close();
  73. die();
  74. }
  75. void ConnectionBase::handle_messages()
  76. {
  77. auto messages = move(m_unprocessed_messages);
  78. for (auto& message : messages) {
  79. if (message.endpoint_magic() == m_local_endpoint_magic) {
  80. if (auto response = m_local_stub.handle(message)) {
  81. if (auto result = post_message(*response); result.is_error()) {
  82. dbgln("IPC::ConnectionBase::handle_messages: {}", result.error());
  83. }
  84. }
  85. }
  86. }
  87. }
  88. void ConnectionBase::wait_for_socket_to_become_readable()
  89. {
  90. auto maybe_did_become_readable = m_socket->can_read_without_blocking(-1);
  91. if (maybe_did_become_readable.is_error()) {
  92. dbgln("ConnectionBase::wait_for_socket_to_become_readable: {}", maybe_did_become_readable.error());
  93. warnln("ConnectionBase::wait_for_socket_to_become_readable: {}", maybe_did_become_readable.error());
  94. VERIFY_NOT_REACHED();
  95. }
  96. VERIFY(maybe_did_become_readable.value());
  97. }
  98. ErrorOr<Vector<u8>> ConnectionBase::read_as_much_as_possible_from_socket_without_blocking()
  99. {
  100. Vector<u8> bytes;
  101. if (!m_unprocessed_bytes.is_empty()) {
  102. bytes.append(m_unprocessed_bytes.data(), m_unprocessed_bytes.size());
  103. m_unprocessed_bytes.clear();
  104. }
  105. u8 buffer[4096];
  106. while (m_socket->is_open()) {
  107. auto maybe_bytes_read = m_socket->read_without_waiting({ buffer, 4096 });
  108. if (maybe_bytes_read.is_error()) {
  109. auto error = maybe_bytes_read.release_error();
  110. if (error.is_syscall() && error.code() == EAGAIN) {
  111. break;
  112. }
  113. dbgln("ConnectionBase::read_as_much_as_possible_from_socket_without_blocking: {}", error);
  114. warnln("ConnectionBase::read_as_much_as_possible_from_socket_without_blocking: {}", error);
  115. VERIFY_NOT_REACHED();
  116. }
  117. auto bytes_read = maybe_bytes_read.release_value();
  118. if (bytes_read.is_empty()) {
  119. deferred_invoke([this] { shutdown(); });
  120. return Error::from_string_literal("IPC connection EOF"sv);
  121. }
  122. bytes.append(bytes_read.data(), bytes_read.size());
  123. }
  124. if (!bytes.is_empty()) {
  125. m_responsiveness_timer->stop();
  126. did_become_responsive();
  127. }
  128. return bytes;
  129. }
  130. ErrorOr<void> ConnectionBase::drain_messages_from_peer()
  131. {
  132. auto bytes = TRY(read_as_much_as_possible_from_socket_without_blocking());
  133. size_t index = 0;
  134. try_parse_messages(bytes, index);
  135. if (index < bytes.size()) {
  136. // Sometimes we might receive a partial message. That's okay, just stash away
  137. // the unprocessed bytes and we'll prepend them to the next incoming message
  138. // in the next run of this function.
  139. auto remaining_bytes = TRY(ByteBuffer::copy(bytes.span().slice(index)));
  140. if (!m_unprocessed_bytes.is_empty()) {
  141. shutdown();
  142. return Error::from_string_literal("drain_messages_from_peer: Already have unprocessed bytes"sv);
  143. }
  144. m_unprocessed_bytes = move(remaining_bytes);
  145. }
  146. if (!m_unprocessed_messages.is_empty()) {
  147. deferred_invoke([this] {
  148. handle_messages();
  149. });
  150. }
  151. return {};
  152. }
  153. OwnPtr<IPC::Message> ConnectionBase::wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id)
  154. {
  155. for (;;) {
  156. // Double check we don't already have the event waiting for us.
  157. // Otherwise we might end up blocked for a while for no reason.
  158. for (size_t i = 0; i < m_unprocessed_messages.size(); ++i) {
  159. auto& message = m_unprocessed_messages[i];
  160. if (message.endpoint_magic() != endpoint_magic)
  161. continue;
  162. if (message.message_id() == message_id)
  163. return m_unprocessed_messages.take(i);
  164. }
  165. if (!m_socket->is_open())
  166. break;
  167. wait_for_socket_to_become_readable();
  168. if (drain_messages_from_peer().is_error())
  169. break;
  170. }
  171. return {};
  172. }
  173. }