diff --git a/kernel/include/kernel/Networking/UNIX/Socket.h b/kernel/include/kernel/Networking/UNIX/Socket.h index efd97c2c..4282c856 100644 --- a/kernel/include/kernel/Networking/UNIX/Socket.h +++ b/kernel/include/kernel/Networking/UNIX/Socket.h @@ -6,7 +6,6 @@ #include #include #include -#include #include namespace Kernel @@ -73,7 +72,7 @@ namespace Kernel BAN::Optional ucred; }; - BAN::ErrorOr add_packet(const msghdr&, PacketInfo&&); + BAN::ErrorOr add_packet(const msghdr&, PacketInfo&&, bool dont_block); private: const Socket::Type m_socket_type; diff --git a/kernel/kernel/Networking/TCPSocket.cpp b/kernel/kernel/Networking/TCPSocket.cpp index 0a81522a..2f3b0d24 100644 --- a/kernel/kernel/Networking/TCPSocket.cpp +++ b/kernel/kernel/Networking/TCPSocket.cpp @@ -245,10 +245,7 @@ namespace Kernel BAN::ErrorOr TCPSocket::sendmsg_impl(const msghdr& message, int flags) { - if (flags & MSG_NOSIGNAL) - dwarnln("sendmsg ignoring MSG_NOSIGNAL"); - flags &= (MSG_EOR | MSG_OOB /* | MSG_NOSIGNAL */); - if (flags != 0) + if (flags & ~(MSG_NOSIGNAL | MSG_DONTWAIT)) { dwarnln("TODO: sendmsg with flags 0x{H}", flags); return BAN::Error::from_errno(ENOTSUP); @@ -264,6 +261,8 @@ namespace Kernel { if (m_state != State::Established) 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)); } diff --git a/kernel/kernel/Networking/UDPSocket.cpp b/kernel/kernel/Networking/UDPSocket.cpp index 9d452fa8..257a27ef 100644 --- a/kernel/kernel/Networking/UDPSocket.cpp +++ b/kernel/kernel/Networking/UDPSocket.cpp @@ -162,10 +162,7 @@ namespace Kernel BAN::ErrorOr UDPSocket::sendmsg_impl(const msghdr& message, int flags) { - if (flags & MSG_NOSIGNAL) - dwarnln("sendmsg ignoring MSG_NOSIGNAL"); - flags &= (MSG_EOR | MSG_OOB /* | MSG_NOSIGNAL */); - if (flags != 0) + if (flags & ~(MSG_NOSIGNAL | MSG_DONTWAIT)) { dwarnln("TODO: sendmsg with flags 0x{H}", flags); return BAN::Error::from_errno(ENOTSUP); diff --git a/kernel/kernel/Networking/UNIX/Socket.cpp b/kernel/kernel/Networking/UNIX/Socket.cpp index ae492498..4fdb4685 100644 --- a/kernel/kernel/Networking/UNIX/Socket.cpp +++ b/kernel/kernel/Networking/UNIX/Socket.cpp @@ -289,17 +289,19 @@ namespace Kernel case Socket::Type::SEQPACKET: case Socket::Type::DGRAM: return false; - default: - ASSERT_NOT_REACHED(); } } - BAN::ErrorOr UnixDomainSocket::add_packet(const msghdr& packet, PacketInfo&& packet_info) + BAN::ErrorOr UnixDomainSocket::add_packet(const msghdr& packet, PacketInfo&& packet_info, bool dont_block) { LockGuard _(m_packet_lock); 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)); + } uint8_t* packet_buffer = reinterpret_cast(m_packet_buffer->vaddr() + m_packet_size_total); @@ -512,10 +514,7 @@ namespace Kernel BAN::ErrorOr UnixDomainSocket::sendmsg_impl(const msghdr& message, int flags) { - if (flags & MSG_NOSIGNAL) - dwarnln("sendmsg ignoring MSG_NOSIGNAL"); - flags &= (MSG_EOR | MSG_OOB /* | MSG_NOSIGNAL */); - if (flags != 0) + if (flags & ~(MSG_NOSIGNAL | MSG_DONTWAIT)) { dwarnln("TODO: sendmsg with flags 0x{H}", flags); return BAN::Error::from_errno(ENOTSUP); @@ -608,7 +607,7 @@ namespace Kernel auto target = connection_info.connection.lock(); if (!target) 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; } else @@ -652,8 +651,7 @@ namespace Kernel if (!target) 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; } } diff --git a/kernel/kernel/OpenFileDescriptorSet.cpp b/kernel/kernel/OpenFileDescriptorSet.cpp index 9e5ea95a..975ee7a4 100644 --- a/kernel/kernel/OpenFileDescriptorSet.cpp +++ b/kernel/kernel/OpenFileDescriptorSet.cpp @@ -795,7 +795,7 @@ namespace Kernel LockGuard _(inode->m_mutex); if (is_nonblock && !inode->can_read()) - return BAN::Error::from_errno(EWOULDBLOCK); + return BAN::Error::from_errno(EAGAIN); return inode->recvmsg(message, flags); } @@ -817,12 +817,13 @@ namespace Kernel LockGuard _(inode->m_mutex); if (inode->has_hungup()) { - Thread::current().add_signal(SIGPIPE, {}); + if (!(flags & MSG_NOSIGNAL)) + Thread::current().add_signal(SIGPIPE, {}); return BAN::Error::from_errno(EPIPE); } if (is_nonblock && !inode->can_write()) - return BAN::Error::from_errno(EWOULDBLOCK); - return inode->sendmsg(message, flags); + return BAN::Error::from_errno(EAGAIN); + return inode->sendmsg(message, flags | (is_nonblock ? MSG_DONTWAIT : 0)); } BAN::ErrorOr OpenFileDescriptorSet::file_of(int fd) const diff --git a/userspace/libraries/LibC/include/sys/socket.h b/userspace/libraries/LibC/include/sys/socket.h index cb8be748..fa1ce66e 100644 --- a/userspace/libraries/LibC/include/sys/socket.h +++ b/userspace/libraries/LibC/include/sys/socket.h @@ -123,14 +123,15 @@ struct linger #define SOMAXCONN 4096 -#define MSG_CTRUNC 0x01 -#define MSG_DONTROUTE 0x02 -#define MSG_EOR 0x04 -#define MSG_OOB 0x08 -#define MSG_NOSIGNAL 0x10 -#define MSG_PEEK 0x20 -#define MSG_TRUNC 0x40 -#define MSG_WAITALL 0x80 +#define MSG_CTRUNC 0x001 +#define MSG_DONTROUTE 0x002 +#define MSG_EOR 0x004 +#define MSG_OOB 0x008 +#define MSG_NOSIGNAL 0x010 +#define MSG_PEEK 0x020 +#define MSG_TRUNC 0x040 +#define MSG_WAITALL 0x080 +#define MSG_DONTWAIT 0x100 #define AF_UNSPEC 0 #define AF_INET 1