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