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