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