57726f
diff --git a/lib/isc/include/isc/result.h b/lib/isc/include/isc/result.h
57726f
index 0389efa..149cde5 100644
57726f
--- a/lib/isc/include/isc/result.h
57726f
+++ b/lib/isc/include/isc/result.h
57726f
@@ -89,7 +89,8 @@
57726f
 #define ISC_R_DISCFULL			67	/*%< disc full */
57726f
 #define ISC_R_DEFAULT			68	/*%< default */
57726f
 #define ISC_R_IPV4PREFIX		69	/*%< IPv4 prefix */
57726f
-#define ISC_R_NRESULTS 			70
57726f
+#define ISC_R_TIMESHIFTED               70      /*%< system time changed */
57726f
+#define ISC_R_NRESULTS 			71
57726f
 
57726f
 ISC_LANG_BEGINDECLS
57726f
 
57726f
diff --git a/lib/isc/include/isc/util.h b/lib/isc/include/isc/util.h
57726f
index 973c348..cceeb5e 100644
57726f
--- a/lib/isc/include/isc/util.h
57726f
+++ b/lib/isc/include/isc/util.h
57726f
@@ -289,6 +289,10 @@ extern void mock_assert(const int result, const char* const expression,
57726f
  * Time
57726f
  */
57726f
 #define TIME_NOW(tp) 	RUNTIME_CHECK(isc_time_now((tp)) == ISC_R_SUCCESS)
57726f
+#ifdef CLOCK_BOOTTIME
57726f
+#define TIME_MONOTONIC(tp) 	RUNTIME_CHECK(isc_time_boottime((tp)) == ISC_R_SUCCESS)
57726f
+#endif
57726f
+
57726f
 
57726f
 /*%
57726f
  * Alignment
57726f
diff --git a/lib/isc/result.c b/lib/isc/result.c
57726f
index a9db132..f33fc6b 100644
57726f
--- a/lib/isc/result.c
57726f
+++ b/lib/isc/result.c
57726f
@@ -105,6 +105,7 @@ static const char *description[ISC_R_NRESULTS] = {
57726f
 	"disc full",				/*%< 67 */
57726f
 	"default",				/*%< 68 */
57726f
 	"IPv4 prefix",				/*%< 69 */
57726f
+	"time changed",                         /*%< 70 */
57726f
 };
57726f
 
57726f
 static const char *identifier[ISC_R_NRESULTS] = {
57726f
@@ -178,6 +179,7 @@ static const char *identifier[ISC_R_NRESULTS] = {
57726f
 	"ISC_R_DISCFULL",
57726f
 	"ISC_R_DEFAULT",
57726f
 	"ISC_R_IPV4PREFIX",
57726f
+	"ISC_R_TIMESHIFTED",
57726f
 };
57726f
 
57726f
 #define ISC_RESULT_RESULTSET			2
57726f
diff --git a/lib/isc/unix/app.c b/lib/isc/unix/app.c
57726f
index a6e9882..286fe95 100644
57726f
--- a/lib/isc/unix/app.c
57726f
+++ b/lib/isc/unix/app.c
57726f
@@ -442,15 +442,47 @@ isc__app_ctxonrun(isc_appctx_t *ctx0, isc_mem_t *mctx, isc_task_t *task,
57726f
 static isc_result_t
57726f
 evloop(isc__appctx_t *ctx) {
57726f
 	isc_result_t result;
57726f
+        isc_time_t now;
57726f
+#ifdef CLOCK_BOOTTIME
57726f
+        isc_time_t monotonic;
57726f
+        isc_uint64_t diff  = 0;
57726f
+#else
57726f
+        isc_time_t prev;
57726f
+        TIME_NOW(&prev;;
57726f
+#endif
57726f
 
57726f
 	while (!ctx->want_shutdown) {
57726f
 		int n;
57726f
-		isc_time_t when, now;
57726f
+		isc_time_t when;
57726f
 		struct timeval tv, *tvp;
57726f
 		isc_socketwait_t *swait;
57726f
 		bool readytasks;
57726f
 		bool call_timer_dispatch = false;
57726f
 
57726f
+                uint64_t us; 
57726f
+
57726f
+#ifdef CLOCK_BOOTTIME
57726f
+                // TBD macros for following three lines
57726f
+                TIME_NOW(&now;;
57726f
+                TIME_MONOTONIC(&monotonic);
57726f
+                INSIST(now.seconds > monotonic.seconds)
57726f
+                us = isc_time_microdiff (&now, &monotonic);
57726f
+                if (us < diff){
57726f
+                  us = diff - us;
57726f
+                  if (us > 1000000){ // ignoring shifts less than one second
57726f
+                    return ISC_R_TIMESHIFTED;
57726f
+                  };
57726f
+                  diff = isc_time_microdiff (&now, &monotonic);
57726f
+                } else {
57726f
+                  diff = isc_time_microdiff (&now, &monotonic);
57726f
+                  // not implemented
57726f
+                }
57726f
+#else
57726f
+                TIME_NOW(&now;;
57726f
+                if (isc_time_compare (&now, &prev) < 0)
57726f
+                  return ISC_R_TIMESHIFTED;
57726f
+                TIME_NOW(&prev;;
57726f
+#endif
57726f
 		/*
57726f
 		 * Check the reload (or suspend) case first for exiting the
57726f
 		 * loop as fast as possible in case:
57726f
@@ -475,7 +507,6 @@ evloop(isc__appctx_t *ctx) {
57726f
 			if (result != ISC_R_SUCCESS)
57726f
 				tvp = NULL;
57726f
 			else {
57726f
-				uint64_t us;
57726f
 
57726f
 				TIME_NOW(&now;;
57726f
 				us = isc_time_microdiff(&when, &now;;
57726f
diff --git a/lib/isc/unix/include/isc/time.h b/lib/isc/unix/include/isc/time.h
57726f
index b864c29..5dd43c9 100644
57726f
--- a/lib/isc/unix/include/isc/time.h
57726f
+++ b/lib/isc/unix/include/isc/time.h
57726f
@@ -132,6 +132,26 @@ isc_time_isepoch(const isc_time_t *t);
57726f
  *\li	't' is a valid pointer.
57726f
  */
57726f
 
57726f
+#ifdef CLOCK_BOOTTIME
57726f
+isc_result_t
57726f
+isc_time_boottime(isc_time_t *t);
57726f
+/*%<
57726f
+ * Set 't' to monotonic time from previous boot
57726f
+ * it's not affected by system time change. It also
57726f
+ * includes the time system was suspended
57726f
+ *
57726f
+ * Requires:
57726f
+ *\li	't' is a valid pointer.
57726f
+ *
57726f
+ * Returns:
57726f
+ *
57726f
+ *\li	Success
57726f
+ *\li	Unexpected error
57726f
+ *		Getting the time from the system failed.
57726f
+ */
57726f
+#endif /* CLOCK_BOOTTIME */
57726f
+ 
57726f
+
57726f
 isc_result_t
57726f
 isc_time_now(isc_time_t *t);
57726f
 /*%<
57726f
diff --git a/lib/isc/unix/time.c b/lib/isc/unix/time.c
57726f
index 8edc9df..fe0bb91 100644
57726f
--- a/lib/isc/unix/time.c
57726f
+++ b/lib/isc/unix/time.c
57726f
@@ -498,3 +498,25 @@ isc_time_formatISO8601ms(const isc_time_t *t, char *buf, unsigned int len) {
57726f
 			 t->nanoseconds / NS_PER_MS);
57726f
 	}
57726f
 }
57726f
+
57726f
+
57726f
+#ifdef CLOCK_BOOTTIME
57726f
+isc_result_t
57726f
+isc_time_boottime(isc_time_t *t) {
57726f
+  struct timespec ts;
57726f
+  
57726f
+  char strbuf[ISC_STRERRORSIZE];
57726f
+
57726f
+  if (clock_gettime (CLOCK_BOOTTIME, &ts) != 0){
57726f
+    isc__strerror(errno, strbuf, sizeof(strbuf));
57726f
+    UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
57726f
+    return (ISC_R_UNEXPECTED);    
57726f
+  }
57726f
+
57726f
+  t->seconds = ts.tv_sec;
57726f
+  t->nanoseconds = ts.tv_nsec;
57726f
+
57726f
+  return (ISC_R_SUCCESS);
57726f
+  
57726f
+};
57726f
+#endif