diff --git a/sbin/dhclient/dhclient.c b/sbin/dhclient/dhclient.c --- a/sbin/dhclient/dhclient.c +++ b/sbin/dhclient/dhclient.c @@ -90,8 +90,8 @@ cap_channel_t *capsyslog; -time_t cur_time; -struct timespec time_now; +time_t cur_time; /* Seconds since epoch. */ +struct timespec time_now; /* CLOCK_MONOTONIC. */ static time_t default_lease_time = 43200; /* 12 hours... */ const char *path_dhclient_conf = _PATH_DHCLIENT_CONF; @@ -450,7 +450,7 @@ tzset(); clock_gettime(CLOCK_MONOTONIC, &time_now); - cur_time = time_now.tv_sec; + cur_time = time(NULL); inaddr_broadcast.s_addr = INADDR_BROADCAST; inaddr_any.s_addr = INADDR_ANY; @@ -1030,13 +1030,13 @@ struct client_lease *lease, *lp; int i; struct timespec arp_timeout_needed; - struct timespec stop_selecting = { .tv_sec = 0, .tv_nsec = 0 }; - time_now.tv_sec = cur_time; - time_now.tv_nsec = 0; - + time_t stop_selecting; + struct timespec stop_time; const char *name = packet->options[DHO_DHCP_MESSAGE_TYPE].len ? "DHCPOFFER" : "BOOTREPLY"; + clock_gettime(CLOCK_MONOTONIC, &time_now); + /* If we're not receptive to an offer right now, or if the offer has an unrecognizable transaction id, then just drop it. */ if (ip->client->state != S_SELECTING || @@ -1095,7 +1095,7 @@ arp_timeout_needed = arp_timeout; /* Figure out when we're supposed to stop selecting. */ - stop_selecting.tv_sec = + stop_selecting = ip->client->first_sending + ip->client->config->select_interval; /* If this is the lease we asked for, put it at the head of the @@ -1116,7 +1116,7 @@ timespecadd(&time_now, &arp_timeout_needed, &interm_struct); if (ip->client->offered_leases && - timespeccmp(&interm_struct, &stop_selecting, >)) + interm_struct.tv_sec >= stop_selecting) arp_timeout_needed = zero_timespec; /* Put the lease at the end of the list. */ @@ -1131,27 +1131,21 @@ } } - /* If we're supposed to stop selecting before we've had time - to wait for the ARPREPLY, add some delay to wait for - the ARPREPLY. */ - struct timespec time_left; - timespecsub(&stop_selecting, &time_now, &time_left); - + /* + * Wait until stop_selecting seconds past the epoch, or until + * arp_timeout_needed past now, whichever is longer. Note that + * the first case only occurs if select-timeout is set to nonzero + * in dhclient.conf. + */ + struct timespec time_left = + {.tv_sec = stop_selecting - cur_time, .tv_nsec = 0}; if (timespeccmp(&time_left, &arp_timeout_needed, <)) { - timespecadd(&time_now, &arp_timeout_needed, &stop_selecting); - } - - /* If the selecting interval has expired, go immediately to - state_selecting(). Otherwise, time out into - state_selecting at the select interval. */ - - - if (timespeccmp(&stop_selecting, &zero_timespec, <=)) - state_selecting(ip); - else { - add_timeout_timespec(stop_selecting, state_selecting, ip); - cancel_timeout(send_discover, ip); + timespecadd(&time_now, &arp_timeout_needed, &stop_time); + } else { + timespecadd(&time_now, &time_left, &stop_time); } + add_timeout_timespec(stop_time, state_selecting, ip); + cancel_timeout(send_discover, ip); } /* Allocate a client_lease structure and initialize it from the parameters diff --git a/sbin/dhclient/dhcpd.h b/sbin/dhclient/dhcpd.h --- a/sbin/dhclient/dhcpd.h +++ b/sbin/dhclient/dhcpd.h @@ -361,8 +361,8 @@ extern cap_channel_t *capsyslog; extern const char *path_dhclient_conf; extern char *path_dhclient_db; -extern struct timespec time_now; -extern time_t cur_time; +extern struct timespec time_now; /* CLOCK_MONOTONIC */ +extern time_t cur_time; /* Seconds since epoch */ extern int log_priority; extern int log_perror; diff --git a/sbin/dhclient/dispatch.c b/sbin/dhclient/dispatch.c --- a/sbin/dhclient/dispatch.c +++ b/sbin/dhclient/dispatch.c @@ -159,8 +159,8 @@ struct protocol *l; struct pollfd *fds; struct timespec howlong; - time_now.tv_sec = cur_time; - time_now.tv_nsec = 0; + + clock_gettime(CLOCK_MONOTONIC, &time_now); for (l = protocols; l; l = l->next) nfds++; @@ -224,7 +224,7 @@ if (count == -1) { if (errno == EAGAIN || errno == EINTR) { clock_gettime(CLOCK_MONOTONIC, &time_now); - cur_time = time_now.tv_sec; + cur_time = time(NULL); continue; } else error("poll: %m"); @@ -232,7 +232,7 @@ /* Get the current time... */ clock_gettime(CLOCK_MONOTONIC, &time_now); - cur_time = time_now.tv_sec; + cur_time = time(NULL); i = 0; for (l = protocols; l; l = l->next) { @@ -365,7 +365,11 @@ void add_timeout(time_t when_s, void (*where)(void *), void *what) { - struct timespec when = { .tv_sec = when_s, .tv_nsec = 0 }; + struct timespec when; + + cur_time = time(NULL); + clock_gettime(CLOCK_MONOTONIC, &when); + when.tv_sec += when_s - cur_time; add_timeout_timespec(when, where, what); }