Skip to content

Commit

Permalink
LibCore: Port SystemServerTakeover.cpp to Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
stasoid committed Dec 17, 2024
1 parent 969fb1a commit 5b2aaf3
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions Libraries/LibCore/SystemServerTakeover.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/*
* Copyright (c) 2022, sin-ack <sin-ack@protonmail.com>
* Copyright (c) 2024, stasoid <stasoid@yahoo.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include "SystemServerTakeover.h"
#include <LibCore/Environment.h>
#include <LibCore/Socket.h>
#include <LibCore/System.h>

Expand All @@ -17,23 +19,27 @@ static void parse_sockets_from_system_server()
{
VERIFY(!s_overtaken_sockets_parsed);

constexpr auto socket_takeover = "SOCKET_TAKEOVER";
char const* sockets = getenv(socket_takeover);
if (!sockets) {
constexpr auto socket_takeover = "SOCKET_TAKEOVER"sv;
auto sockets = Environment::get(socket_takeover);
if (!sockets.has_value()) {
s_overtaken_sockets_parsed = true;
return;
}

for (auto const socket : StringView { sockets, strlen(sockets) }.split_view(';')) {
for (auto socket : sockets->split_view(';')) {
auto params = socket.split_view(':');
VERIFY(params.size() == 2);
#ifndef AK_OS_WINDOWS
s_overtaken_sockets.set(params[0].to_byte_string(), params[1].to_number<int>().value());
#else
s_overtaken_sockets.set(params[0].to_byte_string(), handle_to_fd(params[1].to_number<intptr_t>().value(), System::SocketHandle));
#endif
}

s_overtaken_sockets_parsed = true;
// We wouldn't want our children to think we're passing
// them a socket either, so unset the env variable.
unsetenv(socket_takeover);
MUST(Environment::unset(socket_takeover));
}

ErrorOr<NonnullOwnPtr<Core::LocalSocket>> take_over_socket_from_system_server(ByteString const& socket_path)
Expand All @@ -54,10 +60,8 @@ ErrorOr<NonnullOwnPtr<Core::LocalSocket>> take_over_socket_from_system_server(By
}

// 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("The fd we got from SystemServer is not a socket");
if (!System::is_socket(fd))
return Error::from_string_literal("The fd or handle we got from SystemServer is not a socket");

auto socket = TRY(Core::LocalSocket::adopt_fd(fd));
// It had to be !CLOEXEC for obvious reasons, but we
Expand Down

0 comments on commit 5b2aaf3

Please sign in to comment.