diff --git a/include/timeutils.h b/include/timeutils.h index bcae6137c..97f054489 100644 --- a/include/timeutils.h +++ b/include/timeutils.h @@ -3,6 +3,7 @@ copied to util-linux at August 2013. Copyright 2010 Lennart Poettering + Copyright (C) 2014 Karel Zak systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -52,4 +53,6 @@ typedef uint64_t nsec_t; int parse_timestamp(const char *t, usec_t *usec); +int gettime_monotonic(struct timeval *tv); + #endif /* UTIL_LINUX_TIME_UTIL_H */ diff --git a/lib/timeutils.c b/lib/timeutils.c index b811041e4..bd3d40248 100644 --- a/lib/timeutils.c +++ b/lib/timeutils.c @@ -340,3 +340,26 @@ int parse_timestamp(const char *t, usec_t *usec) return 0; } + + +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 +} diff --git a/libmount/src/lock.c b/libmount/src/lock.c index 8667db999..0a07e9b03 100644 --- a/libmount/src/lock.c +++ b/libmount/src/lock.c @@ -25,6 +25,7 @@ #include "closestream.h" #include "pathnames.h" #include "mountP.h" +#include "timeutils.h" /* * lock handler @@ -258,7 +259,7 @@ static int mnt_wait_mtab_lock(struct libmnt_lock *ml, struct flock *fl, time_t m struct sigaction sa, osa; int ret = 0; - gettimeofday(&now, NULL); + gettime_monotonic(&now); if (now.tv_sec >= maxtime) return 1; /* timeout */ @@ -408,7 +409,7 @@ static int lock_mtab(struct libmnt_lock *ml) } close(i); - gettimeofday(&maxtime, NULL); + gettime_monotonic(&maxtime); maxtime.tv_sec += MOUNTLOCK_MAXTIME; waittime.tv_sec = 0; @@ -434,7 +435,7 @@ static int lock_mtab(struct libmnt_lock *ml) if (ml->lockfile_fd < 0) { /* Strange... Maybe the file was just deleted? */ int errsv = errno; - gettimeofday(&now, NULL); + gettime_monotonic(&now); if (errsv == ENOENT && now.tv_sec < maxtime.tv_sec) { ml->locked = 0; continue; @@ -646,7 +647,7 @@ int test_lock(struct libmnt_test *ts, int argc, char *argv[]) /* start the test in exactly defined time */ if (synctime) { - gettimeofday(&tv, NULL); + gettime_monotonic(&tv); if (synctime && synctime - tv.tv_sec > 1) { usecs = ((synctime - tv.tv_sec) * 1000000UL) - (1000000UL - tv.tv_usec); diff --git a/sys-utils/blkdiscard.c b/sys-utils/blkdiscard.c index 92ca52ae4..094f4e2ff 100644 --- a/sys-utils/blkdiscard.c +++ b/sys-utils/blkdiscard.c @@ -42,6 +42,7 @@ #include "strutils.h" #include "c.h" #include "closestream.h" +#include "timeutils.h" #ifndef BLKDISCARD #define BLKDISCARD _IO(0x12,119) @@ -80,7 +81,7 @@ int main(int argc, char **argv) int c, fd, verbose = 0, secure = 0, secsize; uint64_t end, blksize, step, range[2], stats[2]; struct stat sb; - struct timespec now, last; + struct timeval now, last; static const struct option longopts[] = { { "help", 0, 0, 'h' }, @@ -178,7 +179,7 @@ int main(int argc, char **argv) "to sector size %i"), path, range[1], secsize); stats[0] = range[0], stats[1] = 0; - clock_gettime(CLOCK_MONOTONIC, &last); + gettime_monotonic(&last); for (range[0] = range[0]; range[0] < end; range[0] += range[1]) { if (range[0] + range[1] > end) @@ -194,7 +195,7 @@ int main(int argc, char **argv) /* reporting progress */ if (verbose && step) { - clock_gettime(CLOCK_MONOTONIC, &now); + gettime_monotonic(&now); if (last.tv_sec < now.tv_sec) { print_stats(path, stats); stats[0] = range[0], stats[1] = 0; diff --git a/sys-utils/eject.c b/sys-utils/eject.c index 860610f5a..5a181644d 100644 --- a/sys-utils/eject.c +++ b/sys-utils/eject.c @@ -52,6 +52,7 @@ #include "xalloc.h" #include "pathnames.h" #include "sysfs.h" +#include "timeutils.h" /* * sg_io_hdr_t driver_status -- see kernel include/scsi/scsi.h @@ -462,7 +463,7 @@ static void toggle_tray(int fd) * needed. In my experience the function needs less than 0.05 * seconds if the tray was already open, and at least 1.5 seconds * if it was closed. */ - gettimeofday(&time_start, NULL); + gettime_monotonic(&time_start); /* Send the CDROMEJECT command to the device. */ if (!eject_cdrom(fd)) @@ -470,7 +471,7 @@ static void toggle_tray(int fd) /* Get the second timestamp, to measure the time needed to open * the tray. */ - gettimeofday(&time_stop, NULL); + gettime_monotonic(&time_stop); time_elapsed = (time_stop.tv_sec * 1000000 + time_stop.tv_usec) - (time_start.tv_sec * 1000000 + time_start.tv_usec);