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 2f423a56f5bc74ae338d084abb78191dc7777567..d1362b41ef1511c73470f3dcefe85b22c54c6c5e 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 24382fa979f294eaa1df2695f9c44effffdeab0f..7974d3aa28d2ca7a8a68e0266c1d646ec9573dd8 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 202cf9e102fdeb5458f3966442eb19db464aab57..510c4f5f93fa408a72e39a8d033276bf37ae84e1 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 cd7d615afa0d67a59749b0ae1294f95be275918f..1d97ca9b1e4b064c8ebf1e4d4254f988b789d8a4 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 ac97f033f1a8e1508c89c46178effd9fe7a3c7e4..3c4609e004868af310e2a6b55d9f6848b93bb94d 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 86cb1f798168bb06ba2da3ab45555a6f8ecb7d1f..fec999ae7d45a1500fe1ca9b81a9d4195a21d5c5 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 1998ea243d5839cc53ae2973bfd3f0ead483b3e5..7d25a4ee3eadeb54d4f5b9cd15a42744776e6021 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 d973c063004466a847746ed03edcf4adbfa58912..ae0e60398f4e6f1ff0268c56d64bb0584371cbef 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 8239d1e808f66d41a0e3432c28c38128678ee53c..5980d95192090aecad0639094184a9827b88b47e 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 7c4683a659c2094c8cb3be9bd031160163d39298..353e2ceab7f53fab5a64ad38ed3c5ea16c1645d2 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 7b21c820a0ceb46a9b9ed4b96cc47c08462a31e8..1986695963f3ef841b2cc94ccea4e09156ba00b1 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) {