From: Karel Zak Date: Tue, 6 May 2014 18:27:10 +0000 (+0200) Subject: lib/boottime: add a new file X-Git-Tag: v2.25-rc1~200 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2c5484f7b200ff6ecb444b2bef4830c772064257;p=thirdparty%2Futil-linux.git lib/boottime: add a new file Signed-off-by: Karel Zak --- diff --git a/lib/boottime.c b/lib/boottime.c new file mode 100644 index 0000000000..335570cc19 --- /dev/null +++ b/lib/boottime.c @@ -0,0 +1,34 @@ + +#include +#include +#include + +#include "c.h" +#include "nls.h" +#include "boottime.h" + +int get_boot_time(struct timeval *boot_time) +{ + struct timespec hires_uptime; + struct timeval lores_uptime, now; + struct sysinfo info; + + 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")); + + boot_time->tv_sec = now.tv_sec - info.uptime; + boot_time->tv_usec = 0; + return 0; +}