From fe003939413d0de648618e2cd5f5c72182c7cf7b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 29 Nov 2021 18:42:01 +0100 Subject: [PATCH] LibCore: Change Core::LocalServer::on_ready_to_accept => on_accept Everyone used this hook in the same way: immediately accept() on the socket and then do something with the newly accepted fd. This patch simplifies the hook by having LocalServer do the accepting automatically. --- Userland/Libraries/LibCore/LocalServer.cpp | 6 ++++-- Userland/Libraries/LibCore/LocalServer.h | 1 + Userland/Services/AudioServer/main.cpp | 10 +++------- Userland/Services/Clipboard/main.cpp | 9 ++------- Userland/Services/ConfigServer/main.cpp | 9 ++------- Userland/Services/InspectorServer/main.cpp | 19 ++++--------------- Userland/Services/LaunchServer/main.cpp | 9 ++------- .../Services/LookupServer/LookupServer.cpp | 9 ++------- Userland/Services/NotificationServer/main.cpp | 9 ++------- Userland/Services/SQLServer/main.cpp | 9 ++------- Userland/Services/WindowServer/EventLoop.cpp | 18 ++++-------------- 11 files changed, 28 insertions(+), 80 deletions(-) diff --git a/Userland/Libraries/LibCore/LocalServer.cpp b/Userland/Libraries/LibCore/LocalServer.cpp index 2f423a56f5b..d1362b41ef1 100644 --- a/Userland/Libraries/LibCore/LocalServer.cpp +++ b/Userland/Libraries/LibCore/LocalServer.cpp @@ -83,8 +83,10 @@ void LocalServer::setup_notifier() { m_notifier = Notifier::construct(m_fd, Notifier::Event::Read, this); m_notifier->on_ready_to_read = [this] { - if (on_ready_to_accept) - on_ready_to_accept(); + if (on_accept) { + if (auto client_socket = accept()) + on_accept(client_socket.release_nonnull()); + } }; } diff --git a/Userland/Libraries/LibCore/LocalServer.h b/Userland/Libraries/LibCore/LocalServer.h index 24382fa979f..7974d3aa28d 100644 --- a/Userland/Libraries/LibCore/LocalServer.h +++ b/Userland/Libraries/LibCore/LocalServer.h @@ -22,6 +22,7 @@ public: RefPtr accept(); + Function)> on_accept; Function on_ready_to_accept; private: diff --git a/Userland/Services/AudioServer/main.cpp b/Userland/Services/AudioServer/main.cpp index 202cf9e102f..510c4f5f93f 100644 --- a/Userland/Services/AudioServer/main.cpp +++ b/Userland/Services/AudioServer/main.cpp @@ -34,15 +34,11 @@ int main(int, char**) auto server = Core::LocalServer::construct(); bool ok = server->take_over_from_system_server(); VERIFY(ok); - server->on_ready_to_accept = [&] { - auto client_socket = server->accept(); - if (!client_socket) { - dbgln("AudioServer: accept failed."); - return; - } + + server->on_accept = [&](NonnullRefPtr client_socket) { static int s_next_client_id = 0; int client_id = ++s_next_client_id; - IPC::new_client_connection(client_socket.release_nonnull(), client_id, *mixer); + IPC::new_client_connection(move(client_socket), client_id, *mixer); }; if (pledge("stdio recvfd thread accept cpath rpath wpath", nullptr) < 0) { diff --git a/Userland/Services/Clipboard/main.cpp b/Userland/Services/Clipboard/main.cpp index cd7d615afa0..1d97ca9b1e4 100644 --- a/Userland/Services/Clipboard/main.cpp +++ b/Userland/Services/Clipboard/main.cpp @@ -22,15 +22,10 @@ ErrorOr serenity_main(Main::Arguments) bool ok = server->take_over_from_system_server(); VERIFY(ok); - server->on_ready_to_accept = [&] { - auto client_socket = server->accept(); - if (!client_socket) { - dbgln("Clipboard: accept failed."); - return; - } + server->on_accept = [&](auto client_socket) { static int s_next_client_id = 0; int client_id = ++s_next_client_id; - IPC::new_client_connection(client_socket.release_nonnull(), client_id); + IPC::new_client_connection(move(client_socket), client_id); }; Clipboard::Storage::the().on_content_change = [&] { diff --git a/Userland/Services/ConfigServer/main.cpp b/Userland/Services/ConfigServer/main.cpp index ac97f033f1a..3c4609e0048 100644 --- a/Userland/Services/ConfigServer/main.cpp +++ b/Userland/Services/ConfigServer/main.cpp @@ -21,15 +21,10 @@ ErrorOr serenity_main(Main::Arguments) bool ok = server->take_over_from_system_server(); VERIFY(ok); - server->on_ready_to_accept = [&] { - auto client_socket = server->accept(); - if (!client_socket) { - dbgln("ConfigServer: accept failed."); - return; - } + server->on_accept = [&](auto client_socket) { static int s_next_client_id = 0; int client_id = ++s_next_client_id; - IPC::new_client_connection(client_socket.release_nonnull(), client_id); + IPC::new_client_connection(move(client_socket), client_id); }; return event_loop.exec(); diff --git a/Userland/Services/InspectorServer/main.cpp b/Userland/Services/InspectorServer/main.cpp index 86cb1f79816..fec999ae7d4 100644 --- a/Userland/Services/InspectorServer/main.cpp +++ b/Userland/Services/InspectorServer/main.cpp @@ -22,30 +22,19 @@ int main(int, char**) bool ok = server->take_over_from_system_server("/tmp/portal/inspector"); VERIFY(ok); - server->on_ready_to_accept = [&] { - auto client_socket = server->accept(); - if (!client_socket) { - dbgln("accept failed."); - return; - } + server->on_accept = [&](auto client_socket) { static int s_next_client_id = 0; int client_id = ++s_next_client_id; - IPC::new_client_connection(client_socket.release_nonnull(), client_id); + IPC::new_client_connection(move(client_socket), client_id); }; auto inspectables_server = Core::LocalServer::construct(); if (!inspectables_server->take_over_from_system_server("/tmp/portal/inspectables")) VERIFY_NOT_REACHED(); - inspectables_server->on_ready_to_accept = [&] { - auto client_socket = inspectables_server->accept(); - if (!client_socket) { - dbgln("backdoor accept failed."); - return; - } + inspectables_server->on_accept = [&](auto client_socket) { auto pid = client_socket->peer_pid(); - - InspectorServer::g_processes.set(pid, make(pid, client_socket.release_nonnull())); + InspectorServer::g_processes.set(pid, make(pid, move(client_socket))); }; return event_loop.exec(); diff --git a/Userland/Services/LaunchServer/main.cpp b/Userland/Services/LaunchServer/main.cpp index 1998ea243d5..7d25a4ee3ea 100644 --- a/Userland/Services/LaunchServer/main.cpp +++ b/Userland/Services/LaunchServer/main.cpp @@ -29,15 +29,10 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) bool ok = server->take_over_from_system_server(); VERIFY(ok); - server->on_ready_to_accept = [&] { - auto client_socket = server->accept(); - if (!client_socket) { - dbgln("LaunchServer: accept failed."); - return; - } + server->on_accept = [&](auto client_socket) { static int s_next_client_id = 0; int client_id = ++s_next_client_id; - IPC::new_client_connection(client_socket.release_nonnull(), client_id); + IPC::new_client_connection(move(client_socket), client_id); }; return event_loop.exec(); diff --git a/Userland/Services/LookupServer/LookupServer.cpp b/Userland/Services/LookupServer/LookupServer.cpp index d973c063004..ae0e60398f4 100644 --- a/Userland/Services/LookupServer/LookupServer.cpp +++ b/Userland/Services/LookupServer/LookupServer.cpp @@ -73,15 +73,10 @@ LookupServer::LookupServer() m_mdns = MulticastDNS::construct(this); m_local_server = Core::LocalServer::construct(this); - m_local_server->on_ready_to_accept = [this]() { - auto socket = m_local_server->accept(); - if (!socket) { - dbgln("Failed to accept a client connection"); - return; - } + m_local_server->on_accept = [this](auto client_socket) { static int s_next_client_id = 0; int client_id = ++s_next_client_id; - IPC::new_client_connection(socket.release_nonnull(), client_id); + IPC::new_client_connection(move(client_socket), client_id); }; bool ok = m_local_server->take_over_from_system_server(); VERIFY(ok); diff --git a/Userland/Services/NotificationServer/main.cpp b/Userland/Services/NotificationServer/main.cpp index 8239d1e808f..5980d951920 100644 --- a/Userland/Services/NotificationServer/main.cpp +++ b/Userland/Services/NotificationServer/main.cpp @@ -20,15 +20,10 @@ ErrorOr serenity_main(Main::Arguments arguments) bool ok = server->take_over_from_system_server(); VERIFY(ok); - server->on_ready_to_accept = [&] { - auto client_socket = server->accept(); - if (!client_socket) { - dbgln("NotificationServer: accept failed."); - return; - } + server->on_accept = [&](auto client_socket) { static int s_next_client_id = 0; int client_id = ++s_next_client_id; - IPC::new_client_connection(client_socket.release_nonnull(), client_id); + IPC::new_client_connection(move(client_socket), client_id); }; TRY(Core::System::unveil("/res", "r")); diff --git a/Userland/Services/SQLServer/main.cpp b/Userland/Services/SQLServer/main.cpp index 7c4683a659c..353e2ceab7f 100644 --- a/Userland/Services/SQLServer/main.cpp +++ b/Userland/Services/SQLServer/main.cpp @@ -37,15 +37,10 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) bool ok = server->take_over_from_system_server(); VERIFY(ok); - server->on_ready_to_accept = [&] { - auto client_socket = server->accept(); - if (!client_socket) { - dbgln("SQLServer: accept failed."); - return; - } + server->on_accept = [&](auto client_socket) { static int s_next_client_id = 0; int client_id = ++s_next_client_id; - IPC::new_client_connection(client_socket.release_nonnull(), client_id); + IPC::new_client_connection(client_socket, client_id); }; return event_loop.exec(); diff --git a/Userland/Services/WindowServer/EventLoop.cpp b/Userland/Services/WindowServer/EventLoop.cpp index 7b21c820a0c..1986695963f 100644 --- a/Userland/Services/WindowServer/EventLoop.cpp +++ b/Userland/Services/WindowServer/EventLoop.cpp @@ -30,26 +30,16 @@ EventLoop::EventLoop() ok = m_wm_server->take_over_from_system_server("/tmp/portal/wm"); VERIFY(ok); - m_window_server->on_ready_to_accept = [this] { - auto client_socket = m_window_server->accept(); - if (!client_socket) { - dbgln("WindowServer: accept failed."); - return; - } + m_window_server->on_accept = [&](auto client_socket) { static int s_next_client_id = 0; int client_id = ++s_next_client_id; - IPC::new_client_connection(client_socket.release_nonnull(), client_id); + IPC::new_client_connection(move(client_socket), client_id); }; - m_wm_server->on_ready_to_accept = [this] { - auto client_socket = m_wm_server->accept(); - if (!client_socket) { - dbgln("WindowServer: WM accept failed."); - return; - } + m_wm_server->on_accept = [&](auto client_socket) { static int s_next_wm_id = 0; int wm_id = ++s_next_wm_id; - IPC::new_client_connection(client_socket.release_nonnull(), wm_id); + IPC::new_client_connection(move(client_socket), wm_id); }; if (m_keyboard_fd >= 0) {