summaryrefslogtreecommitdiffstats
path: root/lib/boottime.c
diff options
context:
space:
mode:
authorKarel Zak2014-05-06 20:27:10 +0200
committerKarel Zak2014-05-06 20:27:10 +0200
commit2c5484f7b200ff6ecb444b2bef4830c772064257 (patch)
tree8a8838da598089c056a38686fcb7d303cb70dfb6 /lib/boottime.c
parentinclude/boottime: add a new file (diff)
downloadkernel-qcow2-util-linux-2c5484f7b200ff6ecb444b2bef4830c772064257.tar.gz
kernel-qcow2-util-linux-2c5484f7b200ff6ecb444b2bef4830c772064257.tar.xz
kernel-qcow2-util-linux-2c5484f7b200ff6ecb444b2bef4830c772064257.zip
lib/boottime: add a new file
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib/boottime.c')
-rw-r--r--lib/boottime.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/boottime.c b/lib/boottime.c
new file mode 100644
index 000000000..335570cc1
--- /dev/null
+++ b/lib/boottime.c
@@ -0,0 +1,34 @@
+
+#include <time.h>
+#include <sys/sysinfo.h>
+#include <sys/time.h>
+
+#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;
+}