]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/monotonic.c
2 * Please, don't add this file to libcommon because clock_gettime() requires
3 * -lrt on systems with old libc.
8 #include <sys/sysinfo.h>
13 #include "monotonic.h"
15 int get_boot_time(struct timeval
*boot_time
)
18 struct timespec hires_uptime
;
19 struct timeval lores_uptime
;
26 if (gettimeofday(&now
, NULL
) != 0)
29 if (clock_gettime(CLOCK_BOOTTIME
, &hires_uptime
) == 0) {
30 TIMESPEC_TO_TIMEVAL(&lores_uptime
, &hires_uptime
);
31 timersub(&now
, &lores_uptime
, boot_time
);
37 if (sysinfo(&info
) != 0)
40 boot_time
->tv_sec
= now
.tv_sec
- info
.uptime
;
41 boot_time
->tv_usec
= 0;
48 int gettime_monotonic(struct timeval
*tv
)
50 #ifdef CLOCK_MONOTONIC
51 /* Can slew only by ntp and adjtime */
55 # ifdef CLOCK_MONOTONIC_RAW
56 /* Linux specific, can't slew */
57 if (!(ret
= clock_gettime(CLOCK_MONOTONIC_RAW
, &ts
))) {
59 if (!(ret
= clock_gettime(CLOCK_MONOTONIC
, &ts
))) {
61 tv
->tv_sec
= ts
.tv_sec
;
62 tv
->tv_usec
= ts
.tv_nsec
/ 1000;
66 return gettimeofday(tv
, NULL
);