2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/LocalSocket.h>
|
2019-07-26 22:39:16 +02:00
|
|
|
#include <errno.h>
|
2021-01-12 19:21:59 +01:00
|
|
|
#include <fcntl.h>
|
2020-06-21 21:18:45 +02:00
|
|
|
#include <stdio.h>
|
2021-04-14 21:35:49 +00:00
|
|
|
#include <stdlib.h>
|
2020-02-02 12:34:39 +01:00
|
|
|
#include <sys/socket.h>
|
2020-06-21 21:18:45 +02:00
|
|
|
#include <sys/stat.h>
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2019-12-25 17:09:52 +01:00
|
|
|
#ifndef SOCK_NONBLOCK
|
2020-02-02 12:34:39 +01:00
|
|
|
# include <sys/ioctl.h>
|
2019-12-25 17:09:52 +01:00
|
|
|
#endif
|
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
|
|
LocalSocket::LocalSocket(int fd, Object* parent)
|
|
|
|
|
: Socket(Socket::Type::Local, parent)
|
2019-07-27 10:21:25 +02:00
|
|
|
{
|
2020-03-07 11:37:51 +13:00
|
|
|
// NOTE: This constructor is used by LocalServer::accept(), so the socket is already connected.
|
2019-09-22 21:46:46 +02:00
|
|
|
m_connected = true;
|
2019-07-27 10:21:25 +02:00
|
|
|
set_fd(fd);
|
2021-05-12 13:56:43 +04:30
|
|
|
set_mode(OpenMode::ReadWrite);
|
2019-07-27 10:21:25 +02:00
|
|
|
set_error(0);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
LocalSocket::LocalSocket(Object* parent)
|
|
|
|
|
: Socket(Socket::Type::Local, parent)
|
2019-07-13 19:42:03 +02:00
|
|
|
{
|
2019-12-25 17:09:52 +01:00
|
|
|
#ifdef SOCK_NONBLOCK
|
2019-07-16 18:00:08 +02:00
|
|
|
int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
|
2019-12-25 17:09:52 +01:00
|
|
|
#else
|
|
|
|
|
int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
|
|
|
|
|
int option = 1;
|
|
|
|
|
ioctl(fd, FIONBIO, &option);
|
|
|
|
|
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-07-13 19:42:03 +02:00
|
|
|
if (fd < 0) {
|
2019-08-17 11:07:15 +02:00
|
|
|
set_error(errno);
|
2019-07-13 19:42:03 +02:00
|
|
|
} else {
|
|
|
|
|
set_fd(fd);
|
2021-05-12 13:56:43 +04:30
|
|
|
set_mode(OpenMode::ReadWrite);
|
2019-07-13 19:42:03 +02:00
|
|
|
set_error(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
LocalSocket::~LocalSocket()
|
2019-07-13 19:42:03 +02:00
|
|
|
{
|
|
|
|
|
}
|
2020-02-02 12:34:39 +01:00
|
|
|
|
2021-04-14 21:35:49 +00:00
|
|
|
HashMap<String, int> LocalSocket::s_overtaken_sockets {};
|
|
|
|
|
bool LocalSocket::s_overtaken_sockets_parsed { false };
|
|
|
|
|
|
|
|
|
|
void LocalSocket::parse_sockets_from_system_server()
|
2020-06-21 21:18:45 +02:00
|
|
|
{
|
2021-04-14 21:35:49 +00:00
|
|
|
VERIFY(!s_overtaken_sockets_parsed);
|
|
|
|
|
|
2020-06-21 21:18:45 +02:00
|
|
|
constexpr auto socket_takeover = "SOCKET_TAKEOVER";
|
2021-04-14 21:35:49 +00:00
|
|
|
const char* sockets = getenv(socket_takeover);
|
|
|
|
|
if (!sockets) {
|
|
|
|
|
s_overtaken_sockets_parsed = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto& socket : StringView(sockets).split_view(' ')) {
|
|
|
|
|
auto params = socket.split_view(':');
|
|
|
|
|
s_overtaken_sockets.set(params[0].to_string(), strtol(params[1].to_string().characters(), nullptr, 10));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2020-06-21 21:18:45 +02:00
|
|
|
|
2021-04-14 21:35:49 +00:00
|
|
|
RefPtr<LocalSocket> LocalSocket::take_over_accepted_socket_from_system_server(String const& socket_path)
|
|
|
|
|
{
|
|
|
|
|
if (!s_overtaken_sockets_parsed)
|
|
|
|
|
parse_sockets_from_system_server();
|
|
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
|
if (socket_path.is_null()) {
|
|
|
|
|
// 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()) {
|
|
|
|
|
dbgln("Non-existent socket requested");
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
fd = it->value;
|
|
|
|
|
}
|
2020-06-21 21:18:45 +02:00
|
|
|
|
|
|
|
|
// Sanity check: it has to be a socket.
|
|
|
|
|
struct stat stat;
|
|
|
|
|
int rc = fstat(fd, &stat);
|
|
|
|
|
if (rc < 0 || !S_ISSOCK(stat.st_mode)) {
|
|
|
|
|
if (rc != 0)
|
|
|
|
|
perror("fstat");
|
2021-01-09 18:51:44 +01:00
|
|
|
dbgln("ERROR: The fd we got from SystemServer is not a socket");
|
2020-06-21 21:18:45 +02:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto socket = LocalSocket::construct(fd);
|
|
|
|
|
|
|
|
|
|
// It had to be !CLOEXEC for obvious reasons, but we
|
|
|
|
|
// don't need it to be !CLOEXEC anymore, so set the
|
|
|
|
|
// CLOEXEC flag now.
|
|
|
|
|
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
|
return socket;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
}
|