Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 20 Aug 2024 05:02:12 GMT
From:      Colin Percival <cperciva@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: d854b4b2aa2b - stable/13 - dhclient: Use clock_gettime() instead of time()
Message-ID:  <202408200502.47K52CL7072152@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by cperciva:

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

commit d854b4b2aa2b8b60e7fbca07e9637d035318aad5
Author:     Isaac Cilia Attard <icattard@FreeBSD.org>
AuthorDate: 2024-07-08 06:23:00 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2024-08-20 04:58:35 +0000

    dhclient: Use clock_gettime() instead of time()
    
    Change the use of time() to clock_gettime() to have millisecond-accurate
    rather than second-accurate timeouts.
    
    Sponsored by:   Google LLC (GSoC 2024)
    Signed-off-by:  Isaac Cilia Attard <icattard@FreeBSD.org>
    MFC after:      10 days
    Reviwed by:     cperciva, brooks, Tom Hukins, Alexander Ziaee
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/1368
    
    (cherry picked from commit f0a38976b01e15956fdba48f8b58db22d0af1f7e)
---
 sbin/dhclient/dhclient.c |  3 ++-
 sbin/dhclient/dispatch.c | 18 +++++++++++-------
 2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/sbin/dhclient/dhclient.c b/sbin/dhclient/dhclient.c
index 1a39ab1517c2..27e027646ef6 100644
--- a/sbin/dhclient/dhclient.c
+++ b/sbin/dhclient/dhclient.c
@@ -446,7 +446,8 @@ main(int argc, char *argv[])
 		log_perror = 0;
 
 	tzset();
-	time(&cur_time);
+	clock_gettime(CLOCK_MONOTONIC, &time_now);
+	cur_time = time_now.tv_sec;
 
 	inaddr_broadcast.s_addr = INADDR_BROADCAST;
 	inaddr_any.s_addr = INADDR_ANY;
diff --git a/sbin/dhclient/dispatch.c b/sbin/dhclient/dispatch.c
index e6b06d8616c8..78d94804468b 100644
--- a/sbin/dhclient/dispatch.c
+++ b/sbin/dhclient/dispatch.c
@@ -56,6 +56,10 @@
 #define assert_aligned(p, align) assert((((uintptr_t)p) & ((align) - 1)) == 0)
 
 static struct protocol *protocols;
+static const struct timespec timespec_intmax_ms = {
+	.tv_sec = INT_MAX / 1000,
+	.tv_nsec = (INT_MAX % 1000) * 1000000
+};
 static struct timeout *timeouts;
 static struct timeout *free_timeouts;
 static int interfaces_invalidated;
@@ -202,9 +206,9 @@ another:
 			 * negative timeout and blocking indefinitely.
 			 */
 			timespecsub(&timeouts->when, &time_now, &howlong);
-			if (howlong.tv_sec > INT_MAX / 1000)
-				howlong.tv_sec = INT_MAX / 1000;
-			to_msec = howlong.tv_sec * 1000;
+			if (timespeccmp(&howlong, &timespec_intmax_ms, >))
+				howlong = timespec_intmax_ms;
+			to_msec = howlong.tv_sec * 1000 + howlong.tv_nsec / 1000000;
 		} else
 			to_msec = -1;
 
@@ -231,16 +235,16 @@ another:
 		/* Not likely to be transitory... */
 		if (count == -1) {
 			if (errno == EAGAIN || errno == EINTR) {
-				time(&cur_time);
-				time_now.tv_sec = cur_time;
+				clock_gettime(CLOCK_MONOTONIC, &time_now);
+				cur_time = time_now.tv_sec;
 				continue;
 			} else
 				error("poll: %m");
 		}
 
 		/* Get the current time... */
-		time(&cur_time);
-		time_now.tv_sec = cur_time;
+		clock_gettime(CLOCK_MONOTONIC, &time_now);
+		cur_time = time_now.tv_sec;
 
 		i = 0;
 		for (l = protocols; l; l = l->next) {



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202408200502.47K52CL7072152>