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

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