Skip to content

Commit

Permalink
dhclient: Use clock_gettime() instead of time()
Browse files Browse the repository at this point in the history
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:	#1368
  • Loading branch information
ica574 authored and cperciva committed Aug 9, 2024
1 parent 76e0ffd commit f0a3897
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
3 changes: 2 additions & 1 deletion sbin/dhclient/dhclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 11 additions & 7 deletions sbin/dhclient/dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -190,9 +194,9 @@ dispatch(void)
* 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;

Expand All @@ -219,16 +223,16 @@ dispatch(void)
/* 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) {
Expand Down

0 comments on commit f0a3897

Please sign in to comment.