From d46c3b0b5bc196bb987d3c77855be678849e2e09 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Sun, 11 Aug 2019 16:28:18 +0300 Subject: [PATCH] Net: Simplify how LocalSocket tracks open fds Now that there can't be multiple clones of the same fd, we only need to track whether or not an fd exists on each side. Also there's no point in tracking connecting fds. --- Kernel/FileSystem/FileDescription.cpp | 5 ++-- Kernel/Net/LocalSocket.cpp | 34 ++++++++++++++------------- Kernel/Net/LocalSocket.h | 5 ++-- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/Kernel/FileSystem/FileDescription.cpp b/Kernel/FileSystem/FileDescription.cpp index cf9d0a52b6..4b122bb256 100644 --- a/Kernel/FileSystem/FileDescription.cpp +++ b/Kernel/FileSystem/FileDescription.cpp @@ -33,6 +33,8 @@ FileDescription::FileDescription(File& file, SocketRole role) if (file.is_inode()) m_inode = static_cast(file).inode(); set_socket_role(role); + if (is_socket()) + socket()->attach(*this); } FileDescription::~FileDescription() @@ -51,10 +53,7 @@ void FileDescription::set_socket_role(SocketRole role) return; ASSERT(is_socket()); - if (m_socket_role != SocketRole::None) - socket()->detach(*this); m_socket_role = role; - socket()->attach(*this); } KResult FileDescription::fstat(stat& buffer) diff --git a/Kernel/Net/LocalSocket.cpp b/Kernel/Net/LocalSocket.cpp index 1309c77078..f4e1377134 100644 --- a/Kernel/Net/LocalSocket.cpp +++ b/Kernel/Net/LocalSocket.cpp @@ -131,16 +131,17 @@ KResult LocalSocket::listen(int backlog) void LocalSocket::attach(FileDescription& description) { + ASSERT(!m_accept_side_fd_open); switch (description.socket_role()) { + case SocketRole::None: + ASSERT(!m_connect_side_fd_open); + m_connect_side_fd_open = true; + break; case SocketRole::Accepted: - ++m_accepted_fds_open; + m_accept_side_fd_open = true; break; case SocketRole::Connected: - ++m_connected_fds_open; - break; - case SocketRole::Connecting: - ++m_connecting_fds_open; - break; + ASSERT_NOT_REACHED(); default: break; } @@ -149,17 +150,18 @@ void LocalSocket::attach(FileDescription& description) void LocalSocket::detach(FileDescription& description) { switch (description.socket_role()) { + case SocketRole::None: + ASSERT(!m_accept_side_fd_open); + ASSERT(m_connect_side_fd_open); + m_connect_side_fd_open = false; + break; case SocketRole::Accepted: - ASSERT(m_accepted_fds_open); - --m_accepted_fds_open; + ASSERT(m_accept_side_fd_open); + m_accept_side_fd_open = false; break; case SocketRole::Connected: - ASSERT(m_connected_fds_open); - --m_connected_fds_open; - break; - case SocketRole::Connecting: - ASSERT(m_connecting_fds_open); - --m_connecting_fds_open; + ASSERT(m_connect_side_fd_open); + m_connect_side_fd_open = false; break; default: break; @@ -181,9 +183,9 @@ bool LocalSocket::can_read(FileDescription& description) const bool LocalSocket::has_attached_peer(const FileDescription& description) const { if (description.socket_role() == SocketRole::Accepted) - return m_connected_fds_open || m_connecting_fds_open; + return m_connect_side_fd_open; if (description.socket_role() == SocketRole::Connected) - return m_accepted_fds_open; + return m_accept_side_fd_open; ASSERT_NOT_REACHED(); } diff --git a/Kernel/Net/LocalSocket.h b/Kernel/Net/LocalSocket.h index ae82b4f005..9cae1adc88 100644 --- a/Kernel/Net/LocalSocket.h +++ b/Kernel/Net/LocalSocket.h @@ -31,9 +31,8 @@ private: RefPtr m_file; bool m_bound { false }; - int m_accepted_fds_open { 0 }; - int m_connected_fds_open { 0 }; - int m_connecting_fds_open { 0 }; + bool m_accept_side_fd_open { false }; + bool m_connect_side_fd_open { false }; sockaddr_un m_address; DoubleBuffer m_for_client;