kernel: Support listening sockets in poll()

This commit is contained in:
apio 2023-08-03 16:28:14 +02:00
parent 5c96a31cd8
commit d98385088a
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 35 additions and 5 deletions

View File

@ -65,6 +65,10 @@ class Socket : public VFS::FileInode
m_metadata.nlinks--; m_metadata.nlinks--;
} }
virtual bool can_accept_connections() const = 0;
virtual bool can_read_data() const = 0;
virtual ~Socket() = default; virtual ~Socket() = default;
protected: protected:

View File

@ -17,6 +17,16 @@ class UnixSocket : public Socket
return (m_state == Connected || m_state == Reset) && !m_data.size(); return (m_state == Connected || m_state == Reset) && !m_data.size();
} }
bool can_read_data() const override
{
return (m_state == Connected || m_state == Reset) && m_data.size();
}
bool can_accept_connections() const override
{
return !m_listen_queue.is_empty();
}
Result<usize> send(const u8*, usize, int) override; Result<usize> send(const u8*, usize, int) override;
Result<usize> recv(u8*, usize, int) const override; Result<usize> recv(u8*, usize, int) const override;

View File

@ -1,6 +1,7 @@
#include "Log.h" #include "Log.h"
#include "fs/VFS.h" #include "fs/VFS.h"
#include "memory/MemoryManager.h" #include "memory/MemoryManager.h"
#include "net/Socket.h"
#include "sys/Syscall.h" #include "sys/Syscall.h"
#include "thread/Scheduler.h" #include "thread/Scheduler.h"
#include <bits/poll.h> #include <bits/poll.h>
@ -43,10 +44,25 @@ Result<u64> sys_poll(Registers* regs, SyscallArgs args)
auto& inode = inodes[i]; auto& inode = inodes[i];
if (!inode) continue; if (!inode) continue;
if (kfds[i].events & POLLIN && !inode->will_block_if_read()) if (kfds[i].events & POLLIN)
{ {
fds_with_events++; if (inode->type() == VFS::InodeType::Socket)
kfds[i].revents |= POLLIN; {
auto socket = (Socket*)inode.ptr();
if (socket->can_read_data() || socket->can_accept_connections())
{
fds_with_events++;
kfds[i].revents |= POLLIN;
}
}
else
{
if (!inode->will_block_if_read())
{
fds_with_events++;
kfds[i].revents |= POLLIN;
}
}
} }
} }

View File

@ -16,7 +16,7 @@ template <typename T, usize Size> class CircularQueue
{ {
} }
bool is_empty() bool is_empty() const
{ {
return m_tail.load() == m_head.load(); return m_tail.load() == m_head.load();
} }
@ -76,7 +76,7 @@ template <typename T> class DynamicCircularQueue
if (m_data) free_impl(m_data); if (m_data) free_impl(m_data);
} }
bool is_empty() bool is_empty() const
{ {
return m_tail.load() == m_head.load(); return m_tail.load() == m_head.load();
} }