]>
Commit | Line | Data |
---|---|---|
2c5484f7 KZ |
1 | |
2 | #include <time.h> | |
3 | #include <sys/sysinfo.h> | |
4 | #include <sys/time.h> | |
5 | ||
6 | #include "c.h" | |
7 | #include "nls.h" | |
8 | #include "boottime.h" | |
9 | ||
10 | int get_boot_time(struct timeval *boot_time) | |
11 | { | |
60cb2c37 | 12 | #ifdef CLOCK_BOOTTIME |
2c5484f7 | 13 | struct timespec hires_uptime; |
60cb2c37 RM |
14 | struct timeval lores_uptime; |
15 | #endif | |
16 | struct timeval now; | |
2c5484f7 KZ |
17 | struct sysinfo info; |
18 | ||
19 | if (gettimeofday(&now, NULL) != 0) { | |
20 | warn(_("gettimeofday failed")); | |
21 | return -errno; | |
22 | } | |
23 | #ifdef CLOCK_BOOTTIME | |
24 | if (clock_gettime(CLOCK_BOOTTIME, &hires_uptime) == 0) { | |
25 | TIMESPEC_TO_TIMEVAL(&lores_uptime, &hires_uptime); | |
26 | timersub(&now, &lores_uptime, boot_time); | |
27 | return 0; | |
28 | } | |
29 | #endif | |
30 | /* fallback */ | |
31 | if (sysinfo(&info) != 0) | |
32 | warn(_("sysinfo failed")); | |
33 | ||
34 | boot_time->tv_sec = now.tv_sec - info.uptime; | |
35 | boot_time->tv_usec = 0; | |
36 | return 0; | |
37 | } |