From 63b616dc2ecced7bc35f4d7ad2a24b8de69c4333 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 22 Aug 2024 14:15:15 +0300 Subject: [PATCH] Kernel: Hack non-blocking support for sockets This is not thread safe and can still block if two programs refering to the same socket try to read data at the same time --- kernel/kernel/Process.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index ad6f5d56..af9e8a28 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -1154,7 +1154,6 @@ namespace Kernel return 0; } - dprintln("SYS_GETSOCKOPT {}, {}, {}, {}, {}", socket, level, option_name, option_value, option_len); return BAN::Error::from_errno(ENOTSUP); } @@ -1243,6 +1242,10 @@ namespace Kernel if (!inode->mode().ifsock()) return BAN::Error::from_errno(ENOTSOCK); + auto flags = TRY(m_open_file_descriptors.flags_of(arguments->socket)); + if ((flags & O_NONBLOCK) && !inode->can_write()) + return BAN::Error::from_errno(EAGAIN); + BAN::ConstByteSpan message { reinterpret_cast(arguments->message), arguments->length }; return TRY(inode->sendto(message, arguments->dest_addr, arguments->dest_len)); } @@ -1267,6 +1270,10 @@ namespace Kernel if (!inode->mode().ifsock()) return BAN::Error::from_errno(ENOTSOCK); + auto flags = TRY(m_open_file_descriptors.flags_of(arguments->socket)); + if ((flags & O_NONBLOCK) && !inode->can_read()) + return BAN::Error::from_errno(EAGAIN); + BAN::ByteSpan buffer { reinterpret_cast(arguments->buffer), arguments->length }; return TRY(inode->recvfrom(buffer, arguments->address, arguments->address_len)); }