unix: respect MSG_DONTWAIT on sendmsg()

fix bug #18539

Change-Id: Id21362028287d1cbdac469226e6b52f4547a276f
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6796
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
This commit is contained in:
Jérôme Duval 2023-08-06 20:21:49 +02:00
parent 13e2db9609
commit 3487453788
10 changed files with 74 additions and 20 deletions

View File

@ -108,7 +108,8 @@ struct net_protocol_module_info {
ssize_t (*send_data_no_buffer)(net_protocol* self, const iovec* vecs,
size_t vecCount, ancillary_data_container* ancillaryData,
const struct sockaddr* address, socklen_t addressLength);
const struct sockaddr* address, socklen_t addressLength,
int flags);
ssize_t (*read_data_no_buffer)(net_protocol* self, const iovec* vecs,
size_t vecCount, ancillary_data_container** _ancillaryData,
struct sockaddr* _address, socklen_t* _addressLength);

View File

@ -210,17 +210,19 @@ UnixDatagramEndpoint::Accept(net_socket** _acceptedSocket)
ssize_t
UnixDatagramEndpoint::Send(const iovec* vecs, size_t vecCount,
ancillary_data_container* ancillaryData, const struct sockaddr* address,
socklen_t addressLength)
socklen_t addressLength, int flags)
{
TRACE("[%" B_PRId32 "] %p->UnixDatagramEndpoint::Send()\n",
find_thread(NULL), this);
bigtime_t timeout = absolute_timeout(socket->send.timeout);
if (gStackModule->is_restarted_syscall())
timeout = gStackModule->restore_syscall_restart_timeout();
else
gStackModule->store_syscall_restart_timeout(timeout);
bigtime_t timeout = 0;
if ((flags & MSG_DONTWAIT) == 0) {
timeout = absolute_timeout(socket->send.timeout);
if (gStackModule->is_restarted_syscall())
timeout = gStackModule->restore_syscall_restart_timeout();
else
gStackModule->store_syscall_restart_timeout(timeout);
}
UnixDatagramEndpointLocker endpointLocker(this);
if (fShutdownWrite)

View File

@ -35,7 +35,7 @@ public:
ssize_t Send(const iovec* vecs, size_t vecCount,
ancillary_data_container* ancillaryData,
const struct sockaddr* address,
socklen_t addressLength) override;
socklen_t addressLength, int flags) override;
ssize_t Receive(const iovec* vecs, size_t vecCount,
ancillary_data_container** _ancillaryData,
struct sockaddr* _address,

View File

@ -56,7 +56,7 @@ public:
virtual ssize_t Send(const iovec* vecs, size_t vecCount,
ancillary_data_container* ancillaryData,
const struct sockaddr* address,
socklen_t addressLength) = 0;
socklen_t addressLength, int flags) = 0;
virtual ssize_t Receive(const iovec* vecs, size_t vecCount,
ancillary_data_container** _ancillaryData,
struct sockaddr* _address, socklen_t* _addressLength) = 0;

View File

@ -374,16 +374,19 @@ UnixStreamEndpoint::Accept(net_socket** _acceptedSocket)
ssize_t
UnixStreamEndpoint::Send(const iovec* vecs, size_t vecCount,
ancillary_data_container* ancillaryData,
const struct sockaddr* address, socklen_t addressLength)
const struct sockaddr* address, socklen_t addressLength, int flags)
{
TRACE("[%" B_PRId32 "] %p->UnixStreamEndpoint::Send(%p, %ld, %p)\n",
find_thread(NULL), this, vecs, vecCount, ancillaryData);
bigtime_t timeout = absolute_timeout(socket->send.timeout);
if (gStackModule->is_restarted_syscall())
timeout = gStackModule->restore_syscall_restart_timeout();
else
gStackModule->store_syscall_restart_timeout(timeout);
bigtime_t timeout = 0;
if ((flags & MSG_DONTWAIT) == 0) {
timeout = absolute_timeout(socket->send.timeout);
if (gStackModule->is_restarted_syscall())
timeout = gStackModule->restore_syscall_restart_timeout();
else
gStackModule->store_syscall_restart_timeout(timeout);
}
UnixStreamEndpointLocker locker(this);

View File

@ -51,7 +51,7 @@ public:
ssize_t Send(const iovec* vecs, size_t vecCount,
ancillary_data_container* ancillaryData,
const struct sockaddr* address,
socklen_t addressLength) override;
socklen_t addressLength, int flags) override;
ssize_t Receive(const iovec* vecs, size_t vecCount,
ancillary_data_container** _ancillaryData,
struct sockaddr* _address,

View File

@ -408,10 +408,10 @@ unix_process_ancillary_data(net_protocol *self,
ssize_t
unix_send_data_no_buffer(net_protocol *_protocol, const iovec *vecs,
size_t vecCount, ancillary_data_container *ancillaryData,
const struct sockaddr *address, socklen_t addressLength)
const struct sockaddr *address, socklen_t addressLength, int flags)
{
return ((UnixEndpoint*)_protocol)->Send(vecs, vecCount, ancillaryData,
address, addressLength);
address, addressLength, flags);
}

View File

@ -1393,7 +1393,7 @@ socket_send(net_socket* socket, msghdr* header, const void* data, size_t length,
ssize_t written = socket->first_info->send_data_no_buffer(
socket->first_protocol, vecs, vecCount, ancillaryData, address,
addressLength);
addressLength, flags);
if (written > 0)
ancillaryDataDeleter.Detach();
return written;

View File

@ -19,6 +19,8 @@ SimpleTest if_nameindex : if_nameindex.c : $(TARGET_NETWORK_LIBS) ;
SimpleTest unix_dgram_test : unix_dgram_test.cpp : $(TARGET_NETWORK_LIBS) ;
SimpleTest unix_send_test : unix_send_test.c : $(TARGET_NETWORK_LIBS) ;
SimpleTest tcp_connection_test : tcp_connection_test.cpp
: $(TARGET_NETWORK_LIBS) ;

View File

@ -0,0 +1,46 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <stdbool.h>
int main() {
int fds[2];
int domain;
domain = AF_UNIX;
// domain = AF_INET;
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
};
// uncomment to allow send to timeout with ETIMEDOUT after 1 second
//ret = setsockopt(fds[0], SOL_SOCKET, SO_SNDTIMEO, &v, sizeof(v));
if(ret) {
perror("setsockopt");
}
size_t bufLen = 1024;
char *buf = calloc(bufLen, 1);
int ok = 0;
while(true) {
printf("send %i\n", ok);
ret = send(fds[0], &buf[0], bufLen, MSG_DONTWAIT);
// eventually: EAGAIN (on Linux and Haiku), ENOBUFS (on macOS)
if(ret < 0) {
perror("send");
break;
} else {
ok++;
}
}
return 0;
}