1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-17 08:57:35 +00:00

Kernel: Robustify and rename Inode bound socket API

Rename the bound socket accessor from socket() to bound_socket().
Also return RefPtr<LocalSocket> instead of a raw pointer, to make it
harder for callers to mess up.
This commit is contained in:
Andreas Kling 2022-02-07 12:57:57 +01:00
parent 4bfed6a5ed
commit cda56f8049
4 changed files with 16 additions and 10 deletions

View file

@ -136,21 +136,26 @@ void Inode::set_shared_vmobject(Memory::SharedInodeVMObject& vmobject)
m_shared_vmobject = vmobject; m_shared_vmobject = vmobject;
} }
RefPtr<LocalSocket> Inode::bound_socket() const
{
return m_bound_socket;
}
bool Inode::bind_socket(LocalSocket& socket) bool Inode::bind_socket(LocalSocket& socket)
{ {
MutexLocker locker(m_inode_lock); MutexLocker locker(m_inode_lock);
if (m_socket) if (m_bound_socket)
return false; return false;
m_socket = socket; m_bound_socket = socket;
return true; return true;
} }
bool Inode::unbind_socket() bool Inode::unbind_socket()
{ {
MutexLocker locker(m_inode_lock); MutexLocker locker(m_inode_lock);
if (!m_socket) if (!m_bound_socket)
return false; return false;
m_socket = nullptr; m_bound_socket = nullptr;
return true; return true;
} }

View file

@ -66,8 +66,7 @@ public:
virtual ErrorOr<int> get_block_address(int) { return ENOTSUP; } virtual ErrorOr<int> get_block_address(int) { return ENOTSUP; }
LocalSocket* socket() { return m_socket.ptr(); } RefPtr<LocalSocket> bound_socket() const;
const LocalSocket* socket() const { return m_socket.ptr(); }
bool bind_socket(LocalSocket&); bool bind_socket(LocalSocket&);
bool unbind_socket(); bool unbind_socket();
@ -117,7 +116,7 @@ private:
FileSystem& m_file_system; FileSystem& m_file_system;
InodeIndex m_index { 0 }; InodeIndex m_index { 0 };
WeakPtr<Memory::SharedInodeVMObject> m_shared_vmobject; WeakPtr<Memory::SharedInodeVMObject> m_shared_vmobject;
RefPtr<LocalSocket> m_socket; RefPtr<LocalSocket> m_bound_socket;
SpinlockProtected<HashTable<InodeWatcher*>> m_watchers; SpinlockProtected<HashTable<InodeWatcher*>> m_watchers;
bool m_metadata_dirty { false }; bool m_metadata_dirty { false };
RefPtr<FIFO> m_fifo; RefPtr<FIFO> m_fifo;

View file

@ -174,7 +174,9 @@ ErrorOr<void> LocalSocket::connect(OpenFileDescription& description, Userspace<c
m_inode = inode; m_inode = inode;
VERIFY(inode); VERIFY(inode);
if (!inode->socket())
auto peer = inode->bound_socket();
if (!peer)
return set_so_error(ECONNREFUSED); return set_so_error(ECONNREFUSED);
m_path = move(path); m_path = move(path);
@ -182,7 +184,6 @@ ErrorOr<void> LocalSocket::connect(OpenFileDescription& description, Userspace<c
VERIFY(m_connect_side_fd == &description); VERIFY(m_connect_side_fd == &description);
set_connect_side_role(Role::Connecting); set_connect_side_role(Role::Connecting);
auto peer = file->inode()->socket();
auto result = peer->queue_connection_from(*this); auto result = peer->queue_connection_from(*this);
if (result.is_error()) { if (result.is_error()) {
set_connect_side_role(Role::None); set_connect_side_role(Role::None);

View file

@ -7,6 +7,7 @@
#include <Kernel/Debug.h> #include <Kernel/Debug.h>
#include <Kernel/FileSystem/Custody.h> #include <Kernel/FileSystem/Custody.h>
#include <Kernel/FileSystem/VirtualFileSystem.h> #include <Kernel/FileSystem/VirtualFileSystem.h>
#include <Kernel/Net/LocalSocket.h>
#include <Kernel/Process.h> #include <Kernel/Process.h>
namespace Kernel { namespace Kernel {
@ -56,7 +57,7 @@ ErrorOr<FlatPtr> Process::sys$open(Userspace<const Syscall::SC_open_params*> use
auto description = TRY(VirtualFileSystem::the().open(path->view(), options, mode & ~umask(), *base)); auto description = TRY(VirtualFileSystem::the().open(path->view(), options, mode & ~umask(), *base));
if (description->inode() && description->inode()->socket()) if (description->inode() && description->inode()->bound_socket())
return ENXIO; return ENXIO;
return m_fds.with_exclusive([&](auto& fds) -> ErrorOr<FlatPtr> { return m_fds.with_exclusive([&](auto& fds) -> ErrorOr<FlatPtr> {