133
141
rawstats.flag = FGEN_FLAG_LINK; /* not yet enabled !!*/
134
142
filegen_register("rawstats", &rawstats);
145
sysstats.prefix = &statsdir[0];
146
sysstats.basename = (char*)emalloc(strlen(STANAME)+1);
147
strcpy(sysstats.basename, STANAME);
149
sysstats.type = FILEGEN_DAY;
150
sysstats.flag = FGEN_FLAG_LINK; /* not yet enabled !!*/
151
filegen_register("sysstats", &sysstats);
154
cryptostats.fp = NULL;
155
cryptostats.prefix = &statsdir[0];
156
cryptostats.basename = (char*)emalloc(strlen(CRYPTONAME)+1);
157
strcpy(cryptostats.basename, CRYPTONAME);
159
cryptostats.type = FILEGEN_DAY;
160
cryptostats.flag = FGEN_FLAG_LINK; /* not yet enabled !!*/
161
filegen_register("cryptostats", &cryptostats);
184
215
#if !defined(VMS)
185
216
/* (prr) getpriority returns -1 on error, but -1 is also a valid
186
* return value (!), so instead we have to zero errno before the call
187
* and check it for non-zero afterwards.
217
* return value (!), so instead we have to zero errno before the
218
* call and check it for non-zero afterwards.
192
222
o_prio = getpriority(PRIO_PROCESS,0); /* Save setting */
194
/* (prr) if getpriority succeeded, call setpriority to raise
225
* (prr) if getpriority succeeded, call setpriority to raise
195
226
* scheduling priority as high as possible. If that succeeds
196
227
* as well, set the prio_set flag so we remember to reset
197
* priority to its previous value below. Note that on Solaris 2.6
198
* (and beyond?), both getpriority and setpriority will fail with
199
* ESRCH, because sched_setscheduler (called from main) put us in
200
* the real-time scheduling class which setpriority doesn't know about.
201
* Being in the real-time class is better than anything setpriority
202
* can do, anyhow, so this error is silently ignored.
228
* priority to its previous value below. Note that on Solaris
229
* 2.6 (and beyond?), both getpriority and setpriority will fail
230
* with ESRCH, because sched_setscheduler (called from main) put
231
* us in the real-time scheduling class which setpriority
232
* doesn't know about. Being in the real-time class is better
233
* than anything setpriority can do, anyhow, so this error is
205
236
if ((errno == 0) && (setpriority(PRIO_PROCESS,0,-20) == 0))
206
prio_set = 1; /* overdrive */
237
prio_set = 1; /* overdrive */
208
239
#ifdef HAVE_GETCLOCK
209
240
(void) getclock(TIMEOFDAY, &ts);
212
243
#else /* not HAVE_GETCLOCK */
213
244
GETTIMEOFDAY(&tv,(struct timezone *)NULL);
214
245
#endif /* not HAVE_GETCLOCK */
215
if (ntp_set_tod(&tv,(struct timezone *)NULL) != 0)
246
if (ntp_set_tod(&tv,(struct timezone *)NULL) != 0) {
217
247
msyslog(LOG_ERR, "can't sync battery time: %m");
219
249
#if !defined(VMS)
221
setpriority(PRIO_PROCESS, 0, o_prio); /* downshift */
251
setpriority(PRIO_PROCESS, 0, o_prio); /* downshift */
223
253
#endif /* DOSYNCTODR */
225
255
NLOG(NLOG_SYSSTATIST)
226
256
msyslog(LOG_INFO,
227
257
"offset %.6f sec freq %.3f ppm error %.6f poll %d",
228
last_offset, drift_comp * 1e6, sys_jitter, sys_poll);
258
last_offset, drift_comp * 1e6, sys_jitter,
231
263
if (stats_drift_file != 0) {
232
264
if ((fp = fopen(stats_temp_file, "w")) == NULL) {
233
265
msyslog(LOG_ERR, "can't open %s: %m",
281
313
double old_drift;
284
/* Expand environment strings under Windows NT, since the command
285
* interpreter doesn't do this, the program must.
317
* Expand environment strings under Windows NT, since the
318
* command interpreter doesn't do this, the program must.
288
321
char newvalue[MAX_PATH], parameter[MAX_PATH];
290
if (!ExpandEnvironmentStrings(invalue, newvalue, MAX_PATH))
323
if (!ExpandEnvironmentStrings(invalue, newvalue, MAX_PATH)) {
293
325
case STATS_FREQ_FILE:
294
326
strcpy(parameter,"STATS_FREQ_FILE");
331
361
stats_drift_file = (char*)emalloc((u_int)(len + 1));
332
362
#if !defined(VMS)
333
stats_temp_file = (char*)emalloc((u_int)(len + sizeof(".TEMP")));
363
stats_temp_file = (char*)emalloc((u_int)(len +
335
stats_temp_file = (char*)emalloc((u_int)(len + sizeof("-TEMP")));
366
stats_temp_file = (char*)emalloc((u_int)(len +
337
369
memmove(stats_drift_file, value, (unsigned)(len+1));
338
370
memmove(stats_temp_file, value, (unsigned)len);
339
371
#if !defined(VMS)
340
memmove(stats_temp_file + len, ".TEMP", sizeof(".TEMP"));
372
memmove(stats_temp_file + len, ".TEMP",
342
memmove(stats_temp_file + len, "-TEMP", sizeof("-TEMP"));
375
memmove(stats_temp_file + len, "-TEMP",
346
* Open drift file and read frequency
380
* Open drift file and read frequency. If the file is
381
* missing or contains errors, tell the loop to reset.
348
383
if ((fp = fopen(stats_drift_file, "r")) == NULL) {
384
loop_config(LOOP_DRIFTCOMP, 1e9);
351
387
if (fscanf(fp, "%lf", &old_drift) != 1) {
352
msyslog(LOG_ERR, "Un-parsable frequency in %s",
388
msyslog(LOG_ERR, "Frequency format error in %s",
353
389
stats_drift_file);
390
loop_config(LOOP_DRIFTCOMP, 1e9);
361
#else /* not HAVE_FINITE */
362
# ifdef HAVE_ISFINITE
364
# else /* not HAVE_ISFINITE */
366
# endif /* not HAVE_ISFINITE */
367
#endif /* not HAVE_FINITE */
368
|| (fabs(old_drift) > (NTP_MAXFREQ * 1e6))) {
369
msyslog(LOG_ERR, "invalid frequency (%f) in %s",
370
old_drift, stats_drift_file);
373
msyslog(LOG_INFO, "frequency initialized %.3f from %s",
374
old_drift, stats_drift_file);
396
"frequency initialized %.3f PPM from %s",
397
old_drift, stats_drift_file);
375
398
loop_config(LOOP_DRIFTCOMP, old_drift / 1e6);
444
481
record_peer_stats(
445
struct sockaddr_in *addr,
482
struct sockaddr_storage *addr,
457
u_long day, sec, msec;
459
493
if (!stats_control)
462
(void) getclock(TIMEOFDAY, &ts);
463
tv.tv_sec = ts.tv_sec;
464
tv.tv_usec = ts.tv_nsec / 1000;
465
#else /* not HAVE_GETCLOCK */
466
GETTIMEOFDAY(&tv, (struct timezone *)NULL);
467
#endif /* not HAVE_GETCLOCK */
468
day = tv.tv_sec / 86400 + MJD_1970;
469
sec = tv.tv_sec % 86400;
470
msec = tv.tv_usec / 1000;
472
filegen_setup(&peerstats, (u_long)(tv.tv_sec + JAN_1970));
497
filegen_setup(&peerstats, now.l_ui);
498
day = now.l_ui / 86400 + MJD_1900;
473
500
if (peerstats.fp != NULL) {
474
501
fprintf(peerstats.fp,
475
"%lu %lu.%03lu %s %x %.9f %.9f %.9f %.9f\n",
476
day, sec, msec, ntoa(addr), status, offset,
502
"%lu %s %s %x %.9f %.9f %.9f %.9f\n",
503
day, ulfptoa(&now, 3), stoa(addr), status, offset,
477
504
delay, dispersion, skew);
478
505
fflush(peerstats.fp);
492
519
record_loop_stats(
504
u_long day, sec, msec;
506
530
if (!stats_control)
509
(void) getclock(TIMEOFDAY, &ts);
510
tv.tv_sec = ts.tv_sec;
511
tv.tv_usec = ts.tv_nsec / 1000;
512
#else /* not HAVE_GETCLOCK */
513
GETTIMEOFDAY(&tv, (struct timezone *)NULL);
514
#endif /* not HAVE_GETCLOCK */
515
day = tv.tv_sec / 86400 + MJD_1970;
516
sec = tv.tv_sec % 86400;
517
msec = tv.tv_usec / 1000;
519
filegen_setup(&loopstats, (u_long)(tv.tv_sec + JAN_1970));
534
filegen_setup(&loopstats, now.l_ui);
535
day = now.l_ui / 86400 + MJD_1900;
520
537
if (loopstats.fp != NULL) {
521
fprintf(loopstats.fp, "%lu %lu.%03lu %.9f %.6f %.9f %.6f %d\n",
522
day, sec, msec, offset, freq * 1e6, jitter,
523
stability * 1e6, poll);
538
fprintf(loopstats.fp, "%lu %s %.9f %.6f %.9f %.6f %d\n",
539
day, ulfptoa(&now, 3), offset, freq * 1e6, jitter,
540
stability * 1e6, spoll);
524
541
fflush(loopstats.fp);
538
555
record_clock_stats(
539
struct sockaddr_in *addr,
556
struct sockaddr_storage *addr,
547
u_long day, sec, msec;
549
563
if (!stats_control)
552
(void) getclock(TIMEOFDAY, &ts);
553
tv.tv_sec = ts.tv_sec;
554
tv.tv_usec = ts.tv_nsec / 1000;
555
#else /* not HAVE_GETCLOCK */
556
GETTIMEOFDAY(&tv, (struct timezone *)NULL);
557
#endif /* not HAVE_GETCLOCK */
558
day = tv.tv_sec / 86400 + MJD_1970;
559
sec = tv.tv_sec % 86400;
560
msec = tv.tv_usec / 1000;
562
filegen_setup(&clockstats, (u_long)(tv.tv_sec + JAN_1970));
567
filegen_setup(&clockstats, now.l_ui);
568
day = now.l_ui / 86400 + MJD_1900;
563
570
if (clockstats.fp != NULL) {
564
fprintf(clockstats.fp, "%lu %lu.%03lu %s %s\n",
565
day, sec, msec, ntoa(addr), text);
571
fprintf(clockstats.fp, "%lu %s %s %s\n",
572
day, ulfptoa(&now, 3), stoa(addr), text);
566
573
fflush(clockstats.fp);
581
588
record_raw_stats(
582
struct sockaddr_in *srcadr,
583
struct sockaddr_in *dstadr,
589
struct sockaddr_storage *srcadr,
590
struct sockaddr_storage *dstadr,
594
u_long day, sec, msec;
596
600
if (!stats_control)
599
(void) getclock(TIMEOFDAY, &ts);
600
tv.tv_sec = ts.tv_sec;
601
tv.tv_usec = ts.tv_nsec / 1000;
602
#else /* not HAVE_GETCLOCK */
603
GETTIMEOFDAY(&tv, (struct timezone *)NULL);
604
#endif /* not HAVE_GETCLOCK */
605
day = tv.tv_sec / 86400 + MJD_1970;
606
sec = tv.tv_sec % 86400;
607
msec = tv.tv_usec / 1000;
609
filegen_setup(&rawstats, (u_long)(tv.tv_sec + JAN_1970));
604
filegen_setup(&rawstats, now.l_ui);
605
day = now.l_ui / 86400 + MJD_1900;
610
607
if (rawstats.fp != NULL) {
611
fprintf(rawstats.fp, "%lu %lu.%03lu %s %s %s %s %s %s\n",
612
day, sec, msec, ntoa(srcadr), ntoa(dstadr),
608
fprintf(rawstats.fp, "%lu %s %s %s %s %s %s %s\n",
609
day, ulfptoa(&now, 3), stoa(srcadr), stoa(dstadr),
613
610
ulfptoa(t1, 9), ulfptoa(t2, 9), ulfptoa(t3, 9),
615
612
fflush(rawstats.fp);
618
* record_sys_stats - write system statistics to file
621
* time (s past midnight)
622
* time since startup (hr)
629
* bad length or format
634
record_sys_stats(void)
643
filegen_setup(&sysstats, now.l_ui);
644
day = now.l_ui / 86400 + MJD_1900;
646
if (sysstats.fp != NULL) {
648
"%lu %s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
649
day, ulfptoa(&now, 3), sys_stattime / 3600,
650
sys_received, sys_processed, sys_newversionpkt,
651
sys_oldversionpkt, sys_unknownversion,
652
sys_restricted, sys_badlength, sys_badauth,
662
* record_crypto_stats - write crypto statistics to file
666
* time (s past midnight)
672
struct sockaddr_storage *addr,
683
filegen_setup(&cryptostats, now.l_ui);
684
day = now.l_ui / 86400 + MJD_1900;
686
if (cryptostats.fp != NULL) {
688
fprintf(cryptostats.fp, "%lu %s %s\n",
689
day, ulfptoa(&now, 3), text);
691
fprintf(cryptostats.fp, "%lu %s %s %s\n",
692
day, ulfptoa(&now, 3), stoa(addr), text);
693
fflush(cryptostats.fp);
620
700
* getauthkeys - read the authentication keys from the specified file
667
747
if (key_file_name != 0)
668
748
authreadkeys(key_file_name);
752
* sock_hash - hash an sockaddr_storage structure
756
struct sockaddr_storage *addr
767
* We can't just hash the whole thing because there are hidden
768
* fields in sockaddr_in6 that might be filled in by recvfrom(),
769
* so just use the family, port and address.
771
ch = (char *)&addr->ss_family;
772
hashVal = 37 * hashVal + (int)*ch;
773
if (sizeof(addr->ss_family) > 1) {
775
hashVal = 37 * hashVal + (int)*ch;
777
switch(addr->ss_family) {
779
ch = (char *)&((struct sockaddr_in *)addr)->sin_addr;
780
len = sizeof(struct in_addr);
783
ch = (char *)&((struct sockaddr_in6 *)addr)->sin6_addr;
784
len = sizeof(struct in6_addr);
788
for (i = 0; i < len ; i++)
789
hashVal = 37 * hashVal + (int)*(ch + i);
791
hashVal = hashVal % 128; /* % MON_HASH_SIZE hardcoded */