]>
Commit | Line | Data |
---|---|---|
cd2876d2 KZ |
1 | /* |
2 | * Please, don't add this file to libcommon because clock_gettime() requires | |
3 | * -lrt on systems with old libc. | |
4 | */ | |
2c5484f7 | 5 | #include <time.h> |
378543e1 | 6 | #include <signal.h> |
440d1db7 | 7 | #ifdef HAVE_SYSINFO |
2c5484f7 | 8 | #include <sys/sysinfo.h> |
440d1db7 | 9 | #endif |
2c5484f7 KZ |
10 | #include <sys/time.h> |
11 | ||
12 | #include "c.h" | |
cd2876d2 | 13 | #include "monotonic.h" |
2c5484f7 KZ |
14 | |
15 | int get_boot_time(struct timeval *boot_time) | |
16 | { | |
60cb2c37 | 17 | #ifdef CLOCK_BOOTTIME |
2c5484f7 | 18 | struct timespec hires_uptime; |
60cb2c37 RM |
19 | struct timeval lores_uptime; |
20 | #endif | |
21 | struct timeval now; | |
88e0f3df | 22 | #ifdef HAVE_SYSINFO |
2c5484f7 | 23 | struct sysinfo info; |
88e0f3df | 24 | #endif |
2c5484f7 | 25 | |
26e8964b | 26 | if (gettimeofday(&now, NULL) != 0) |
2c5484f7 | 27 | return -errno; |
2c5484f7 KZ |
28 | #ifdef CLOCK_BOOTTIME |
29 | if (clock_gettime(CLOCK_BOOTTIME, &hires_uptime) == 0) { | |
30 | TIMESPEC_TO_TIMEVAL(&lores_uptime, &hires_uptime); | |
31 | timersub(&now, &lores_uptime, boot_time); | |
32 | return 0; | |
33 | } | |
34 | #endif | |
88e0f3df | 35 | #ifdef HAVE_SYSINFO |
2c5484f7 KZ |
36 | /* fallback */ |
37 | if (sysinfo(&info) != 0) | |
26e8964b | 38 | return -errno; |
2c5484f7 KZ |
39 | |
40 | boot_time->tv_sec = now.tv_sec - info.uptime; | |
41 | boot_time->tv_usec = 0; | |
42 | return 0; | |
88e0f3df ST |
43 | #else |
44 | return -ENOSYS; | |
45 | #endif | |
2c5484f7 | 46 | } |
cd2876d2 KZ |
47 | |
48 | int gettime_monotonic(struct timeval *tv) | |
49 | { | |
50 | #ifdef CLOCK_MONOTONIC | |
51 | /* Can slew only by ntp and adjtime */ | |
52 | int ret; | |
53 | struct timespec ts; | |
54 | ||
3fd1f771 | 55 | /* Linux specific, can't slew */ |
ee6e3930 | 56 | if (!(ret = clock_gettime(UL_CLOCK_MONOTONIC, &ts))) { |
cd2876d2 KZ |
57 | tv->tv_sec = ts.tv_sec; |
58 | tv->tv_usec = ts.tv_nsec / 1000; | |
59 | } | |
60 | return ret; | |
61 | #else | |
62 | return gettimeofday(tv, NULL); | |
63 | #endif | |
64 | } | |
378543e1 | 65 | |
46cd5b17 | 66 |