summaryrefslogtreecommitdiffstats
path: root/lib/monotonic.c
diff options
context:
space:
mode:
authorKarel Zak2014-11-19 11:54:47 +0100
committerKarel Zak2014-11-19 11:54:47 +0100
commitcd2876d252215fb3fbe46f787cb37cd4fbd64e53 (patch)
tree8b12443080179b8e99a010b88b0446c061711e66 /lib/monotonic.c
parentbuild-sys: use CLOCKGETTIME_LIBS (diff)
downloadkernel-qcow2-util-linux-cd2876d252215fb3fbe46f787cb37cd4fbd64e53.tar.gz
kernel-qcow2-util-linux-cd2876d252215fb3fbe46f787cb37cd4fbd64e53.tar.xz
kernel-qcow2-util-linux-cd2876d252215fb3fbe46f787cb37cd4fbd64e53.zip
build-sys: move all around clock_gettime() to monotonic.c
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib/monotonic.c')
-rw-r--r--lib/monotonic.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/lib/monotonic.c b/lib/monotonic.c
new file mode 100644
index 000000000..3d4a4438e
--- /dev/null
+++ b/lib/monotonic.c
@@ -0,0 +1,68 @@
+/*
+ * Please, don't add this file to libcommon because clock_gettime() requires
+ * -lrt on systems with old libc.
+ */
+#include <time.h>
+#include <sys/sysinfo.h>
+#include <sys/time.h>
+
+#include "c.h"
+#include "nls.h"
+#include "monotonic.h"
+
+int get_boot_time(struct timeval *boot_time)
+{
+#ifdef CLOCK_BOOTTIME
+ struct timespec hires_uptime;
+ struct timeval lores_uptime;
+#endif
+ struct timeval now;
+#ifdef HAVE_SYSINFO
+ struct sysinfo info;
+#endif
+
+ 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
+#ifdef HAVE_SYSINFO
+ /* fallback */
+ if (sysinfo(&info) != 0)
+ warn(_("sysinfo failed"));
+
+ boot_time->tv_sec = now.tv_sec - info.uptime;
+ boot_time->tv_usec = 0;
+ return 0;
+#else
+ return -ENOSYS;
+#endif
+}
+
+int gettime_monotonic(struct timeval *tv)
+{
+#ifdef CLOCK_MONOTONIC
+ /* Can slew only by ntp and adjtime */
+ int ret;
+ struct timespec ts;
+
+# ifdef CLOCK_MONOTONIC_RAW
+ /* Linux specific, cant slew */
+ if (!(ret = clock_gettime(CLOCK_MONOTONIC_RAW, &ts))) {
+# else
+ if (!(ret = clock_gettime(CLOCK_MONOTONIC, &ts))) {
+# endif
+ tv->tv_sec = ts.tv_sec;
+ tv->tv_usec = ts.tv_nsec / 1000;
+ }
+ return ret;
+#else
+ return gettimeofday(tv, NULL);
+#endif
+}