git: 1fb5bcdfa288 - stable/14 - libfetch: Use monotonic clock for timeouts

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Sun, 15 Nov 2026 17:28:47 UTC
The branch stable/14 has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=1fb5bcdfa288da0b8905458931613b812fce8d40

commit 1fb5bcdfa288da0b8905458931613b812fce8d40
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-14 15:49:24 +0000

    libfetch: Use monotonic clock for timeouts
    
    MFC after:      1 week
    Reviewed by:    imp
    Differential Revision:  https://reviews.freebsd.org/D61225
    
    (cherry picked from commit feab010f44143bda407438ede72f4b8e0c1bd15d)
---
 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 5ebaee23e084..a68b9fe67fa6 100644
--- a/lib/libfetch/common.c
+++ b/lib/libfetch/common.c
@@ -579,18 +579,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;
 
@@ -631,10 +644,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) {
@@ -666,22 +677,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)
@@ -1220,9 +1227,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;
 
@@ -1254,10 +1261,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) {
@@ -1275,15 +1280,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) {
@@ -1393,15 +1395,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;
@@ -1454,15 +1453,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) {
@@ -1638,15 +1634,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;
@@ -1702,15 +1695,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) {