Blob Blame History Raw
diff -up ntp-4.2.4p7/include/ntp_refclock.h.sleep ntp-4.2.4p7/include/ntp_refclock.h
--- ntp-4.2.4p7/include/ntp_refclock.h.sleep	2006-06-06 22:16:19.000000000 +0200
+++ ntp-4.2.4p7/include/ntp_refclock.h	2009-07-21 16:38:47.000000000 +0200
@@ -260,6 +260,7 @@ extern	void	refclock_control P((struct s
 				    struct refclockstat *));
 extern	int	refclock_open	P((char *, u_int, u_int));
 extern	int	refclock_setup	P((int, u_int, u_int));
+extern	int	refclock_timer_needed	P((struct peer *));
 extern	void	refclock_timer	P((struct peer *));
 extern	void	refclock_transmit P((struct peer *));
 extern	int	refclock_ioctl	P((int, u_int));
diff -up ntp-4.2.4p7/include/ntp_stdlib.h.sleep ntp-4.2.4p7/include/ntp_stdlib.h
--- ntp-4.2.4p7/include/ntp_stdlib.h.sleep	2006-12-28 13:03:05.000000000 +0100
+++ ntp-4.2.4p7/include/ntp_stdlib.h	2009-07-21 16:38:47.000000000 +0200
@@ -101,6 +101,7 @@ extern	const char *	FindConfig	P((const 
 extern	void	signal_no_reset P((int, RETSIGTYPE (*func)(int)));
 
 extern	void	getauthkeys 	P((const char *));
+extern	int	auth_agekeys_is_needed P((void));
 extern	void	auth_agekeys	P((void));
 extern	void	rereadkeys	P((void));
 
diff -up ntp-4.2.4p7/include/ntpd.h.sleep ntp-4.2.4p7/include/ntpd.h
--- ntp-4.2.4p7/include/ntpd.h.sleep	2009-07-21 16:38:47.000000000 +0200
+++ ntp-4.2.4p7/include/ntpd.h	2009-07-21 16:38:47.000000000 +0200
@@ -120,8 +120,10 @@ extern	int	leap_actual P((int));
 /* ntp_loopfilter.c */
 extern	void	init_loopfilter P((void));
 extern	int 	local_clock P((struct peer *, double));
-extern	void	adj_host_clock	P((void));
+extern	int	adj_host_clock_is_needed P((void));
+extern	void	adj_host_clock	P((int));
 extern	void	loop_config P((int, double));
+extern	int	huffpuff_enabled P((void));
 extern	void	huffpuff	P((void));
 extern	u_long	sys_clocktime;
 extern	u_long	sys_tai;
@@ -221,6 +223,7 @@ extern	void	hack_restrict	P((int, struct
 /* ntp_timer.c */
 extern	void	init_timer	P((void));
 extern	void	reinit_timer	P((void));
+extern	int	when_next_event	P((void));
 extern	void	timer		P((void));
 extern	void	timer_clr_stats P((void));
 extern  void    timer_interfacetimeout P((u_long));
diff -up ntp-4.2.4p7/libntp/authkeys.c.sleep ntp-4.2.4p7/libntp/authkeys.c
--- ntp-4.2.4p7/libntp/authkeys.c.sleep	2004-02-25 06:58:03.000000000 +0100
+++ ntp-4.2.4p7/libntp/authkeys.c	2009-07-21 16:38:47.000000000 +0200
@@ -394,6 +394,24 @@ auth_delkeys(void)
 	}
 }
 
+int auth_agekeys_is_needed() {
+	struct savekey *sk;
+	int i;
+
+	if (authnumkeys > 20)
+		return 1;
+
+	for (i = 0; i < HASHSIZE; i++) {
+		sk = key_hash[i];
+		while (sk != 0) {
+			if (sk->lifetime > 0)
+				return 1;
+			sk = sk->next;
+		}
+	}
+	return 0;
+}
+
 /*
  * auth_agekeys - delete keys whose lifetimes have expired
  */
diff -up ntp-4.2.4p7/ntpd/ntp_loopfilter.c.sleep ntp-4.2.4p7/ntpd/ntp_loopfilter.c
--- ntp-4.2.4p7/ntpd/ntp_loopfilter.c.sleep	2009-07-21 16:38:47.000000000 +0200
+++ ntp-4.2.4p7/ntpd/ntp_loopfilter.c	2009-07-21 16:38:47.000000000 +0200
@@ -752,6 +752,10 @@ local_clock(
 #endif /* LOCKCLOCK */
 }
 
+int adj_host_clock_is_needed() {
+	return !(!ntp_enable || mode_ntpdate || (pll_control &&
+	    kern_enable));
+}
 
 /*
  * adj_host_clock - Called once every second to update the local clock.
@@ -761,7 +765,7 @@ local_clock(
  */
 void
 adj_host_clock(
-	void
+	int time_elapsed
 	)
 {
 	double	adjustment;
@@ -776,7 +780,8 @@ adj_host_clock(
 	 * maximum error and the local clock driver will pick it up and
 	 * pass to the common refclock routines. Very elegant.
 	 */
-	sys_rootdispersion += clock_phi;
+	sys_rootdispersion += clock_phi * time_elapsed;
+	DPRINTF(2, ("loopfilter: %d\n", time_elapsed));
 
 #ifndef LOCKCLOCK
 	/*
@@ -833,6 +838,11 @@ rstclock(
 }
 
 
+int huffpuff_enabled()
+{
+	return sys_huffpuff != NULL;
+}
+
 /*
  * huff-n'-puff filter
  */
diff -up ntp-4.2.4p7/ntpd/ntp_refclock.c.sleep ntp-4.2.4p7/ntpd/ntp_refclock.c
--- ntp-4.2.4p7/ntpd/ntp_refclock.c.sleep	2006-06-06 22:16:43.000000000 +0200
+++ ntp-4.2.4p7/ntpd/ntp_refclock.c	2009-07-21 16:38:47.000000000 +0200
@@ -309,6 +309,21 @@ refclock_unpeer(
 }
 
 
+int
+refclock_timer_needed(
+	struct peer *peer	/* peer structure pointer */
+	)
+{
+	u_char clktype;
+	int unit;
+
+	clktype = peer->refclktype;
+	unit = peer->refclkunit;
+	if (refclock_conf[clktype]->clock_timer != noentry)
+		return 1;
+	return 0;
+}
+
 /*
  * refclock_timer - called once per second for housekeeping.
  */
diff -up ntp-4.2.4p7/ntpd/ntp_timer.c.sleep ntp-4.2.4p7/ntpd/ntp_timer.c
--- ntp-4.2.4p7/ntpd/ntp_timer.c.sleep	2009-05-12 07:58:55.000000000 +0200
+++ ntp-4.2.4p7/ntpd/ntp_timer.c	2009-07-21 16:38:47.000000000 +0200
@@ -63,6 +63,7 @@ volatile u_long alarm_overflow;
 #define HOUR	(60*60)
 
 u_long current_time;
+l_fp timer_base;
 
 /*
  * Stats.  Number of overflows and number of calls to transmit().
@@ -99,6 +100,8 @@ static	RETSIGTYPE alarming P((int));
 void 
 reinit_timer(void)
 {
+	get_systime(&timer_base);
+#if 0
 #if !defined(SYS_WINNT) && !defined(VMS)
 #  if defined(HAVE_TIMER_CREATE) && defined(HAVE_TIMER_SETTIME)
 	timer_gettime(ntpd_timerid, &itimer);
@@ -132,6 +135,7 @@ reinit_timer(void)
 	setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
 #  endif
 # endif /* VMS */
+#endif
 }
 
 /*
@@ -159,6 +163,8 @@ init_timer(void)
 	timer_xmtcalls = 0;
 	timer_timereset = 0;
 
+	get_systime(&timer_base);
+#if 0
 #if !defined(SYS_WINNT)
 	/*
 	 * Set up the alarm interrupt.	The first comes 2**EVENT_TIMEOUT
@@ -242,6 +248,7 @@ init_timer(void)
 	}
 
 #endif /* SYS_WINNT */
+#endif
 }
 
 #if defined(SYS_WINNT)
@@ -252,6 +259,46 @@ get_timer_handle(void)
 }
 #endif
 
+int when_next_event() {
+	register struct peer *peer, *next_peer;
+	u_int n;
+	int next = current_time + HOUR;
+
+	if (adj_host_clock_is_needed())
+		return 1;
+	for (n = 0; n < NTP_HASH_SIZE; n++) {
+		for (peer = peer_hash[n]; peer != 0; peer = next_peer) {
+			next_peer = peer->next;
+#ifdef REFCLOCK
+			if (peer->flags & FLAG_REFCLOCK && refclock_timer_needed(peer))
+				return 1;
+#endif /* REFCLOCK */
+			if (peer->action && peer->nextaction < next)
+				next = peer->nextaction;
+			if (peer->nextdate < next)
+				next = peer->nextdate;
+		}
+	}
+
+	if (auth_agekeys_is_needed() && keys_timer < next)
+		next = keys_timer;
+	if (huffpuff_enabled() && huffpuff_timer < next)
+		next = huffpuff_timer;
+#ifdef OPENSSL
+	if (revoke_timer < next)
+		next = revoke_timer;
+#endif /* OPENSSL */
+	if (interface_interval && interface_timer < next)
+		next = interface_timer;
+	if (stats_timer < next)
+		next = stats_timer;
+
+	next -= current_time;
+	if (next <= 0)
+		next = 1;
+	return next;
+}
+
 /*
  * timer - dispatch anyone who needs to be
  */
@@ -264,14 +311,12 @@ timer(void)
 #endif /* OPENSSL */
 	u_int n;
 
-	current_time += (1<<EVENT_TIMEOUT);
-
 	/*
 	 * Adjustment timeout first.
 	 */
 	if (adjust_timer <= current_time) {
-		adjust_timer += 1;
-		adj_host_clock();
+		adj_host_clock(current_time - adjust_timer + 1);
+		adjust_timer = current_time + 1;
 		kod_proto();
 #ifdef REFCLOCK
 		for (n = 0; n < NTP_HASH_SIZE; n++) {
diff -up ntp-4.2.4p7/ntpd/ntpd.c.sleep ntp-4.2.4p7/ntpd/ntpd.c
--- ntp-4.2.4p7/ntpd/ntpd.c.sleep	2009-07-21 16:38:47.000000000 +0200
+++ ntp-4.2.4p7/ntpd/ntpd.c	2009-07-21 16:39:29.000000000 +0200
@@ -179,8 +179,6 @@ extern const char *Version;
 
 char const *progname;
 
-int was_alarmed;
-
 #ifdef DECL_SYSCALL
 /*
  * We put this here, since the argument profile is syscall-specific
@@ -434,6 +432,7 @@ set_process_priority(void)
 		msyslog(LOG_ERR, "set_process_priority: No way found to improve our priority");
 }
 
+#define TS_LAST_SIZE 2
 
 /*
  * Main program.  Initialize us, disconnect us from the tty if necessary,
@@ -446,6 +445,9 @@ ntpdmain(
 	)
 {
 	l_fp now;
+	l_fp ts_last[TS_LAST_SIZE];
+	unsigned int ts_last_index;
+	int time_elapsed;
 	struct recvbuf *rbuf;
 #ifdef _AIX			/* HMS: ifdef SIGDANGER? */
 	struct sigaction sa;
@@ -998,7 +1000,11 @@ getgroup:	
 #else /* normal I/O */
 
 	BLOCK_IO_AND_ALARM();
-	was_alarmed = 0;
+
+	for (ts_last_index = 0; ts_last_index < TS_LAST_SIZE; ts_last_index++)
+		L_CLR(&ts_last[ts_last_index]);
+	time_elapsed = ts_last_index = 0;
+
 	for (;;)
 	{
 # if !defined(HAVE_SIGNALED_IO) 
@@ -1009,39 +1015,66 @@ getgroup:	
 		int nfound;
 # endif
 
-		if (alarm_flag) 	/* alarmed? */
-		{
-			was_alarmed = 1;
-			alarm_flag = 0;
-		}
-
-		if (!was_alarmed && has_full_recv_buffer() == ISC_FALSE)
+		if (has_full_recv_buffer() == ISC_FALSE)
 		{
 			/*
 			 * Nothing to do.  Wait for something.
 			 */
 # ifndef HAVE_SIGNALED_IO
+			extern l_fp timer_base;
+			l_fp ts, ts2, ts3;
+			double d;
+
 			rdfdes = activefds;
-#  if defined(VMS) || defined(SYS_VXWORKS)
-			/* make select() wake up after one second */
-			{
+			ts2 = timer_base;
+			get_systime(&ts);
+			ts3 = ts;
+			L_SUB(&ts3, &ts_last[ts_last_index]);
+
+			/* don't call when_next_event() too often */
+			if (ts3.l_ui)
+				ts2.l_ui += when_next_event();
+			else
+				ts2.l_ui += 1;
+
+			L_SUB(&ts2, &ts);
+			LFPTOD(&ts2, d);
+			DPRINTF(2, ("main: scheduled event in %f\n", d));
+			if (d > 0.0) {
 				struct timeval t1;
 
-				t1.tv_sec = 1; t1.tv_usec = 0;
+				t1.tv_sec = d;
+				t1.tv_usec = (d - t1.tv_sec) * 1000000;
 				nfound = select(maxactivefd+1, &rdfdes, (fd_set *)0,
 						(fd_set *)0, &t1);
+				get_systime(&ts);
+			} else
+				nfound = 0;
+
+			ts3 = ts;
+			L_SUB(&ts3, &timer_base);
+#ifdef DEBUG 
+			LFPTOD(&ts3, d);
+			DPRINTF(2, ("main: elapsed %f\n", d));
+#endif
+
+			if (ts3.l_i < 0 || ts3.l_ui > ts2.l_ui + 10) {
+#ifdef DEBUG 
+				DPRINTF(2, ("main: unexpected time jump\n"));
+#endif
+				ts3.l_ui = 0;
+				reinit_timer();
 			}
-#  else
-			nfound = select(maxactivefd+1, &rdfdes, (fd_set *)0,
-					(fd_set *)0, (struct timeval *)0);
-#  endif /* VMS */
-			if (nfound > 0)
-			{
-				l_fp ts;
 
-				get_systime(&ts);
+			time_elapsed += ts3.l_ui;
+			current_time += ts3.l_ui;
+			timer_base.l_ui += ts3.l_ui;
 
+			if (nfound > 0)
+			{
 				(void)input_handler(&ts);
+				ts_last[ts_last_index] = ts;
+				ts_last_index = (ts_last_index + 1) % TS_LAST_SIZE;
 			}
 			else if (nfound == -1 && errno != EINTR)
 				netsyslog(LOG_ERR, "select() error: %m");
@@ -1050,17 +1083,12 @@ getgroup:	
 				netsyslog(LOG_DEBUG, "select(): nfound=%d, error: %m", nfound);
 #  endif /* DEBUG */
 # else /* HAVE_SIGNALED_IO */
-                        
+#  error not supported
 			wait_for_signal();
 # endif /* HAVE_SIGNALED_IO */
-			if (alarm_flag) 	/* alarmed? */
-			{
-				was_alarmed = 1;
-				alarm_flag = 0;
-			}
 		}
 
-		if (was_alarmed)
+		if (time_elapsed)
 		{
 			UNBLOCK_IO_AND_ALARM();
 			/*
@@ -1068,7 +1096,7 @@ getgroup:	
 			 * to process expiry.
 			 */
 			timer();
-			was_alarmed = 0;
+			time_elapsed = 0;
                         BLOCK_IO_AND_ALARM();
 		}
 
@@ -1086,19 +1114,8 @@ getgroup:	
 			rbuf = get_full_recv_buffer();
 			while (rbuf != NULL)
 			{
-				if (alarm_flag)
-				{
-					was_alarmed = 1;
-					alarm_flag = 0;
-				}
 				UNBLOCK_IO_AND_ALARM();
 
-				if (was_alarmed)
-				{	/* avoid timer starvation during lengthy I/O handling */
-					timer();
-					was_alarmed = 0;
-				}
-
 				/*
 				 * Call the data procedure to handle each received
 				 * packet.