2021-10-23 19:48:42 +00:00
|
|
|
/*
|
2024-09-06 04:47:26 +00:00
|
|
|
* Copyright (c) 2021-2024, Andreas Kling <andreas@ladybird.org>
|
2022-03-03 18:37:49 +00:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2021-10-23 19:48:42 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2024-06-28 12:24:49 +00:00
|
|
|
#include <AK/Vector.h>
|
|
|
|
#include <LibCore/Socket.h>
|
|
|
|
#include <LibCore/Timer.h>
|
2021-10-23 19:48:42 +00:00
|
|
|
#include <LibIPC/Connection.h>
|
2024-06-28 12:24:49 +00:00
|
|
|
#include <LibIPC/Message.h>
|
2021-10-23 19:48:42 +00:00
|
|
|
#include <LibIPC/Stub.h>
|
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
2024-10-22 21:47:33 +00:00
|
|
|
ConnectionBase::ConnectionBase(IPC::Stub& local_stub, Transport transport, u32 local_endpoint_magic)
|
2021-10-23 19:48:42 +00:00
|
|
|
: m_local_stub(local_stub)
|
2024-10-22 21:47:33 +00:00
|
|
|
, m_transport(move(transport))
|
2021-10-23 20:59:04 +00:00
|
|
|
, m_local_endpoint_magic(local_endpoint_magic)
|
2021-10-23 19:48:42 +00:00
|
|
|
{
|
2024-04-16 18:34:01 +00:00
|
|
|
m_responsiveness_timer = Core::Timer::create_single_shot(3000, [this] { may_have_become_unresponsive(); });
|
2024-10-22 21:47:33 +00:00
|
|
|
|
|
|
|
m_transport.set_up_read_hook([this] {
|
2024-06-28 12:24:49 +00:00
|
|
|
NonnullRefPtr protect = *this;
|
|
|
|
// FIXME: Do something about errors.
|
|
|
|
(void)drain_messages_from_peer();
|
|
|
|
handle_messages();
|
2024-10-22 21:47:33 +00:00
|
|
|
});
|
2024-09-06 04:47:26 +00:00
|
|
|
|
|
|
|
m_send_queue = adopt_ref(*new SendQueue);
|
|
|
|
m_send_thread = Threading::Thread::construct([this, queue = m_send_queue]() -> intptr_t {
|
|
|
|
for (;;) {
|
|
|
|
queue->mutex.lock();
|
2024-09-21 13:36:45 +00:00
|
|
|
while (queue->messages.is_empty() && queue->running)
|
2024-09-06 04:47:26 +00:00
|
|
|
queue->condition.wait();
|
|
|
|
|
|
|
|
if (!queue->running) {
|
|
|
|
queue->mutex.unlock();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto message = queue->messages.take_first();
|
|
|
|
queue->mutex.unlock();
|
|
|
|
|
2024-10-22 21:47:33 +00:00
|
|
|
if (auto result = message.transfer_message(m_transport); result.is_error()) {
|
2024-09-06 04:47:26 +00:00
|
|
|
dbgln("ConnectionBase::send_thread: {}", result.error());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
m_send_thread->start();
|
2024-06-28 12:24:49 +00:00
|
|
|
}
|
|
|
|
|
2024-09-06 04:47:26 +00:00
|
|
|
ConnectionBase::~ConnectionBase()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Threading::MutexLocker locker(m_send_queue->mutex);
|
|
|
|
m_send_queue->running = false;
|
|
|
|
m_send_queue->condition.signal();
|
|
|
|
}
|
|
|
|
m_send_thread->detach();
|
|
|
|
}
|
2024-06-28 12:24:49 +00:00
|
|
|
|
|
|
|
bool ConnectionBase::is_open() const
|
|
|
|
{
|
2024-10-22 21:47:33 +00:00
|
|
|
return m_transport.is_open();
|
2021-10-23 19:48:42 +00:00
|
|
|
}
|
|
|
|
|
2021-11-28 08:59:36 +00:00
|
|
|
ErrorOr<void> ConnectionBase::post_message(Message const& message)
|
2021-10-23 19:48:42 +00:00
|
|
|
{
|
2023-01-02 04:58:49 +00:00
|
|
|
return post_message(TRY(message.encode()));
|
2021-10-23 19:48:42 +00:00
|
|
|
}
|
|
|
|
|
2021-11-28 08:59:36 +00:00
|
|
|
ErrorOr<void> ConnectionBase::post_message(MessageBuffer buffer)
|
2021-10-23 19:48:42 +00: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 21:47:33 +00: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 19:48:42 +00:00
|
|
|
|
2024-09-06 04:47:26 +00:00
|
|
|
{
|
|
|
|
Threading::MutexLocker locker(m_send_queue->mutex);
|
|
|
|
m_send_queue->messages.append(move(buffer));
|
|
|
|
m_send_queue->condition.signal();
|
2022-07-03 16:29:06 +00:00
|
|
|
}
|
2021-10-23 19:48:42 +00:00
|
|
|
|
2023-04-24 10:25:14 +00:00
|
|
|
m_responsiveness_timer->start();
|
2021-11-28 08:59:36 +00:00
|
|
|
return {};
|
2021-10-23 19:48:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionBase::shutdown()
|
|
|
|
{
|
2024-10-22 21:47:33 +00:00
|
|
|
m_transport.close();
|
2021-10-23 19:48:42 +00:00
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
2022-06-10 15:21:00 +00:00
|
|
|
void ConnectionBase::shutdown_with_error(Error const& error)
|
|
|
|
{
|
|
|
|
dbgln("IPC::ConnectionBase ({:p}) had an error ({}), disconnecting.", this, error);
|
|
|
|
shutdown();
|
|
|
|
}
|
|
|
|
|
2021-10-23 20:59:04 +00:00
|
|
|
void ConnectionBase::handle_messages()
|
2021-10-23 19:48:42 +00:00
|
|
|
{
|
|
|
|
auto messages = move(m_unprocessed_messages);
|
|
|
|
for (auto& message : messages) {
|
2023-03-06 16:16:25 +00:00
|
|
|
if (message->endpoint_magic() == m_local_endpoint_magic) {
|
|
|
|
auto handler_result = m_local_stub.handle(*message);
|
2023-01-02 04:58:49 +00:00
|
|
|
if (handler_result.is_error()) {
|
|
|
|
dbgln("IPC::ConnectionBase::handle_messages: {}", handler_result.error());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto response = handler_result.release_value()) {
|
|
|
|
if (auto post_result = post_message(*response); post_result.is_error()) {
|
|
|
|
dbgln("IPC::ConnectionBase::handle_messages: {}", post_result.error());
|
2021-11-28 08:59:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-23 19:48:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-22 21:47:33 +00:00
|
|
|
void ConnectionBase::wait_for_transport_to_become_readable()
|
2021-10-23 19:48:42 +00:00
|
|
|
{
|
2024-10-22 21:47:33 +00:00
|
|
|
m_transport.wait_until_readable();
|
2021-10-23 19:48:42 +00:00
|
|
|
}
|
|
|
|
|
2024-10-22 21:47:33 +00:00
|
|
|
ErrorOr<Vector<u8>> ConnectionBase::read_as_much_as_possible_from_transport_without_blocking()
|
2021-10-23 19:48:42 +00:00
|
|
|
{
|
|
|
|
Vector<u8> bytes;
|
|
|
|
|
|
|
|
if (!m_unprocessed_bytes.is_empty()) {
|
|
|
|
bytes.append(m_unprocessed_bytes.data(), m_unprocessed_bytes.size());
|
|
|
|
m_unprocessed_bytes.clear();
|
|
|
|
}
|
|
|
|
|
2022-11-24 07:03:11 +00:00
|
|
|
bool should_shut_down = false;
|
|
|
|
auto schedule_shutdown = [this, &should_shut_down]() {
|
|
|
|
should_shut_down = true;
|
2024-06-28 10:06:31 +00:00
|
|
|
deferred_invoke([this] {
|
|
|
|
shutdown();
|
2022-11-24 07:03:11 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-10-22 21:47:33 +00:00
|
|
|
auto&& [new_bytes, received_fds] = m_transport.read_as_much_as_possible_without_blocking(move(schedule_shutdown));
|
|
|
|
bytes.append(new_bytes.data(), new_bytes.size());
|
2022-01-14 13:12:49 +00:00
|
|
|
|
2024-10-22 21:47:33 +00:00
|
|
|
for (auto const& fd : received_fds)
|
|
|
|
m_unprocessed_fds.enqueue(IPC::File::adopt_fd(fd));
|
2021-10-23 19:48:42 +00:00
|
|
|
|
|
|
|
if (!bytes.is_empty()) {
|
|
|
|
m_responsiveness_timer->stop();
|
|
|
|
did_become_responsive();
|
2022-11-24 07:03:11 +00:00
|
|
|
} else if (should_shut_down) {
|
|
|
|
return Error::from_string_literal("IPC connection EOF");
|
2021-10-23 19:48:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
2021-11-07 10:59:43 +00:00
|
|
|
ErrorOr<void> ConnectionBase::drain_messages_from_peer()
|
2021-10-23 20:16:53 +00:00
|
|
|
{
|
2024-10-22 21:47:33 +00:00
|
|
|
auto bytes = TRY(read_as_much_as_possible_from_transport_without_blocking());
|
2021-10-23 20:16:53 +00:00
|
|
|
|
|
|
|
size_t index = 0;
|
|
|
|
try_parse_messages(bytes, index);
|
|
|
|
|
|
|
|
if (index < bytes.size()) {
|
|
|
|
// Sometimes we might receive a partial message. That's okay, just stash away
|
|
|
|
// the unprocessed bytes and we'll prepend them to the next incoming message
|
|
|
|
// in the next run of this function.
|
2022-01-20 17:47:39 +00:00
|
|
|
auto remaining_bytes = TRY(ByteBuffer::copy(bytes.span().slice(index)));
|
2021-10-23 20:16:53 +00:00
|
|
|
if (!m_unprocessed_bytes.is_empty()) {
|
|
|
|
shutdown();
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("drain_messages_from_peer: Already have unprocessed bytes");
|
2021-10-23 20:16:53 +00:00
|
|
|
}
|
2022-01-20 17:47:39 +00:00
|
|
|
m_unprocessed_bytes = move(remaining_bytes);
|
2021-10-23 20:16:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_unprocessed_messages.is_empty()) {
|
2024-06-28 10:06:31 +00:00
|
|
|
deferred_invoke([this] {
|
|
|
|
handle_messages();
|
2021-11-03 18:55:59 +00:00
|
|
|
});
|
2021-10-23 20:16:53 +00:00
|
|
|
}
|
2021-11-07 10:59:43 +00:00
|
|
|
return {};
|
2021-10-23 20:16:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 20:59:04 +00:00
|
|
|
OwnPtr<IPC::Message> ConnectionBase::wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id)
|
2021-10-23 20:21:47 +00: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 16:16:25 +00:00
|
|
|
if (message->endpoint_magic() != endpoint_magic)
|
2021-10-23 20:21:47 +00:00
|
|
|
continue;
|
2023-03-06 16:16:25 +00:00
|
|
|
if (message->message_id() == message_id)
|
2021-10-23 20:21:47 +00:00
|
|
|
return m_unprocessed_messages.take(i);
|
|
|
|
}
|
|
|
|
|
2024-10-22 21:47:33 +00:00
|
|
|
if (!is_open())
|
2021-10-23 20:21:47 +00:00
|
|
|
break;
|
|
|
|
|
2024-10-22 21:47:33 +00:00
|
|
|
wait_for_transport_to_become_readable();
|
2021-11-07 10:59:43 +00:00
|
|
|
if (drain_messages_from_peer().is_error())
|
2021-10-23 20:21:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-06-28 10:17:47 +00:00
|
|
|
void ConnectionBase::try_parse_messages(Vector<u8> const& bytes, size_t& index)
|
|
|
|
{
|
|
|
|
u32 message_size = 0;
|
|
|
|
for (; index + sizeof(message_size) < bytes.size(); index += message_size) {
|
|
|
|
memcpy(&message_size, bytes.data() + index, sizeof(message_size));
|
|
|
|
if (message_size == 0 || bytes.size() - index - sizeof(uint32_t) < message_size)
|
|
|
|
break;
|
|
|
|
index += sizeof(message_size);
|
|
|
|
auto remaining_bytes = ReadonlyBytes { bytes.data() + index, message_size };
|
|
|
|
|
|
|
|
if (auto message = try_parse_message(remaining_bytes, m_unprocessed_fds)) {
|
|
|
|
m_unprocessed_messages.append(message.release_nonnull());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbgln("Failed to parse IPC message:");
|
|
|
|
dbgln("{:hex-dump}", remaining_bytes);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 19:48:42 +00:00
|
|
|
}
|