diff options
author | Tom Hughes <tom.hughes@palm.com> | 2009-12-13 13:30:09 -0800 |
---|---|---|
committer | Colin Walters <walters@verbum.org> | 2010-02-02 10:29:03 -0500 |
commit | 1c6596eb52fdf196705efe6c06dcd9fe8f836afc (patch) | |
tree | f89fd1383a246ad90ec3284f65c8e3861e306e21 | |
parent | 0607bdb676c8fd95126ba994ab72e71becc232d8 (diff) |
Use monotonic clock for _dbus_get_current_time() if it's available.
_dbus_get_current_time() is used for timeouts, but uses gettimeofday(), which
relies on the wall clock time, which can change. If the time is changed forwards
or backwards, the timeouts are no longer valid, so the monotonic clock must be used.
https://bugs.freedesktop.org/show_bug.cgi?id=25624
Signed-off-by: Colin Walters <walters@verbum.org>
-rw-r--r-- | dbus/dbus-sysdeps-unix.c | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index 66bcf3cb..ce3475a6 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -2036,7 +2036,8 @@ _dbus_poll (DBusPollFD *fds, } /** - * Get current time, as in gettimeofday(). + * Get current time, as in gettimeofday(). Use the monotonic clock if + * available, to avoid problems when the system time changes. * * @param tv_sec return location for number of seconds * @param tv_usec return location for number of microseconds (thousandths) @@ -2047,12 +2048,22 @@ _dbus_get_current_time (long *tv_sec, { struct timeval t; +#ifdef HAVE_MONOTONIC_CLOCK + struct timespec ts; + clock_gettime (CLOCK_MONOTONIC, &ts); + + if (tv_sec) + *tv_sec = ts.tv_sec; + if (tv_usec) + *tv_usec = ts.tv_nsec / 1000; +#else gettimeofday (&t, NULL); if (tv_sec) *tv_sec = t.tv_sec; if (tv_usec) *tv_usec = t.tv_usec; +#endif } /** |