[dovecot-cvs] dovecot/src/lib ioloop.c,1.40,1.41

tss at dovecot.org tss at dovecot.org
Tue Mar 6 20:27:17 EET 2007


Update of /var/lib/cvs/dovecot/src/lib
In directory talvi:/tmp/cvs-serv14602

Modified Files:
	ioloop.c 
Log Message:
If time moves backwards only max. 5 seconds, sleep instead of killing
ourself.



Index: ioloop.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/ioloop.c,v
retrieving revision 1.40
retrieving revision 1.41
diff -u -d -r1.40 -r1.41
--- ioloop.c	28 Feb 2007 20:19:42 -0000	1.40
+++ ioloop.c	6 Mar 2007 18:27:15 -0000	1.41
@@ -1,8 +1,13 @@
-/* Copyright (c) 2002-2003 Timo Sirainen */
+/* Copyright (c) 2002-2007 Timo Sirainen */
 
 #include "lib.h"
 #include "ioloop-internal.h"
 
+#include <unistd.h>
+
+/* If time moves backwards more than this, kill ourself instead of sleeping. */
+#define IOLOOP_MAX_TIME_BACKWARDS_SLEEP 5
+
 #define timer_is_larger(tvp, uvp) \
 	((tvp)->tv_sec > (uvp)->tv_sec || \
 	 ((tvp)->tv_sec == (uvp)->tv_sec && \
@@ -220,11 +225,25 @@
 	if (gettimeofday(&ioloop_timeval, &ioloop_timezone) < 0)
 		i_fatal("gettimeofday(): %m");
 
+	/* Don't bother comparing usecs. */
 	if (ioloop_time > ioloop_timeval.tv_sec) {
-		i_fatal("Time just moved backwards (%s -> %s)! "
-			"This might cause a lot of problems, "
-			"so I'll just kill myself now.",
-			dec2str(ioloop_time), dec2str(ioloop_timeval.tv_sec));
+		time_t diff = ioloop_time - ioloop_timeval.tv_sec;
+
+		if (diff > IOLOOP_MAX_TIME_BACKWARDS_SLEEP) {
+			i_fatal("Time just moved backwards by %ld seconds. "
+				"This might cause a lot of problems, "
+				"so I'll just kill myself now.", (long)diff);
+		} else {
+			i_error("Time just moved backwards by %ld seconds. "
+				"I'll sleep now until we're back in present.",
+				(long)diff);
+			/* Sleep extra second to make sure usecs also grows. */
+			if (sleep(diff + 1) != 0)
+				i_fatal("Sleep interrupted, byebye.");
+
+			/* Try again. */
+			io_loop_handle_timeouts(ioloop);
+		}
 	}
 
 	ioloop_time = ioloop_timeval.tv_sec;



More information about the dovecot-cvs mailing list