Kernel: Fix non blocking sockets blocking :D

This commit is contained in:
Bananymous 2026-02-08 19:33:28 +02:00
parent a51a81b6cd
commit 2e59373a1e
6 changed files with 27 additions and 32 deletions

View File

@ -6,7 +6,6 @@
#include <kernel/FS/Socket.h> #include <kernel/FS/Socket.h>
#include <kernel/FS/TmpFS/Inode.h> #include <kernel/FS/TmpFS/Inode.h>
#include <kernel/FS/VirtualFileSystem.h> #include <kernel/FS/VirtualFileSystem.h>
#include <kernel/Lock/SpinLock.h>
#include <kernel/OpenFileDescriptorSet.h> #include <kernel/OpenFileDescriptorSet.h>
namespace Kernel namespace Kernel
@ -73,7 +72,7 @@ namespace Kernel
BAN::Optional<struct ucred> ucred; BAN::Optional<struct ucred> ucred;
}; };
BAN::ErrorOr<void> add_packet(const msghdr&, PacketInfo&&); BAN::ErrorOr<size_t> add_packet(const msghdr&, PacketInfo&&, bool dont_block);
private: private:
const Socket::Type m_socket_type; const Socket::Type m_socket_type;

View File

@ -245,10 +245,7 @@ namespace Kernel
BAN::ErrorOr<size_t> TCPSocket::sendmsg_impl(const msghdr& message, int flags) BAN::ErrorOr<size_t> TCPSocket::sendmsg_impl(const msghdr& message, int flags)
{ {
if (flags & MSG_NOSIGNAL) if (flags & ~(MSG_NOSIGNAL | MSG_DONTWAIT))
dwarnln("sendmsg ignoring MSG_NOSIGNAL");
flags &= (MSG_EOR | MSG_OOB /* | MSG_NOSIGNAL */);
if (flags != 0)
{ {
dwarnln("TODO: sendmsg with flags 0x{H}", flags); dwarnln("TODO: sendmsg with flags 0x{H}", flags);
return BAN::Error::from_errno(ENOTSUP); return BAN::Error::from_errno(ENOTSUP);
@ -264,6 +261,8 @@ namespace Kernel
{ {
if (m_state != State::Established) if (m_state != State::Established)
return return_with_maybe_zero(); return return_with_maybe_zero();
if (flags & MSG_DONTWAIT)
return BAN::Error::from_errno(EAGAIN);
TRY(Thread::current().block_or_eintr_indefinite(m_thread_blocker, &m_mutex)); TRY(Thread::current().block_or_eintr_indefinite(m_thread_blocker, &m_mutex));
} }

View File

@ -162,10 +162,7 @@ namespace Kernel
BAN::ErrorOr<size_t> UDPSocket::sendmsg_impl(const msghdr& message, int flags) BAN::ErrorOr<size_t> UDPSocket::sendmsg_impl(const msghdr& message, int flags)
{ {
if (flags & MSG_NOSIGNAL) if (flags & ~(MSG_NOSIGNAL | MSG_DONTWAIT))
dwarnln("sendmsg ignoring MSG_NOSIGNAL");
flags &= (MSG_EOR | MSG_OOB /* | MSG_NOSIGNAL */);
if (flags != 0)
{ {
dwarnln("TODO: sendmsg with flags 0x{H}", flags); dwarnln("TODO: sendmsg with flags 0x{H}", flags);
return BAN::Error::from_errno(ENOTSUP); return BAN::Error::from_errno(ENOTSUP);

View File

@ -289,17 +289,19 @@ namespace Kernel
case Socket::Type::SEQPACKET: case Socket::Type::SEQPACKET:
case Socket::Type::DGRAM: case Socket::Type::DGRAM:
return false; return false;
default:
ASSERT_NOT_REACHED();
} }
} }
BAN::ErrorOr<void> UnixDomainSocket::add_packet(const msghdr& packet, PacketInfo&& packet_info) BAN::ErrorOr<size_t> UnixDomainSocket::add_packet(const msghdr& packet, PacketInfo&& packet_info, bool dont_block)
{ {
LockGuard _(m_packet_lock); LockGuard _(m_packet_lock);
while (m_packet_infos.full() || m_packet_size_total + packet_info.size > s_packet_buffer_size) while (m_packet_infos.full() || m_packet_size_total + packet_info.size > s_packet_buffer_size)
{
if (dont_block)
return BAN::Error::from_errno(EAGAIN);
TRY(Thread::current().block_or_eintr_indefinite(m_packet_thread_blocker, &m_packet_lock)); TRY(Thread::current().block_or_eintr_indefinite(m_packet_thread_blocker, &m_packet_lock));
}
uint8_t* packet_buffer = reinterpret_cast<uint8_t*>(m_packet_buffer->vaddr() + m_packet_size_total); uint8_t* packet_buffer = reinterpret_cast<uint8_t*>(m_packet_buffer->vaddr() + m_packet_size_total);
@ -512,10 +514,7 @@ namespace Kernel
BAN::ErrorOr<size_t> UnixDomainSocket::sendmsg_impl(const msghdr& message, int flags) BAN::ErrorOr<size_t> UnixDomainSocket::sendmsg_impl(const msghdr& message, int flags)
{ {
if (flags & MSG_NOSIGNAL) if (flags & ~(MSG_NOSIGNAL | MSG_DONTWAIT))
dwarnln("sendmsg ignoring MSG_NOSIGNAL");
flags &= (MSG_EOR | MSG_OOB /* | MSG_NOSIGNAL */);
if (flags != 0)
{ {
dwarnln("TODO: sendmsg with flags 0x{H}", flags); dwarnln("TODO: sendmsg with flags 0x{H}", flags);
return BAN::Error::from_errno(ENOTSUP); return BAN::Error::from_errno(ENOTSUP);
@ -608,7 +607,7 @@ namespace Kernel
auto target = connection_info.connection.lock(); auto target = connection_info.connection.lock();
if (!target) if (!target)
return BAN::Error::from_errno(ENOTCONN); return BAN::Error::from_errno(ENOTCONN);
TRY(target->add_packet(message, BAN::move(packet_info))); TRY(target->add_packet(message, BAN::move(packet_info), flags & MSG_DONTWAIT));
return total_message_size; return total_message_size;
} }
else else
@ -652,8 +651,7 @@ namespace Kernel
if (!target) if (!target)
return BAN::Error::from_errno(EDESTADDRREQ); return BAN::Error::from_errno(EDESTADDRREQ);
TRY(target->add_packet(message, BAN::move(packet_info))); TRY(target->add_packet(message, BAN::move(packet_info), flags & MSG_DONTWAIT));
return total_message_size; return total_message_size;
} }
} }

View File

@ -795,7 +795,7 @@ namespace Kernel
LockGuard _(inode->m_mutex); LockGuard _(inode->m_mutex);
if (is_nonblock && !inode->can_read()) if (is_nonblock && !inode->can_read())
return BAN::Error::from_errno(EWOULDBLOCK); return BAN::Error::from_errno(EAGAIN);
return inode->recvmsg(message, flags); return inode->recvmsg(message, flags);
} }
@ -817,12 +817,13 @@ namespace Kernel
LockGuard _(inode->m_mutex); LockGuard _(inode->m_mutex);
if (inode->has_hungup()) if (inode->has_hungup())
{ {
Thread::current().add_signal(SIGPIPE, {}); if (!(flags & MSG_NOSIGNAL))
Thread::current().add_signal(SIGPIPE, {});
return BAN::Error::from_errno(EPIPE); return BAN::Error::from_errno(EPIPE);
} }
if (is_nonblock && !inode->can_write()) if (is_nonblock && !inode->can_write())
return BAN::Error::from_errno(EWOULDBLOCK); return BAN::Error::from_errno(EAGAIN);
return inode->sendmsg(message, flags); return inode->sendmsg(message, flags | (is_nonblock ? MSG_DONTWAIT : 0));
} }
BAN::ErrorOr<VirtualFileSystem::File> OpenFileDescriptorSet::file_of(int fd) const BAN::ErrorOr<VirtualFileSystem::File> OpenFileDescriptorSet::file_of(int fd) const

View File

@ -123,14 +123,15 @@ struct linger
#define SOMAXCONN 4096 #define SOMAXCONN 4096
#define MSG_CTRUNC 0x01 #define MSG_CTRUNC 0x001
#define MSG_DONTROUTE 0x02 #define MSG_DONTROUTE 0x002
#define MSG_EOR 0x04 #define MSG_EOR 0x004
#define MSG_OOB 0x08 #define MSG_OOB 0x008
#define MSG_NOSIGNAL 0x10 #define MSG_NOSIGNAL 0x010
#define MSG_PEEK 0x20 #define MSG_PEEK 0x020
#define MSG_TRUNC 0x40 #define MSG_TRUNC 0x040
#define MSG_WAITALL 0x80 #define MSG_WAITALL 0x080
#define MSG_DONTWAIT 0x100
#define AF_UNSPEC 0 #define AF_UNSPEC 0
#define AF_INET 1 #define AF_INET 1