2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <andreas@ladybird.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
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/LocalServer.h>
|
|
|
|
#include <LibCore/Notifier.h>
|
2023-02-08 23:05:44 +01:00
|
|
|
#include <LibCore/Socket.h>
|
2021-12-06 16:27:57 +01:00
|
|
|
#include <LibCore/System.h>
|
2022-01-15 12:01:56 +00:00
|
|
|
#include <LibCore/SystemServerTakeover.h>
|
2021-01-12 19:21:59 +01:00
|
|
|
#include <fcntl.h>
|
2019-07-27 10:21:25 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/socket.h>
|
2019-11-26 19:27:21 +03:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2020-05-29 22:22:01 +02:00
|
|
|
|
2019-12-25 17:09:52 +01:00
|
|
|
#ifndef SOCK_NONBLOCK
|
2020-05-29 22:22:01 +02:00
|
|
|
# include <sys/ioctl.h>
|
2019-12-25 17:09:52 +01:00
|
|
|
#endif
|
2019-07-27 10:21:25 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
namespace Core {
|
|
|
|
|
2025-08-10 16:29:21 +02:00
|
|
|
LocalServer::LocalServer() = default;
|
2019-07-27 10:21:25 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
LocalServer::~LocalServer()
|
2019-07-27 10:21:25 +02:00
|
|
|
{
|
2020-08-08 15:54:22 +03:00
|
|
|
if (m_fd >= 0)
|
|
|
|
::close(m_fd);
|
2019-07-27 10:21:25 +02:00
|
|
|
}
|
|
|
|
|
2024-04-26 15:17:51 -06:00
|
|
|
ErrorOr<void> LocalServer::take_over_fd(int socket_fd)
|
|
|
|
{
|
|
|
|
if (m_listening)
|
|
|
|
return Error::from_string_literal("Core::LocalServer: Can't perform socket takeover when already listening");
|
|
|
|
|
|
|
|
m_fd = socket_fd;
|
|
|
|
m_listening = true;
|
|
|
|
setup_notifier();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
void LocalServer::setup_notifier()
|
2019-11-26 19:27:21 +03:00
|
|
|
{
|
2025-08-10 16:00:40 +02:00
|
|
|
m_notifier = Notifier::construct(m_fd, Notifier::Type::Read);
|
2023-04-23 20:59:32 +02:00
|
|
|
m_notifier->on_activation = [this] {
|
2021-11-29 18:42:01 +01:00
|
|
|
if (on_accept) {
|
2022-01-14 13:12:49 +00:00
|
|
|
auto maybe_client_socket = accept();
|
|
|
|
if (maybe_client_socket.is_error()) {
|
2022-11-08 09:30:25 -05:00
|
|
|
dbgln("LocalServer::on_ready_to_read: Error accepting a connection: {}", maybe_client_socket.error());
|
|
|
|
if (on_accept_error)
|
|
|
|
on_accept_error(maybe_client_socket.release_error());
|
2022-01-14 13:12:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
on_accept(maybe_client_socket.release_value());
|
2021-11-29 18:42:01 +01:00
|
|
|
}
|
2019-11-26 19:27:21 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
bool LocalServer::listen(ByteString const& address)
|
2019-07-27 10:21:25 +02:00
|
|
|
{
|
|
|
|
if (m_listening)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int rc;
|
|
|
|
|
2019-12-25 17:09:52 +01:00
|
|
|
#ifdef SOCK_NONBLOCK
|
2019-11-26 19:27:21 +03:00
|
|
|
m_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
|
2019-12-25 17:09:52 +01:00
|
|
|
#else
|
|
|
|
m_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
|
|
|
|
int option = 1;
|
|
|
|
ioctl(m_fd, FIONBIO, &option);
|
|
|
|
fcntl(m_fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
#endif
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_fd >= 0);
|
2024-02-27 14:32:29 +01:00
|
|
|
#if !defined(AK_OS_MACOS) && !defined(AK_OS_IOS)
|
2020-01-03 20:21:39 +01:00
|
|
|
rc = fchmod(m_fd, 0600);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("fchmod");
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-01-03 20:21:39 +01:00
|
|
|
}
|
2020-05-29 22:22:01 +02:00
|
|
|
#endif
|
2020-01-03 20:21:39 +01:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
auto socket_address = SocketAddress::local(address);
|
2020-08-23 13:47:52 +02:00
|
|
|
auto un_optional = socket_address.to_sockaddr_un();
|
|
|
|
if (!un_optional.has_value()) {
|
|
|
|
perror("bind");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto un = un_optional.value();
|
2022-04-01 20:58:27 +03:00
|
|
|
rc = ::bind(m_fd, (sockaddr const*)&un, sizeof(un));
|
2019-11-26 19:27:21 +03:00
|
|
|
if (rc < 0) {
|
|
|
|
perror("bind");
|
2020-05-28 18:19:49 +03:00
|
|
|
return false;
|
2019-11-26 19:27:21 +03:00
|
|
|
}
|
2019-07-27 10:21:25 +02:00
|
|
|
|
|
|
|
rc = ::listen(m_fd, 5);
|
2019-11-26 19:27:21 +03:00
|
|
|
if (rc < 0) {
|
|
|
|
perror("listen");
|
2020-05-28 18:19:49 +03:00
|
|
|
return false;
|
2019-11-26 19:27:21 +03:00
|
|
|
}
|
2019-07-27 10:21:25 +02:00
|
|
|
|
2019-11-26 19:27:21 +03:00
|
|
|
m_listening = true;
|
|
|
|
setup_notifier();
|
2019-07-27 10:21:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-08 23:05:44 +01:00
|
|
|
ErrorOr<NonnullOwnPtr<LocalSocket>> LocalServer::accept()
|
2019-07-27 10:21:25 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_listening);
|
2019-07-27 10:21:25 +02:00
|
|
|
sockaddr_un un;
|
|
|
|
socklen_t un_size = sizeof(un);
|
2024-02-27 14:32:29 +01:00
|
|
|
#if !defined(AK_OS_MACOS) && !defined(AK_OS_IOS) && !defined(AK_OS_HAIKU)
|
2021-05-16 12:13:19 +02:00
|
|
|
int accepted_fd = ::accept4(m_fd, (sockaddr*)&un, &un_size, SOCK_NONBLOCK | SOCK_CLOEXEC);
|
|
|
|
#else
|
2019-07-27 10:21:25 +02:00
|
|
|
int accepted_fd = ::accept(m_fd, (sockaddr*)&un, &un_size);
|
2021-05-16 12:13:19 +02:00
|
|
|
#endif
|
2019-07-27 10:21:25 +02:00
|
|
|
if (accepted_fd < 0) {
|
2025-05-10 16:24:35 -04:00
|
|
|
return Error::from_syscall("accept"sv, errno);
|
2019-07-27 10:21:25 +02:00
|
|
|
}
|
|
|
|
|
2024-02-27 14:32:29 +01:00
|
|
|
#if defined(AK_OS_MACOS) || defined(AK_OS_IOS) || defined(AK_OS_HAIKU)
|
2021-05-16 12:13:19 +02:00
|
|
|
int option = 1;
|
|
|
|
ioctl(m_fd, FIONBIO, &option);
|
|
|
|
(void)fcntl(accepted_fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
#endif
|
|
|
|
|
2024-04-05 00:07:32 +02:00
|
|
|
return LocalSocket::adopt_fd(accepted_fd);
|
2020-02-02 12:34:39 +01:00
|
|
|
}
|
|
|
|
|
2019-07-27 10:21:25 +02:00
|
|
|
}
|