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-08-24 19:35:19 -06:00
|
|
|
#include <AK/Singleton.h>
|
2019-08-10 18:58:06 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2023-01-07 13:52:06 -07:00
|
|
|
#include <Kernel/API/Ioctl.h>
|
2021-09-12 11:29:28 +00:00
|
|
|
#include <Kernel/API/POSIX/errno.h>
|
2021-01-25 16:07:10 +01:00
|
|
|
#include <Kernel/Debug.h>
|
2021-09-07 13:39:11 +02:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2019-04-03 12:25:24 +02:00
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
2023-02-24 20:10:59 +02:00
|
|
|
#include <Kernel/Library/StdLib.h>
|
2021-07-18 09:25:13 +02:00
|
|
|
#include <Kernel/Locking/Mutex.h>
|
2021-08-21 23:31:15 +02:00
|
|
|
#include <Kernel/Locking/MutexProtected.h>
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <Kernel/Net/LocalSocket.h>
|
2023-02-24 19:45:37 +02:00
|
|
|
#include <Kernel/Tasks/Process.h>
|
2020-06-24 22:57:37 +02:00
|
|
|
#include <Kernel/UnixTypes.h>
|
2019-02-14 14:17:38 +01:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-08-21 23:31:15 +02:00
|
|
|
static Singleton<MutexProtected<LocalSocket::List>> s_list;
|
2020-08-24 19:35:19 -06:00
|
|
|
|
2021-08-21 23:31:15 +02:00
|
|
|
static MutexProtected<LocalSocket::List>& all_sockets()
|
2019-08-10 18:58:06 +03:00
|
|
|
{
|
|
|
|
return *s_list;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void LocalSocket::for_each(Function<void(LocalSocket const&)> callback)
|
2019-08-10 18:58:06 +03:00
|
|
|
{
|
2022-04-01 20:58:27 +03:00
|
|
|
all_sockets().for_each_shared([&](auto const& socket) {
|
2019-08-10 18:58:06 +03:00
|
|
|
callback(socket);
|
2021-07-18 11:52:40 +02:00
|
|
|
});
|
2019-08-10 18:58:06 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<void> LocalSocket::try_for_each(Function<ErrorOr<void>(LocalSocket const&)> callback)
|
2022-02-24 20:05:10 +02:00
|
|
|
{
|
2022-04-01 20:58:27 +03:00
|
|
|
return all_sockets().with_shared([&](auto const& sockets) -> ErrorOr<void> {
|
2022-02-24 20:05:10 +02:00
|
|
|
for (auto& socket : sockets)
|
|
|
|
TRY(callback(socket));
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-10 07:53:02 +01:00
|
|
|
ErrorOr<NonnullRefPtr<LocalSocket>> LocalSocket::try_create(int type)
|
2019-02-14 14:17:38 +01:00
|
|
|
{
|
2022-04-11 00:08:07 +02:00
|
|
|
auto client_buffer = TRY(DoubleBuffer::try_create("LocalSocket: Client buffer"sv));
|
|
|
|
auto server_buffer = TRY(DoubleBuffer::try_create("LocalSocket: Server buffer"sv));
|
2023-03-10 07:53:02 +01:00
|
|
|
return adopt_nonnull_ref_or_enomem(new (nothrow) LocalSocket(type, move(client_buffer), move(server_buffer)));
|
2019-02-14 14:17:38 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<SocketPair> LocalSocket::try_create_connected_pair(int type)
|
2021-04-28 12:39:12 +02:00
|
|
|
{
|
2021-09-05 14:01:09 +02:00
|
|
|
auto socket = TRY(LocalSocket::try_create(type));
|
2021-09-07 13:39:11 +02:00
|
|
|
auto description1 = TRY(OpenFileDescription::try_create(*socket));
|
2021-04-28 12:39:12 +02:00
|
|
|
|
2021-09-06 20:26:03 +02:00
|
|
|
TRY(socket->try_set_path("[socketpair]"sv));
|
2021-04-28 12:39:12 +02:00
|
|
|
|
2021-08-29 01:30:05 +02:00
|
|
|
socket->set_acceptor(Process::current());
|
2021-04-28 12:39:12 +02:00
|
|
|
socket->set_connected(true);
|
|
|
|
socket->set_connect_side_role(Role::Connected);
|
2021-08-29 02:04:30 +02:00
|
|
|
socket->set_role(Role::Accepted);
|
2021-04-28 12:39:12 +02:00
|
|
|
|
2021-09-07 13:39:11 +02:00
|
|
|
auto description2 = TRY(OpenFileDescription::try_create(*socket));
|
2021-04-28 12:39:12 +02:00
|
|
|
|
2021-09-05 14:01:09 +02:00
|
|
|
return SocketPair { move(description1), move(description2) };
|
2021-04-28 12:39:12 +02:00
|
|
|
}
|
|
|
|
|
2021-08-01 02:34:10 -07:00
|
|
|
LocalSocket::LocalSocket(int type, NonnullOwnPtr<DoubleBuffer> client_buffer, NonnullOwnPtr<DoubleBuffer> server_buffer)
|
2019-02-14 14:17:38 +01:00
|
|
|
: Socket(AF_LOCAL, type, 0)
|
2021-08-01 02:34:10 -07:00
|
|
|
, m_for_client(move(client_buffer))
|
|
|
|
, m_for_server(move(server_buffer))
|
2019-02-14 14:17:38 +01:00
|
|
|
{
|
2021-08-19 22:45:07 +03:00
|
|
|
auto& current_process = Process::current();
|
2022-08-21 12:40:19 +01:00
|
|
|
auto current_process_credentials = current_process.credentials();
|
|
|
|
m_prebind_uid = current_process_credentials->euid();
|
|
|
|
m_prebind_gid = current_process_credentials->egid();
|
2020-01-03 20:14:56 +01:00
|
|
|
m_prebind_mode = 0666;
|
|
|
|
|
2021-08-01 02:34:10 -07:00
|
|
|
m_for_client->set_unblock_callback([this]() {
|
2020-11-29 16:05:27 -07:00
|
|
|
evaluate_block_conditions();
|
|
|
|
});
|
2021-08-01 02:34:10 -07:00
|
|
|
m_for_server->set_unblock_callback([this]() {
|
2020-11-29 16:05:27 -07:00
|
|
|
evaluate_block_conditions();
|
|
|
|
});
|
|
|
|
|
2021-07-18 11:52:40 +02:00
|
|
|
all_sockets().with_exclusive([&](auto& list) {
|
|
|
|
list.append(*this);
|
|
|
|
});
|
|
|
|
|
2021-02-07 15:33:24 +03:30
|
|
|
dbgln_if(LOCAL_SOCKET_DEBUG, "LocalSocket({}) created with type={}", this, type);
|
2019-02-14 14:17:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LocalSocket::~LocalSocket()
|
|
|
|
{
|
2021-07-18 11:52:40 +02:00
|
|
|
all_sockets().with_exclusive([&](auto& list) {
|
|
|
|
list.remove(*this);
|
|
|
|
});
|
2019-02-14 14:17:38 +01:00
|
|
|
}
|
|
|
|
|
2020-02-07 23:42:28 +01:00
|
|
|
void LocalSocket::get_local_address(sockaddr* address, socklen_t* address_size)
|
2019-02-14 15:17:30 +01:00
|
|
|
{
|
2022-04-21 16:23:05 +02:00
|
|
|
auto& address_un = *reinterpret_cast<sockaddr_un*>(address);
|
|
|
|
address_un = {
|
|
|
|
.sun_family = AF_UNIX,
|
|
|
|
.sun_path = {},
|
|
|
|
};
|
|
|
|
|
2021-08-29 01:59:34 +02:00
|
|
|
if (!m_path || m_path->is_empty()) {
|
2022-04-21 16:23:05 +02:00
|
|
|
*address_size = sizeof(address_un.sun_family);
|
|
|
|
return;
|
2021-08-29 01:59:34 +02:00
|
|
|
}
|
2022-04-21 16:23:05 +02:00
|
|
|
|
|
|
|
size_t bytes_to_copy = min(m_path->length() + 1, min(static_cast<size_t>(*address_size), sizeof(address_un.sun_path)));
|
|
|
|
memcpy(address_un.sun_path, m_path->characters(), bytes_to_copy);
|
|
|
|
*address_size = sizeof(address_un.sun_family) + bytes_to_copy;
|
2019-02-14 15:17:30 +01:00
|
|
|
}
|
|
|
|
|
2020-02-07 23:42:28 +01:00
|
|
|
void LocalSocket::get_peer_address(sockaddr* address, socklen_t* address_size)
|
2019-05-20 20:33:03 +02:00
|
|
|
{
|
2020-02-07 23:42:28 +01:00
|
|
|
get_local_address(address, address_size);
|
2019-05-20 20:33:03 +02:00
|
|
|
}
|
|
|
|
|
2022-08-21 16:33:09 +02:00
|
|
|
ErrorOr<void> LocalSocket::bind(Credentials const& credentials, Userspace<sockaddr const*> user_address, socklen_t address_size)
|
2019-02-14 14:38:30 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(setup_state() == SetupState::Unstarted);
|
2022-04-21 16:24:47 +02:00
|
|
|
if (address_size > sizeof(sockaddr_un))
|
2021-08-01 11:27:23 -04:00
|
|
|
return set_so_error(EINVAL);
|
2020-01-11 12:07:45 +01:00
|
|
|
|
2021-08-29 01:59:34 +02:00
|
|
|
sockaddr_un address = {};
|
2022-04-21 16:24:47 +02:00
|
|
|
SOCKET_TRY(copy_from_user(&address, user_address, address_size));
|
2020-01-11 12:07:45 +01:00
|
|
|
|
|
|
|
if (address.sun_family != AF_LOCAL)
|
2021-08-01 11:27:23 -04:00
|
|
|
return set_so_error(EINVAL);
|
2019-02-14 14:38:30 +01:00
|
|
|
|
2021-09-07 15:05:51 +02:00
|
|
|
auto path = SOCKET_TRY(KString::try_create(StringView { address.sun_path, strnlen(address.sun_path, sizeof(address.sun_path)) }));
|
2021-02-07 15:33:24 +03:30
|
|
|
dbgln_if(LOCAL_SOCKET_DEBUG, "LocalSocket({}) bind({})", this, path);
|
2019-02-14 14:38:30 +01:00
|
|
|
|
2021-01-23 15:35:20 +01:00
|
|
|
mode_t mode = S_IFSOCK | (m_prebind_mode & 0777);
|
2020-01-03 20:14:56 +01:00
|
|
|
UidAndGid owner { m_prebind_uid, m_prebind_gid };
|
2022-08-21 16:33:09 +02:00
|
|
|
auto result = VirtualFileSystem::the().open(credentials, path->view(), O_CREAT | O_EXCL | O_NOFOLLOW_NOERROR, mode, Process::current().current_directory(), owner);
|
2019-03-06 22:14:31 +01:00
|
|
|
if (result.is_error()) {
|
2021-11-08 00:51:39 +01:00
|
|
|
if (result.error().code() == EEXIST)
|
2021-08-01 11:27:23 -04:00
|
|
|
return set_so_error(EADDRINUSE);
|
2021-11-08 00:51:39 +01:00
|
|
|
return result.release_error();
|
2019-02-14 14:38:30 +01:00
|
|
|
}
|
2019-02-14 15:17:30 +01:00
|
|
|
|
2020-01-30 22:15:45 +01:00
|
|
|
auto file = move(result.value());
|
2021-09-16 00:10:03 +00:00
|
|
|
auto inode = file->inode();
|
2020-01-30 22:15:45 +01:00
|
|
|
|
2021-09-16 00:10:03 +00:00
|
|
|
VERIFY(inode);
|
|
|
|
if (!inode->bind_socket(*this))
|
2021-08-01 11:27:23 -04:00
|
|
|
return set_so_error(EADDRINUSE);
|
2020-01-30 22:15:45 +01:00
|
|
|
|
2021-09-16 00:10:03 +00:00
|
|
|
m_inode = inode;
|
2019-02-14 15:55:19 +01:00
|
|
|
|
2021-08-29 01:59:34 +02:00
|
|
|
m_path = move(path);
|
2019-02-14 15:17:30 +01:00
|
|
|
m_bound = true;
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2019-02-14 14:38:30 +01:00
|
|
|
}
|
2019-02-14 15:55:19 +01:00
|
|
|
|
2022-08-21 16:35:03 +02:00
|
|
|
ErrorOr<void> LocalSocket::connect(Credentials const& credentials, OpenFileDescription& description, Userspace<sockaddr const*> user_address, socklen_t address_size)
|
2019-02-14 15:55:19 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!m_bound);
|
2022-07-10 02:00:59 +03:00
|
|
|
|
|
|
|
if (address_size > sizeof(sockaddr_un))
|
2021-08-01 11:27:23 -04:00
|
|
|
return set_so_error(EINVAL);
|
2022-07-10 02:00:59 +03:00
|
|
|
|
|
|
|
sockaddr_un address = {};
|
|
|
|
SOCKET_TRY(copy_from_user(&address, user_address, address_size));
|
|
|
|
|
|
|
|
if (address.sun_family != AF_LOCAL)
|
2021-08-01 11:27:23 -04:00
|
|
|
return set_so_error(EINVAL);
|
2022-07-10 02:00:59 +03:00
|
|
|
|
2020-01-09 21:30:56 +01:00
|
|
|
if (is_connected())
|
2021-08-01 11:27:23 -04:00
|
|
|
return set_so_error(EISCONN);
|
2019-02-14 15:55:19 +01:00
|
|
|
|
2022-07-10 02:00:59 +03:00
|
|
|
auto path = SOCKET_TRY(KString::try_create(StringView { address.sun_path, strnlen(address.sun_path, sizeof(address.sun_path)) }));
|
2021-08-29 01:59:34 +02:00
|
|
|
dbgln_if(LOCAL_SOCKET_DEBUG, "LocalSocket({}) connect({})", this, *path);
|
2019-02-14 15:55:19 +01:00
|
|
|
|
2022-08-21 16:35:03 +02:00
|
|
|
auto file = SOCKET_TRY(VirtualFileSystem::the().open(credentials, path->view(), O_RDWR, 0, Process::current().current_directory()));
|
2021-09-16 00:10:03 +00:00
|
|
|
auto inode = file->inode();
|
|
|
|
m_inode = inode;
|
2019-03-06 22:14:31 +01:00
|
|
|
|
2021-09-16 00:10:03 +00:00
|
|
|
VERIFY(inode);
|
2022-02-07 12:57:57 +01:00
|
|
|
|
|
|
|
auto peer = inode->bound_socket();
|
|
|
|
if (!peer)
|
2021-08-01 11:27:23 -04:00
|
|
|
return set_so_error(ECONNREFUSED);
|
2019-02-14 15:55:19 +01:00
|
|
|
|
2021-08-29 01:59:34 +02:00
|
|
|
m_path = move(path);
|
2019-02-14 17:18:35 +01:00
|
|
|
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_connect_side_fd == &description);
|
2020-11-29 16:05:27 -07:00
|
|
|
set_connect_side_role(Role::Connecting);
|
2019-08-11 16:38:20 +03:00
|
|
|
|
2019-03-06 22:14:31 +01:00
|
|
|
auto result = peer->queue_connection_from(*this);
|
2019-08-11 16:38:20 +03:00
|
|
|
if (result.is_error()) {
|
2020-11-29 16:05:27 -07:00
|
|
|
set_connect_side_role(Role::None);
|
2019-03-06 22:14:31 +01:00
|
|
|
return result;
|
2019-08-11 16:38:20 +03:00
|
|
|
}
|
2019-02-14 17:18:35 +01:00
|
|
|
|
2019-08-11 16:38:20 +03:00
|
|
|
if (is_connected()) {
|
2020-11-29 16:05:27 -07:00
|
|
|
set_connect_side_role(Role::Connected);
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2019-08-11 16:38:20 +03:00
|
|
|
}
|
2019-07-20 11:10:46 +02:00
|
|
|
|
2021-09-07 13:39:11 +02:00
|
|
|
auto unblock_flags = Thread::OpenFileDescriptionBlocker::BlockFlags::None;
|
2021-01-10 16:29:28 -07:00
|
|
|
if (Thread::current()->block<Thread::ConnectBlocker>({}, description, unblock_flags).was_interrupted()) {
|
2020-11-29 16:05:27 -07:00
|
|
|
set_connect_side_role(Role::None);
|
2021-08-01 11:27:23 -04:00
|
|
|
return set_so_error(EINTR);
|
2019-08-11 16:38:20 +03:00
|
|
|
}
|
2019-07-20 11:10:46 +02:00
|
|
|
|
2021-08-29 01:59:34 +02:00
|
|
|
dbgln_if(LOCAL_SOCKET_DEBUG, "LocalSocket({}) connect({}) status is {}", this, *m_path, to_string(setup_state()));
|
2019-08-10 13:17:00 +10:00
|
|
|
|
2021-09-07 13:39:11 +02:00
|
|
|
if (!has_flag(unblock_flags, Thread::OpenFileDescriptionBlocker::BlockFlags::Connect)) {
|
2020-11-29 16:05:27 -07:00
|
|
|
set_connect_side_role(Role::None);
|
2021-08-01 11:27:23 -04:00
|
|
|
return set_so_error(ECONNREFUSED);
|
2019-08-11 16:38:20 +03:00
|
|
|
}
|
2020-11-29 16:05:27 -07:00
|
|
|
set_connect_side_role(Role::Connected);
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2019-02-14 15:55:19 +01:00
|
|
|
}
|
2019-02-14 16:01:08 +01:00
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> LocalSocket::listen(size_t backlog)
|
2019-08-06 23:40:38 +10:00
|
|
|
{
|
2021-08-29 13:10:55 +02:00
|
|
|
MutexLocker locker(mutex());
|
2019-08-06 23:40:38 +10:00
|
|
|
if (type() != SOCK_STREAM)
|
2021-08-01 11:27:23 -04:00
|
|
|
return set_so_error(EOPNOTSUPP);
|
2019-08-06 23:40:38 +10:00
|
|
|
set_backlog(backlog);
|
2020-11-29 16:05:27 -07:00
|
|
|
auto previous_role = m_role;
|
2021-08-29 02:04:30 +02:00
|
|
|
set_role(Role::Listener);
|
2020-11-29 16:05:27 -07:00
|
|
|
set_connect_side_role(Role::Listener, previous_role != m_role);
|
2021-01-14 22:37:57 +01:00
|
|
|
|
2021-02-07 15:33:24 +03:30
|
|
|
dbgln_if(LOCAL_SOCKET_DEBUG, "LocalSocket({}) listening with backlog={}", this, backlog);
|
2021-01-14 22:37:57 +01:00
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2019-08-06 23:40:38 +10:00
|
|
|
}
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> LocalSocket::attach(OpenFileDescription& description)
|
2019-02-17 00:13:47 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!m_accept_side_fd_open);
|
2019-08-11 16:38:20 +03:00
|
|
|
if (m_connect_side_role == Role::None) {
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_connect_side_fd == nullptr);
|
2019-08-11 16:38:20 +03:00
|
|
|
m_connect_side_fd = &description;
|
|
|
|
} else {
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_connect_side_fd != &description);
|
2019-08-11 16:28:18 +03:00
|
|
|
m_accept_side_fd_open = true;
|
2019-02-17 11:00:35 +01:00
|
|
|
}
|
2020-11-29 16:05:27 -07:00
|
|
|
|
|
|
|
evaluate_block_conditions();
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2019-02-17 11:00:35 +01:00
|
|
|
}
|
|
|
|
|
2021-09-07 13:39:11 +02:00
|
|
|
void LocalSocket::detach(OpenFileDescription& description)
|
2019-02-17 11:00:35 +01:00
|
|
|
{
|
2019-08-11 16:38:20 +03:00
|
|
|
if (m_connect_side_fd == &description) {
|
|
|
|
m_connect_side_fd = nullptr;
|
|
|
|
} else {
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_accept_side_fd_open);
|
2019-08-11 16:28:18 +03:00
|
|
|
m_accept_side_fd_open = false;
|
2021-09-16 00:10:03 +00:00
|
|
|
|
|
|
|
if (m_bound) {
|
2023-02-12 00:49:15 -07:00
|
|
|
if (m_inode)
|
|
|
|
m_inode->unbind_socket();
|
2021-09-16 00:10:03 +00:00
|
|
|
}
|
2019-02-17 11:00:35 +01:00
|
|
|
}
|
2020-11-29 16:05:27 -07:00
|
|
|
|
|
|
|
evaluate_block_conditions();
|
2019-02-17 00:13:47 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool LocalSocket::can_read(OpenFileDescription const& description, u64) const
|
2019-02-14 16:01:08 +01:00
|
|
|
{
|
2019-08-11 16:38:20 +03:00
|
|
|
auto role = this->role(description);
|
|
|
|
if (role == Role::Listener)
|
2019-02-14 16:03:37 +01:00
|
|
|
return can_accept();
|
2019-08-11 16:38:20 +03:00
|
|
|
if (role == Role::Accepted)
|
2021-08-01 02:34:10 -07:00
|
|
|
return !has_attached_peer(description) || !m_for_server->is_empty();
|
2019-08-11 16:38:20 +03:00
|
|
|
if (role == Role::Connected)
|
2021-08-01 02:34:10 -07:00
|
|
|
return !has_attached_peer(description) || !m_for_client->is_empty();
|
2019-11-09 22:15:35 +01:00
|
|
|
return false;
|
2019-02-14 16:01:08 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool LocalSocket::has_attached_peer(OpenFileDescription const& description) const
|
2019-05-20 02:54:52 +02:00
|
|
|
{
|
2019-08-11 16:38:20 +03:00
|
|
|
auto role = this->role(description);
|
|
|
|
if (role == Role::Accepted)
|
|
|
|
return m_connect_side_fd != nullptr;
|
|
|
|
if (role == Role::Connected)
|
2019-08-11 16:28:18 +03:00
|
|
|
return m_accept_side_fd_open;
|
2021-08-31 13:34:35 +01:00
|
|
|
return false;
|
2019-05-20 02:54:52 +02:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool LocalSocket::can_write(OpenFileDescription const& description, u64) const
|
2019-02-14 16:01:08 +01:00
|
|
|
{
|
2019-08-11 16:38:20 +03:00
|
|
|
auto role = this->role(description);
|
|
|
|
if (role == Role::Accepted)
|
2021-08-01 02:34:10 -07:00
|
|
|
return !has_attached_peer(description) || m_for_client->space_for_writing();
|
2019-08-11 16:38:20 +03:00
|
|
|
if (role == Role::Connected)
|
2021-08-01 02:34:10 -07:00
|
|
|
return !has_attached_peer(description) || m_for_server->space_for_writing();
|
2019-11-09 22:15:35 +01:00
|
|
|
return false;
|
2019-02-14 16:01:08 +01:00
|
|
|
}
|
2019-03-12 15:51:42 +01:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<size_t> LocalSocket::sendto(OpenFileDescription& description, UserOrKernelBuffer const& data, size_t data_size, int, Userspace<sockaddr const*>, socklen_t)
|
2019-03-12 15:51:42 +01:00
|
|
|
{
|
2019-08-05 10:03:19 +02:00
|
|
|
if (!has_attached_peer(description))
|
2021-08-01 11:27:23 -04:00
|
|
|
return set_so_error(EPIPE);
|
2020-12-23 20:24:00 +01:00
|
|
|
auto* socket_buffer = send_buffer_for(description);
|
|
|
|
if (!socket_buffer)
|
2021-08-01 11:27:23 -04:00
|
|
|
return set_so_error(EINVAL);
|
2021-06-16 15:33:14 +02:00
|
|
|
auto nwritten_or_error = socket_buffer->write(data, data_size);
|
|
|
|
if (!nwritten_or_error.is_error() && nwritten_or_error.value() > 0)
|
|
|
|
Thread::current()->did_unix_socket_write(nwritten_or_error.value());
|
|
|
|
return nwritten_or_error;
|
2019-12-01 17:36:06 +01:00
|
|
|
}
|
|
|
|
|
2021-09-07 13:39:11 +02:00
|
|
|
DoubleBuffer* LocalSocket::receive_buffer_for(OpenFileDescription& description)
|
2019-12-01 17:36:06 +01:00
|
|
|
{
|
2019-08-11 16:38:20 +03:00
|
|
|
auto role = this->role(description);
|
|
|
|
if (role == Role::Accepted)
|
2021-08-01 02:34:10 -07:00
|
|
|
return m_for_server.ptr();
|
2019-08-11 16:38:20 +03:00
|
|
|
if (role == Role::Connected)
|
2021-08-01 02:34:10 -07:00
|
|
|
return m_for_client.ptr();
|
2020-12-23 20:24:00 +01:00
|
|
|
return nullptr;
|
2019-03-12 15:51:42 +01:00
|
|
|
}
|
2019-03-12 17:27:07 +01:00
|
|
|
|
2021-09-07 13:39:11 +02:00
|
|
|
DoubleBuffer* LocalSocket::send_buffer_for(OpenFileDescription& description)
|
2019-03-12 17:27:07 +01:00
|
|
|
{
|
2019-08-11 16:38:20 +03:00
|
|
|
auto role = this->role(description);
|
2019-09-22 21:30:30 +02:00
|
|
|
if (role == Role::Connected)
|
2021-08-01 02:34:10 -07:00
|
|
|
return m_for_server.ptr();
|
2019-12-01 17:36:06 +01:00
|
|
|
if (role == Role::Accepted)
|
2021-08-01 02:34:10 -07:00
|
|
|
return m_for_client.ptr();
|
2020-12-23 20:24:00 +01:00
|
|
|
return nullptr;
|
2019-09-22 21:30:30 +02:00
|
|
|
}
|
|
|
|
|
2023-03-13 22:11:13 +01:00
|
|
|
ErrorOr<size_t> LocalSocket::recvfrom(OpenFileDescription& description, UserOrKernelBuffer& buffer, size_t buffer_size, int, Userspace<sockaddr*>, Userspace<socklen_t*>, UnixDateTime&, bool blocking)
|
2019-09-22 21:30:30 +02:00
|
|
|
{
|
2020-12-23 20:24:00 +01:00
|
|
|
auto* socket_buffer = receive_buffer_for(description);
|
|
|
|
if (!socket_buffer)
|
2021-08-01 11:27:23 -04:00
|
|
|
return set_so_error(EINVAL);
|
2022-08-21 16:45:42 +02:00
|
|
|
if (!blocking) {
|
2020-12-23 20:24:00 +01:00
|
|
|
if (socket_buffer->is_empty()) {
|
2019-09-22 21:30:30 +02:00
|
|
|
if (!has_attached_peer(description))
|
|
|
|
return 0;
|
2021-08-01 11:27:23 -04:00
|
|
|
return set_so_error(EAGAIN);
|
2019-08-05 10:03:19 +02:00
|
|
|
}
|
2020-04-10 19:44:42 +10:00
|
|
|
} else if (!can_read(description, 0)) {
|
2021-09-07 13:39:11 +02:00
|
|
|
auto unblock_flags = Thread::OpenFileDescriptionBlocker::BlockFlags::None;
|
2021-08-01 16:36:28 -04:00
|
|
|
if (Thread::current()->block<Thread::ReadBlocker>({}, description, unblock_flags).was_interrupted())
|
2021-08-01 11:27:23 -04:00
|
|
|
return set_so_error(EINTR);
|
2019-08-05 10:03:19 +02:00
|
|
|
}
|
2020-12-23 20:24:00 +01:00
|
|
|
if (!has_attached_peer(description) && socket_buffer->is_empty())
|
2019-09-22 21:30:30 +02:00
|
|
|
return 0;
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!socket_buffer->is_empty());
|
2021-06-16 15:33:14 +02:00
|
|
|
auto nread_or_error = socket_buffer->read(buffer, buffer_size);
|
|
|
|
if (!nread_or_error.is_error() && nread_or_error.value() > 0)
|
|
|
|
Thread::current()->did_unix_socket_read(nread_or_error.value());
|
|
|
|
return nread_or_error;
|
2019-03-12 17:27:07 +01:00
|
|
|
}
|
2019-08-10 18:55:54 +03:00
|
|
|
|
|
|
|
StringView LocalSocket::socket_path() const
|
|
|
|
{
|
2021-08-29 01:59:34 +02:00
|
|
|
if (!m_path)
|
|
|
|
return {};
|
|
|
|
return m_path->view();
|
2019-08-10 18:55:54 +03:00
|
|
|
}
|
2019-08-10 19:10:36 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<NonnullOwnPtr<KString>> LocalSocket::pseudo_path(OpenFileDescription const& description) const
|
2019-08-10 19:10:36 +03:00
|
|
|
{
|
|
|
|
StringBuilder builder;
|
2022-07-11 17:32:29 +00:00
|
|
|
TRY(builder.try_append("socket:"sv));
|
2022-01-09 00:59:12 -08:00
|
|
|
TRY(builder.try_append(socket_path()));
|
2019-08-10 19:10:36 +03:00
|
|
|
|
|
|
|
switch (role(description)) {
|
|
|
|
case Role::Listener:
|
2022-07-11 17:32:29 +00:00
|
|
|
TRY(builder.try_append(" (listening)"sv));
|
2019-08-10 19:10:36 +03:00
|
|
|
break;
|
|
|
|
case Role::Accepted:
|
2022-01-09 00:59:12 -08:00
|
|
|
TRY(builder.try_appendff(" (accepted from pid {})", origin_pid()));
|
2019-08-10 19:10:36 +03:00
|
|
|
break;
|
|
|
|
case Role::Connected:
|
2022-01-09 00:59:12 -08:00
|
|
|
TRY(builder.try_appendff(" (connected to pid {})", acceptor_pid()));
|
2019-08-10 19:10:36 +03:00
|
|
|
break;
|
|
|
|
case Role::Connecting:
|
2022-07-11 17:32:29 +00:00
|
|
|
TRY(builder.try_append(" (connecting)"sv));
|
2019-08-10 19:10:36 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-12-30 13:43:45 +01:00
|
|
|
return KString::try_create(builder.string_view());
|
2019-08-10 19:10:36 +03:00
|
|
|
}
|
2019-12-06 18:38:36 +01:00
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> LocalSocket::getsockopt(OpenFileDescription& description, int level, int option, Userspace<void*> value, Userspace<socklen_t*> value_size)
|
2019-12-06 18:38:36 +01:00
|
|
|
{
|
|
|
|
if (level != SOL_SOCKET)
|
|
|
|
return Socket::getsockopt(description, level, option, value, value_size);
|
|
|
|
|
2021-12-28 15:16:57 +01:00
|
|
|
MutexLocker locker(mutex());
|
|
|
|
|
2020-08-07 02:29:05 -07:00
|
|
|
socklen_t size;
|
2021-09-05 17:38:37 +02:00
|
|
|
TRY(copy_from_user(&size, value_size.unsafe_userspace_ptr()));
|
2020-08-07 02:29:05 -07:00
|
|
|
|
2019-12-06 18:38:36 +01:00
|
|
|
switch (option) {
|
2021-01-19 01:33:23 +03:30
|
|
|
case SO_SNDBUF:
|
2021-08-02 22:13:54 -06:00
|
|
|
return ENOTSUP;
|
2021-01-19 01:33:23 +03:30
|
|
|
case SO_RCVBUF:
|
2021-08-02 22:13:54 -06:00
|
|
|
return ENOTSUP;
|
2019-12-06 18:38:36 +01:00
|
|
|
case SO_PEERCRED: {
|
2020-08-07 02:29:05 -07:00
|
|
|
if (size < sizeof(ucred))
|
2021-01-20 23:11:17 +01:00
|
|
|
return EINVAL;
|
2019-12-06 18:38:36 +01:00
|
|
|
switch (role(description)) {
|
|
|
|
case Role::Accepted:
|
2021-09-05 17:38:37 +02:00
|
|
|
TRY(copy_to_user(static_ptr_cast<ucred*>(value), &m_origin));
|
2020-08-07 02:29:05 -07:00
|
|
|
size = sizeof(ucred);
|
2021-09-05 17:38:37 +02:00
|
|
|
TRY(copy_to_user(value_size, &size));
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2019-12-06 18:38:36 +01:00
|
|
|
case Role::Connected:
|
2021-09-05 17:38:37 +02:00
|
|
|
TRY(copy_to_user(static_ptr_cast<ucred*>(value), &m_acceptor));
|
2020-08-07 02:29:05 -07:00
|
|
|
size = sizeof(ucred);
|
2021-09-05 17:38:37 +02:00
|
|
|
TRY(copy_to_user(value_size, &size));
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2019-12-06 18:38:36 +01:00
|
|
|
case Role::Connecting:
|
2021-01-20 23:11:17 +01:00
|
|
|
return ENOTCONN;
|
2019-12-06 18:38:36 +01:00
|
|
|
default:
|
2021-01-20 23:11:17 +01:00
|
|
|
return EINVAL;
|
2019-12-06 18:38:36 +01:00
|
|
|
}
|
2021-10-07 13:51:24 -04:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-12-06 18:38:36 +01:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return Socket::getsockopt(description, level, option, value, value_size);
|
|
|
|
}
|
|
|
|
}
|
2020-01-03 20:14:56 +01:00
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> LocalSocket::ioctl(OpenFileDescription& description, unsigned request, Userspace<void*> arg)
|
2021-07-26 23:06:22 -06:00
|
|
|
{
|
|
|
|
switch (request) {
|
|
|
|
case FIONREAD: {
|
|
|
|
int readable = receive_buffer_for(description)->immediately_readable();
|
2021-11-14 15:43:43 -07:00
|
|
|
return copy_to_user(static_ptr_cast<int*>(arg), &readable);
|
2021-07-26 23:06:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 23:45:35 +02:00
|
|
|
return EINVAL;
|
2021-07-26 23:06:22 -06:00
|
|
|
}
|
|
|
|
|
2022-08-21 16:22:34 +02:00
|
|
|
ErrorOr<void> LocalSocket::chmod(Credentials const& credentials, OpenFileDescription& description, mode_t mode)
|
2020-01-03 20:14:56 +01:00
|
|
|
{
|
2022-08-21 16:22:34 +02:00
|
|
|
if (m_inode) {
|
|
|
|
if (auto custody = description.custody())
|
|
|
|
return VirtualFileSystem::the().chmod(credentials, *custody, mode);
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
2020-01-03 20:14:56 +01:00
|
|
|
|
2021-01-23 15:35:20 +01:00
|
|
|
m_prebind_mode = mode & 0777;
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2020-01-03 20:14:56 +01:00
|
|
|
}
|
|
|
|
|
2022-08-21 16:22:34 +02:00
|
|
|
ErrorOr<void> LocalSocket::chown(Credentials const& credentials, OpenFileDescription& description, UserID uid, GroupID gid)
|
2020-01-03 20:14:56 +01:00
|
|
|
{
|
2022-08-21 16:22:34 +02:00
|
|
|
if (m_inode) {
|
|
|
|
if (auto custody = description.custody())
|
|
|
|
return VirtualFileSystem::the().chown(credentials, *custody, uid, gid);
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
2020-01-03 20:14:56 +01:00
|
|
|
|
2022-08-21 16:15:29 +02:00
|
|
|
if (!credentials.is_superuser() && (credentials.euid() != uid || !credentials.in_group(gid)))
|
2021-08-01 11:27:23 -04:00
|
|
|
return set_so_error(EPERM);
|
2020-01-03 20:14:56 +01:00
|
|
|
|
|
|
|
m_prebind_uid = uid;
|
|
|
|
m_prebind_gid = gid;
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2020-01-03 20:14:56 +01:00
|
|
|
}
|
2020-02-16 01:27:42 +01:00
|
|
|
|
2023-03-06 19:29:25 +01:00
|
|
|
Vector<NonnullRefPtr<OpenFileDescription>>& LocalSocket::recvfd_queue_for(OpenFileDescription const& description)
|
2020-06-24 22:57:37 +02:00
|
|
|
{
|
2023-03-06 19:29:25 +01:00
|
|
|
VERIFY(mutex().is_exclusively_locked_by_current_thread());
|
2020-06-24 22:57:37 +02:00
|
|
|
auto role = this->role(description);
|
|
|
|
if (role == Role::Connected)
|
|
|
|
return m_fds_for_client;
|
|
|
|
if (role == Role::Accepted)
|
|
|
|
return m_fds_for_server;
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-06-24 22:57:37 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 19:29:25 +01:00
|
|
|
Vector<NonnullRefPtr<OpenFileDescription>>& LocalSocket::sendfd_queue_for(OpenFileDescription const& description)
|
2020-06-24 22:57:37 +02:00
|
|
|
{
|
2023-03-06 19:29:25 +01:00
|
|
|
VERIFY(mutex().is_exclusively_locked_by_current_thread());
|
2020-06-24 22:57:37 +02:00
|
|
|
auto role = this->role(description);
|
|
|
|
if (role == Role::Connected)
|
|
|
|
return m_fds_for_server;
|
|
|
|
if (role == Role::Accepted)
|
|
|
|
return m_fds_for_client;
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-06-24 22:57:37 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 19:29:25 +01:00
|
|
|
ErrorOr<void> LocalSocket::sendfd(OpenFileDescription const& socket_description, NonnullRefPtr<OpenFileDescription> passing_description)
|
2020-06-24 22:57:37 +02:00
|
|
|
{
|
2021-08-29 13:10:55 +02:00
|
|
|
MutexLocker locker(mutex());
|
2020-06-24 22:57:37 +02:00
|
|
|
auto role = this->role(socket_description);
|
|
|
|
if (role != Role::Connected && role != Role::Accepted)
|
2021-08-01 11:27:23 -04:00
|
|
|
return set_so_error(EINVAL);
|
2020-06-24 22:57:37 +02:00
|
|
|
auto& queue = sendfd_queue_for(socket_description);
|
|
|
|
// FIXME: Figure out how we should limit this properly.
|
2021-01-29 22:11:59 +01:00
|
|
|
if (queue.size() > 128)
|
2021-08-01 11:27:23 -04:00
|
|
|
return set_so_error(EBUSY);
|
2021-11-10 11:55:37 +01:00
|
|
|
SOCKET_TRY(queue.try_append(move(passing_description)));
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2020-06-24 22:57:37 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 19:29:25 +01:00
|
|
|
ErrorOr<NonnullRefPtr<OpenFileDescription>> LocalSocket::recvfd(OpenFileDescription const& socket_description)
|
2020-06-24 22:57:37 +02:00
|
|
|
{
|
2021-08-29 13:10:55 +02:00
|
|
|
MutexLocker locker(mutex());
|
2020-06-24 22:57:37 +02:00
|
|
|
auto role = this->role(socket_description);
|
|
|
|
if (role != Role::Connected && role != Role::Accepted)
|
2021-08-01 11:27:23 -04:00
|
|
|
return set_so_error(EINVAL);
|
2020-06-24 22:57:37 +02:00
|
|
|
auto& queue = recvfd_queue_for(socket_description);
|
|
|
|
if (queue.is_empty()) {
|
|
|
|
// FIXME: Figure out the perfect error code for this.
|
2021-08-01 11:27:23 -04:00
|
|
|
return set_so_error(EAGAIN);
|
2020-06-24 22:57:37 +02:00
|
|
|
}
|
|
|
|
return queue.take_first();
|
|
|
|
}
|
|
|
|
|
2023-03-06 19:29:25 +01:00
|
|
|
ErrorOr<Vector<NonnullRefPtr<OpenFileDescription>>> LocalSocket::recvfds(OpenFileDescription const& socket_description, int n)
|
2023-02-13 16:01:13 -07:00
|
|
|
{
|
|
|
|
MutexLocker locker(mutex());
|
2023-03-06 19:29:25 +01:00
|
|
|
Vector<NonnullRefPtr<OpenFileDescription>> fds;
|
2023-02-13 16:01:13 -07:00
|
|
|
|
|
|
|
auto role = this->role(socket_description);
|
|
|
|
if (role != Role::Connected && role != Role::Accepted)
|
|
|
|
return set_so_error(EINVAL);
|
|
|
|
auto& queue = recvfd_queue_for(socket_description);
|
|
|
|
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
if (queue.is_empty())
|
|
|
|
break;
|
|
|
|
|
|
|
|
fds.append(queue.take_first());
|
|
|
|
}
|
|
|
|
|
|
|
|
return fds;
|
|
|
|
}
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> LocalSocket::try_set_path(StringView path)
|
2021-08-29 01:59:34 +02:00
|
|
|
{
|
2021-09-06 19:24:54 +02:00
|
|
|
m_path = TRY(KString::try_create(path));
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2021-08-29 01:59:34 +02:00
|
|
|
}
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
}
|