302b22
diff --git a/lib/isc/include/isc/result.h b/lib/isc/include/isc/result.h
302b22
index 246aefb..0007543 100644
302b22
--- a/lib/isc/include/isc/result.h
302b22
+++ b/lib/isc/include/isc/result.h
302b22
@@ -83,9 +83,9 @@
302b22
 #define ISC_R_UNSET			61	/*%< unset */
302b22
 #define ISC_R_MULTIPLE			62	/*%< multiple */
302b22
 #define ISC_R_WOULDBLOCK		63	/*%< would block */
302b22
-
302b22
+#define ISC_R_TIMESHIFTED               64      /*%< system time changed */
302b22
 /*% Not a result code: the number of results. */
302b22
-#define ISC_R_NRESULTS 			64
302b22
+#define ISC_R_NRESULTS 			66
302b22
 
302b22
 ISC_LANG_BEGINDECLS
302b22
 
302b22
diff --git a/lib/isc/include/isc/util.h b/lib/isc/include/isc/util.h
302b22
index 332dc0c..f81967d 100644
302b22
--- a/lib/isc/include/isc/util.h
302b22
+++ b/lib/isc/include/isc/util.h
302b22
@@ -233,6 +233,10 @@
302b22
  * Time
302b22
  */
302b22
 #define TIME_NOW(tp) 	RUNTIME_CHECK(isc_time_now((tp)) == ISC_R_SUCCESS)
302b22
+#ifdef CLOCK_BOOTTIME
302b22
+#define TIME_MONOTONIC(tp) 	RUNTIME_CHECK(isc_time_boottime((tp)) == ISC_R_SUCCESS)
302b22
+#endif
302b22
+
302b22
 
302b22
 /*%
302b22
  * Misc
302b22
diff --git a/lib/isc/result.c b/lib/isc/result.c
302b22
index a707c32..6776fc6 100644
302b22
--- a/lib/isc/result.c
302b22
+++ b/lib/isc/result.c
302b22
@@ -99,6 +99,7 @@ static const char *description[ISC_R_NRESULTS] = {
302b22
 	"unset",				/*%< 61 */
302b22
 	"multiple",				/*%< 62 */
302b22
 	"would block",				/*%< 63 */
302b22
+        "time changed",                         /*%< 64 */
302b22
 };
302b22
 
302b22
 static const char *identifier[ISC_R_NRESULTS] = {
302b22
@@ -166,6 +167,7 @@ static const char *identifier[ISC_R_NRESULTS] = {
302b22
 	"ISC_R_UNSET",
302b22
 	"ISC_R_MULTIPLE",
302b22
 	"ISC_R_WOULDBLOCK",
302b22
+        "ISC_R_TIMESHIFTED",
302b22
 };
302b22
 
302b22
 #define ISC_RESULT_RESULTSET			2
302b22
diff --git a/lib/isc/unix/app.c b/lib/isc/unix/app.c
302b22
index bace2bd..e9814d2 100644
302b22
--- a/lib/isc/unix/app.c
302b22
+++ b/lib/isc/unix/app.c
302b22
@@ -441,15 +441,51 @@ isc__app_ctxonrun(isc_appctx_t *ctx0, isc_mem_t *mctx, isc_task_t *task,
302b22
 static isc_result_t
302b22
 evloop(isc__appctx_t *ctx) {
302b22
 	isc_result_t result;
302b22
+        isc_time_t now;
302b22
+#ifdef CLOCK_BOOTTIME
302b22
+        isc_time_t monotonic;
302b22
+        isc_uint64_t diff  = 0;
302b22
+#else
302b22
+        isc_time_t prev;
302b22
+        TIME_NOW(&prev;;
302b22
+#endif
302b22
+
302b22
+
302b22
+
302b22
 
302b22
 	while (!ctx->want_shutdown) {
302b22
 		int n;
302b22
-		isc_time_t when, now;
302b22
+		isc_time_t when;
302b22
+                
302b22
 		struct timeval tv, *tvp;
302b22
 		isc_socketwait_t *swait;
302b22
 		isc_boolean_t readytasks;
302b22
 		isc_boolean_t call_timer_dispatch = ISC_FALSE;
302b22
 
302b22
+                isc_uint64_t us; 
302b22
+
302b22
+#ifdef CLOCK_BOOTTIME
302b22
+                // TBD macros for following three lines
302b22
+                TIME_NOW(&now;;
302b22
+                TIME_MONOTONIC(&monotonic);
302b22
+                // INSIST(now.seconds > monotonic.seconds) 
302b22
+                us = isc_time_microdiff (&now, &monotonic);
302b22
+                if (us < diff){ 
302b22
+                  us = diff - us;
302b22
+                  if (us > 1000000){ // ignoring shifts less than one second
302b22
+                    return ISC_R_TIMESHIFTED;
302b22
+                  };
302b22
+                  diff = isc_time_microdiff (&now, &monotonic);
302b22
+                } else {
302b22
+                  diff = isc_time_microdiff (&now, &monotonic);
302b22
+                  // not implemented
302b22
+                }
302b22
+#else
302b22
+                TIME_NOW(&now;;
302b22
+                if (isc_time_compare (&now, &prev) < 0)
302b22
+                  return ISC_R_TIMESHIFTED;
302b22
+                TIME_NOW(&prev;;
302b22
+#endif                
302b22
 		/*
302b22
 		 * Check the reload (or suspend) case first for exiting the
302b22
 		 * loop as fast as possible in case:
302b22
@@ -474,9 +510,10 @@ evloop(isc__appctx_t *ctx) {
302b22
 			if (result != ISC_R_SUCCESS)
302b22
 				tvp = NULL;
302b22
 			else {
302b22
-				isc_uint64_t us;
302b22
+
302b22
 
302b22
 				TIME_NOW(&now;;
302b22
+
302b22
 				us = isc_time_microdiff(&when, &now;;
302b22
 				if (us == 0)
302b22
 					call_timer_dispatch = ISC_TRUE;
302b22
diff --git a/lib/isc/unix/include/isc/time.h b/lib/isc/unix/include/isc/time.h
302b22
index 75e24b9..de8b399 100644
302b22
--- a/lib/isc/unix/include/isc/time.h
302b22
+++ b/lib/isc/unix/include/isc/time.h
302b22
@@ -129,6 +129,26 @@ isc_time_isepoch(const isc_time_t *t);
302b22
  *\li	't' is a valid pointer.
302b22
  */
302b22
 
302b22
+#ifdef CLOCK_BOOTTIME
302b22
+isc_result_t
302b22
+isc_time_boottime(isc_time_t *t);
302b22
+/*%<
302b22
+ * Set 't' to monotonic time from previous boot
302b22
+ * it's not affected by system time change. It also
302b22
+ * includes the time system was suspended
302b22
+ *
302b22
+ * Requires:
302b22
+ *\li	't' is a valid pointer.
302b22
+ *
302b22
+ * Returns:
302b22
+ *
302b22
+ *\li	Success
302b22
+ *\li	Unexpected error
302b22
+ *		Getting the time from the system failed.
302b22
+ */
302b22
+#endif /* CLOCK_BOOTTIME */
302b22
+ 
302b22
+
302b22
 isc_result_t
302b22
 isc_time_now(isc_time_t *t);
302b22
 /*%<
302b22
diff --git a/lib/isc/unix/time.c b/lib/isc/unix/time.c
302b22
index 2210240..d7613b8 100644
302b22
--- a/lib/isc/unix/time.c
302b22
+++ b/lib/isc/unix/time.c
302b22
@@ -496,3 +496,25 @@ isc_time_formatISO8601ms(const isc_time_t *t, char *buf, unsigned int len) {
302b22
 			 t->nanoseconds / NS_PER_MS);
302b22
 	}
302b22
 }
302b22
+
302b22
+
302b22
+#ifdef CLOCK_BOOTTIME
302b22
+isc_result_t
302b22
+isc_time_boottime(isc_time_t *t) {
302b22
+  struct timespec ts;
302b22
+  
302b22
+  char strbuf[ISC_STRERRORSIZE];
302b22
+
302b22
+  if (clock_gettime (CLOCK_BOOTTIME, &ts) != 0){
302b22
+    isc__strerror(errno, strbuf, sizeof(strbuf));
302b22
+    UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
302b22
+    return (ISC_R_UNEXPECTED);    
302b22
+  }
302b22
+
302b22
+  t->seconds = ts.tv_sec;
302b22
+  t->nanoseconds = ts.tv_nsec;
302b22
+
302b22
+  return (ISC_R_SUCCESS);
302b22
+  
302b22
+};
302b22
+#endif