2 * ntp_timer.c - event timer support routines
8 #include "ntp_machine.h"
10 #include "ntp_stdlib.h"
14 #ifdef HAVE_SYS_SIGNAL_H
15 # include <sys/signal.h>
21 #if defined(HAVE_IO_COMPLETION_PORT)
22 # include "ntp_iocompletionport.h"
23 # include "ntp_timer.h"
27 * These routines provide support for the event timer. The timer is
28 * implemented by an interrupt routine which sets a flag once every
29 * 2**EVENT_TIMEOUT seconds (currently 4), and a timer routine which
30 * is called when the mainline code gets around to seeing the flag.
31 * The timer routine dispatches the clock adjustment code if its time
32 * has come, then searches the timer queue for expiries which are
33 * dispatched to the transmit procedure. Finally, we call the hourly
34 * procedure to do cleanup and print a message.
38 * Alarm flag. The mainline code imports this.
40 volatile int alarm_flag;
45 static u_long adjust_timer; /* second timer */
46 static u_long keys_timer; /* minute timer */
47 static u_long hourly_timer; /* hour timer */
48 static u_long huffpuff_timer; /* huff-n'-puff timer */
50 static u_long revoke_timer; /* keys revoke timer */
51 u_char sys_revoke = KEY_REVOKE; /* keys revoke timeout (log2 s) */
55 * Statistics counter for the interested.
57 volatile u_long alarm_overflow;
65 * Stats. Number of overflows and number of calls to transmit().
67 u_long timer_timereset;
68 u_long timer_overflows;
69 u_long timer_xmtcalls;
72 static int vmstimer[2]; /* time for next timer AST */
73 static int vmsinc[2]; /* timer increment */
77 static HANDLE WaitableTimerHandle = NULL;
79 static RETSIGTYPE alarming P((int));
80 #endif /* SYS_WINNT */
83 # if !defined SYS_WINNT || defined(SYS_CYGWIN32)
84 # ifndef HAVE_TIMER_SETTIME
85 struct itimerval itimer;
87 static timer_t ntpd_timerid;
88 struct itimerspec itimer;
89 # endif /* HAVE_TIMER_SETTIME */
90 # endif /* SYS_WINNT */
94 * reinit_timer - reinitialize interval timer.
99 #if !defined(SYS_WINNT) && !defined(VMS)
100 # if defined(HAVE_TIMER_CREATE) && defined(HAVE_TIMER_SETTIME)
101 timer_gettime(ntpd_timerid, &itimer);
102 if (itimer.it_value.tv_sec < 0 || itimer.it_value.tv_sec > (1<<EVENT_TIMEOUT)) {
103 itimer.it_value.tv_sec = (1<<EVENT_TIMEOUT);
105 if (itimer.it_value.tv_nsec < 0 ) {
106 itimer.it_value.tv_nsec = 0;
108 if (itimer.it_value.tv_sec == 0 && itimer.it_value.tv_nsec == 0) {
109 itimer.it_value.tv_sec = (1<<EVENT_TIMEOUT);
110 itimer.it_value.tv_nsec = 0;
112 itimer.it_interval.tv_sec = (1<<EVENT_TIMEOUT);
113 itimer.it_interval.tv_nsec = 0;
114 timer_settime(ntpd_timerid, 0 /*!TIMER_ABSTIME*/, &itimer, NULL);
116 getitimer(ITIMER_REAL, &itimer);
117 if (itimer.it_value.tv_sec < 0 || itimer.it_value.tv_sec > (1<<EVENT_TIMEOUT)) {
118 itimer.it_value.tv_sec = (1<<EVENT_TIMEOUT);
120 if (itimer.it_value.tv_usec < 0 ) {
121 itimer.it_value.tv_usec = 0;
123 if (itimer.it_value.tv_sec == 0 && itimer.it_value.tv_usec == 0) {
124 itimer.it_value.tv_sec = (1<<EVENT_TIMEOUT);
125 itimer.it_value.tv_usec = 0;
127 itimer.it_interval.tv_sec = (1<<EVENT_TIMEOUT);
128 itimer.it_interval.tv_usec = 0;
129 setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
135 * init_timer - initialize the timer data structures
140 # if defined SYS_WINNT & !defined(SYS_CYGWIN32)
142 TOKEN_PRIVILEGES tkp;
143 # endif /* SYS_WINNT */
158 #if !defined(SYS_WINNT)
160 * Set up the alarm interrupt. The first comes 2**EVENT_TIMEOUT
161 * seconds from now and they continue on every 2**EVENT_TIMEOUT
165 # if defined(HAVE_TIMER_CREATE) && defined(HAVE_TIMER_SETTIME)
166 if (timer_create (CLOCK_REALTIME, NULL, &ntpd_timerid) ==
174 fprintf (stderr, "timer create FAILED\n");
177 (void) signal_no_reset(SIGALRM, alarming);
178 itimer.it_interval.tv_sec = itimer.it_value.tv_sec = (1<<EVENT_TIMEOUT);
179 itimer.it_interval.tv_nsec = itimer.it_value.tv_nsec = 0;
180 timer_settime(ntpd_timerid, 0 /*!TIMER_ABSTIME*/, &itimer, NULL);
182 (void) signal_no_reset(SIGALRM, alarming);
183 itimer.it_interval.tv_sec = itimer.it_value.tv_sec = (1<<EVENT_TIMEOUT);
184 itimer.it_interval.tv_usec = itimer.it_value.tv_usec = 0;
185 setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
188 vmsinc[0] = 10000000; /* 1 sec */
190 lib$emul(&(1<<EVENT_TIMEOUT), &vmsinc, &0, &vmsinc);
192 sys$gettim(&vmstimer); /* that's "now" as abstime */
194 lib$addx(&vmsinc, &vmstimer, &vmstimer);
195 sys$setimr(0, &vmstimer, alarming, alarming, 0);
197 #else /* SYS_WINNT */
201 * Get privileges needed for fiddling with the clock
204 /* get the current process token handle */
205 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
206 msyslog(LOG_ERR, "OpenProcessToken failed: %m");
209 /* get the LUID for system-time privilege. */
210 LookupPrivilegeValue(NULL, SE_SYSTEMTIME_NAME, &tkp.Privileges[0].Luid);
211 tkp.PrivilegeCount = 1; /* one privilege to set */
212 tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
213 /* get set-time privilege for this process. */
214 AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
215 /* cannot test return value of AdjustTokenPrivileges. */
216 if (GetLastError() != ERROR_SUCCESS) {
217 msyslog(LOG_ERR, "AdjustTokenPrivileges failed: %m");
221 * Set up timer interrupts for every 2**EVENT_TIMEOUT seconds
225 WaitableTimerHandle = CreateWaitableTimer(NULL, FALSE, NULL);
226 if (WaitableTimerHandle == NULL) {
227 msyslog(LOG_ERR, "CreateWaitableTimer failed: %m");
231 DWORD Period = (1<<EVENT_TIMEOUT) * 1000;
232 LARGE_INTEGER DueTime;
233 DueTime.QuadPart = Period * 10000i64;
234 if (!SetWaitableTimer(WaitableTimerHandle, &DueTime, Period, NULL, NULL, FALSE) != NO_ERROR) {
235 msyslog(LOG_ERR, "SetWaitableTimer failed: %m");
240 #endif /* SYS_WINNT */
243 #if defined(SYS_WINNT)
245 get_timer_handle(void)
247 return WaitableTimerHandle;
252 * timer - dispatch anyone who needs to be
257 register struct peer *peer, *next_peer;
259 char statstr[NTP_MAXSTRLEN]; /* statistics for filegen */
263 current_time += (1<<EVENT_TIMEOUT);
266 * Adjustment timeout first.
268 if (adjust_timer <= current_time) {
275 * Now dispatch any peers whose event timer has expired. Be careful
276 * here, since the peer structure might go away as the result of
279 for (n = 0; n < HASH_SIZE; n++) {
280 for (peer = peer_hash[n]; peer != 0; peer = next_peer) {
281 next_peer = peer->next;
282 if (peer->action && peer->nextaction <= current_time)
284 if (peer->nextdate <= current_time) {
286 if (peer->flags & FLAG_REFCLOCK)
287 refclock_transmit(peer);
292 #endif /* REFCLOCK */
298 * Garbage collect expired keys.
300 if (keys_timer <= current_time) {
301 keys_timer += MINUTE;
306 * Huff-n'-puff filter
308 if (huffpuff_timer <= current_time) {
309 huffpuff_timer += HUFFPUFF;
315 * Garbage collect old keys and generate new private value
317 if (revoke_timer <= current_time) {
318 revoke_timer += RANDPOLL(sys_revoke);
320 sprintf(statstr, "refresh ts %u", ntohl(hostval.tstamp));
321 record_crypto_stats(NULL, statstr);
324 printf("timer: %s\n", statstr);
330 * Finally, call the hourly routine.
332 if (hourly_timer <= current_time) {
333 hourly_timer += HOUR;
341 * alarming - tell the world we've been alarmed
355 #else /* VMS AST routine */
357 if (alarm_flag) alarm_overflow++;
358 else alarm_flag = 1; /* increment is no good */
360 lib$addx(&vmsinc,&vmstimer,&vmstimer);
361 sys$setimr(0,&vmstimer,alarming,alarming,0);
364 #endif /* SYS_WINNT */
368 * timer_clr_stats - clear timer module stat counters
371 timer_clr_stats(void)
375 timer_timereset = current_time;