2022-01-14 13:12:49 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, sin-ack <sin-ack@protonmail.com>
|
2024-12-14 13:06:54 +05:00
|
|
|
* Copyright (c) 2025, stasoid <stasoid@yahoo.com>
|
2022-01-14 13:12:49 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SystemServerTakeover.h"
|
2024-12-14 13:06:54 +05:00
|
|
|
#include <LibCore/Environment.h>
|
2023-02-08 23:05:44 +01:00
|
|
|
#include <LibCore/Socket.h>
|
2022-01-14 13:12:49 +00:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
|
2022-01-15 11:56:48 +00:00
|
|
|
namespace Core {
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
HashMap<ByteString, int> s_overtaken_sockets {};
|
2022-01-14 13:12:49 +00:00
|
|
|
bool s_overtaken_sockets_parsed { false };
|
|
|
|
|
2022-01-15 11:56:48 +00:00
|
|
|
static void parse_sockets_from_system_server()
|
2022-01-14 13:12:49 +00:00
|
|
|
{
|
|
|
|
VERIFY(!s_overtaken_sockets_parsed);
|
|
|
|
|
2024-12-14 13:06:54 +05:00
|
|
|
constexpr auto socket_takeover = "SOCKET_TAKEOVER"sv;
|
|
|
|
auto sockets = Environment::get(socket_takeover);
|
|
|
|
if (!sockets.has_value()) {
|
2022-01-14 13:12:49 +00:00
|
|
|
s_overtaken_sockets_parsed = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-12-14 13:06:54 +05:00
|
|
|
for (auto socket : sockets->split_view(';')) {
|
2022-01-14 13:12:49 +00:00
|
|
|
auto params = socket.split_view(':');
|
2023-10-30 16:08:01 -04:00
|
|
|
VERIFY(params.size() == 2);
|
2023-12-23 15:59:14 +13:00
|
|
|
s_overtaken_sockets.set(params[0].to_byte_string(), params[1].to_number<int>().value());
|
2022-01-14 13:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2024-12-14 13:06:54 +05:00
|
|
|
MUST(Environment::unset(socket_takeover));
|
2022-01-14 13:12:49 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ErrorOr<NonnullOwnPtr<Core::LocalSocket>> take_over_socket_from_system_server(ByteString const& socket_path)
|
2022-01-14 13:12:49 +00:00
|
|
|
{
|
|
|
|
if (!s_overtaken_sockets_parsed)
|
|
|
|
parse_sockets_from_system_server();
|
|
|
|
|
|
|
|
int fd;
|
2023-10-10 15:00:58 +03:30
|
|
|
if (socket_path.is_empty()) {
|
2022-01-14 13:12:49 +00:00
|
|
|
// We want the first (and only) socket.
|
|
|
|
VERIFY(s_overtaken_sockets.size() == 1);
|
|
|
|
fd = s_overtaken_sockets.begin()->value;
|
|
|
|
} else {
|
|
|
|
auto it = s_overtaken_sockets.find(socket_path);
|
|
|
|
if (it == s_overtaken_sockets.end())
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("Non-existent socket requested");
|
2022-01-14 13:12:49 +00:00
|
|
|
fd = it->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check: it has to be a socket.
|
2024-12-14 13:06:54 +05:00
|
|
|
if (!System::is_socket(fd))
|
|
|
|
return Error::from_string_literal("The fd or handle we got from SystemServer is not a socket");
|
2022-01-14 13:12:49 +00:00
|
|
|
|
2023-02-08 23:05:44 +01:00
|
|
|
auto socket = TRY(Core::LocalSocket::adopt_fd(fd));
|
2022-01-14 13:12:49 +00:00
|
|
|
// 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(socket->set_close_on_exec(true));
|
|
|
|
|
|
|
|
return socket;
|
|
|
|
}
|
2022-01-15 11:56:48 +00:00
|
|
|
|
|
|
|
}
|