2 * ntp_util.c - stuff I didn't have any other place for
11 #include "ntp_unixtime.h"
12 #include "ntp_filegen.h"
14 #include "ntp_stdlib.h"
18 #include <sys/types.h>
19 #ifdef HAVE_SYS_IOCTL_H
20 # include <sys/ioctl.h>
32 #include <sys/resource.h>
41 * This contains odds and ends. Right now the only thing you'll find
42 * in here is the hourly stats printer and some code to support
43 * rereading the keys file, but I may eventually put other things in
44 * here such as code to do something with the leap bits.
47 * Name of the keys file
49 static char *key_file_name;
52 * The name of the drift_comp file and the temporary.
54 static char *stats_drift_file;
55 static char *stats_temp_file;
58 * Statistics file stuff
62 #define NTP_VAR "/var/NTP/" /* NOTE the trailing '/' */
64 #define NTP_VAR "c:\\var\\ntp\\" /* NOTE the trailing '\\' */
65 #endif /* SYS_WINNT */
69 #define MAXPATHLEN 256
72 static char statsdir[MAXPATHLEN] = NTP_VAR;
74 static FILEGEN peerstats;
75 static FILEGEN loopstats;
76 static FILEGEN clockstats;
77 static FILEGEN rawstats;
78 static FILEGEN sysstats;
80 static FILEGEN cryptostats;
84 * This controls whether stats are written to the fileset. Provided
85 * so that ntpdc can turn off stats when the file system fills up.
90 * init_util - initialize the utilities
99 #define PEERNAME "peerstats"
100 #define LOOPNAME "loopstats"
101 #define CLOCKNAME "clockstats"
102 #define RAWNAME "rawstats"
103 #define STANAME "systats"
105 #define CRYPTONAME "cryptostats"
109 peerstats.prefix = &statsdir[0];
110 peerstats.basename = (char*)emalloc(strlen(PEERNAME)+1);
111 strcpy(peerstats.basename, PEERNAME);
113 peerstats.type = FILEGEN_DAY;
114 peerstats.flag = FGEN_FLAG_LINK; /* not yet enabled !!*/
115 filegen_register("peerstats", &peerstats);
118 loopstats.prefix = &statsdir[0];
119 loopstats.basename = (char*)emalloc(strlen(LOOPNAME)+1);
120 strcpy(loopstats.basename, LOOPNAME);
122 loopstats.type = FILEGEN_DAY;
123 loopstats.flag = FGEN_FLAG_LINK; /* not yet enabled !!*/
124 filegen_register("loopstats", &loopstats);
126 clockstats.fp = NULL;
127 clockstats.prefix = &statsdir[0];
128 clockstats.basename = (char*)emalloc(strlen(CLOCKNAME)+1);
129 strcpy(clockstats.basename, CLOCKNAME);
131 clockstats.type = FILEGEN_DAY;
132 clockstats.flag = FGEN_FLAG_LINK; /* not yet enabled !!*/
133 filegen_register("clockstats", &clockstats);
136 rawstats.prefix = &statsdir[0];
137 rawstats.basename = (char*)emalloc(strlen(RAWNAME)+1);
138 strcpy(rawstats.basename, RAWNAME);
140 rawstats.type = FILEGEN_DAY;
141 rawstats.flag = FGEN_FLAG_LINK; /* not yet enabled !!*/
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);
176 * hourly_stats - print some interesting stats
194 * Sometimes having a Sun can be a drag.
196 * The kernel variable dosynctodr controls whether the system's
197 * soft clock is kept in sync with the battery clock. If it
198 * is zero, then the soft clock is not synced, and the battery
199 * clock is simply left to rot. That means that when the system
200 * reboots, the battery clock (which has probably gone wacky)
201 * sets the soft clock. That means ntpd starts off with a very
202 * confused idea of what time it is. It then takes a large
203 * amount of time to figure out just how wacky the battery clock
204 * has made things drift, etc, etc. The solution is to make the
205 * battery clock sync up to system time. The way to do THAT is
206 * to simply set the time of day to the current time of day, but
207 * as quickly as possible. This may, or may not be a sensible
210 * CAVEAT: settimeofday() steps the sun clock by about 800 us,
211 * so setting DOSYNCTODR seems a bad idea in the
212 * case of us resolution
216 /* (prr) getpriority returns -1 on error, but -1 is also a valid
217 * return value (!), so instead we have to zero errno before the
218 * call and check it for non-zero afterwards.
222 o_prio = getpriority(PRIO_PROCESS,0); /* Save setting */
225 * (prr) if getpriority succeeded, call setpriority to raise
226 * scheduling priority as high as possible. If that succeeds
227 * as well, set the prio_set flag so we remember to reset
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
236 if ((errno == 0) && (setpriority(PRIO_PROCESS,0,-20) == 0))
237 prio_set = 1; /* overdrive */
240 (void) getclock(TIMEOFDAY, &ts);
241 tv.tv_sec = ts.tv_sec;
242 tv.tv_usec = ts.tv_nsec / 1000;
243 #else /* not HAVE_GETCLOCK */
244 GETTIMEOFDAY(&tv,(struct timezone *)NULL);
245 #endif /* not HAVE_GETCLOCK */
246 if (ntp_set_tod(&tv,(struct timezone *)NULL) != 0) {
247 msyslog(LOG_ERR, "can't sync battery time: %m");
251 setpriority(PRIO_PROCESS, 0, o_prio); /* downshift */
253 #endif /* DOSYNCTODR */
255 NLOG(NLOG_SYSSTATIST)
257 "offset %.6f sec freq %.3f ppm error %.6f poll %d",
258 last_offset, drift_comp * 1e6, sys_jitter,
263 if (stats_drift_file != 0) {
264 if ((fp = fopen(stats_temp_file, "w")) == NULL) {
265 msyslog(LOG_ERR, "can't open %s: %m",
269 fprintf(fp, "%.3f\n", drift_comp * 1e6);
273 (void) _unlink(stats_drift_file); /* rename semantics differ under NT */
274 #endif /* SYS_WINNT */
277 (void) rename(stats_temp_file, stats_drift_file);
279 /* we have no rename NFS of ftp in use*/
280 if ((fp = fopen(stats_drift_file, "w")) == NULL) {
281 msyslog(LOG_ERR, "can't open %s: %m",
291 $DESCRIPTOR(oldvers,";-1");
292 struct dsc$descriptor driftdsc = {
293 strlen(stats_drift_file),0,0,stats_drift_file };
295 while(lib$delete_file(&oldvers,&driftdsc) & 1) ;
303 * stats_config - configure the stats operation
308 char *invalue /* only one type so far */
317 * Expand environment strings under Windows NT, since the
318 * command interpreter doesn't do this, the program must.
321 char newvalue[MAX_PATH], parameter[MAX_PATH];
323 if (!ExpandEnvironmentStrings(invalue, newvalue, MAX_PATH)) {
325 case STATS_FREQ_FILE:
326 strcpy(parameter,"STATS_FREQ_FILE");
329 strcpy(parameter,"STATS_STATSDIR");
332 strcpy(parameter,"STATS_PID_FILE");
335 strcpy(parameter,"UNKNOWN");
341 "ExpandEnvironmentStrings(%s) failed: %m\n", parameter);
347 #endif /* SYS_WINNT */
350 case STATS_FREQ_FILE:
351 if (stats_drift_file != 0) {
352 (void) free(stats_drift_file);
353 (void) free(stats_temp_file);
354 stats_drift_file = 0;
358 if (value == 0 || (len = strlen(value)) == 0)
361 stats_drift_file = (char*)emalloc((u_int)(len + 1));
363 stats_temp_file = (char*)emalloc((u_int)(len +
366 stats_temp_file = (char*)emalloc((u_int)(len +
369 memmove(stats_drift_file, value, (unsigned)(len+1));
370 memmove(stats_temp_file, value, (unsigned)len);
372 memmove(stats_temp_file + len, ".TEMP",
375 memmove(stats_temp_file + len, "-TEMP",
380 * Open drift file and read frequency. If the file is
381 * missing or contains errors, tell the loop to reset.
383 if ((fp = fopen(stats_drift_file, "r")) == NULL) {
384 loop_config(LOOP_DRIFTCOMP, 1e9);
387 if (fscanf(fp, "%lf", &old_drift) != 1) {
388 msyslog(LOG_ERR, "Frequency format error in %s",
390 loop_config(LOOP_DRIFTCOMP, 1e9);
396 "frequency initialized %.3f PPM from %s",
397 old_drift, stats_drift_file);
398 loop_config(LOOP_DRIFTCOMP, old_drift / 1e6);
402 if (strlen(value) >= sizeof(statsdir)) {
404 "value for statsdir too long (>%d, sigh)",
405 (int)sizeof(statsdir)-1);
410 strcpy(statsdir,value);
411 if(peerstats.prefix == &statsdir[0] &&
412 peerstats.fp != NULL) {
413 fclose(peerstats.fp);
415 filegen_setup(&peerstats, now.l_ui);
417 if(loopstats.prefix == &statsdir[0] &&
418 loopstats.fp != NULL) {
419 fclose(loopstats.fp);
421 filegen_setup(&loopstats, now.l_ui);
423 if(clockstats.prefix == &statsdir[0] &&
424 clockstats.fp != NULL) {
425 fclose(clockstats.fp);
426 clockstats.fp = NULL;
427 filegen_setup(&clockstats, now.l_ui);
429 if(rawstats.prefix == &statsdir[0] &&
430 rawstats.fp != NULL) {
433 filegen_setup(&rawstats, now.l_ui);
435 if(sysstats.prefix == &statsdir[0] &&
436 sysstats.fp != NULL) {
439 filegen_setup(&sysstats, now.l_ui);
442 if(cryptostats.prefix == &statsdir[0] &&
443 cryptostats.fp != NULL) {
444 fclose(cryptostats.fp);
445 cryptostats.fp = NULL;
446 filegen_setup(&cryptostats, now.l_ui);
453 if ((fp = fopen(value, "w")) == NULL) {
454 msyslog(LOG_ERR, "Can't open %s: %m", value);
457 fprintf(fp, "%d", (int) getpid());
468 * record_peer_stats - write peer statistics to file
472 * time (s past UTC midnight)
474 * peer status word (hex)
477 * peer error bound (s)
482 struct sockaddr_storage *addr,
497 filegen_setup(&peerstats, now.l_ui);
498 day = now.l_ui / 86400 + MJD_1900;
500 if (peerstats.fp != NULL) {
501 fprintf(peerstats.fp,
502 "%lu %s %s %x %.9f %.9f %.9f %.9f\n",
503 day, ulfptoa(&now, 3), stoa(addr), status, offset,
504 delay, dispersion, skew);
505 fflush(peerstats.fp);
509 * record_loop_stats - write loop filter statistics to file
513 * time (s past midnight)
515 * frequency (approx ppm)
516 * time constant (log base 2)
534 filegen_setup(&loopstats, now.l_ui);
535 day = now.l_ui / 86400 + MJD_1900;
537 if (loopstats.fp != NULL) {
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);
541 fflush(loopstats.fp);
546 * record_clock_stats - write clock statistics to file
550 * time (s past midnight)
556 struct sockaddr_storage *addr,
567 filegen_setup(&clockstats, now.l_ui);
568 day = now.l_ui / 86400 + MJD_1900;
570 if (clockstats.fp != NULL) {
571 fprintf(clockstats.fp, "%lu %s %s %s\n",
572 day, ulfptoa(&now, 3), stoa(addr), text);
573 fflush(clockstats.fp);
578 * record_raw_stats - write raw timestamps to file
582 * time (s past midnight)
585 * t1 t2 t3 t4 timestamps
589 struct sockaddr_storage *srcadr,
590 struct sockaddr_storage *dstadr,
604 filegen_setup(&rawstats, now.l_ui);
605 day = now.l_ui / 86400 + MJD_1900;
607 if (rawstats.fp != NULL) {
608 fprintf(rawstats.fp, "%lu %s %s %s %s %s %s %s\n",
609 day, ulfptoa(&now, 3), stoa(srcadr), stoa(dstadr),
610 ulfptoa(t1, 9), ulfptoa(t2, 9), ulfptoa(t3, 9),
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);
700 * getauthkeys - read the authentication keys from the specified file
709 len = strlen(keyfile);
713 if (key_file_name != 0) {
714 if (len > (int)strlen(key_file_name)) {
715 (void) free(key_file_name);
720 if (key_file_name == 0) {
722 key_file_name = (char*)emalloc((u_int) (len + 1));
724 key_file_name = (char*)emalloc((u_int) (MAXPATHLEN));
728 memmove(key_file_name, keyfile, (unsigned)(len+1));
730 if (!ExpandEnvironmentStrings(keyfile, key_file_name, MAXPATHLEN))
733 "ExpandEnvironmentStrings(KEY_FILE) failed: %m\n");
735 #endif /* SYS_WINNT */
737 authreadkeys(key_file_name);
742 * rereadkeys - read the authentication key file over again.
747 if (key_file_name != 0)
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 */