1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-08-05 18:37:47 +00:00

LibCore+LibIPC+Everywhere: Return Stream::LocalSocket from LocalServer

This change unfortunately cannot be atomically made without a single
commit changing everything.

Most of the important changes are in LibIPC/Connection.cpp,
LibIPC/ServerConnection.cpp and LibCore/LocalServer.cpp.

The notable changes are:
- IPCCompiler now generates the decode and decode_message functions such
  that they take a Core::Stream::LocalSocket instead of the socket fd.
- IPC::Decoder now uses the receive_fd method of LocalSocket instead of
  doing system calls directly on the fd.
- IPC::ConnectionBase and related classes now use the Stream API
  functions.
- IPC::ServerConnection no longer constructs the socket itself; instead,
  a convenience macro, IPC_CLIENT_CONNECTION, is used in place of
  C_OBJECT and will generate a static try_create factory function for
  the ServerConnection subclass. The subclass is now responsible for
  passing the socket constructed in this function to its
  ServerConnection base; the socket is passed as the first argument to
  the constructor (as a NonnullOwnPtr<Core::Stream::LocalServer>) before
  any other arguments.
- The functionality regarding taking over sockets from SystemServer has
  been moved to LibIPC/SystemServerTakeover.cpp. The Core::LocalSocket
  implementation of this functionality hasn't been deleted due to my
  intention of removing this class in the near future and to reduce
  noise on this (already quite noisy) PR.
This commit is contained in:
sin-ack 2022-01-14 13:12:49 +00:00 committed by Ali Mohammad Pur
parent 4cad0dd74c
commit 2e1bbcb0fa
94 changed files with 378 additions and 252 deletions

View file

@ -22,7 +22,7 @@ void ClientConnection::for_each(Function<void(ClientConnection&)> callback)
callback(connection);
}
ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> client_socket, int client_id, Mixer& mixer)
ClientConnection::ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket> client_socket, int client_id, Mixer& mixer)
: IPC::ClientConnection<AudioClientEndpoint, AudioServerEndpoint>(*this, move(client_socket), client_id)
, m_mixer(mixer)
{

View file

@ -35,7 +35,7 @@ public:
static void for_each(Function<void(ClientConnection&)>);
private:
explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>, int client_id, Mixer& mixer);
explicit ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket>, int client_id, Mixer& mixer);
virtual Messages::AudioServer::GetMainMixVolumeResponse get_main_mix_volume() override;
virtual void set_main_mix_volume(double) override;

View file

@ -25,7 +25,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
auto server = TRY(Core::LocalServer::try_create());
TRY(server->take_over_from_system_server());
server->on_accept = [&](NonnullRefPtr<Core::LocalSocket> client_socket) {
server->on_accept = [&](NonnullOwnPtr<Core::Stream::LocalSocket> client_socket) {
static int s_next_client_id = 0;
int client_id = ++s_next_client_id;
(void)IPC::new_client_connection<AudioServer::ClientConnection>(move(client_socket), client_id, *mixer);

View file

@ -19,7 +19,7 @@ void ClientConnection::for_each_client(Function<void(ClientConnection&)> callbac
}
}
ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
ClientConnection::ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket> socket, int client_id)
: IPC::ClientConnection<ClipboardClientEndpoint, ClipboardServerEndpoint>(*this, move(socket), client_id)
{
s_connections.set(client_id, *this);

View file

@ -27,7 +27,7 @@ public:
void notify_about_clipboard_change();
private:
explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>, int client_id);
explicit ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket>, int client_id);
virtual Messages::ClipboardServer::GetClipboardDataResponse get_clipboard_data() override;
virtual void set_clipboard_data(Core::AnonymousBuffer const&, String const&, IPC::Dictionary const&) override;

View file

@ -74,7 +74,7 @@ static Core::ConfigFile& ensure_domain_config(String const& domain)
return *config;
}
ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> client_socket, int client_id)
ClientConnection::ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket> client_socket, int client_id)
: IPC::ClientConnection<ConfigClientEndpoint, ConfigServerEndpoint>(*this, move(client_socket), client_id)
, m_sync_timer(Core::Timer::create_single_shot(s_disk_sync_delay_ms, [this]() { sync_dirty_domains_to_disk(); }))
{

View file

@ -23,7 +23,7 @@ public:
bool is_monitoring_domain(String const& domain) const { return m_monitored_domains.contains(domain); }
private:
explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>, int client_id);
explicit ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket>, int client_id);
virtual void pledge_domains(Vector<String> const&) override;
virtual void monitor_domain(String const&) override;

View file

@ -20,7 +20,7 @@ namespace FileSystemAccessServer {
static HashMap<int, NonnullRefPtr<ClientConnection>> s_connections;
ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket)
ClientConnection::ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
: IPC::ClientConnection<FileSystemAccessClientEndpoint, FileSystemAccessServerEndpoint>(*this, move(socket), 1)
{
s_connections.set(1, *this);
@ -72,7 +72,7 @@ void ClientConnection::request_file_handler(i32 window_server_client_id, i32 par
else if (has_flag(requested_access, Core::OpenMode::WriteOnly))
access_string = "write to";
auto pid = this->socket().peer_pid();
auto pid = this->socket().peer_pid().release_value_but_fixme_should_propagate_errors();
auto exe_link = LexicalPath("/proc").append(String::number(pid)).append("exe").string();
auto exe_path = Core::File::real_path_for(exe_link);

View file

@ -25,7 +25,7 @@ public:
virtual void die() override;
private:
explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>);
explicit ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket>);
virtual void request_file_read_only_approved(i32, i32, String const&) override;
virtual void request_file(i32, i32, String const&, Core::OpenMode const&) override;

View file

@ -12,7 +12,7 @@
namespace ImageDecoder {
ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket)
ClientConnection::ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
: IPC::ClientConnection<ImageDecoderClientEndpoint, ImageDecoderServerEndpoint>(*this, move(socket), 1)
{
}

View file

@ -25,7 +25,7 @@ public:
virtual void die() override;
private:
explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>);
explicit ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket>);
virtual Messages::ImageDecoderServer::DecodeImageResponse decode_image(Core::AnonymousBuffer const&) override;
};

View file

@ -12,7 +12,7 @@ namespace InspectorServer {
static HashMap<int, RefPtr<ClientConnection>> s_connections;
ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
ClientConnection::ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket> socket, int client_id)
: IPC::ClientConnection<InspectorClientEndpoint, InspectorServerEndpoint>(*this, move(socket), client_id)
{
s_connections.set(client_id, *this);

View file

@ -23,7 +23,7 @@ public:
virtual void die() override;
private:
explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>, int client_id);
explicit ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket>, int client_id);
virtual Messages::InspectorServer::GetAllObjectsResponse get_all_objects(pid_t) override;
virtual Messages::InspectorServer::SetInspectedObjectResponse set_inspected_object(pid_t, u64 object_id) override;

View file

@ -16,14 +16,17 @@ InspectableProcess* InspectableProcess::from_pid(pid_t pid)
return g_processes.get(pid).value_or(nullptr);
}
InspectableProcess::InspectableProcess(pid_t pid, NonnullRefPtr<Core::LocalSocket> socket)
InspectableProcess::InspectableProcess(pid_t pid, NonnullOwnPtr<Core::Stream::LocalSocket> socket)
: m_pid(pid)
, m_socket(move(socket))
{
m_socket->set_blocking(true);
// FIXME: Propagate errors
MUST(m_socket->set_blocking(true));
m_socket->on_ready_to_read = [this] {
[[maybe_unused]] auto buffer = m_socket->read(1);
if (m_socket->eof()) {
char c;
[[maybe_unused]] auto buffer = m_socket->read({ &c, 1 });
if (m_socket->is_eof()) {
g_processes.remove(m_pid);
return;
}
@ -36,46 +39,51 @@ InspectableProcess::~InspectableProcess()
String InspectableProcess::wait_for_response()
{
if (m_socket->eof()) {
if (m_socket->is_eof()) {
dbgln("InspectableProcess disconnected: PID {}", m_pid);
m_socket->close();
return {};
}
u32 length {};
auto nread = m_socket->read((u8*)&length, sizeof(length));
auto nread = m_socket->read({ (u8*)&length, sizeof(length) }).release_value_but_fixme_should_propagate_errors();
if (nread != sizeof(length)) {
dbgln("InspectableProcess got malformed data: PID {}", m_pid);
m_socket->close();
return {};
}
ByteBuffer data;
size_t remaining_bytes = length;
auto data_buffer = ByteBuffer::create_uninitialized(length).release_value();
auto remaining_data_buffer = data_buffer.bytes();
while (remaining_bytes) {
auto packet = m_socket->read(remaining_bytes);
if (packet.size() == 0)
break;
if (auto result = data.try_append(packet.data(), packet.size()); result.is_error()) {
dbgln("Failed to append {} bytes to data buffer: {}", packet.size(), result.error());
while (!remaining_data_buffer.is_empty()) {
auto maybe_nread = m_socket->read(remaining_data_buffer);
if (maybe_nread.is_error()) {
dbgln("InspectableProcess::wait_for_response: Failed to read data: {}", maybe_nread.error());
break;
}
remaining_bytes -= packet.size();
auto nread = maybe_nread.release_value();
if (nread == 0)
break;
remaining_data_buffer = remaining_data_buffer.slice(nread);
}
VERIFY(data.size() == length);
VERIFY(data_buffer.size() == length);
dbgln("Got data size {} and read that many bytes", length);
return String::copy(data);
return String::copy(data_buffer);
}
void InspectableProcess::send_request(JsonObject const& request)
{
auto serialized = request.to_string();
u32 length = serialized.length();
m_socket->write((u8 const*)&length, sizeof(length));
m_socket->write(serialized);
// FIXME: Propagate errors
MUST(m_socket->write({ (u8 const*)&length, sizeof(length) }));
MUST(m_socket->write(serialized.bytes()));
}
}

View file

@ -6,13 +6,13 @@
#pragma once
#include <LibCore/LocalSocket.h>
#include <LibCore/Stream.h>
namespace InspectorServer {
class InspectableProcess {
public:
InspectableProcess(pid_t, NonnullRefPtr<Core::LocalSocket>);
InspectableProcess(pid_t, NonnullOwnPtr<Core::Stream::LocalSocket>);
~InspectableProcess();
void send_request(JsonObject const& request);
@ -22,7 +22,7 @@ public:
private:
pid_t m_pid { 0 };
NonnullRefPtr<Core::LocalSocket> m_socket;
NonnullOwnPtr<Core::Stream::LocalSocket> m_socket;
};
extern HashMap<pid_t, NonnullOwnPtr<InspectorServer::InspectableProcess>> g_processes;

View file

@ -25,7 +25,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
TRY(inspectables_server->take_over_from_system_server("/tmp/portal/inspectables"));
inspectables_server->on_accept = [&](auto client_socket) {
auto pid = client_socket->peer_pid();
auto pid = client_socket->peer_pid().release_value_but_fixme_should_propagate_errors();
InspectorServer::g_processes.set(pid, make<InspectorServer::InspectableProcess>(pid, move(client_socket)));
};

View file

@ -13,7 +13,7 @@
namespace LaunchServer {
static HashMap<int, RefPtr<ClientConnection>> s_connections;
ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> client_socket, int client_id)
ClientConnection::ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket> client_socket, int client_id)
: IPC::ClientConnection<LaunchClientEndpoint, LaunchServerEndpoint>(*this, move(client_socket), client_id)
{
s_connections.set(client_id, *this);

View file

@ -20,7 +20,7 @@ public:
virtual void die() override;
private:
explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>, int client_id);
explicit ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket>, int client_id);
virtual Messages::LaunchServer::OpenUrlResponse open_url(URL const&, String const&) override;
virtual Messages::LaunchServer::GetHandlersForUrlResponse get_handlers_for_url(URL const&) override;

View file

@ -13,7 +13,7 @@ namespace LookupServer {
static HashMap<int, RefPtr<ClientConnection>> s_connections;
ClientConnection::ClientConnection(AK::NonnullRefPtr<Core::LocalSocket> socket, int client_id)
ClientConnection::ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket> socket, int client_id)
: IPC::ClientConnection<LookupClientEndpoint, LookupServerEndpoint>(*this, move(socket), client_id)
{
s_connections.set(client_id, *this);

View file

@ -23,7 +23,7 @@ public:
virtual void die() override;
private:
explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>, int client_id);
explicit ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket>, int client_id);
virtual Messages::LookupServer::LookupNameResponse lookup_name(String const&) override;
virtual Messages::LookupServer::LookupAddressResponse lookup_address(String const&) override;

View file

@ -13,7 +13,7 @@ namespace NotificationServer {
static HashMap<int, RefPtr<ClientConnection>> s_connections;
ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> client_socket, int client_id)
ClientConnection::ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket> client_socket, int client_id)
: IPC::ClientConnection<NotificationClientEndpoint, NotificationServerEndpoint>(*this, move(client_socket), client_id)
{
s_connections.set(client_id, *this);

View file

@ -23,7 +23,7 @@ public:
virtual void die() override;
private:
explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>, int client_id);
explicit ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket>, int client_id);
virtual void show_notification(String const&, String const&, Gfx::ShareableBitmap const&) override;
virtual void close_notification() override;

View file

@ -15,7 +15,7 @@ namespace RequestServer {
static HashMap<int, RefPtr<ClientConnection>> s_connections;
ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket)
ClientConnection::ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
: IPC::ClientConnection<RequestClientEndpoint, RequestServerEndpoint>(*this, move(socket), 1)
{
s_connections.set(1, *this);

View file

@ -29,7 +29,7 @@ public:
void did_request_certificates(Badge<Request>, Request&);
private:
explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>);
explicit ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket>);
virtual Messages::RequestServer::IsSupportedProtocolResponse is_supported_protocol(String const&) override;
virtual Messages::RequestServer::StartRequestResponse start_request(String const&, URL const&, IPC::Dictionary const&, ByteBuffer const&) override;

View file

@ -23,7 +23,7 @@ RefPtr<ClientConnection> ClientConnection::client_connection_for(int client_id)
return nullptr;
}
ClientConnection::ClientConnection(AK::NonnullRefPtr<Core::LocalSocket> socket, int client_id)
ClientConnection::ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket> socket, int client_id)
: IPC::ClientConnection<SQLClientEndpoint, SQLServerEndpoint>(*this, move(socket), client_id)
{
s_connections.set(client_id, *this);

View file

@ -25,7 +25,7 @@ public:
static RefPtr<ClientConnection> client_connection_for(int client_id);
private:
explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>, int client_id);
explicit ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket>, int client_id);
virtual Messages::SQLServer::ConnectResponse connect(String const&) override;
virtual Messages::SQLServer::SqlStatementResponse sql_statement(int, String const&) override;

View file

@ -15,7 +15,7 @@
class ClipboardServerConnection final
: public IPC::ServerConnection<ClipboardClientEndpoint, ClipboardServerEndpoint>
, public ClipboardClientEndpoint {
C_OBJECT(ClipboardServerConnection);
IPC_CLIENT_CONNECTION(ClipboardServerConnection, "/tmp/portal/clipboard")
public:
Function<void()> on_data_changed;
@ -23,8 +23,8 @@ public:
void set_bitmap(Gfx::Bitmap const& bitmap);
private:
ClipboardServerConnection()
: IPC::ServerConnection<ClipboardClientEndpoint, ClipboardServerEndpoint>(*this, "/tmp/portal/clipboard")
ClipboardServerConnection(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
: IPC::ServerConnection<ClipboardClientEndpoint, ClipboardServerEndpoint>(*this, move(socket))
{
}
virtual void clipboard_data_changed(String const&) override

View file

@ -29,7 +29,7 @@
namespace WebContent {
ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket)
ClientConnection::ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
: IPC::ClientConnection<WebContentClientEndpoint, WebContentServerEndpoint>(*this, move(socket), 1)
, m_page_host(PageHost::create(*this))
{

View file

@ -32,7 +32,7 @@ public:
void initialize_js_console(Badge<PageHost>);
private:
explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>);
explicit ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket>);
Web::Page& page();
const Web::Page& page() const;

View file

@ -13,7 +13,7 @@ namespace WebSocket {
static HashMap<int, RefPtr<ClientConnection>> s_connections;
ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket)
ClientConnection::ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
: IPC::ClientConnection<WebSocketClientEndpoint, WebSocketServerEndpoint>(*this, move(socket), 1)
{
s_connections.set(1, *this);

View file

@ -24,7 +24,7 @@ public:
virtual void die() override;
private:
explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>);
explicit ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket>);
virtual Messages::WebSocketServer::ConnectResponse connect(URL const&, String const&, Vector<String> const&, Vector<String> const&, IPC::Dictionary const&) override;
virtual Messages::WebSocketServer::ReadyStateResponse ready_state(i32) override;

View file

@ -45,7 +45,7 @@ ClientConnection* ClientConnection::from_client_id(int client_id)
return (*it).value.ptr();
}
ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> client_socket, int client_id)
ClientConnection::ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket> client_socket, int client_id)
: IPC::ClientConnection<WindowClientEndpoint, WindowServerEndpoint>(*this, move(client_socket), client_id)
{
if (!s_connections)

View file

@ -81,7 +81,7 @@ public:
void notify_display_link(Badge<Compositor>);
private:
explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>, int client_id);
explicit ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket>, int client_id);
// ^ClientConnection
virtual void die() override;

View file

@ -13,7 +13,7 @@ namespace WindowServer {
HashMap<int, NonnullRefPtr<WMClientConnection>> WMClientConnection::s_connections {};
WMClientConnection::WMClientConnection(NonnullRefPtr<Core::LocalSocket> client_socket, int client_id)
WMClientConnection::WMClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket> client_socket, int client_id)
: IPC::ClientConnection<WindowManagerClientEndpoint, WindowManagerServerEndpoint>(*this, move(client_socket), client_id)
{
s_connections.set(client_id, *this);

View file

@ -36,7 +36,7 @@ public:
int window_id() const { return m_window_id; }
private:
explicit WMClientConnection(NonnullRefPtr<Core::LocalSocket> client_socket, int client_id);
explicit WMClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket> client_socket, int client_id);
// ^ClientConnection
virtual void die() override;