Blame SOURCES/0025-bind-Detect-system-time-changes.patch

f9ed25
From ef4f5e80d8a1ea1507829ea6f5214f276478f475 Mon Sep 17 00:00:00 2001
f9ed25
From: Pavel Zhukov <pzhukov@redhat.com>
f9ed25
Date: Tue, 22 Oct 2019 16:23:24 +0200
f9ed25
Subject: [PATCH 25/27] bind: Detect system time changes
f9ed25
Cc: pzhukov@redhat.com
f9ed25
f9ed25
---
f9ed25
 bind/bind/lib/isc/include/isc/result.h    |  4 ++-
f9ed25
 bind/bind/lib/isc/include/isc/util.h      |  4 +++
f9ed25
 bind/bind/lib/isc/result.c                |  2 ++
f9ed25
 bind/bind/lib/isc/unix/app.c              | 41 ++++++++++++++++++++++++++++---
f9ed25
 bind/bind/lib/isc/unix/include/isc/time.h | 20 +++++++++++++++
f9ed25
 bind/bind/lib/isc/unix/time.c             | 22 +++++++++++++++++
f9ed25
 6 files changed, 89 insertions(+), 4 deletions(-)
f9ed25
f9ed25
diff --git a/bind/bind/lib/isc/include/isc/result.h b/bind/bind/lib/isc/include/isc/result.h
f9ed25
index 0389efa..0e35f98 100644
f9ed25
--- a/bind/bind/lib/isc/include/isc/result.h
f9ed25
+++ b/bind/bind/lib/isc/include/isc/result.h
f9ed25
@@ -89,7 +89,9 @@
f9ed25
 #define ISC_R_DISCFULL			67	/*%< disc full */
f9ed25
 #define ISC_R_DEFAULT			68	/*%< default */
f9ed25
 #define ISC_R_IPV4PREFIX		69	/*%< IPv4 prefix */
f9ed25
-#define ISC_R_NRESULTS 			70
f9ed25
+#define ISC_R_TIMESHIFTED               70      /*%< system time changed */
f9ed25
+/*% Not a result code: the number of results. */
f9ed25
+#define ISC_R_NRESULTS 			71
f9ed25
 
f9ed25
 ISC_LANG_BEGINDECLS
f9ed25
 
f9ed25
diff --git a/bind/bind/lib/isc/include/isc/util.h b/bind/bind/lib/isc/include/isc/util.h
f9ed25
index 973c348..cceeb5e 100644
f9ed25
--- a/bind/bind/lib/isc/include/isc/util.h
f9ed25
+++ b/bind/bind/lib/isc/include/isc/util.h
f9ed25
@@ -289,6 +289,10 @@ extern void mock_assert(const int result, const char* const expression,
f9ed25
  * Time
f9ed25
  */
f9ed25
 #define TIME_NOW(tp) 	RUNTIME_CHECK(isc_time_now((tp)) == ISC_R_SUCCESS)
f9ed25
+#ifdef CLOCK_BOOTTIME
f9ed25
+#define TIME_MONOTONIC(tp) 	RUNTIME_CHECK(isc_time_boottime((tp)) == ISC_R_SUCCESS)
f9ed25
+#endif
f9ed25
+
f9ed25
 
f9ed25
 /*%
f9ed25
  * Alignment
f9ed25
diff --git a/bind/bind/lib/isc/result.c b/bind/bind/lib/isc/result.c
f9ed25
index a9db132..7c04831 100644
f9ed25
--- a/bind/bind/lib/isc/result.c
f9ed25
+++ b/bind/bind/lib/isc/result.c
f9ed25
@@ -105,6 +105,7 @@ static const char *description[ISC_R_NRESULTS] = {
f9ed25
 	"disc full",				/*%< 67 */
f9ed25
 	"default",				/*%< 68 */
f9ed25
 	"IPv4 prefix",				/*%< 69 */
f9ed25
+        "time changed",                         /*%< 70 */
f9ed25
 };
f9ed25
 
f9ed25
 static const char *identifier[ISC_R_NRESULTS] = {
f9ed25
@@ -178,6 +179,7 @@ static const char *identifier[ISC_R_NRESULTS] = {
f9ed25
 	"ISC_R_DISCFULL",
f9ed25
 	"ISC_R_DEFAULT",
f9ed25
 	"ISC_R_IPV4PREFIX",
f9ed25
+        "ISC_R_TIMESHIFTED",
f9ed25
 };
f9ed25
 
f9ed25
 #define ISC_RESULT_RESULTSET			2
f9ed25
diff --git a/bind/bind/lib/isc/unix/app.c b/bind/bind/lib/isc/unix/app.c
f9ed25
index a6e9882..dbd23f7 100644
f9ed25
--- a/bind/bind/lib/isc/unix/app.c
f9ed25
+++ b/bind/bind/lib/isc/unix/app.c
f9ed25
@@ -442,15 +442,51 @@ isc__app_ctxonrun(isc_appctx_t *ctx0, isc_mem_t *mctx, isc_task_t *task,
f9ed25
 static isc_result_t
f9ed25
 evloop(isc__appctx_t *ctx) {
f9ed25
 	isc_result_t result;
f9ed25
+        isc_time_t now;
f9ed25
+#ifdef CLOCK_BOOTTIME
f9ed25
+        isc_time_t monotonic;
f9ed25
+        uint64_t diff  = 0;
f9ed25
+#else
f9ed25
+        isc_time_t prev;
f9ed25
+        TIME_NOW(&prev;;
f9ed25
+#endif
f9ed25
+
f9ed25
+
f9ed25
+
f9ed25
 
f9ed25
 	while (!ctx->want_shutdown) {
f9ed25
 		int n;
f9ed25
-		isc_time_t when, now;
f9ed25
+		isc_time_t when;
f9ed25
+                
f9ed25
 		struct timeval tv, *tvp;
f9ed25
 		isc_socketwait_t *swait;
f9ed25
 		bool readytasks;
f9ed25
 		bool call_timer_dispatch = false;
f9ed25
 
f9ed25
+                uint64_t us; 
f9ed25
+
f9ed25
+#ifdef CLOCK_BOOTTIME
f9ed25
+                // TBD macros for following three lines
f9ed25
+                TIME_NOW(&now;;
f9ed25
+                TIME_MONOTONIC(&monotonic);
f9ed25
+                INSIST(now.seconds > monotonic.seconds)
f9ed25
+                us = isc_time_microdiff (&now, &monotonic);
f9ed25
+                if (us < diff){ 
f9ed25
+                  us = diff - us;
f9ed25
+                  if (us > 1000000){ // ignoring shifts less than one second
f9ed25
+                    return ISC_R_TIMESHIFTED;
f9ed25
+                  };
f9ed25
+                  diff = isc_time_microdiff (&now, &monotonic);
f9ed25
+                } else {
f9ed25
+                  diff = isc_time_microdiff (&now, &monotonic);
f9ed25
+                  // not implemented
f9ed25
+                }
f9ed25
+#else
f9ed25
+                TIME_NOW(&now;;
f9ed25
+                if (isc_time_compare (&now, &prev) < 0)
f9ed25
+                  return ISC_R_TIMESHIFTED;
f9ed25
+                TIME_NOW(&prev;;
f9ed25
+#endif                
f9ed25
 		/*
f9ed25
 		 * Check the reload (or suspend) case first for exiting the
f9ed25
 		 * loop as fast as possible in case:
f9ed25
@@ -475,9 +511,8 @@ evloop(isc__appctx_t *ctx) {
f9ed25
 			if (result != ISC_R_SUCCESS)
f9ed25
 				tvp = NULL;
f9ed25
 			else {
f9ed25
-				uint64_t us;
f9ed25
-
f9ed25
 				TIME_NOW(&now;;
f9ed25
+
f9ed25
 				us = isc_time_microdiff(&when, &now;;
f9ed25
 				if (us == 0)
f9ed25
 					call_timer_dispatch = true;
f9ed25
diff --git a/bind/bind/lib/isc/unix/include/isc/time.h b/bind/bind/lib/isc/unix/include/isc/time.h
f9ed25
index b864c29..5dd43c9 100644
f9ed25
--- a/bind/bind/lib/isc/unix/include/isc/time.h
f9ed25
+++ b/bind/bind/lib/isc/unix/include/isc/time.h
f9ed25
@@ -132,6 +132,26 @@ isc_time_isepoch(const isc_time_t *t);
f9ed25
  *\li	't' is a valid pointer.
f9ed25
  */
f9ed25
 
f9ed25
+#ifdef CLOCK_BOOTTIME
f9ed25
+isc_result_t
f9ed25
+isc_time_boottime(isc_time_t *t);
f9ed25
+/*%<
f9ed25
+ * Set 't' to monotonic time from previous boot
f9ed25
+ * it's not affected by system time change. It also
f9ed25
+ * includes the time system was suspended
f9ed25
+ *
f9ed25
+ * Requires:
f9ed25
+ *\li	't' is a valid pointer.
f9ed25
+ *
f9ed25
+ * Returns:
f9ed25
+ *
f9ed25
+ *\li	Success
f9ed25
+ *\li	Unexpected error
f9ed25
+ *		Getting the time from the system failed.
f9ed25
+ */
f9ed25
+#endif /* CLOCK_BOOTTIME */
f9ed25
+ 
f9ed25
+
f9ed25
 isc_result_t
f9ed25
 isc_time_now(isc_time_t *t);
f9ed25
 /*%<
f9ed25
diff --git a/bind/bind/lib/isc/unix/time.c b/bind/bind/lib/isc/unix/time.c
f9ed25
index 8edc9df..fe0bb91 100644
f9ed25
--- a/bind/bind/lib/isc/unix/time.c
f9ed25
+++ b/bind/bind/lib/isc/unix/time.c
f9ed25
@@ -498,3 +498,25 @@ isc_time_formatISO8601ms(const isc_time_t *t, char *buf, unsigned int len) {
f9ed25
 			 t->nanoseconds / NS_PER_MS);
f9ed25
 	}
f9ed25
 }
f9ed25
+
f9ed25
+
f9ed25
+#ifdef CLOCK_BOOTTIME
f9ed25
+isc_result_t
f9ed25
+isc_time_boottime(isc_time_t *t) {
f9ed25
+  struct timespec ts;
f9ed25
+  
f9ed25
+  char strbuf[ISC_STRERRORSIZE];
f9ed25
+
f9ed25
+  if (clock_gettime (CLOCK_BOOTTIME, &ts) != 0){
f9ed25
+    isc__strerror(errno, strbuf, sizeof(strbuf));
f9ed25
+    UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
f9ed25
+    return (ISC_R_UNEXPECTED);    
f9ed25
+  }
f9ed25
+
f9ed25
+  t->seconds = ts.tv_sec;
f9ed25
+  t->nanoseconds = ts.tv_nsec;
f9ed25
+
f9ed25
+  return (ISC_R_SUCCESS);
f9ed25
+  
f9ed25
+};
f9ed25
+#endif
f9ed25
-- 
f9ed25
2.14.5
f9ed25