2022-01-14 13:12:49 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, sin-ack <sin-ack@protonmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SystemServerTakeover.h"
|
2023-02-08 22:05:44 +00: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 14:19:34 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
constexpr auto socket_takeover = "SOCKET_TAKEOVER";
|
2022-04-01 17:58:27 +00:00
|
|
|
char const* sockets = getenv(socket_takeover);
|
2022-01-14 13:12:49 +00:00
|
|
|
if (!sockets) {
|
|
|
|
s_overtaken_sockets_parsed = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-15 13:16:31 +00:00
|
|
|
for (auto const socket : StringView { sockets, strlen(sockets) }.split_view(';')) {
|
2022-01-14 13:12:49 +00:00
|
|
|
auto params = socket.split_view(':');
|
2023-10-30 20:08:01 +00:00
|
|
|
VERIFY(params.size() == 2);
|
2023-12-23 02:59:14 +00: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.
|
|
|
|
unsetenv(socket_takeover);
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
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 11:30:58 +00:00
|
|
|
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.
|
|
|
|
auto stat = TRY(Core::System::fstat(fd));
|
|
|
|
|
|
|
|
if (!S_ISSOCK(stat.st_mode))
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("The fd we got from SystemServer is not a socket");
|
2022-01-14 13:12:49 +00:00
|
|
|
|
2023-02-08 22:05:44 +00: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
|
|
|
|
|
|
|
}
|