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>
|
2021-01-25 18:07:10 +03:00
|
|
|
#include <Kernel/Debug.h>
|
2019-06-07 10:36:51 +03:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
2019-04-03 13:25:24 +03:00
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.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>
|
|
|
|
#include <Kernel/Process.h>
|
2020-05-16 13:00:04 +03:00
|
|
|
#include <Kernel/StdLib.h>
|
2020-06-24 23:57:37 +03:00
|
|
|
#include <Kernel/UnixTypes.h>
|
2019-02-14 16:38:30 +03:00
|
|
|
#include <LibC/errno_numbers.h>
|
2021-07-27 08:06:22 +03:00
|
|
|
#include <LibC/sys/ioctl_numbers.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;
|
|
|
|
}
|
|
|
|
|
2020-04-18 12:50:35 +03:00
|
|
|
void LocalSocket::for_each(Function<void(const LocalSocket&)> callback)
|
2019-08-10 18:58:06 +03:00
|
|
|
{
|
2021-07-18 12:52:40 +03:00
|
|
|
all_sockets().for_each_shared([&](const auto& 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
|
|
|
}
|
|
|
|
|
2021-08-29 02:22:34 +03:00
|
|
|
KResultOr<NonnullRefPtr<LocalSocket>> LocalSocket::try_create(int type)
|
2019-02-14 16:17:38 +03:00
|
|
|
{
|
2021-08-01 12:34:10 +03:00
|
|
|
auto client_buffer = DoubleBuffer::try_create();
|
|
|
|
if (!client_buffer)
|
|
|
|
return ENOMEM;
|
|
|
|
auto server_buffer = DoubleBuffer::try_create();
|
|
|
|
if (!server_buffer)
|
|
|
|
return ENOMEM;
|
|
|
|
auto socket = adopt_ref_if_nonnull(new (nothrow) LocalSocket(type, client_buffer.release_nonnull(), server_buffer.release_nonnull()));
|
2021-05-13 10:06:09 +03:00
|
|
|
if (socket)
|
|
|
|
return socket.release_nonnull();
|
|
|
|
return ENOMEM;
|
2019-02-14 16:17:38 +03:00
|
|
|
}
|
|
|
|
|
2021-08-29 02:31:45 +03:00
|
|
|
KResultOr<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));
|
|
|
|
auto description1 = TRY(FileDescription::try_create(*socket));
|
2021-04-28 13:39:12 +03:00
|
|
|
|
2021-08-29 02:59:34 +03:00
|
|
|
if (auto result = socket->try_set_path("[socketpair]"sv); result.is_error())
|
|
|
|
return result;
|
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-05 15:01:09 +03:00
|
|
|
auto description2 = TRY(FileDescription::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();
|
|
|
|
m_prebind_uid = current_process.euid();
|
|
|
|
m_prebind_gid = current_process.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
|
|
|
{
|
2021-08-29 02:59:34 +03:00
|
|
|
if (!m_path || m_path->is_empty()) {
|
|
|
|
size_t bytes_to_copy = min(static_cast<size_t>(*address_size), sizeof(sockaddr_un));
|
|
|
|
memset(address, 0, bytes_to_copy);
|
|
|
|
} else {
|
|
|
|
size_t bytes_to_copy = min(m_path->length(), min(static_cast<size_t>(*address_size), sizeof(sockaddr_un)));
|
|
|
|
memcpy(address, m_path->characters(), bytes_to_copy);
|
|
|
|
}
|
2019-02-14 17:17:30 +03:00
|
|
|
*address_size = sizeof(sockaddr_un);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-10 01:13:43 +03:00
|
|
|
KResult LocalSocket::bind(Userspace<const sockaddr*> 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);
|
2019-03-07 00:14:31 +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 = {};
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!copy_from_user(&address, user_address, sizeof(sockaddr_un)))
|
2021-08-01 18:27:23 +03:00
|
|
|
return set_so_error(EFAULT);
|
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-08-29 02:59:34 +03:00
|
|
|
auto path = KString::try_create(StringView { address.sun_path, strnlen(address.sun_path, sizeof(address.sun_path)) });
|
|
|
|
if (!path)
|
|
|
|
return set_so_error(ENOMEM);
|
2019-02-14 16:38:30 +03:00
|
|
|
|
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 };
|
2021-08-29 02:59:34 +03:00
|
|
|
auto result = VirtualFileSystem::the().open(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-08-14 16:15:11 +03:00
|
|
|
if (result.error() == EEXIST)
|
2021-08-01 18:27:23 +03:00
|
|
|
return set_so_error(EADDRINUSE);
|
2019-03-07 00:14:31 +03:00
|
|
|
return result.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-02-23 22:42:32 +03:00
|
|
|
VERIFY(file->inode());
|
2020-01-31 00:15:45 +03:00
|
|
|
if (!file->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
|
|
|
|
|
|
|
m_file = move(file);
|
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;
|
2019-03-07 00:14:31 +03:00
|
|
|
return KSuccess;
|
2019-02-14 16:38:30 +03:00
|
|
|
}
|
2019-02-14 17:55:19 +03:00
|
|
|
|
2020-09-12 06:11:07 +03:00
|
|
|
KResult LocalSocket::connect(FileDescription& description, Userspace<const sockaddr*> address, socklen_t address_size, ShouldBlock)
|
2019-02-14 17:55:19 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!m_bound);
|
2019-03-07 00:14:31 +03:00
|
|
|
if (address_size != sizeof(sockaddr_un))
|
2021-08-01 18:27:23 +03:00
|
|
|
return set_so_error(EINVAL);
|
2020-09-12 06:11:07 +03:00
|
|
|
u16 sa_family_copy;
|
|
|
|
auto* user_address = reinterpret_cast<const sockaddr*>(address.unsafe_userspace_ptr());
|
|
|
|
if (!copy_from_user(&sa_family_copy, &user_address->sa_family, sizeof(u16)))
|
2021-08-01 18:27:23 +03:00
|
|
|
return set_so_error(EFAULT);
|
2020-09-12 06:11:07 +03:00
|
|
|
if (sa_family_copy != AF_LOCAL)
|
2021-08-01 18:27:23 +03:00
|
|
|
return set_so_error(EINVAL);
|
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
|
|
|
|
2021-08-29 02:59:34 +03:00
|
|
|
OwnPtr<KString> maybe_path;
|
|
|
|
{
|
|
|
|
auto const& local_address = *reinterpret_cast<sockaddr_un const*>(user_address);
|
|
|
|
char safe_address[sizeof(local_address.sun_path) + 1] = { 0 };
|
|
|
|
if (!copy_from_user(&safe_address[0], &local_address.sun_path[0], sizeof(safe_address) - 1))
|
|
|
|
return set_so_error(EFAULT);
|
|
|
|
safe_address[sizeof(safe_address) - 1] = '\0';
|
|
|
|
maybe_path = KString::try_create(safe_address);
|
|
|
|
if (!maybe_path)
|
|
|
|
return set_so_error(ENOMEM);
|
|
|
|
}
|
2019-02-14 17:55:19 +03:00
|
|
|
|
2021-08-29 02:59:34 +03:00
|
|
|
auto path = maybe_path.release_nonnull();
|
|
|
|
dbgln_if(LOCAL_SOCKET_DEBUG, "LocalSocket({}) connect({})", this, *path);
|
2019-02-14 17:55:19 +03:00
|
|
|
|
2021-08-29 02:59:34 +03:00
|
|
|
auto description_or_error = VirtualFileSystem::the().open(path->view(), O_RDWR, 0, Process::current().current_directory());
|
2019-06-13 23:03:04 +03:00
|
|
|
if (description_or_error.is_error())
|
2021-08-01 18:27:23 +03:00
|
|
|
return set_so_error(ECONNREFUSED);
|
2019-08-10 18:58:06 +03:00
|
|
|
|
2019-06-13 23:03:04 +03:00
|
|
|
m_file = move(description_or_error.value());
|
2019-03-07 00:14:31 +03:00
|
|
|
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(m_file->inode());
|
2019-03-07 00:14:31 +03:00
|
|
|
if (!m_file->inode()->socket())
|
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-02-14 19:18:35 +03:00
|
|
|
auto peer = m_file->inode()->socket();
|
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);
|
2019-07-20 12:10:46 +03:00
|
|
|
return KSuccess;
|
2019-08-11 16:38:20 +03:00
|
|
|
}
|
2019-07-20 12:10:46 +03:00
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
auto unblock_flags = Thread::FileDescriptionBlocker::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-03-07 14:01:11 +03:00
|
|
|
if (!has_flag(unblock_flags, Thread::FileDescriptionBlocker::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);
|
2019-07-20 12:10:46 +03:00
|
|
|
return KSuccess;
|
2019-02-14 17:55:19 +03:00
|
|
|
}
|
2019-02-14 18:01:08 +03:00
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
KResult 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
|
|
|
|
2019-08-06 16:40:38 +03:00
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2020-09-17 22:51:09 +03:00
|
|
|
KResult LocalSocket::attach(FileDescription& 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();
|
2020-09-17 22:51:09 +03:00
|
|
|
return KSuccess;
|
2019-02-17 13:00:35 +03:00
|
|
|
}
|
|
|
|
|
2019-06-13 23:03:04 +03:00
|
|
|
void LocalSocket::detach(FileDescription& 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;
|
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
|
|
|
}
|
|
|
|
|
2020-04-10 12:44:42 +03:00
|
|
|
bool LocalSocket::can_read(const FileDescription& description, size_t) 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
|
|
|
}
|
|
|
|
|
2019-06-13 23:03:04 +03:00
|
|
|
bool LocalSocket::has_attached_peer(const FileDescription& 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
|
|
|
}
|
|
|
|
|
2020-04-10 12:44:42 +03:00
|
|
|
bool LocalSocket::can_write(const FileDescription& description, size_t) 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
|
|
|
|
2020-09-12 06:11:07 +03:00
|
|
|
KResultOr<size_t> LocalSocket::sendto(FileDescription& description, const UserOrKernelBuffer& data, size_t data_size, int, Userspace<const sockaddr*>, 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
|
|
|
}
|
|
|
|
|
2020-12-23 22:24:00 +03:00
|
|
|
DoubleBuffer* LocalSocket::receive_buffer_for(FileDescription& 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
|
|
|
|
2020-12-23 22:24:00 +03:00
|
|
|
DoubleBuffer* LocalSocket::send_buffer_for(FileDescription& 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
|
|
|
}
|
|
|
|
|
2021-02-28 04:48:45 +03:00
|
|
|
KResultOr<size_t> LocalSocket::recvfrom(FileDescription& description, UserOrKernelBuffer& buffer, size_t buffer_size, int, Userspace<sockaddr*>, Userspace<socklen_t*>, Time&)
|
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);
|
2019-09-22 22:30:30 +03:00
|
|
|
if (!description.is_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)) {
|
2020-11-30 02:05:27 +03:00
|
|
|
auto unblock_flags = Thread::FileDescriptionBlocker::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
|
|
|
|
|
|
|
String LocalSocket::absolute_path(const FileDescription& description) const
|
|
|
|
{
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.append("socket:");
|
|
|
|
builder.append(socket_path());
|
|
|
|
|
|
|
|
switch (role(description)) {
|
|
|
|
case Role::Listener:
|
|
|
|
builder.append(" (listening)");
|
|
|
|
break;
|
|
|
|
case Role::Accepted:
|
2021-02-09 18:08:43 +03:00
|
|
|
builder.appendff(" (accepted from pid {})", origin_pid());
|
2019-08-10 19:10:36 +03:00
|
|
|
break;
|
|
|
|
case Role::Connected:
|
2021-02-09 18:08:43 +03:00
|
|
|
builder.appendff(" (connected to pid {})", acceptor_pid());
|
2019-08-10 19:10:36 +03:00
|
|
|
break;
|
|
|
|
case Role::Connecting:
|
|
|
|
builder.append(" (connecting)");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.to_string();
|
|
|
|
}
|
2019-12-06 20:38:36 +03:00
|
|
|
|
2020-08-07 12:29:05 +03:00
|
|
|
KResult LocalSocket::getsockopt(FileDescription& 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);
|
|
|
|
|
2020-08-07 12:29:05 +03:00
|
|
|
socklen_t size;
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!copy_from_user(&size, value_size.unsafe_userspace_ptr()))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EFAULT;
|
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:
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!copy_to_user(static_ptr_cast<ucred*>(value), &m_origin))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EFAULT;
|
2020-08-07 12:29:05 +03:00
|
|
|
size = sizeof(ucred);
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!copy_to_user(value_size, &size))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EFAULT;
|
2019-12-06 20:38:36 +03:00
|
|
|
return KSuccess;
|
|
|
|
case Role::Connected:
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!copy_to_user(static_ptr_cast<ucred*>(value), &m_acceptor))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EFAULT;
|
2020-08-07 12:29:05 +03:00
|
|
|
size = sizeof(ucred);
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!copy_to_user(value_size, &size))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EFAULT;
|
2019-12-06 20:38:36 +03:00
|
|
|
return KSuccess;
|
|
|
|
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
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return Socket::getsockopt(description, level, option, value, value_size);
|
|
|
|
}
|
|
|
|
}
|
2020-01-03 22:14:56 +03:00
|
|
|
|
2021-07-27 08:06:22 +03:00
|
|
|
KResult LocalSocket::ioctl(FileDescription& description, unsigned request, Userspace<void*> arg)
|
|
|
|
{
|
|
|
|
switch (request) {
|
|
|
|
case FIONREAD: {
|
|
|
|
int readable = receive_buffer_for(description)->immediately_readable();
|
|
|
|
if (!copy_to_user(Userspace<int*>(arg), &readable))
|
|
|
|
return EFAULT;
|
|
|
|
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ENOTTY;
|
|
|
|
}
|
|
|
|
|
2020-05-28 17:32:20 +03:00
|
|
|
KResult LocalSocket::chmod(FileDescription&, mode_t mode)
|
2020-01-03 22:14:56 +03:00
|
|
|
{
|
|
|
|
if (m_file)
|
|
|
|
return m_file->chmod(mode);
|
|
|
|
|
2021-01-23 17:35:20 +03:00
|
|
|
m_prebind_mode = mode & 0777;
|
2020-01-03 22:14:56 +03:00
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2021-08-28 23:11:16 +03:00
|
|
|
KResult LocalSocket::chown(FileDescription&, UserID uid, GroupID gid)
|
2020-01-03 22:14:56 +03:00
|
|
|
{
|
|
|
|
if (m_file)
|
|
|
|
return m_file->chown(uid, gid);
|
|
|
|
|
2021-08-19 22:45:07 +03:00
|
|
|
auto& current_process = Process::current();
|
|
|
|
if (!current_process.is_superuser() && (current_process.euid() != uid || !current_process.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;
|
|
|
|
return KSuccess;
|
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
2020-06-25 11:51:30 +03:00
|
|
|
NonnullRefPtrVector<FileDescription>& LocalSocket::recvfd_queue_for(const FileDescription& description)
|
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
|
|
|
}
|
|
|
|
|
2020-06-25 11:51:30 +03:00
|
|
|
NonnullRefPtrVector<FileDescription>& LocalSocket::sendfd_queue_for(const FileDescription& description)
|
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
|
|
|
}
|
|
|
|
|
2020-06-25 11:51:30 +03:00
|
|
|
KResult LocalSocket::sendfd(const FileDescription& socket_description, FileDescription& 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-04-30 11:59:55 +03:00
|
|
|
if (!queue.try_append(move(passing_description)))
|
2021-08-01 18:27:23 +03:00
|
|
|
return set_so_error(ENOMEM);
|
2020-06-24 23:57:37 +03:00
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2020-06-25 11:51:30 +03:00
|
|
|
KResultOr<NonnullRefPtr<FileDescription>> LocalSocket::recvfd(const FileDescription& 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();
|
|
|
|
}
|
|
|
|
|
2021-08-29 02:59:34 +03:00
|
|
|
KResult LocalSocket::try_set_path(StringView path)
|
|
|
|
{
|
|
|
|
auto kstring = KString::try_create(path);
|
|
|
|
if (!kstring)
|
|
|
|
return ENOMEM;
|
|
|
|
m_path = move(kstring);
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
}
|