ntp-4.2.6p1-sleep.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. diff -up ntp-4.2.6p1/include/ntp_refclock.h.sleep ntp-4.2.6p1/include/ntp_refclock.h
  2. --- ntp-4.2.6p1/include/ntp_refclock.h.sleep 2009-12-09 08:36:35.000000000 +0100
  3. +++ ntp-4.2.6p1/include/ntp_refclock.h 2010-03-10 19:27:46.000000000 +0100
  4. @@ -260,6 +260,7 @@ extern void refclock_control (sockaddr_u
  5. struct refclockstat *);
  6. extern int refclock_open (char *, u_int, u_int);
  7. extern int refclock_setup (int, u_int, u_int);
  8. +extern int refclock_timer_needed (struct peer *);
  9. extern void refclock_timer (struct peer *);
  10. extern void refclock_transmit (struct peer *);
  11. extern int refclock_ioctl (int, u_int);
  12. diff -up ntp-4.2.6p1/include/ntp_stdlib.h.sleep ntp-4.2.6p1/include/ntp_stdlib.h
  13. --- ntp-4.2.6p1/include/ntp_stdlib.h.sleep 2009-12-09 08:36:35.000000000 +0100
  14. +++ ntp-4.2.6p1/include/ntp_stdlib.h 2010-03-10 19:27:46.000000000 +0100
  15. @@ -116,6 +116,7 @@ extern const char * FindConfig (const ch
  16. extern void signal_no_reset (int, RETSIGTYPE (*func)(int));
  17. extern void getauthkeys (const char *);
  18. +extern int auth_agekeys_needed (void);
  19. extern void auth_agekeys (void);
  20. extern void rereadkeys (void);
  21. diff -up ntp-4.2.6p1/include/ntpd.h.sleep ntp-4.2.6p1/include/ntpd.h
  22. --- ntp-4.2.6p1/include/ntpd.h.sleep 2009-12-09 08:36:35.000000000 +0100
  23. +++ ntp-4.2.6p1/include/ntpd.h 2010-03-10 19:27:46.000000000 +0100
  24. @@ -112,8 +112,10 @@ extern void block_io_and_alarm (void);
  25. /* ntp_loopfilter.c */
  26. extern void init_loopfilter(void);
  27. extern int local_clock(struct peer *, double);
  28. -extern void adj_host_clock(void);
  29. +extern int adj_host_clock_needed(void);
  30. +extern void adj_host_clock(int);
  31. extern void loop_config(int, double);
  32. +extern int huffpuff_enabled(void);
  33. extern void huffpuff(void);
  34. extern u_long sys_clocktime;
  35. extern u_int sys_tai;
  36. @@ -219,6 +221,8 @@ extern void hack_restrict (int, sockaddr
  37. /* ntp_timer.c */
  38. extern void init_timer (void);
  39. extern void reinit_timer (void);
  40. +extern double get_timeout (l_fp *);
  41. +extern int timer_elapsed (l_fp, int);
  42. extern void timer (void);
  43. extern void timer_clr_stats (void);
  44. extern void timer_interfacetimeout (u_long);
  45. diff -up ntp-4.2.6p1/libntp/authkeys.c.sleep ntp-4.2.6p1/libntp/authkeys.c
  46. --- ntp-4.2.6p1/libntp/authkeys.c.sleep 2009-12-09 08:36:35.000000000 +0100
  47. +++ ntp-4.2.6p1/libntp/authkeys.c 2010-03-10 19:27:46.000000000 +0100
  48. @@ -445,6 +445,25 @@ auth_delkeys(void)
  49. }
  50. }
  51. +int
  52. +auth_agekeys_needed(void) {
  53. + struct savekey *sk;
  54. + int i;
  55. +
  56. + if (authnumkeys > 20)
  57. + return 1;
  58. +
  59. + for (i = 0; i < HASHSIZE; i++) {
  60. + sk = key_hash[i];
  61. + while (sk != 0) {
  62. + if (sk->lifetime > 0)
  63. + return 1;
  64. + sk = sk->next;
  65. + }
  66. + }
  67. + return 0;
  68. +}
  69. +
  70. /*
  71. * auth_agekeys - delete keys whose lifetimes have expired
  72. */
  73. diff -up ntp-4.2.6p1/ntpd/ntp_loopfilter.c.sleep ntp-4.2.6p1/ntpd/ntp_loopfilter.c
  74. --- ntp-4.2.6p1/ntpd/ntp_loopfilter.c.sleep 2009-12-09 08:36:36.000000000 +0100
  75. +++ ntp-4.2.6p1/ntpd/ntp_loopfilter.c 2010-03-10 19:27:46.000000000 +0100
  76. @@ -677,6 +677,13 @@ local_clock(
  77. #endif /* LOCKCLOCK */
  78. }
  79. +int
  80. +adj_host_clock_needed(void)
  81. +{
  82. + return !(!ntp_enable || mode_ntpdate || (pll_control &&
  83. + kern_enable));
  84. +}
  85. +
  86. /*
  87. * adj_host_clock - Called once every second to update the local clock.
  88. @@ -686,7 +693,7 @@ local_clock(
  89. */
  90. void
  91. adj_host_clock(
  92. - void
  93. + int time_elapsed
  94. )
  95. {
  96. double adjustment;
  97. @@ -698,7 +705,7 @@ adj_host_clock(
  98. * since the poll interval can exceed one day, the old test
  99. * would be counterproductive.
  100. */
  101. - sys_rootdisp += clock_phi;
  102. + sys_rootdisp += clock_phi * time_elapsed;
  103. #ifndef LOCKCLOCK
  104. /*
  105. @@ -819,6 +826,12 @@ set_freq(
  106. #endif /* KERNEL_PLL */
  107. }
  108. +int
  109. +huffpuff_enabled(void)
  110. +{
  111. + return sys_huffpuff != NULL;
  112. +}
  113. +
  114. /*
  115. * huff-n'-puff filter
  116. */
  117. diff -up ntp-4.2.6p1/ntpd/ntp_refclock.c.sleep ntp-4.2.6p1/ntpd/ntp_refclock.c
  118. --- ntp-4.2.6p1/ntpd/ntp_refclock.c.sleep 2009-12-09 08:36:36.000000000 +0100
  119. +++ ntp-4.2.6p1/ntpd/ntp_refclock.c 2010-03-10 19:27:46.000000000 +0100
  120. @@ -268,6 +268,21 @@ refclock_unpeer(
  121. }
  122. +int
  123. +refclock_timer_needed(
  124. + struct peer *peer /* peer structure pointer */
  125. + )
  126. +{
  127. + u_char clktype;
  128. + int unit;
  129. +
  130. + clktype = peer->refclktype;
  131. + unit = peer->refclkunit;
  132. + if (refclock_conf[clktype]->clock_timer != noentry)
  133. + return 1;
  134. + return 0;
  135. +}
  136. +
  137. /*
  138. * refclock_timer - called once per second for housekeeping.
  139. */
  140. diff -up ntp-4.2.6p1/ntpd/ntp_timer.c.sleep ntp-4.2.6p1/ntpd/ntp_timer.c
  141. --- ntp-4.2.6p1/ntpd/ntp_timer.c.sleep 2009-12-09 08:36:35.000000000 +0100
  142. +++ ntp-4.2.6p1/ntpd/ntp_timer.c 2010-03-11 15:23:59.000000000 +0100
  143. @@ -56,7 +56,6 @@ static u_long adjust_timer; /* second ti
  144. static u_long stats_timer; /* stats timer */
  145. static u_long huffpuff_timer; /* huff-n'-puff timer */
  146. u_long leapsec; /* leapseconds countdown */
  147. -l_fp sys_time; /* current system time */
  148. #ifdef OPENSSL
  149. static u_long revoke_timer; /* keys revoke timer */
  150. static u_long keys_timer; /* session key timer */
  151. @@ -74,6 +73,12 @@ volatile u_long alarm_overflow;
  152. #define DAY (24 * HOUR)
  153. u_long current_time; /* seconds since startup */
  154. +l_fp timer_base;
  155. +int time_elapsed;
  156. +
  157. +#define TIMEOUT_TS_SIZE 2
  158. +l_fp timeout_ts[TIMEOUT_TS_SIZE];
  159. +unsigned int timeout_ts_index;
  160. /*
  161. * Stats. Number of overflows and number of calls to transmit().
  162. @@ -110,6 +115,8 @@ static RETSIGTYPE alarming (int);
  163. void
  164. reinit_timer(void)
  165. {
  166. + get_systime(&timer_base);
  167. +#if 0
  168. #if !defined(SYS_WINNT) && !defined(VMS)
  169. # if defined(HAVE_TIMER_CREATE) && defined(HAVE_TIMER_SETTIME)
  170. timer_gettime(ntpd_timerid, &itimer);
  171. @@ -143,6 +150,7 @@ reinit_timer(void)
  172. setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
  173. # endif
  174. # endif /* VMS */
  175. +#endif
  176. }
  177. /*
  178. @@ -165,6 +173,12 @@ init_timer(void)
  179. timer_xmtcalls = 0;
  180. timer_timereset = 0;
  181. + get_systime(&timer_base);
  182. +
  183. + for (timeout_ts_index = 0; timeout_ts_index < TIMEOUT_TS_SIZE; timeout_ts_index++)
  184. + L_CLR(&timeout_ts[timeout_ts_index]);
  185. + timeout_ts_index = 0;
  186. +#if 0
  187. #if !defined(SYS_WINNT)
  188. /*
  189. * Set up the alarm interrupt. The first comes 2**EVENT_TIMEOUT
  190. @@ -226,6 +240,7 @@ init_timer(void)
  191. }
  192. #endif /* SYS_WINNT */
  193. +#endif
  194. }
  195. #if defined(SYS_WINNT)
  196. @@ -236,6 +251,104 @@ get_timer_handle(void)
  197. }
  198. #endif
  199. +double
  200. +get_timeout(l_fp *now)
  201. +{
  202. + register struct peer *peer, *next_peer;
  203. + u_int n;
  204. + double r;
  205. + int next;
  206. + l_fp ts;
  207. +
  208. + ts = *now;
  209. + L_SUB(&ts, &timeout_ts[timeout_ts_index]);
  210. + timeout_ts[timeout_ts_index] = *now;
  211. + timeout_ts_index = (timeout_ts_index + 1) % TIMEOUT_TS_SIZE;
  212. +
  213. + /* don't waste CPU time if called too frequently */
  214. + if (ts.l_ui == 0) {
  215. + next = 1;
  216. + goto finish;
  217. + }
  218. +
  219. + next = current_time + HOUR;
  220. +
  221. + if (adj_host_clock_needed()) {
  222. + next = 1;
  223. + goto finish;
  224. + }
  225. + for (n = 0; n < NTP_HASH_SIZE; n++) {
  226. + for (peer = peer_hash[n]; peer != 0; peer = next_peer) {
  227. + next_peer = peer->next;
  228. +#ifdef REFCLOCK
  229. + if (peer->flags & FLAG_REFCLOCK && refclock_timer_needed(peer)) {
  230. + next = 1;
  231. + goto finish;
  232. + }
  233. +#endif /* REFCLOCK */
  234. + if (peer->action)
  235. + next = min(next, peer->nextaction);
  236. + next = min(next, peer->nextdate);
  237. + }
  238. + }
  239. +
  240. + if (leapsec > 0)
  241. + next = min(next, leapsec);
  242. +
  243. + if (huffpuff_enabled())
  244. + next = min(next, huffpuff_timer);
  245. +
  246. +#ifdef OPENSSL
  247. + if (auth_agekeys_needed())
  248. + next = min(next, keys_timer);
  249. + if (sys_leap != LEAP_NOTINSYNC)
  250. + next = min(next, revoke_timer);
  251. +#endif /* OPENSSL */
  252. +
  253. + if (interface_interval)
  254. + next = min(next, interface_timer);
  255. +
  256. + next = min(next, stats_timer);
  257. +
  258. + next -= current_time;
  259. + if (next <= 0)
  260. + next = 1;
  261. +finish:
  262. + ts = timer_base;
  263. + ts.l_ui += next;
  264. + L_SUB(&ts, now);
  265. + LFPTOD(&ts, r);
  266. +#ifdef DEBUG
  267. + DPRINTF(2, ("timer: timeout %f\n", r));
  268. +#endif
  269. +
  270. + return r;
  271. +}
  272. +
  273. +int
  274. +timer_elapsed(l_fp now, int timeout)
  275. +{
  276. + int elapsed;
  277. +
  278. + L_SUB(&now, &timer_base);
  279. + elapsed = now.l_i;
  280. + if (elapsed < 0 || elapsed > timeout + 10) {
  281. +#ifdef DEBUG
  282. + DPRINTF(2, ("timer: unexpected time jump\n"));
  283. +#endif
  284. + elapsed = 0;
  285. + reinit_timer();
  286. +
  287. + }
  288. + timer_base.l_ui += elapsed;
  289. + time_elapsed += elapsed;
  290. + current_time += elapsed;
  291. +#ifdef DEBUG
  292. + DPRINTF(2, ("timer: time elapsed %d\n", time_elapsed));
  293. +#endif
  294. + return time_elapsed;
  295. +}
  296. +
  297. /*
  298. * timer - event timer
  299. */
  300. @@ -251,11 +364,9 @@ timer(void)
  301. * kiss-o'-deatch function and implement the association
  302. * polling function..
  303. */
  304. - current_time++;
  305. - get_systime(&sys_time);
  306. if (adjust_timer <= current_time) {
  307. - adjust_timer += 1;
  308. - adj_host_clock();
  309. + adjust_timer += time_elapsed;
  310. + adj_host_clock(time_elapsed);
  311. #ifdef REFCLOCK
  312. for (n = 0; n < NTP_HASH_SIZE; n++) {
  313. for (peer = peer_hash[n]; peer != 0; peer = next_peer) {
  314. @@ -286,7 +397,7 @@ timer(void)
  315. * 128 s or less.
  316. */
  317. if (peer->throttle > 0)
  318. - peer->throttle--;
  319. + peer->throttle -= min(peer->throttle, time_elapsed);
  320. if (peer->nextdate <= current_time) {
  321. #ifdef REFCLOCK
  322. if (peer->flags & FLAG_REFCLOCK)
  323. @@ -333,7 +444,7 @@ timer(void)
  324. * set.
  325. */
  326. if (leapsec > 0) {
  327. - leapsec--;
  328. + leapsec -= min(leapsec, time_elapsed);
  329. if (leapsec == 0) {
  330. sys_leap = LEAP_NOWARNING;
  331. sys_tai = leap_tai;
  332. @@ -398,11 +509,15 @@ timer(void)
  333. * Finally, write hourly stats.
  334. */
  335. if (stats_timer <= current_time) {
  336. + l_fp sys_time;
  337. + get_systime(&sys_time);
  338. stats_timer += HOUR;
  339. write_stats();
  340. if (sys_tai != 0 && sys_time.l_ui > leap_expire)
  341. report_event(EVNT_LEAPVAL, NULL, NULL);
  342. }
  343. +
  344. + time_elapsed = 0;
  345. }
  346. diff -up ntp-4.2.6p1/ntpd/ntpd.c.sleep ntp-4.2.6p1/ntpd/ntpd.c
  347. --- ntp-4.2.6p1/ntpd/ntpd.c.sleep 2010-03-10 19:27:46.000000000 +0100
  348. +++ ntp-4.2.6p1/ntpd/ntpd.c 2010-03-10 19:27:46.000000000 +0100
  349. @@ -195,8 +195,6 @@ extern const char *Version;
  350. char const *progname;
  351. -int was_alarmed;
  352. -
  353. #ifdef DECL_SYSCALL
  354. /*
  355. * We put this here, since the argument profile is syscall-specific
  356. @@ -1033,7 +1031,7 @@ getgroup:
  357. #else /* normal I/O */
  358. BLOCK_IO_AND_ALARM();
  359. - was_alarmed = 0;
  360. +
  361. for (;;)
  362. {
  363. # if !defined(HAVE_SIGNALED_IO)
  364. @@ -1041,42 +1039,39 @@ getgroup:
  365. extern int maxactivefd;
  366. fd_set rdfdes;
  367. - int nfound;
  368. -# endif
  369. + int nfound, time_elapsed;
  370. - if (alarm_flag) /* alarmed? */
  371. - {
  372. - was_alarmed = 1;
  373. - alarm_flag = 0;
  374. - }
  375. + time_elapsed = 0;
  376. +# endif
  377. - if (!was_alarmed && has_full_recv_buffer() == ISC_FALSE)
  378. + if (has_full_recv_buffer() == ISC_FALSE)
  379. {
  380. /*
  381. * Nothing to do. Wait for something.
  382. */
  383. # ifndef HAVE_SIGNALED_IO
  384. + double timeout;
  385. +
  386. rdfdes = activefds;
  387. -# if defined(VMS) || defined(SYS_VXWORKS)
  388. - /* make select() wake up after one second */
  389. - {
  390. - struct timeval t1;
  391. + get_systime(&now);
  392. + timeout = get_timeout(&now);
  393. - t1.tv_sec = 1; t1.tv_usec = 0;
  394. + if (timeout > 0.0) {
  395. + struct timeval t1;
  396. +
  397. + t1.tv_sec = timeout;
  398. + t1.tv_usec = (timeout - t1.tv_sec) * 1000000;
  399. nfound = select(maxactivefd+1, &rdfdes, (fd_set *)0,
  400. (fd_set *)0, &t1);
  401. - }
  402. -# else
  403. - nfound = select(maxactivefd+1, &rdfdes, (fd_set *)0,
  404. - (fd_set *)0, (struct timeval *)0);
  405. -# endif /* VMS */
  406. - if (nfound > 0)
  407. - {
  408. - l_fp ts;
  409. + get_systime(&now);
  410. + } else
  411. + nfound = 0;
  412. - get_systime(&ts);
  413. + time_elapsed = timer_elapsed(now, timeout);
  414. - (void)input_handler(&ts);
  415. + if (nfound > 0)
  416. + {
  417. + (void)input_handler(&now);
  418. }
  419. else if (nfound == -1 && errno != EINTR)
  420. msyslog(LOG_ERR, "select() error: %m");
  421. @@ -1085,17 +1080,13 @@ getgroup:
  422. msyslog(LOG_DEBUG, "select(): nfound=%d, error: %m", nfound);
  423. # endif /* DEBUG */
  424. # else /* HAVE_SIGNALED_IO */
  425. +# error not supported by sleep patch
  426. wait_for_signal();
  427. # endif /* HAVE_SIGNALED_IO */
  428. - if (alarm_flag) /* alarmed? */
  429. - {
  430. - was_alarmed = 1;
  431. - alarm_flag = 0;
  432. - }
  433. }
  434. - if (was_alarmed)
  435. + if (time_elapsed > 0)
  436. {
  437. UNBLOCK_IO_AND_ALARM();
  438. /*
  439. @@ -1103,7 +1094,6 @@ getgroup:
  440. * to process expiry.
  441. */
  442. timer();
  443. - was_alarmed = 0;
  444. BLOCK_IO_AND_ALARM();
  445. }
  446. @@ -1121,19 +1111,8 @@ getgroup:
  447. rbuf = get_full_recv_buffer();
  448. while (rbuf != NULL)
  449. {
  450. - if (alarm_flag)
  451. - {
  452. - was_alarmed = 1;
  453. - alarm_flag = 0;
  454. - }
  455. UNBLOCK_IO_AND_ALARM();
  456. - if (was_alarmed)
  457. - { /* avoid timer starvation during lengthy I/O handling */
  458. - timer();
  459. - was_alarmed = 0;
  460. - }
  461. -
  462. /*
  463. * Call the data procedure to handle each received
  464. * packet.