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
Notes: sideshowbarker 2024-07-17 19:40:34 +09:00
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;
}
RefPtr<LocalSocket> Inode::bound_socket() const
{
return m_bound_socket;
}
bool Inode::bind_socket(LocalSocket& socket)
{
MutexLocker locker(m_inode_lock);
if (m_socket)
if (m_bound_socket)
return false;
m_socket = socket;
m_bound_socket = socket;
return true;
}
bool Inode::unbind_socket()
{
MutexLocker locker(m_inode_lock);
if (!m_socket)
if (!m_bound_socket)
return false;
m_socket = nullptr;
m_bound_socket = nullptr;
return true;
}

View File

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

View File

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

View File

@ -7,6 +7,7 @@
#include <Kernel/Debug.h>
#include <Kernel/FileSystem/Custody.h>
#include <Kernel/FileSystem/VirtualFileSystem.h>
#include <Kernel/Net/LocalSocket.h>
#include <Kernel/Process.h>
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));
if (description->inode() && description->inode()->socket())
if (description->inode() && description->inode()->bound_socket())
return ENXIO;
return m_fds.with_exclusive([&](auto& fds) -> ErrorOr<FlatPtr> {