Blame SOURCES/ntp-4.2.6p1-sleep.patch

473877
diff -up ntp-4.2.6p1/include/ntp_refclock.h.sleep ntp-4.2.6p1/include/ntp_refclock.h
473877
--- ntp-4.2.6p1/include/ntp_refclock.h.sleep	2009-12-09 08:36:35.000000000 +0100
473877
+++ ntp-4.2.6p1/include/ntp_refclock.h	2010-03-10 19:27:46.000000000 +0100
473877
@@ -260,6 +260,7 @@ extern	void	refclock_control (sockaddr_u
473877
 				    struct refclockstat *);
473877
 extern	int	refclock_open	(char *, u_int, u_int);
473877
 extern	int	refclock_setup	(int, u_int, u_int);
473877
+extern	int	refclock_timer_needed	(struct peer *);
473877
 extern	void	refclock_timer	(struct peer *);
473877
 extern	void	refclock_transmit (struct peer *);
473877
 extern	int	refclock_ioctl	(int, u_int);
473877
diff -up ntp-4.2.6p1/include/ntp_stdlib.h.sleep ntp-4.2.6p1/include/ntp_stdlib.h
473877
--- ntp-4.2.6p1/include/ntp_stdlib.h.sleep	2009-12-09 08:36:35.000000000 +0100
473877
+++ ntp-4.2.6p1/include/ntp_stdlib.h	2010-03-10 19:27:46.000000000 +0100
473877
@@ -116,6 +116,7 @@ extern	const char * FindConfig	(const ch
473877
 extern	void	signal_no_reset (int, RETSIGTYPE (*func)(int));
473877
 
473877
 extern	void	getauthkeys 	(const char *);
473877
+extern	int	auth_agekeys_needed (void);
473877
 extern	void	auth_agekeys	(void);
473877
 extern	void	rereadkeys	(void);
473877
 
473877
diff -up ntp-4.2.6p1/include/ntpd.h.sleep ntp-4.2.6p1/include/ntpd.h
473877
--- ntp-4.2.6p1/include/ntpd.h.sleep	2009-12-09 08:36:35.000000000 +0100
473877
+++ ntp-4.2.6p1/include/ntpd.h	2010-03-10 19:27:46.000000000 +0100
473877
@@ -112,8 +112,10 @@ extern	void	block_io_and_alarm	(void);
473877
 /* ntp_loopfilter.c */
473877
 extern	void	init_loopfilter(void);
473877
 extern	int 	local_clock(struct peer *, double);
473877
-extern	void	adj_host_clock(void);
473877
+extern	int	adj_host_clock_needed(void);
473877
+extern	void	adj_host_clock(int);
473877
 extern	void	loop_config(int, double);
473877
+extern	int	huffpuff_enabled(void);
473877
 extern	void	huffpuff(void);
473877
 extern	u_long	sys_clocktime;
473877
 extern	u_int	sys_tai;
473877
@@ -219,6 +221,8 @@ extern	void	hack_restrict	(int, sockaddr
473877
 /* ntp_timer.c */
473877
 extern	void	init_timer	(void);
473877
 extern	void	reinit_timer	(void);
473877
+extern	double	get_timeout	(l_fp *);
473877
+extern	int	timer_elapsed	(l_fp, int);
473877
 extern	void	timer		(void);
473877
 extern	void	timer_clr_stats (void);
473877
 extern  void    timer_interfacetimeout (u_long);
473877
diff -up ntp-4.2.6p1/libntp/authkeys.c.sleep ntp-4.2.6p1/libntp/authkeys.c
473877
--- ntp-4.2.6p1/libntp/authkeys.c.sleep	2009-12-09 08:36:35.000000000 +0100
473877
+++ ntp-4.2.6p1/libntp/authkeys.c	2010-03-10 19:27:46.000000000 +0100
473877
@@ -445,6 +445,25 @@ auth_delkeys(void)
473877
 	}
473877
 }
473877
 
473877
+int
473877
+auth_agekeys_needed(void) {
473877
+	struct savekey *sk;
473877
+	int i;
473877
+
473877
+	if (authnumkeys > 20)
473877
+		return 1;
473877
+
473877
+	for (i = 0; i < HASHSIZE; i++) {
473877
+		sk = key_hash[i];
473877
+		while (sk != 0) {
473877
+			if (sk->lifetime > 0)
473877
+				return 1;
473877
+			sk = sk->next;
473877
+		}
473877
+	}
473877
+	return 0;
473877
+}
473877
+
473877
 /*
473877
  * auth_agekeys - delete keys whose lifetimes have expired
473877
  */
473877
diff -up ntp-4.2.6p1/ntpd/ntp_loopfilter.c.sleep ntp-4.2.6p1/ntpd/ntp_loopfilter.c
473877
--- ntp-4.2.6p1/ntpd/ntp_loopfilter.c.sleep	2009-12-09 08:36:36.000000000 +0100
473877
+++ ntp-4.2.6p1/ntpd/ntp_loopfilter.c	2010-03-10 19:27:46.000000000 +0100
473877
@@ -677,6 +677,13 @@ local_clock(
473877
 #endif /* LOCKCLOCK */
473877
 }
473877
 
473877
+int
473877
+adj_host_clock_needed(void)
473877
+{
473877
+	return !(!ntp_enable || mode_ntpdate || (pll_control &&
473877
+	    kern_enable));
473877
+}
473877
+ 
473877
 
473877
 /*
473877
  * adj_host_clock - Called once every second to update the local clock.
473877
@@ -686,7 +693,7 @@ local_clock(
473877
  */
473877
 void
473877
 adj_host_clock(
473877
-	void
473877
+	int time_elapsed
473877
 	)
473877
 {
473877
 	double	adjustment;
473877
@@ -698,7 +705,7 @@ adj_host_clock(
473877
 	 * since the poll interval can exceed one day, the old test
473877
 	 * would be counterproductive.
473877
 	 */
473877
-	sys_rootdisp += clock_phi;
473877
+	sys_rootdisp += clock_phi * time_elapsed;
473877
 
473877
 #ifndef LOCKCLOCK
473877
 	/*
473877
@@ -819,6 +826,12 @@ set_freq(
473877
 #endif /* KERNEL_PLL */
473877
 }
473877
 
473877
+int
473877
+huffpuff_enabled(void)
473877
+{
473877
+	return sys_huffpuff != NULL;
473877
+}
473877
+
473877
 /*
473877
  * huff-n'-puff filter
473877
  */
473877
diff -up ntp-4.2.6p1/ntpd/ntp_refclock.c.sleep ntp-4.2.6p1/ntpd/ntp_refclock.c
473877
--- ntp-4.2.6p1/ntpd/ntp_refclock.c.sleep	2009-12-09 08:36:36.000000000 +0100
473877
+++ ntp-4.2.6p1/ntpd/ntp_refclock.c	2010-03-10 19:27:46.000000000 +0100
473877
@@ -268,6 +268,21 @@ refclock_unpeer(
473877
 }
473877
 
473877
 
473877
+int
473877
+refclock_timer_needed(
473877
+	struct peer *peer	/* peer structure pointer */
473877
+	)
473877
+{
473877
+	u_char clktype;
473877
+	int unit;
473877
+
473877
+	clktype = peer->refclktype;
473877
+	unit = peer->refclkunit;
473877
+	if (refclock_conf[clktype]->clock_timer != noentry)
473877
+		return 1;
473877
+	return 0;
473877
+}
473877
+
473877
 /*
473877
  * refclock_timer - called once per second for housekeeping.
473877
  */
473877
diff -up ntp-4.2.6p1/ntpd/ntp_timer.c.sleep ntp-4.2.6p1/ntpd/ntp_timer.c
473877
--- ntp-4.2.6p1/ntpd/ntp_timer.c.sleep	2009-12-09 08:36:35.000000000 +0100
473877
+++ ntp-4.2.6p1/ntpd/ntp_timer.c	2010-03-11 15:23:59.000000000 +0100
473877
@@ -56,7 +56,6 @@ static	u_long adjust_timer;	/* second ti
473877
 static	u_long stats_timer;	/* stats timer */
473877
 static	u_long huffpuff_timer;	/* huff-n'-puff timer */
473877
 u_long	leapsec;		/* leapseconds countdown */
473877
-l_fp	sys_time;		/* current system time */
473877
 #ifdef OPENSSL
473877
 static	u_long revoke_timer;	/* keys revoke timer */
473877
 static	u_long keys_timer;	/* session key timer */
473877
@@ -74,6 +73,12 @@ volatile u_long alarm_overflow;
473877
 #define	DAY	(24 * HOUR)
473877
 
473877
 u_long current_time;		/* seconds since startup */
473877
+l_fp timer_base;
473877
+int time_elapsed;
473877
+
473877
+#define TIMEOUT_TS_SIZE 2
473877
+l_fp timeout_ts[TIMEOUT_TS_SIZE];
473877
+unsigned int timeout_ts_index;
473877
 
473877
 /*
473877
  * Stats.  Number of overflows and number of calls to transmit().
473877
@@ -110,6 +115,8 @@ static	RETSIGTYPE alarming (int);
473877
 void 
473877
 reinit_timer(void)
473877
 {
473877
+	get_systime(&timer_base);
473877
+#if 0
473877
 #if !defined(SYS_WINNT) && !defined(VMS)
473877
 #  if defined(HAVE_TIMER_CREATE) && defined(HAVE_TIMER_SETTIME)
473877
 	timer_gettime(ntpd_timerid, &itimer);
473877
@@ -143,6 +150,7 @@ reinit_timer(void)
473877
 	setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
473877
 #  endif
473877
 # endif /* VMS */
473877
+#endif
473877
 }
473877
 
473877
 /*
473877
@@ -165,6 +173,12 @@ init_timer(void)
473877
 	timer_xmtcalls = 0;
473877
 	timer_timereset = 0;
473877
 
473877
+	get_systime(&timer_base);
473877
+
473877
+	for (timeout_ts_index = 0; timeout_ts_index < TIMEOUT_TS_SIZE; timeout_ts_index++)
473877
+		L_CLR(&timeout_ts[timeout_ts_index]);
473877
+	timeout_ts_index = 0;
473877
+#if 0
473877
 #if !defined(SYS_WINNT)
473877
 	/*
473877
 	 * Set up the alarm interrupt.	The first comes 2**EVENT_TIMEOUT
473877
@@ -226,6 +240,7 @@ init_timer(void)
473877
 	}
473877
 
473877
 #endif /* SYS_WINNT */
473877
+#endif
473877
 }
473877
 
473877
 #if defined(SYS_WINNT)
473877
@@ -236,6 +251,104 @@ get_timer_handle(void)
473877
 }
473877
 #endif
473877
 
473877
+double
473877
+get_timeout(l_fp *now)
473877
+{
473877
+	register struct peer *peer, *next_peer;
473877
+	u_int	n;
473877
+	double r;
473877
+	int next;
473877
+	l_fp ts;
473877
+
473877
+	ts = *now;
473877
+	L_SUB(&ts, &timeout_ts[timeout_ts_index]);
473877
+	timeout_ts[timeout_ts_index] = *now;
473877
+	timeout_ts_index = (timeout_ts_index + 1) % TIMEOUT_TS_SIZE;
473877
+
473877
+	/* don't waste CPU time if called too frequently */
473877
+	if (ts.l_ui == 0) {
473877
+		next = 1;
473877
+		goto finish;
473877
+	}
473877
+
473877
+	next = current_time + HOUR;
473877
+
473877
+	if (adj_host_clock_needed()) {
473877
+		next = 1;
473877
+		goto finish;
473877
+	}
473877
+	for (n = 0; n < NTP_HASH_SIZE; n++) {
473877
+		for (peer = peer_hash[n]; peer != 0; peer = next_peer) {
473877
+			next_peer = peer->next;
473877
+#ifdef REFCLOCK
473877
+			if (peer->flags & FLAG_REFCLOCK && refclock_timer_needed(peer)) {
473877
+				next = 1;
473877
+				goto finish;
473877
+			}
473877
+#endif /* REFCLOCK */
473877
+			if (peer->action)
473877
+			       next = min(next, peer->nextaction);
473877
+			next = min(next, peer->nextdate);
473877
+		}
473877
+	}
473877
+
473877
+	if (leapsec > 0)
473877
+		next = min(next, leapsec);
473877
+
473877
+	if (huffpuff_enabled())
473877
+		next = min(next, huffpuff_timer);
473877
+
473877
+#ifdef OPENSSL
473877
+	if (auth_agekeys_needed())
473877
+		next = min(next, keys_timer);
473877
+	if (sys_leap != LEAP_NOTINSYNC)
473877
+		next = min(next, revoke_timer);
473877
+#endif /* OPENSSL */
473877
+
473877
+	if (interface_interval)
473877
+		next = min(next, interface_timer);
473877
+
473877
+	next = min(next, stats_timer);
473877
+
473877
+	next -= current_time;
473877
+	if (next <= 0)
473877
+		next = 1;
473877
+finish:
473877
+	ts = timer_base;
473877
+	ts.l_ui += next;
473877
+	L_SUB(&ts, now);
473877
+	LFPTOD(&ts, r);
473877
+#ifdef DEBUG 
473877
+	DPRINTF(2, ("timer: timeout %f\n", r));
473877
+#endif
473877
+
473877
+	return r;
473877
+}
473877
+
473877
+int
473877
+timer_elapsed(l_fp now, int timeout)
473877
+{
473877
+	int elapsed;
473877
+
473877
+	L_SUB(&now, &timer_base);
473877
+	elapsed = now.l_i;
473877
+	if (elapsed < 0 || elapsed > timeout + 10) {
473877
+#ifdef DEBUG 
473877
+		DPRINTF(2, ("timer: unexpected time jump\n"));
473877
+#endif
473877
+		elapsed = 0;
473877
+		reinit_timer();
473877
+
473877
+	}
473877
+	timer_base.l_ui += elapsed;
473877
+	time_elapsed += elapsed;
473877
+	current_time += elapsed;
473877
+#ifdef DEBUG 
473877
+	DPRINTF(2, ("timer: time elapsed %d\n", time_elapsed));
473877
+#endif
473877
+	return time_elapsed;
473877
+}
473877
+
473877
 /*
473877
  * timer - event timer
473877
  */
473877
@@ -251,11 +364,9 @@ timer(void)
473877
 	 * kiss-o'-deatch function and implement the association
473877
 	 * polling function..
473877
 	 */
473877
-	current_time++;
473877
-	get_systime(&sys_time);
473877
 	if (adjust_timer <= current_time) {
473877
-		adjust_timer += 1;
473877
-		adj_host_clock();
473877
+		adjust_timer += time_elapsed;
473877
+		adj_host_clock(time_elapsed);
473877
 #ifdef REFCLOCK
473877
 		for (n = 0; n < NTP_HASH_SIZE; n++) {
473877
 			for (peer = peer_hash[n]; peer != 0; peer = next_peer) {
473877
@@ -286,7 +397,7 @@ timer(void)
473877
 			 * 128 s or less.
473877
 			 */
473877
 			if (peer->throttle > 0)
473877
-				peer->throttle--;
473877
+				peer->throttle -= min(peer->throttle, time_elapsed);
473877
 			if (peer->nextdate <= current_time) {
473877
 #ifdef REFCLOCK
473877
 				if (peer->flags & FLAG_REFCLOCK)
473877
@@ -333,7 +444,7 @@ timer(void)
473877
 	 * set.
473877
 	 */
473877
 	if (leapsec > 0) {
473877
-		leapsec--;
473877
+		leapsec -= min(leapsec, time_elapsed);
473877
 		if (leapsec == 0) {
473877
 			sys_leap = LEAP_NOWARNING;
473877
 			sys_tai = leap_tai;
473877
@@ -398,11 +509,15 @@ timer(void)
473877
 	 * Finally, write hourly stats.
473877
 	 */
473877
 	if (stats_timer <= current_time) {
473877
+		l_fp sys_time;
473877
+		get_systime(&sys_time);
473877
 		stats_timer += HOUR;
473877
 		write_stats();
473877
 		if (sys_tai != 0 && sys_time.l_ui > leap_expire)
473877
 			report_event(EVNT_LEAPVAL, NULL, NULL);
473877
 	}
473877
+
473877
+	time_elapsed = 0;
473877
 }
473877
 
473877
 
473877
diff -up ntp-4.2.6p1/ntpd/ntpd.c.sleep ntp-4.2.6p1/ntpd/ntpd.c
473877
--- ntp-4.2.6p1/ntpd/ntpd.c.sleep	2010-03-10 19:27:46.000000000 +0100
473877
+++ ntp-4.2.6p1/ntpd/ntpd.c	2010-03-10 19:27:46.000000000 +0100
473877
@@ -195,8 +195,6 @@ extern const char *Version;
473877
 
473877
 char const *progname;
473877
 
473877
-int was_alarmed;
473877
-
473877
 #ifdef DECL_SYSCALL
473877
 /*
473877
  * We put this here, since the argument profile is syscall-specific
473877
@@ -1033,7 +1031,7 @@ getgroup:
473877
 #else /* normal I/O */
473877
 
473877
 	BLOCK_IO_AND_ALARM();
473877
-	was_alarmed = 0;
473877
+
473877
 	for (;;)
473877
 	{
473877
 # if !defined(HAVE_SIGNALED_IO)
473877
@@ -1041,42 +1039,39 @@ getgroup:
473877
 		extern int maxactivefd;
473877
 
473877
 		fd_set rdfdes;
473877
-		int nfound;
473877
-# endif
473877
+		int nfound, time_elapsed;
473877
 
473877
-		if (alarm_flag)		/* alarmed? */
473877
-		{
473877
-			was_alarmed = 1;
473877
-			alarm_flag = 0;
473877
-		}
473877
+		time_elapsed = 0;
473877
+# endif
473877
 
473877
-		if (!was_alarmed && has_full_recv_buffer() == ISC_FALSE)
473877
+		if (has_full_recv_buffer() == ISC_FALSE)
473877
 		{
473877
 			/*
473877
 			 * Nothing to do.  Wait for something.
473877
 			 */
473877
 # ifndef HAVE_SIGNALED_IO
473877
+			double timeout;
473877
+
473877
 			rdfdes = activefds;
473877
-#  if defined(VMS) || defined(SYS_VXWORKS)
473877
-			/* make select() wake up after one second */
473877
-			{
473877
-				struct timeval t1;
473877
+			get_systime(&now;;
473877
+			timeout = get_timeout(&now;;
473877
 
473877
-				t1.tv_sec = 1; t1.tv_usec = 0;
473877
+			if (timeout > 0.0) {
473877
+ 				struct timeval t1;
473877
+
473877
+				t1.tv_sec = timeout;
473877
+				t1.tv_usec = (timeout - t1.tv_sec) * 1000000;
473877
 				nfound = select(maxactivefd+1, &rdfdes, (fd_set *)0,
473877
 						(fd_set *)0, &t1;;
473877
-			}
473877
-#  else
473877
-			nfound = select(maxactivefd+1, &rdfdes, (fd_set *)0,
473877
-					(fd_set *)0, (struct timeval *)0);
473877
-#  endif /* VMS */
473877
-			if (nfound > 0)
473877
-			{
473877
-				l_fp ts;
473877
+				get_systime(&now;;
473877
+			} else
473877
+				nfound = 0;
473877
 
473877
-				get_systime(&ts);
473877
+			time_elapsed = timer_elapsed(now, timeout);
473877
 
473877
-				(void)input_handler(&ts);
473877
+			if (nfound > 0)
473877
+			{
473877
+				(void)input_handler(&now;;
473877
 			}
473877
 			else if (nfound == -1 && errno != EINTR)
473877
 				msyslog(LOG_ERR, "select() error: %m");
473877
@@ -1085,17 +1080,13 @@ getgroup:
473877
 				msyslog(LOG_DEBUG, "select(): nfound=%d, error: %m", nfound);
473877
 #  endif /* DEBUG */
473877
 # else /* HAVE_SIGNALED_IO */
473877
+#  error not supported by sleep patch
473877
 
473877
 			wait_for_signal();
473877
 # endif /* HAVE_SIGNALED_IO */
473877
-			if (alarm_flag)		/* alarmed? */
473877
-			{
473877
-				was_alarmed = 1;
473877
-				alarm_flag = 0;
473877
-			}
473877
 		}
473877
 
473877
-		if (was_alarmed)
473877
+		if (time_elapsed > 0)
473877
 		{
473877
 			UNBLOCK_IO_AND_ALARM();
473877
 			/*
473877
@@ -1103,7 +1094,6 @@ getgroup:
473877
 			 * to process expiry.
473877
 			 */
473877
 			timer();
473877
-			was_alarmed = 0;
473877
 			BLOCK_IO_AND_ALARM();
473877
 		}
473877
 
473877
@@ -1121,19 +1111,8 @@ getgroup:
473877
 			rbuf = get_full_recv_buffer();
473877
 			while (rbuf != NULL)
473877
 			{
473877
-				if (alarm_flag)
473877
-				{
473877
-					was_alarmed = 1;
473877
-					alarm_flag = 0;
473877
-				}
473877
 				UNBLOCK_IO_AND_ALARM();
473877
 
473877
-				if (was_alarmed)
473877
-				{	/* avoid timer starvation during lengthy I/O handling */
473877
-					timer();
473877
-					was_alarmed = 0;
473877
-				}
473877
-
473877
 				/*
473877
 				 * Call the data procedure to handle each received
473877
 				 * packet.