git: feab010f4414 - main - libfetch: Use monotonic clock for timeouts
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Fri, 06 Nov 2026 11:17:27 UTC
The branch main has been updated by des:
URL: https://cgit.FreeBSD.org/src/commit/?id=feab010f44143bda407438ede72f4b8e0c1bd15d
commit feab010f44143bda407438ede72f4b8e0c1bd15d
Author: Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-11-06 11:16:42 +0000
Commit: Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-11-06 11:16:42 +0000
libfetch: Use monotonic clock for timeouts
MFC after: 1 week
Reviewed by: imp
Differential Revision: https://reviews.freebsd.org/D61225
---
lib/libfetch/common.c | 86 +++++++++++++++++++++++----------------------------
1 file changed, 38 insertions(+), 48 deletions(-)
diff --git a/lib/libfetch/common.c b/lib/libfetch/common.c
index c15b22a41603..023c491aa852 100644
--- a/lib/libfetch/common.c
+++ b/lib/libfetch/common.c
@@ -578,18 +578,31 @@ fetch_socks5_getenv(char **host, int *port)
}
+/*
+ * Return current time in milliseconds since an arbitrary epoch.
+ */
+typedef unsigned long mstime_t;
+static mstime_t
+mstime_mono(void)
+{
+ struct timespec ts;
+
+ (void)clock_gettime(CLOCK_MONOTONIC, &ts);
+ return ((mstime_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
+}
+
/*
* Establish a TCP connection to the specified port on the specified host.
*/
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 deltams, err = 0, serrno, sd = -1;
+ mstime_t now, deadline, deltams;
+ int err = 0, serrno, sd = -1;
char *sockshost;
int socksport;
@@ -630,10 +643,8 @@ fetch_connect(const char *host, int port, int af, int verbose)
/* try each server address in turn */
for (err = 0, sai = sais; sai != NULL; sai = sai->ai_next) {
/* start the clock */
- if (fetchTimeout > 0) {
- gettimeofday(&timeout, NULL);
- timeout.tv_sec += fetchTimeout;
- }
+ if (fetchTimeout > 0)
+ deadline = mstime_mono() + fetchTimeout * 1000;
/* open socket */
if ((sd = socket(sai->ai_family, SOCK_STREAM, 0)) < 0) {
@@ -665,22 +676,18 @@ fetch_connect(const char *host, int port, int af, int verbose)
goto next;
/* wait for connection */
+ deltams = INFTIM;
+ pfd.fd = sd;
+ pfd.events = POLLOUT;
for (;;) {
- deltams = INFTIM;
- pfd.fd = sd;
- pfd.events = POLLOUT;
-
/* check the clock */
if (fetchTimeout > 0) {
- gettimeofday(&now, NULL);
- if (!timercmp(&timeout, &now, >)) {
+ if ((now = mstime_mono()) > deadline) {
errno = ETIMEDOUT;
err = -1;
goto next;
}
- timersub(&timeout, &now, &delta);
- deltams = delta.tv_sec * 1000 +
- delta.tv_usec / 1000;
+ deltams = deadline - now;
}
/* wait for something to happen */
if ((err = poll(&pfd, 1, deltams)) > 0)
@@ -1219,9 +1226,9 @@ int
fetch_ssl(conn_t *conn, const struct url *URL, int verbose)
{
#ifdef WITH_SSL
- struct timeval now, timeout, delta;
struct pollfd pfd;
- int deltams, ret, ssl_err;
+ mstime_t now, deadline, deltams;
+ int ret, ssl_err;
X509_NAME *name;
char *str;
@@ -1253,10 +1260,8 @@ fetch_ssl(conn_t *conn, const struct url *URL, int verbose)
return (-1);
}
#endif
- if (fetchTimeout > 0) {
- gettimeofday(&timeout, NULL);
- timeout.tv_sec += fetchTimeout;
- }
+ if (fetchTimeout > 0)
+ deadline = mstime_mono() + fetchTimeout * 1000;
deltams = INFTIM;
pfd.fd = conn->sd;
while ((ret = SSL_connect(conn->ssl)) == -1) {
@@ -1274,15 +1279,12 @@ fetch_ssl(conn_t *conn, const struct url *URL, int verbose)
}
/* check what's left of our timeout */
if (fetchTimeout > 0) {
- gettimeofday(&now, NULL);
- if (!timercmp(&timeout, &now, >)) {
+ if ((now = mstime_mono()) > deadline) {
errno = ETIMEDOUT;
fetch_syserr();
return (-1);
}
- timersub(&timeout, &now, &delta);
- deltams = delta.tv_sec * 1000 +
- delta.tv_usec / 1000;
+ deltams = deadline - now;
}
ret = poll(&pfd, 1, deltams);
if (ret == 0) {
@@ -1392,15 +1394,12 @@ fetch_ssl_write(SSL *ssl, void *buf, size_t len)
ssize_t
fetch_read(conn_t *conn, void *buf, size_t len)
{
- struct timeval now, timeout, delta;
struct pollfd pfd;
ssize_t rlen, total;
- int deltams;
+ mstime_t now, deadline, deltams;
- if (fetchTimeout > 0) {
- gettimeofday(&timeout, NULL);
- timeout.tv_sec += fetchTimeout;
- }
+ if (fetchTimeout > 0)
+ deadline = mstime_mono() + fetchTimeout * 1000;
deltams = INFTIM;
pfd.fd = conn->sd;
@@ -1453,15 +1452,12 @@ fetch_read(conn_t *conn, void *buf, size_t len)
}
/* check what's left of our timeout */
if (fetchTimeout > 0) {
- gettimeofday(&now, NULL);
- if (!timercmp(&timeout, &now, >)) {
+ if ((now = mstime_mono()) > deadline) {
errno = ETIMEDOUT;
fetch_syserr();
return (-1);
}
- timersub(&timeout, &now, &delta);
- deltams = delta.tv_sec * 1000 +
- delta.tv_usec / 1000;
+ deltams = deadline - now;
}
/* wait for the socket to become readable */
if (poll(&pfd, 1, deltams) < 0) {
@@ -1637,15 +1633,12 @@ fetch_write(conn_t *conn, const void *buf, size_t len)
ssize_t
fetch_writev(conn_t *conn, struct iovec *iov, int iovcnt)
{
- struct timeval now, timeout, delta;
struct pollfd pfd;
ssize_t wlen, total;
- int deltams;
+ mstime_t now, deadline, deltams;
- if (fetchTimeout > 0) {
- gettimeofday(&timeout, NULL);
- timeout.tv_sec += fetchTimeout;
- }
+ if (fetchTimeout > 0)
+ deadline = mstime_mono() + fetchTimeout * 1000;
deltams = INFTIM;
pfd.fd = conn->sd;
@@ -1701,15 +1694,12 @@ fetch_writev(conn_t *conn, struct iovec *iov, int iovcnt)
}
/* check what's left of our timeout */
if (fetchTimeout > 0) {
- gettimeofday(&now, NULL);
- if (!timercmp(&timeout, &now, >)) {
+ if ((now = mstime_mono()) > deadline) {
errno = ETIMEDOUT;
fetch_syserr();
return (-1);
}
- timersub(&timeout, &now, &delta);
- deltams = delta.tv_sec * 1000 +
- delta.tv_usec / 1000;
+ deltams = deadline - now;
}
/* wait for the socket to become writeable */
if (poll(&pfd, 1, deltams) < 0) {