2014-11-19 11:54:47 +01:00
|
|
|
/*
|
|
|
|
* Please, don't add this file to libcommon because clock_gettime() requires
|
|
|
|
* -lrt on systems with old libc.
|
2020-01-13 10:39:52 +01:00
|
|
|
*
|
|
|
|
* No copyright is claimed. This code is in the public domain; do with
|
|
|
|
* it what you wish.
|
2014-11-19 11:54:47 +01:00
|
|
|
*/
|
2014-05-06 20:27:10 +02:00
|
|
|
#include <time.h>
|
2015-02-09 23:18:09 +00:00
|
|
|
#include <signal.h>
|
2016-02-18 03:18:38 +01:00
|
|
|
#ifdef HAVE_SYSINFO
|
2014-05-06 20:27:10 +02:00
|
|
|
#include <sys/sysinfo.h>
|
2016-02-18 03:18:38 +01:00
|
|
|
#endif
|
2014-05-06 20:27:10 +02:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#include "c.h"
|
2014-11-19 11:54:47 +01:00
|
|
|
#include "monotonic.h"
|
2014-05-06 20:27:10 +02:00
|
|
|
|
|
|
|
int get_boot_time(struct timeval *boot_time)
|
|
|
|
{
|
2014-05-30 01:18:09 +02:00
|
|
|
#ifdef CLOCK_BOOTTIME
|
2014-05-06 20:27:10 +02:00
|
|
|
struct timespec hires_uptime;
|
2014-05-30 01:18:09 +02:00
|
|
|
struct timeval lores_uptime;
|
|
|
|
#endif
|
|
|
|
struct timeval now;
|
2014-06-19 10:35:23 +02:00
|
|
|
#ifdef HAVE_SYSINFO
|
2014-05-06 20:27:10 +02:00
|
|
|
struct sysinfo info;
|
2014-06-19 10:35:23 +02:00
|
|
|
#endif
|
2014-05-06 20:27:10 +02:00
|
|
|
|
2015-03-06 13:17:20 +01:00
|
|
|
if (gettimeofday(&now, NULL) != 0)
|
2014-05-06 20:27:10 +02: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 10:35:23 +02:00
|
|
|
#ifdef HAVE_SYSINFO
|
2014-05-06 20:27:10 +02:00
|
|
|
/* fallback */
|
|
|
|
if (sysinfo(&info) != 0)
|
2015-03-06 13:17:20 +01:00
|
|
|
return -errno;
|
2014-05-06 20:27:10 +02:00
|
|
|
|
|
|
|
boot_time->tv_sec = now.tv_sec - info.uptime;
|
|
|
|
boot_time->tv_usec = 0;
|
|
|
|
return 0;
|
2014-06-19 10:35:23 +02:00
|
|
|
#else
|
|
|
|
return -ENOSYS;
|
|
|
|
#endif
|
2014-05-06 20:27:10 +02:00
|
|
|
}
|
2014-11-19 11:54:47 +01:00
|
|
|
|
2023-01-21 00:21:10 +00:00
|
|
|
usec_t get_suspended_time(void)
|
2020-06-01 22:21:34 +03:00
|
|
|
{
|
|
|
|
#if defined(CLOCK_BOOTTIME) && defined(CLOCK_MONOTONIC)
|
|
|
|
struct timespec boot, mono;
|
|
|
|
|
|
|
|
if (clock_gettime(CLOCK_BOOTTIME, &boot) == 0 &&
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &mono) == 0)
|
2023-01-21 00:21:10 +00:00
|
|
|
return timespec_to_usec(&boot) - timespec_to_usec(&mono);
|
2020-06-01 22:21:34 +03:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-19 11:54:47 +01: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 15:00:37 +01:00
|
|
|
/* Linux specific, can't slew */
|
2019-01-07 21:31:34 +00:00
|
|
|
if (!(ret = clock_gettime(UL_CLOCK_MONOTONIC, &ts))) {
|
2014-11-19 11:54:47 +01: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 23:18:09 +00:00
|
|
|
|
2015-03-05 10:47:59 +01:00
|
|
|
|