LibCore: Port SystemServerTakeover.cpp to Windows

This commit is contained in:
stasoid 2024-12-14 13:06:54 +05:00 committed by Andrew Kaster
commit f696f20cd8
Notes: github-actions[bot] 2025-02-06 22:26:16 +00:00

View file

@ -1,10 +1,12 @@
/* /*
* Copyright (c) 2022, sin-ack <sin-ack@protonmail.com> * Copyright (c) 2022, sin-ack <sin-ack@protonmail.com>
* Copyright (c) 2025, stasoid <stasoid@yahoo.com>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include "SystemServerTakeover.h" #include "SystemServerTakeover.h"
#include <LibCore/Environment.h>
#include <LibCore/Socket.h> #include <LibCore/Socket.h>
#include <LibCore/System.h> #include <LibCore/System.h>
@ -17,14 +19,14 @@ static void parse_sockets_from_system_server()
{ {
VERIFY(!s_overtaken_sockets_parsed); VERIFY(!s_overtaken_sockets_parsed);
constexpr auto socket_takeover = "SOCKET_TAKEOVER"; constexpr auto socket_takeover = "SOCKET_TAKEOVER"sv;
char const* sockets = getenv(socket_takeover); auto sockets = Environment::get(socket_takeover);
if (!sockets) { if (!sockets.has_value()) {
s_overtaken_sockets_parsed = true; s_overtaken_sockets_parsed = true;
return; return;
} }
for (auto const socket : StringView { sockets, strlen(sockets) }.split_view(';')) { for (auto socket : sockets->split_view(';')) {
auto params = socket.split_view(':'); auto params = socket.split_view(':');
VERIFY(params.size() == 2); VERIFY(params.size() == 2);
s_overtaken_sockets.set(params[0].to_byte_string(), params[1].to_number<int>().value()); s_overtaken_sockets.set(params[0].to_byte_string(), params[1].to_number<int>().value());
@ -33,7 +35,7 @@ static void parse_sockets_from_system_server()
s_overtaken_sockets_parsed = true; s_overtaken_sockets_parsed = true;
// We wouldn't want our children to think we're passing // We wouldn't want our children to think we're passing
// them a socket either, so unset the env variable. // 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) ErrorOr<NonnullOwnPtr<Core::LocalSocket>> take_over_socket_from_system_server(ByteString const& socket_path)
@ -54,10 +56,8 @@ ErrorOr<NonnullOwnPtr<Core::LocalSocket>> take_over_socket_from_system_server(By
} }
// Sanity check: it has to be a socket. // Sanity check: it has to be a socket.
auto stat = TRY(Core::System::fstat(fd)); if (!System::is_socket(fd))
return Error::from_string_literal("The fd or handle we got from SystemServer is not a socket");
if (!S_ISSOCK(stat.st_mode))
return Error::from_string_literal("The fd we got from SystemServer is not a socket");
auto socket = TRY(Core::LocalSocket::adopt_fd(fd)); auto socket = TRY(Core::LocalSocket::adopt_fd(fd));
// It had to be !CLOEXEC for obvious reasons, but we // It had to be !CLOEXEC for obvious reasons, but we