summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2022-02-07 12:57:57 +0100
committerAndreas Kling <kling@serenityos.org>2022-02-07 13:02:34 +0100
commitcda56f8049df93a659b228088d2f3d1387558116 (patch)
tree8ebc38e7384a4ccd864abcb51a9d6d95ca3b4b44
parent4bfed6a5ede4595301285c0d6fc89ea461cfdde2 (diff)
downloadserenity-cda56f8049df93a659b228088d2f3d1387558116.zip
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.
-rw-r--r--Kernel/FileSystem/Inode.cpp13
-rw-r--r--Kernel/FileSystem/Inode.h5
-rw-r--r--Kernel/Net/LocalSocket.cpp5
-rw-r--r--Kernel/Syscalls/open.cpp3
4 files changed, 16 insertions, 10 deletions
diff --git a/Kernel/FileSystem/Inode.cpp b/Kernel/FileSystem/Inode.cpp
index 1b8a606c9e..a8822a1e23 100644
--- a/Kernel/FileSystem/Inode.cpp
+++ b/Kernel/FileSystem/Inode.cpp
@@ -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;
}
diff --git a/Kernel/FileSystem/Inode.h b/Kernel/FileSystem/Inode.h
index 775e4ad020..7351e892e2 100644
--- a/Kernel/FileSystem/Inode.h
+++ b/Kernel/FileSystem/Inode.h
@@ -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;
diff --git a/Kernel/Net/LocalSocket.cpp b/Kernel/Net/LocalSocket.cpp
index 14b84ae98c..de46196624 100644
--- a/Kernel/Net/LocalSocket.cpp
+++ b/Kernel/Net/LocalSocket.cpp
@@ -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);
diff --git a/Kernel/Syscalls/open.cpp b/Kernel/Syscalls/open.cpp
index 8943ad8086..34b1754d3b 100644
--- a/Kernel/Syscalls/open.cpp
+++ b/Kernel/Syscalls/open.cpp
@@ -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> {