struct timeval lasttime; /* last printed timestamp */
struct tm lasttm; /* last localtime */
- time_t boot_time; /* system boot time */
+ struct timeval boot_time; /* system boot time */
int action; /* SYSLOG_ACTION_* */
int method; /* DMESG_METHOD_* */
return n > 0 ? n : 0;
}
-static time_t get_boot_time(void)
+static int get_boot_time(struct timeval *boot_time)
{
+ struct timespec hires_uptime;
+ struct timeval lores_uptime, now;
struct sysinfo info;
- struct timeval tv;
+ if (gettimeofday(&now, NULL) != 0) {
+ warn(_("gettimeofday failed"));
+ 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
+ /* fallback */
if (sysinfo(&info) != 0)
warn(_("sysinfo failed"));
- else if (gettimeofday(&tv, NULL) != 0)
- warn(_("gettimeofday failed"));
- else
- return tv.tv_sec -= info.uptime;
+
+ boot_time->tv_sec = now.tv_sec - info.uptime;
+ boot_time->tv_usec = 0;
return 0;
}
struct dmesg_record *rec,
struct tm *tm)
{
- time_t t = ctl->boot_time + rec->tv.tv_sec;
+ time_t t = ctl->boot_time.tv_sec + rec->tv.tv_sec;
return localtime_r(&t, tm);
}
is_timefmt(&ctl, CTIME) ||
is_timefmt(&ctl, ISO8601)) {
- ctl.boot_time = get_boot_time();
- if (!ctl.boot_time)
+ if (get_boot_time(&ctl.boot_time) != 0)
ctl.time_fmt = DMESG_TIMEFTM_NONE;
}