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