Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F164572885
D57909.id180823.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
D57909.id180823.diff
View Options
diff --git a/lib/libfetch/common.c b/lib/libfetch/common.c
--- a/lib/libfetch/common.c
+++ b/lib/libfetch/common.c
@@ -593,10 +593,13 @@
conn_t *
fetch_connect(const char *host, int port, int af, int verbose)
{
+ struct timeval now, timeout, delta;
+ struct pollfd pfd;
struct addrinfo *cais = NULL, *sais = NULL, *cai, *sai;
const char *bindaddr;
conn_t *conn = NULL;
int err = 0, sd = -1;
+ int deltams;
char *sockshost;
int socksport;
@@ -654,15 +657,47 @@
fetch_verbose("failed to bind to %s", bindaddr);
goto syserr;
}
+ /* make the socket non-blocking */
+ (void)fcntl(sd, F_SETFL, O_NONBLOCK);
+ /* start the clock */
+ if (fetchTimeout > 0) {
+ gettimeofday(&timeout, NULL);
+ timeout.tv_sec += fetchTimeout;
+ deltams = fetchTimeout * 1000;
+ }
/* attempt to connect to server address */
- while ((err = connect(sd, sai->ai_addr, sai->ai_addrlen)) < 0) {
- if (errno == EINTR && fetchRestartCalls)
- continue;
+ if ((err = connect(sd, sai->ai_addr, sai->ai_addrlen)) == 0)
break;
+ /* wait for connection */
+ if (errno == EINPROGRESS) {
+ deltams = INFTIM;
+ pfd.fd = sd;
+ pfd.events = POLLOUT;
+ for (;;) {
+ /* wait for something to happen */
+ if (poll(&pfd, 1, deltams) >= 0)
+ break;
+ if (errno == EINTR && !fetchRestartCalls)
+ break;
+ /* check the clock */
+ if (fetchTimeout > 0) {
+ gettimeofday(&now, NULL);
+ if (!timercmp(&timeout, &now, >)) {
+ errno = ETIMEDOUT;
+ pfd.revents = POLLERR;
+ break;
+ }
+ timersub(&timeout, &now, &delta);
+ deltams = delta.tv_sec * 1000 +
+ delta.tv_usec / 1000;
+ }
+ }
+ if (pfd.revents == POLLOUT) {
+ /* connection established */
+ err = 0;
+ break;
+ }
}
- /* success? */
- if (err == 0)
- break;
/* clean up before next attempt */
close(sd);
sd = -1;
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Aug 3, 4:02 AM (10 h, 39 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
34438222
Default Alt Text
D57909.id180823.diff (1 KB)
Attached To
Mode
D57909: libfetch: Apply timeout to connection attempts
Attached
Detach File
Event Timeline
Log In to Comment