2014-11-19 04:54:47 -06:00
|
|
|
/*
|
|
|
|
* Please, don't add this file to libcommon because clock_gettime() requires
|
|
|
|
* -lrt on systems with old libc.
|
|
|
|
*/
|
2014-05-06 13:27:10 -05:00
|
|
|
#include <time.h>
|
2015-02-09 17:18:09 -06:00
|
|
|
#include <signal.h>
|
2016-02-17 20:18:38 -06:00
|
|
|
#ifdef HAVE_SYSINFO
|
2014-05-06 13:27:10 -05:00
|
|
|
#include <sys/sysinfo.h>
|
2016-02-17 20:18:38 -06:00
|
|
|
#endif
|
2014-05-06 13:27:10 -05:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#include "c.h"
|
2014-11-19 04:54:47 -06:00
|
|
|
#include "monotonic.h"
|
2014-05-06 13:27:10 -05:00
|
|
|
|
|
|
|
int get_boot_time(struct timeval *boot_time)
|
|
|
|
{
|
2014-05-29 18:18:09 -05:00
|
|
|
#ifdef CLOCK_BOOTTIME
|
2014-05-06 13:27:10 -05:00
|
|
|
struct timespec hires_uptime;
|
2014-05-29 18:18:09 -05:00
|
|
|
struct timeval lores_uptime;
|
|
|
|
#endif
|
|
|
|
struct timeval now;
|
2014-06-19 03:35:23 -05:00
|
|
|
#ifdef HAVE_SYSINFO
|
2014-05-06 13:27:10 -05:00
|
|
|
struct sysinfo info;
|
2014-06-19 03:35:23 -05:00
|
|
|
#endif
|
2014-05-06 13:27:10 -05:00
|
|
|
|
2015-03-06 06:17:20 -06:00
|
|
|
if (gettimeofday(&now, NULL) != 0)
|
2014-05-06 13:27:10 -05:00
|
|
|
return -errno;
|
|
|
|
#ifdef CLOCK_BOOTTIME
|
|
|
|
if (clock_gettime(CLOCK_BOOTTIME, &hires_uptime) == 0) {
|
|
|
|
TIMESPEC_TO_TIMEVAL(&lores_uptime, &hires_uptime);
|
|
|
|
timersub(&now, &lores_uptime, boot_time);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2014-06-19 03:35:23 -05:00
|
|
|
#ifdef HAVE_SYSINFO
|
2014-05-06 13:27:10 -05:00
|
|
|
/* fallback */
|
|
|
|
if (sysinfo(&info) != 0)
|
2015-03-06 06:17:20 -06:00
|
|
|
return -errno;
|
2014-05-06 13:27:10 -05:00
|
|
|
|
|
|
|
boot_time->tv_sec = now.tv_sec - info.uptime;
|
|
|
|
boot_time->tv_usec = 0;
|
|
|
|
return 0;
|
2014-06-19 03:35:23 -05:00
|
|
|
#else
|
|
|
|
return -ENOSYS;
|
|
|
|
#endif
|
2014-05-06 13:27:10 -05:00
|
|
|
}
|
2014-11-19 04:54:47 -06:00
|
|
|
|
|
|
|
int gettime_monotonic(struct timeval *tv)
|
|
|
|
{
|
|
|
|
#ifdef CLOCK_MONOTONIC
|
|
|
|
/* Can slew only by ntp and adjtime */
|
|
|
|
int ret;
|
|
|
|
struct timespec ts;
|
|
|
|
|
2016-02-03 08:00:37 -06:00
|
|
|
/* Linux specific, can't slew */
|
2019-01-07 15:31:34 -06:00
|
|
|
if (!(ret = clock_gettime(UL_CLOCK_MONOTONIC, &ts))) {
|
2014-11-19 04:54:47 -06:00
|
|
|
tv->tv_sec = ts.tv_sec;
|
|
|
|
tv->tv_usec = ts.tv_nsec / 1000;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
#else
|
|
|
|
return gettimeofday(tv, NULL);
|
|
|
|
#endif
|
|
|
|
}
|
2015-02-09 17:18:09 -06:00
|
|
|
|
2015-03-05 03:47:59 -06:00
|
|
|
|