Просмотр исходного кода

s-s-d: Switch to use a monotonic clock if available

Use clock_gettime(CLOCK_MONOTONIC) if available instead of
gettimeofday() which gets affected by abrupt system clock changes, and
might mess with the timeout calculations.

Closes: #783014
Suggested-by: Jose M Calhariz <jose.calhariz@hds.com>
Guillem Jover лет назад: 11
Родитель
Сommit
033b295069
2 измененных файлов с 10 добавлено и 0 удалено
  1. 3 0
      debian/changelog
  2. 7 0
      utils/start-stop-daemon.c

+ 3 - 0
debian/changelog

@@ -58,6 +58,9 @@ dpkg (1.18.0) UNRELEASED; urgency=low
     original. Closes: #773718
   * Do not leak kvm descriptors in start-stop-daemon on GNU/kFreeBSD systems.
     Based on a patch by Jeff Epler <jepler@unpythonic.net>. Closes: #779467
+  * Switch start-stop-daemon to use a monotonic clock if available. This
+    makes the timeout checks resilient to abrupt system clock changes.
+    Suggested by Jose M Calhariz <jose.calhariz@hds.com>. Closes: #783014
 
   * Perl modules:
     - Rename and deprecate Dpkg::Gettext _g function with new g_.

+ 7 - 0
utils/start-stop-daemon.c

@@ -73,6 +73,7 @@
 #include <assert.h>
 #include <errno.h>
 #include <limits.h>
+#include <time.h>
 #include <fcntl.h>
 #include <dirent.h>
 #include <ctype.h>
@@ -309,6 +310,11 @@ xstrdup(const char *str)
 static void
 timespec_gettime(struct timespec *ts)
 {
+#if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && \
+    defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK > 0
+	if (clock_gettime(CLOCK_MONOTONIC, ts) < 0)
+		fatal("clock_gettime failed");
+#else
 	struct timeval tv;
 
 	if (gettimeofday(&tv, NULL) != 0)
@@ -316,6 +322,7 @@ timespec_gettime(struct timespec *ts)
 
 	ts->tv_sec = tv.tv_sec;
 	ts->tv_nsec = tv.tv_usec * NANOSEC_IN_MICROSEC;
+#endif
 }
 
 #define timespec_cmp(a, b, OP) \