unix: respect MSG_DONTWAIT on recvmsg

fix #18548

Change-Id: I33f502c2a376be6dbdf913f9613aab9d4c5a3644
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6802
Reviewed-by: waddlesplash <[email protected]>
This commit is contained in:
Jérôme Duval
2023-08-09 17:10:28 +00:00
committed by waddlesplash
parent 9c8f4ac5d6
commit b761f9250a
10 changed files with 74 additions and 19 deletions
+2 -1
View File
@@ -112,7 +112,8 @@ struct net_protocol_module_info {
int flags); int flags);
ssize_t (*read_data_no_buffer)(net_protocol* self, const iovec* vecs, ssize_t (*read_data_no_buffer)(net_protocol* self, const iovec* vecs,
size_t vecCount, ancillary_data_container** _ancillaryData, size_t vecCount, ancillary_data_container** _ancillaryData,
struct sockaddr* _address, socklen_t* _addressLength); struct sockaddr* _address, socklen_t* _addressLength,
int flags);
}; };
@@ -320,16 +320,19 @@ UnixDatagramEndpoint::Send(const iovec* vecs, size_t vecCount,
ssize_t ssize_t
UnixDatagramEndpoint::Receive(const iovec* vecs, size_t vecCount, UnixDatagramEndpoint::Receive(const iovec* vecs, size_t vecCount,
ancillary_data_container** _ancillaryData, struct sockaddr* _address, ancillary_data_container** _ancillaryData, struct sockaddr* _address,
socklen_t* _addressLength) socklen_t* _addressLength, int flags)
{ {
TRACE("[%" B_PRId32 "] %p->UnixDatagramEndpoint::Receive()\n", TRACE("[%" B_PRId32 "] %p->UnixDatagramEndpoint::Receive()\n",
find_thread(NULL), this); find_thread(NULL), this);
bigtime_t timeout = absolute_timeout(socket->receive.timeout); bigtime_t timeout = 0;
if ((flags & MSG_DONTWAIT) == 0) {
timeout = absolute_timeout(socket->receive.timeout);
if (gStackModule->is_restarted_syscall()) if (gStackModule->is_restarted_syscall())
timeout = gStackModule->restore_syscall_restart_timeout(); timeout = gStackModule->restore_syscall_restart_timeout();
else else
gStackModule->store_syscall_restart_timeout(timeout); gStackModule->store_syscall_restart_timeout(timeout);
}
UnixDatagramEndpointLocker endpointLocker(this); UnixDatagramEndpointLocker endpointLocker(this);
@@ -39,7 +39,7 @@ public:
ssize_t Receive(const iovec* vecs, size_t vecCount, ssize_t Receive(const iovec* vecs, size_t vecCount,
ancillary_data_container** _ancillaryData, ancillary_data_container** _ancillaryData,
struct sockaddr* _address, struct sockaddr* _address,
socklen_t* _addressLength) override; socklen_t* _addressLength, int flags) override;
ssize_t Sendable() override; ssize_t Sendable() override;
ssize_t Receivable() override; ssize_t Receivable() override;
@@ -59,7 +59,8 @@ public:
socklen_t addressLength, int flags) = 0; socklen_t addressLength, int flags) = 0;
virtual ssize_t Receive(const iovec* vecs, size_t vecCount, virtual ssize_t Receive(const iovec* vecs, size_t vecCount,
ancillary_data_container** _ancillaryData, ancillary_data_container** _ancillaryData,
struct sockaddr* _address, socklen_t* _addressLength) = 0; struct sockaddr* _address, socklen_t* _addressLength,
int flags) = 0;
virtual ssize_t Sendable() = 0; virtual ssize_t Sendable() = 0;
virtual ssize_t Receivable() = 0; virtual ssize_t Receivable() = 0;
@@ -468,16 +468,19 @@ UnixStreamEndpoint::Send(const iovec* vecs, size_t vecCount,
ssize_t ssize_t
UnixStreamEndpoint::Receive(const iovec* vecs, size_t vecCount, UnixStreamEndpoint::Receive(const iovec* vecs, size_t vecCount,
ancillary_data_container** _ancillaryData, struct sockaddr* _address, ancillary_data_container** _ancillaryData, struct sockaddr* _address,
socklen_t* _addressLength) socklen_t* _addressLength, int flags)
{ {
TRACE("[%" B_PRId32 "] %p->UnixStreamEndpoint::Receive(%p, %ld)\n", TRACE("[%" B_PRId32 "] %p->UnixStreamEndpoint::Receive(%p, %ld)\n",
find_thread(NULL), this, vecs, vecCount); find_thread(NULL), this, vecs, vecCount);
bigtime_t timeout = absolute_timeout(socket->receive.timeout); bigtime_t timeout = 0;
if ((flags & MSG_DONTWAIT) == 0) {
timeout = absolute_timeout(socket->receive.timeout);
if (gStackModule->is_restarted_syscall()) if (gStackModule->is_restarted_syscall())
timeout = gStackModule->restore_syscall_restart_timeout(); timeout = gStackModule->restore_syscall_restart_timeout();
else else
gStackModule->store_syscall_restart_timeout(timeout); gStackModule->store_syscall_restart_timeout(timeout);
}
UnixStreamEndpointLocker locker(this); UnixStreamEndpointLocker locker(this);
@@ -55,7 +55,7 @@ public:
ssize_t Receive(const iovec* vecs, size_t vecCount, ssize_t Receive(const iovec* vecs, size_t vecCount,
ancillary_data_container** _ancillaryData, ancillary_data_container** _ancillaryData,
struct sockaddr* _address, struct sockaddr* _address,
socklen_t* _addressLength) override; socklen_t* _addressLength, int flags) override;
ssize_t Sendable() override; ssize_t Sendable() override;
ssize_t Receivable() override; ssize_t Receivable() override;
@@ -418,10 +418,10 @@ unix_send_data_no_buffer(net_protocol *_protocol, const iovec *vecs,
ssize_t ssize_t
unix_read_data_no_buffer(net_protocol *_protocol, const iovec *vecs, unix_read_data_no_buffer(net_protocol *_protocol, const iovec *vecs,
size_t vecCount, ancillary_data_container **_ancillaryData, size_t vecCount, ancillary_data_container **_ancillaryData,
struct sockaddr *_address, socklen_t *_addressLength) struct sockaddr *_address, socklen_t *_addressLength, int flags)
{ {
return ((UnixEndpoint*)_protocol)->Receive(vecs, vecCount, _ancillaryData, return ((UnixEndpoint*)_protocol)->Receive(vecs, vecCount, _ancillaryData,
_address, _addressLength); _address, _addressLength, flags);
} }
@@ -302,7 +302,7 @@ socket_receive_no_buffer(net_socket* socket, msghdr* header, void* data,
ancillary_data_container* ancillaryData = NULL; ancillary_data_container* ancillaryData = NULL;
ssize_t bytesRead = socket->first_info->read_data_no_buffer( ssize_t bytesRead = socket->first_info->read_data_no_buffer(
socket->first_protocol, vecs, vecCount, &ancillaryData, address, socket->first_protocol, vecs, vecCount, &ancillaryData, address,
addressLen); addressLen, flags);
if (bytesRead < 0) if (bytesRead < 0)
return bytesRead; return bytesRead;
+1
View File
@@ -19,6 +19,7 @@ SimpleTest if_nameindex : if_nameindex.c : $(TARGET_NETWORK_LIBS) ;
SimpleTest unix_dgram_test : unix_dgram_test.cpp : $(TARGET_NETWORK_LIBS) ; SimpleTest unix_dgram_test : unix_dgram_test.cpp : $(TARGET_NETWORK_LIBS) ;
SimpleTest unix_recv_test : unix_recv_test.c : $(TARGET_NETWORK_LIBS) ;
SimpleTest unix_send_test : unix_send_test.c : $(TARGET_NETWORK_LIBS) ; SimpleTest unix_send_test : unix_send_test.c : $(TARGET_NETWORK_LIBS) ;
SimpleTest tcp_connection_test : tcp_connection_test.cpp SimpleTest tcp_connection_test : tcp_connection_test.cpp
+46
View File
@@ -0,0 +1,46 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <stdbool.h>
int main() {
int fds[2];
int domain;
domain = AF_UNIX;
// domain = AF_INET; // works
printf("Domain: %i\n", domain);
int ret = socketpair(domain, SOCK_DGRAM, 0, fds); // try also: SOCK_STREAM
if(ret) {
perror("Could not get socketpair");
return 1;
}
/*
struct timeval v = {
.tv_sec = 1,
.tv_usec = 0
};
ret = setsockopt(fds[0], SOL_SOCKET, SO_RCVTIMEO, &v, sizeof(v));
if(ret) {
perror("setsockopt");
}
*/
size_t bufLen = 1024;
char *buf = calloc(bufLen, 1);
int ok = 0;
while(true) {
printf("recv %i\n", ok);
ret = recv(fds[0], &buf[0], bufLen, MSG_DONTWAIT);
// expected: EWOULDBLOCK/EAGAIN (on Linux, macOS, Haiku)
printf("%i\n", ret);
if(ret < 0) {
perror("recv");
break;
} else {
ok++;
}
}
return 0;
}