LibCore: Port SystemServerTakeover.cpp to Windows

This commit is contained in:
stasoid 2024-12-14 13:06:54 +05:00
parent 969fb1a3a8
commit 5b2aaf36ba

View file

@ -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>
@ -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)
@ -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