2021-10-23 21:48:42 +02:00
|
|
|
/*
|
2024-09-06 06:47:26 +02:00
|
|
|
* Copyright (c) 2021-2024, Andreas Kling <andreas@ladybird.org>
|
2025-04-07 04:17:36 +02:00
|
|
|
* Copyright (c) 2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
2022-03-03 11:37:49 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2021-10-23 21:48:42 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2024-06-28 14:24:49 +02:00
|
|
|
#include <AK/Vector.h>
|
|
|
|
#include <LibCore/Socket.h>
|
|
|
|
#include <LibCore/Timer.h>
|
2021-10-23 21:48:42 +02:00
|
|
|
#include <LibIPC/Connection.h>
|
2024-06-28 14:24:49 +02:00
|
|
|
#include <LibIPC/Message.h>
|
2021-10-23 21:48:42 +02:00
|
|
|
#include <LibIPC/Stub.h>
|
2025-04-07 04:17:36 +02:00
|
|
|
#include <LibIPC/UnprocessedFileDescriptors.h>
|
2021-10-23 21:48:42 +02:00
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
2025-04-07 23:41:24 +02:00
|
|
|
ConnectionBase::ConnectionBase(IPC::Stub& local_stub, Transport transport, u32 local_endpoint_magic)
|
2021-10-23 21:48:42 +02:00
|
|
|
: m_local_stub(local_stub)
|
2024-10-22 15:47:33 -06:00
|
|
|
, m_transport(move(transport))
|
2021-10-23 22:59:04 +02:00
|
|
|
, m_local_endpoint_magic(local_endpoint_magic)
|
2021-10-23 21:48:42 +02:00
|
|
|
{
|
2024-04-16 20:34:01 +02:00
|
|
|
m_responsiveness_timer = Core::Timer::create_single_shot(3000, [this] { may_have_become_unresponsive(); });
|
2024-10-22 15:47:33 -06:00
|
|
|
|
|
|
|
m_transport.set_up_read_hook([this] {
|
2024-06-28 14:24:49 +02:00
|
|
|
NonnullRefPtr protect = *this;
|
|
|
|
// FIXME: Do something about errors.
|
|
|
|
(void)drain_messages_from_peer();
|
|
|
|
handle_messages();
|
2024-10-22 15:47:33 -06:00
|
|
|
});
|
2024-06-28 14:24:49 +02:00
|
|
|
}
|
|
|
|
|
2025-04-08 04:55:50 +02:00
|
|
|
ConnectionBase::~ConnectionBase() = default;
|
2024-06-28 14:24:49 +02:00
|
|
|
|
|
|
|
bool ConnectionBase::is_open() const
|
|
|
|
{
|
2024-10-22 15:47:33 -06:00
|
|
|
return m_transport.is_open();
|
2021-10-23 21:48:42 +02:00
|
|
|
}
|
|
|
|
|
2021-11-28 09:59:36 +01:00
|
|
|
ErrorOr<void> ConnectionBase::post_message(Message const& message)
|
2021-10-23 21:48:42 +02:00
|
|
|
{
|
2025-04-03 02:55:11 +02:00
|
|
|
return post_message(message.endpoint_magic(), TRY(message.encode()));
|
2021-10-23 21:48:42 +02:00
|
|
|
}
|
|
|
|
|
2025-04-07 23:41:24 +02:00
|
|
|
ErrorOr<void> ConnectionBase::post_message(u32 endpoint_magic, MessageBuffer buffer)
|
2021-10-23 21:48:42 +02:00
|
|
|
{
|
|
|
|
// NOTE: If this connection is being shut down, but has not yet been destroyed,
|
|
|
|
// the socket will be closed. Don't try to send more messages.
|
2024-10-22 15:47:33 -06:00
|
|
|
if (!m_transport.is_open())
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("Trying to post_message during IPC shutdown");
|
2021-10-23 21:48:42 +02:00
|
|
|
|
2025-04-03 02:55:11 +02:00
|
|
|
if (buffer.data().size() > TransportSocket::SOCKET_BUFFER_SIZE) {
|
|
|
|
auto wrapper = LargeMessageWrapper::create(endpoint_magic, buffer);
|
|
|
|
buffer = MUST(wrapper->encode());
|
|
|
|
}
|
|
|
|
|
2025-04-08 04:55:50 +02:00
|
|
|
MUST(buffer.transfer_message(m_transport));
|
2021-10-23 21:48:42 +02:00
|
|
|
|
2023-04-24 12:25:14 +02:00
|
|
|
m_responsiveness_timer->start();
|
2021-11-28 09:59:36 +01:00
|
|
|
return {};
|
2021-10-23 21:48:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionBase::shutdown()
|
|
|
|
{
|
2024-10-22 15:47:33 -06:00
|
|
|
m_transport.close();
|
2021-10-23 21:48:42 +02:00
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
2022-06-10 17:21:00 +02:00
|
|
|
void ConnectionBase::shutdown_with_error(Error const& error)
|
|
|
|
{
|
|
|
|
dbgln("IPC::ConnectionBase ({:p}) had an error ({}), disconnecting.", this, error);
|
|
|
|
shutdown();
|
|
|
|
}
|
|
|
|
|
2021-10-23 22:59:04 +02:00
|
|
|
void ConnectionBase::handle_messages()
|
2021-10-23 21:48:42 +02:00
|
|
|
{
|
|
|
|
auto messages = move(m_unprocessed_messages);
|
|
|
|
for (auto& message : messages) {
|
2023-03-06 17:16:25 +01:00
|
|
|
if (message->endpoint_magic() == m_local_endpoint_magic) {
|
2025-03-08 12:22:39 -05:00
|
|
|
auto handler_result = m_local_stub.handle(move(message));
|
2023-01-01 23:58:49 -05:00
|
|
|
if (handler_result.is_error()) {
|
|
|
|
dbgln("IPC::ConnectionBase::handle_messages: {}", handler_result.error());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto response = handler_result.release_value()) {
|
2025-04-03 02:55:11 +02:00
|
|
|
if (auto post_result = post_message(m_local_endpoint_magic, *response); post_result.is_error()) {
|
2023-01-01 23:58:49 -05:00
|
|
|
dbgln("IPC::ConnectionBase::handle_messages: {}", post_result.error());
|
2021-11-28 09:59:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-23 21:48:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-22 15:47:33 -06:00
|
|
|
void ConnectionBase::wait_for_transport_to_become_readable()
|
2021-10-23 21:48:42 +02:00
|
|
|
{
|
2024-10-22 15:47:33 -06:00
|
|
|
m_transport.wait_until_readable();
|
2021-10-23 21:48:42 +02:00
|
|
|
}
|
|
|
|
|
2025-04-07 04:17:36 +02:00
|
|
|
ErrorOr<void> ConnectionBase::drain_messages_from_peer()
|
2021-10-23 21:48:42 +02:00
|
|
|
{
|
2025-04-07 04:17:36 +02:00
|
|
|
auto schedule_shutdown = m_transport.read_as_many_messages_as_possible_without_blocking([&](auto&& unparsed_message) {
|
|
|
|
auto const& bytes = unparsed_message.bytes;
|
|
|
|
UnprocessedFileDescriptors unprocessed_fds;
|
|
|
|
unprocessed_fds.return_fds_to_front_of_queue(move(unparsed_message.fds));
|
|
|
|
if (auto message = try_parse_message(bytes, unprocessed_fds)) {
|
|
|
|
if (message->message_id() == LargeMessageWrapper::MESSAGE_ID) {
|
|
|
|
LargeMessageWrapper* wrapper = static_cast<LargeMessageWrapper*>(message.ptr());
|
|
|
|
auto wrapped_message = wrapper->wrapped_message_data();
|
|
|
|
unprocessed_fds.return_fds_to_front_of_queue(wrapper->take_fds());
|
|
|
|
auto parsed_message = try_parse_message(wrapped_message, unprocessed_fds);
|
|
|
|
VERIFY(parsed_message);
|
|
|
|
m_unprocessed_messages.append(parsed_message.release_nonnull());
|
|
|
|
return;
|
|
|
|
}
|
2022-11-24 08:03:11 +01:00
|
|
|
|
2025-04-07 04:17:36 +02:00
|
|
|
m_unprocessed_messages.append(message.release_nonnull());
|
|
|
|
} else {
|
|
|
|
dbgln("Failed to parse IPC message {:hex-dump}", bytes);
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
});
|
2021-10-23 21:48:42 +02:00
|
|
|
|
2021-10-23 22:16:53 +02:00
|
|
|
if (!m_unprocessed_messages.is_empty()) {
|
2025-04-07 04:17:36 +02:00
|
|
|
m_responsiveness_timer->stop();
|
|
|
|
did_become_responsive();
|
2024-06-28 12:06:31 +02:00
|
|
|
deferred_invoke([this] {
|
|
|
|
handle_messages();
|
2021-11-03 19:55:59 +01:00
|
|
|
});
|
2025-04-07 04:17:36 +02:00
|
|
|
} else if (schedule_shutdown == TransportSocket::ShouldShutdown::Yes) {
|
|
|
|
deferred_invoke([this] {
|
|
|
|
shutdown();
|
|
|
|
});
|
|
|
|
return Error::from_string_literal("IPC connection EOF");
|
2021-10-23 22:16:53 +02:00
|
|
|
}
|
2025-04-07 04:17:36 +02:00
|
|
|
|
2021-11-07 11:59:43 +01:00
|
|
|
return {};
|
2021-10-23 22:16:53 +02:00
|
|
|
}
|
|
|
|
|
2021-10-23 22:59:04 +02:00
|
|
|
OwnPtr<IPC::Message> ConnectionBase::wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id)
|
2021-10-23 22:21:47 +02:00
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
// Double check we don't already have the event waiting for us.
|
|
|
|
// Otherwise we might end up blocked for a while for no reason.
|
|
|
|
for (size_t i = 0; i < m_unprocessed_messages.size(); ++i) {
|
|
|
|
auto& message = m_unprocessed_messages[i];
|
2023-03-06 17:16:25 +01:00
|
|
|
if (message->endpoint_magic() != endpoint_magic)
|
2021-10-23 22:21:47 +02:00
|
|
|
continue;
|
2023-03-06 17:16:25 +01:00
|
|
|
if (message->message_id() == message_id)
|
2021-10-23 22:21:47 +02:00
|
|
|
return m_unprocessed_messages.take(i);
|
|
|
|
}
|
|
|
|
|
2024-10-22 15:47:33 -06:00
|
|
|
if (!is_open())
|
2021-10-23 22:21:47 +02:00
|
|
|
break;
|
|
|
|
|
2024-10-22 15:47:33 -06:00
|
|
|
wait_for_transport_to_become_readable();
|
2021-11-07 11:59:43 +01:00
|
|
|
if (drain_messages_from_peer().is_error())
|
2021-10-23 22:21:47 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-10-23 21:48:42 +02:00
|
|
|
}
|