Use monotonic clock for relative time for eloop if available
Relative time shouldn't be calculated based on gettimeofday because that clock can jump (e.g., when the time is adjusted by the system administrator.) On systems where that is available, use CLOCK_BOOTTIME (on fairly recent Linux systems, this clock takes into account the time spend suspended) or CLOCK_MONOTONIC (on Linux and some POSIX systems, this clock is just freely running with no adjustments.) Reported-by: Holger Schurig <holgerschurig@gmail.com> Signed-hostap: Johannes Berg <johannes.berg@intel.com>
This commit is contained in:
parent
461e3ebe43
commit
594516b4c2
12 changed files with 184 additions and 62 deletions
|
@ -60,6 +60,43 @@ int os_get_time(struct os_time *t)
|
|||
}
|
||||
|
||||
|
||||
int os_get_reltime(struct os_reltime *t)
|
||||
{
|
||||
#if defined(CLOCK_BOOTTIME)
|
||||
static clockid_t clock_id = CLOCK_BOOTTIME;
|
||||
#elif defined(CLOCK_MONOTONIC)
|
||||
static clockid_t clock_id = CLOCK_MONOTONIC;
|
||||
#else
|
||||
static clockid_t clock_id = CLOCK_REALTIME;
|
||||
#endif
|
||||
struct timespec ts;
|
||||
int res;
|
||||
|
||||
while (1) {
|
||||
res = clock_gettime(clock_id, &ts);
|
||||
if (res == 0) {
|
||||
t->sec = ts.tv_sec;
|
||||
t->usec = ts.tv_nsec / 1000;
|
||||
return 0;
|
||||
}
|
||||
switch (clock_id) {
|
||||
#ifdef CLOCK_BOOTTIME
|
||||
case CLOCK_BOOTTIME:
|
||||
clock_id = CLOCK_MONOTONIC;
|
||||
break;
|
||||
#endif
|
||||
#ifdef CLOCK_MONOTONIC
|
||||
case CLOCK_MONOTONIC:
|
||||
clock_id = CLOCK_REALTIME;
|
||||
break;
|
||||
#endif
|
||||
case CLOCK_REALTIME:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int os_mktime(int year, int month, int day, int hour, int min, int sec,
|
||||
os_time_t *t)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue