From f823648bae00b1f96d8ec5d63d82b9d0f8ed0bed Mon Sep 17 00:00:00 2001 From: sin-ack Date: Sat, 15 Jan 2022 12:01:56 +0000 Subject: [PATCH] LibCore: Use SystemServerTakeover functionality in LocalServer This removes the duplicate code in LocalServer::take_over_from_system_server and replaces it with Core::take_over_accepted_socket_from_system_server. --- Userland/Libraries/LibCore/LocalServer.cpp | 36 ++-------------------- 1 file changed, 3 insertions(+), 33 deletions(-) diff --git a/Userland/Libraries/LibCore/LocalServer.cpp b/Userland/Libraries/LibCore/LocalServer.cpp index c76d4e28b90..aae8449d7fb 100644 --- a/Userland/Libraries/LibCore/LocalServer.cpp +++ b/Userland/Libraries/LibCore/LocalServer.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -37,39 +38,8 @@ ErrorOr LocalServer::take_over_from_system_server(String const& socket_pat if (m_listening) return Error::from_string_literal("Core::LocalServer: Can't perform socket takeover when already listening"sv); - if (!LocalSocket::s_overtaken_sockets_parsed) - LocalSocket::parse_sockets_from_system_server(); - - int fd = -1; - if (socket_path.is_null()) { - // We want the first (and only) socket. - if (LocalSocket::s_overtaken_sockets.size() == 1) { - fd = LocalSocket::s_overtaken_sockets.begin()->value; - } - } else { - auto it = LocalSocket::s_overtaken_sockets.find(socket_path); - if (it != LocalSocket::s_overtaken_sockets.end()) { - fd = it->value; - } - } - - if (fd < 0) - return Error::from_string_literal("Core::LocalServer: No file descriptor for socket takeover"sv); - - // Sanity check: it has to be a socket. - auto stat = TRY(Core::System::fstat(fd)); - - if (!S_ISSOCK(stat.st_mode)) - return Error::from_string_literal("Core::LocalServer: Attempted socket takeover with non-socket file descriptor"sv); - - // It had to be !CLOEXEC for obvious reasons, but we - // don't need it to be !CLOEXEC anymore, so set the - // CLOEXEC flag now. - TRY(Core::System::fcntl(fd, F_SETFD, FD_CLOEXEC)); - - // The SystemServer has passed us the socket, so use that instead of - // creating our own. - m_fd = fd; + auto socket = TRY(take_over_socket_from_system_server(socket_path)); + m_fd = TRY(socket->release_fd()); m_listening = true; setup_notifier();