summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsin-ack <sin-ack@users.noreply.github.com>2022-01-15 12:01:56 +0000
committerAndreas Kling <kling@serenityos.org>2022-02-06 10:28:19 +0100
commitf823648bae00b1f96d8ec5d63d82b9d0f8ed0bed (patch)
tree0efb54ebf816fb08373cc475a569b062ddcb9e40
parent259ed0408791f42835aac7f0adce6c98151828ef (diff)
downloadserenity-f823648bae00b1f96d8ec5d63d82b9d0f8ed0bed.zip
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.
-rw-r--r--Userland/Libraries/LibCore/LocalServer.cpp36
1 files changed, 3 insertions, 33 deletions
diff --git a/Userland/Libraries/LibCore/LocalServer.cpp b/Userland/Libraries/LibCore/LocalServer.cpp
index c76d4e28b9..aae8449d7f 100644
--- a/Userland/Libraries/LibCore/LocalServer.cpp
+++ b/Userland/Libraries/LibCore/LocalServer.cpp
@@ -9,6 +9,7 @@
#include <LibCore/Notifier.h>
#include <LibCore/Stream.h>
#include <LibCore/System.h>
+#include <LibCore/SystemServerTakeover.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/socket.h>
@@ -37,39 +38,8 @@ ErrorOr<void> 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();