From 8fc15040e7fb6fb5cbef64952162db216ebdc2a2 Mon Sep 17 00:00:00 2001 From: Shawn Webb Date: Tue, 24 Dec 2013 14:44:58 +0000 Subject: [PATCH] Use a select-based timeout when receiving data back from the stats server --- libclamav/www.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/libclamav/www.c b/libclamav/www.c index cd93f2aae..bc3ba9146 100644 --- a/libclamav/www.c +++ b/libclamav/www.c @@ -59,6 +59,7 @@ int connect_host(const char *host, const char *port, int useAsync) FD_ZERO(&write_fds); FD_SET(sockfd, &write_fds); + /* TODO: Make this timeout configurable */ tv.tv_sec = 10; tv.tv_usec = 0; @@ -130,6 +131,8 @@ void submit_post(const char *host, const char *port, const char *method, const c char *buf, *encoded=NULL; size_t bufsz ; char chunkedlen[21]; + fd_set readfds; + struct timeval tv; char *acceptable_methods[] = { "GET", "PUT", @@ -197,25 +200,33 @@ void submit_post(const char *host, const char *port, const char *method, const c } while (1) { + FD_ZERO(&readfds); + FD_SET(sockfd, &readfds); + /* * Check to make sure the stats submitted okay (so that we don't kill the HTTP request * while it's being processed). - * - * TODO: Add a time limit based on a call to select() to prevent lock-ups or major - * slow downs. */ - memset(buf, 0x00, bufsz); - if (recv(sockfd, buf, bufsz, 0) <= 0) { - if (errno == EAGAIN) - continue; - - cli_warnmsg("Could not receive data back from stats server. Errno: %d\n", errno); - perror("recv"); + tv.tv_sec = 10; + tv.tv_usec = 0; + if (select(sockfd+1, &readfds, NULL, NULL, &tv) <= 0) { break; } - if (strstr(buf, "STATOK")) - break; + if (FD_ISSET(sockfd, &readfds)) { + memset(buf, 0x00, bufsz); + if (recv(sockfd, buf, bufsz, 0) <= 0) { + if (errno == EAGAIN) + continue; + + cli_warnmsg("Could not receive data back from stats server. Errno: %d\n", errno); + perror("recv"); + break; + } + + if (strstr(buf, "STATOK")) + break; + } } close(sockfd);