Allow infinite timeout in helpers

PiperOrigin-RevId: 595462226
This commit is contained in:
Bruno Dal Bo
2024-01-03 11:52:03 -08:00
committed by gVisor bot
parent 1cccd35dcf
commit 9ea77eb8bc
+5 -3
View File
@@ -19,6 +19,7 @@
#include <poll.h>
#include <sys/socket.h>
#include <cstddef>
#include <functional>
#include <memory>
#include <stack>
@@ -766,11 +767,11 @@ PosixErrorOr<int> SendMsg(int sock, msghdr* msg, char buf[], int buf_size) {
PosixErrorOr<int> RecvTimeout(int sock, char buf[], int buf_size, int timeout) {
fd_set rfd;
struct timeval to = {.tv_sec = timeout, .tv_usec = 0};
struct timeval* to_ptr = timeout < 0 ? nullptr : &to;
FD_ZERO(&rfd);
FD_SET(sock, &rfd);
int ret;
RETURN_ERROR_IF_SYSCALL_FAIL(ret = select(1, &rfd, NULL, NULL, &to));
RETURN_ERROR_IF_SYSCALL_FAIL(ret = select(1, &rfd, NULL, NULL, to_ptr));
RETURN_ERROR_IF_SYSCALL_FAIL(
ret = RetryEINTR(recv)(sock, buf, buf_size, MSG_DONTWAIT));
return ret;
@@ -779,11 +780,12 @@ PosixErrorOr<int> RecvTimeout(int sock, char buf[], int buf_size, int timeout) {
PosixErrorOr<int> RecvMsgTimeout(int sock, struct msghdr* msg, int timeout) {
fd_set rfd;
struct timeval to = {.tv_sec = timeout, .tv_usec = 0};
struct timeval* to_ptr = timeout < 0 ? nullptr : &to;
FD_ZERO(&rfd);
FD_SET(sock, &rfd);
int ret;
RETURN_ERROR_IF_SYSCALL_FAIL(ret = select(1, &rfd, NULL, NULL, &to));
RETURN_ERROR_IF_SYSCALL_FAIL(ret = select(1, &rfd, NULL, NULL, to_ptr));
RETURN_ERROR_IF_SYSCALL_FAIL(
ret = RetryEINTR(recvmsg)(sock, msg, MSG_DONTWAIT));
return ret;