]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
ntp_dir_sep.m4, config.h:
authorHarlan Stenn <stenn@ntp.org>
Sun, 10 Jun 2007 07:26:26 +0000 (03:26 -0400)
committerHarlan Stenn <stenn@ntp.org>
Sun, 10 Jun 2007 07:26:26 +0000 (03:26 -0400)
  Use a char for DIR_SEP, not a string
ntp_util.c, ntp_loopfilter.c, ntpd.h, ntp.h, ntp_timer.c, ChangeLog:
  leap file improvements from Dave Mills.

bk: 466ba7a2W0wPuzU5hSVBZNfy64IuWg

ChangeLog
include/ntp.h
include/ntpd.h
m4/ntp_dir_sep.m4
ntpd/ntp_loopfilter.c
ntpd/ntp_timer.c
ntpd/ntp_util.c
ports/winnt/include/config.h

index 584b127bc13f415022f3807b0891c2798d38e336..b25b35cad3df5eede7fd0cba7371958a21d72ea9 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,4 @@
+* Use a char for DIR_SEP, not a string.
 * [Bug 850] driftfile parsing changes.
 * driftfile maintenance changes from Dave Mills.  Use clock_phi instead of
   stats_write_tolerance.
index 83006365328057c8a59a28afe289b853ffce7dbf..3e88d9a2abf87ff1e083df5ebfeda9d231c2a5a6 100644 (file)
@@ -764,6 +764,7 @@ struct pkt {
 #define LOOP_FREQ              10      /* set initial frequency */
 #define LOOP_KERN_CLEAR                11      /* reset kernel pll parameters */
 #define LOOP_CODEC             12      /* set audio codec frequency */
+#define        LOOP_LEAP               13      /* insert leap after second 23:59 */
 
 /*
  * Configuration items for the stats printer
index f5f0976aac378f136c1900d8b7c78dfefa9339ba..24768a1490f9d4bf7ab2f10c8aa850215694c11b 100644 (file)
@@ -119,7 +119,8 @@ extern      void    adj_host_clock  (void);
 extern void    loop_config (int, double);
 extern void    huffpuff        (void);
 extern u_long  sys_clocktime;
-extern u_long  sys_tai;
+extern u_int   sys_tai;
+extern u_long  sys_leapin;
 
 /* ntp_monitor.c */
 extern void    init_mon        (void);
index 6c0d50bf1005b597638246327b9cc441b7e8346b..717b9b173c9209bb89db76ad636623fe7e63bb52 100644 (file)
@@ -6,13 +6,13 @@ AC_CACHE_CHECK([for directory path separator], ac_cv_dir_sep,
   case "$ac_cv_dir_sep" in
    '')
     case "$target_os" in
-      *djgpp | *mingw32* | *emx*) ac_cv_dir_sep='"\\"' ;;
-      *) ac_cv_dir_sep='"/"' ;;
+      *djgpp | *mingw32* | *emx*) ac_cv_dir_sep="'\\'" ;;
+      *) ac_cv_dir_sep="'/'" ;;
     esac
     ;;
   esac
 ])
 AC_DEFINE_UNQUOTED(DIR_SEP,$ac_cv_dir_sep,dnl
-   [Directory separator, usually / or \\])dnl
+   [Directory separator character, usually / or \\])dnl
 ])
 dnl ======================================================================
index 62d68366ab3412e9fc3dd5a231d523a5f436d7e1..ea9f4e529666e1eac172e7c3729dbaacd4ef7966 100644 (file)
@@ -132,7 +132,8 @@ double      clock_stability;        /* frequency stability (wander) (s/s) */
 double clock_codec;            /* audio codec frequency (sambles/s) */
 u_long sys_clocktime;          /* last system clock update */
 u_long pps_control;            /* last pps update */
-u_long sys_tai;                /* UTC offset from TAI (s) */
+u_int  sys_tai;                /* TAI offset from UTC (s) */
+u_long sys_leapin;             /* seconds at next leap (s) */
 static void rstclock (int, u_long, double); /* transition function */
 
 #ifdef KERNEL_PLL
@@ -219,12 +220,6 @@ local_clock(
        double  plladj;         /* PLL frequency adjustment (ppm) */
        double  clock_frequency; /* clock frequency adjustment (ppm) */
        double  dtemp, etemp;   /* double temps */
-#ifdef OPENSSL
-       u_int32 *tpt;
-       int     i;
-       u_int   len;
-       long    togo;
-#endif /* OPENSSL */
 
        /*
         * If the loop is opened or the NIST LOCKCLOCK is in use,
@@ -406,7 +401,6 @@ local_clock(
                        reinit_timer();
                        tc_counter = 0;
                        sys_poll = NTP_MINPOLL;
-                       sys_tai = 0;
                        clock_jitter = LOGTOD(sys_precision);
                        rval = 2;
                        if (state == S_NSET) {
@@ -501,35 +495,6 @@ local_clock(
                }
        }
 
-#ifdef OPENSSL
-       /*
-        * Scan the loopsecond table to determine the TAI offset. If
-        * there is a scheduled leap in future, set the leap warning,
-        * but only if less than 30 days before the leap.
-        */
-       tpt = (u_int32 *)tai_leap.ptr;
-       len = ntohl(tai_leap.vallen) / sizeof(u_int32);
-       if (tpt != NULL) {
-               for (i = 0; i < len; i++) {
-                       togo = (long)ntohl(tpt[i]) -
-                           (long)peer->rec.l_ui;
-                       if (togo > 0) {
-                               if (togo < CLOCK_JUNE)
-                                       leap_next |= LEAP_ADDSECOND;
-                               break;
-                       }
-               }
-#if defined(STA_NANO) && NTP_API == 4
-               if (pll_control && kern_enable && sys_tai == 0) {
-                       memset(&ntv, 0, sizeof(ntv));
-                       ntv.modes = MOD_TAI;
-                       ntv.constant = i + TAI_1972 - 1;
-                       ntp_adjtime(&ntv);
-               }
-#endif /* STA_NANO */
-               sys_tai = i + TAI_1972 - 1;
-       }
-#endif /* OPENSSL */
 #ifdef KERNEL_PLL
        /*
         * This code segment works when clock adjustments are made using
@@ -1039,6 +1004,17 @@ loop_config(
        case LOOP_CODEC:                /* audio codec frequency */
                clock_codec = freq;
                break;
+
+       case LOOP_LEAP:                 /* arm leap insert */
+#if defined(STA_NANO) && NTP_API == 4
+               if (pll_control && kern_enable) {
+                       memset(&ntv, 0, sizeof(ntv));
+                       ntv.modes = MOD_TAI;
+                       ntv.constant = sys_tai - 1;
+                       ntp_adjtime(&ntv);
+               }
+#endif /* STA_NANO */
+               break;
        }
 }
 
index db34a65b92f3dc0146ccf6b581b8f216888bf4d6..54a5be21b8aab96bd4793b07f4f38089ab8ee93f 100644 (file)
@@ -307,6 +307,12 @@ timer(void)
                }
        }
 
+       /*
+        * Leapseconds.
+        */
+       if (sys_tai > 0 && current_time == sys_leapin)
+               loop_config(LOOP_LEAP, sys_tai);
+
        /*
         * Garbage collect expired keys.
         */
index ac55ad0e0b730144522b486b7f97bc34c91ec8e2..f7da0cb808edc933ea5be6572ee645e0d575b041 100644 (file)
@@ -36,6 +36,8 @@
 # include <descrip.h>
 #endif /* VMS */
 
+#define        MAX_LEAP        100     /* max TAI offset (s) */
+
 /*
  * This contains odds and ends.  Right now the only thing you'll find
  * in here is the hourly stats printer and some code to support
@@ -62,7 +64,7 @@ static double prev_drift_comp;
 # ifndef SYS_WINNT
 #  define NTP_VAR "/var/NTP/"          /* NOTE the trailing '/' */
 # else
-#  define NTP_VAR "c:\\var\\ntp\\"             /* NOTE the trailing '\\' */
+#  define NTP_VAR "c:\\var\\ntp\\"     /* NOTE the trailing '\\' */
 # endif /* SYS_WINNT */
 #endif
 
@@ -95,6 +97,8 @@ int stats_control;
  */
 double old_drift;
 
+static void leap_file(char *);
+
 /*
  * init_util - initialize the utilities
  */
@@ -125,17 +129,19 @@ init_util(void)
 void
 write_stats(void)
 {
-       FILE *fp;
+       FILE    *fp;
+       double  ftemp;
+
 
 #ifdef DOSYNCTODR
        struct timeval tv;
 #if !defined(VMS)
-       int prio_set;
+       int     prio_set;
 #endif
 #ifdef HAVE_GETCLOCK
         struct timespec ts;
 #endif
-       int o_prio;
+       int     o_prio;
 
        /*
         * Sometimes having a Sun can be a drag.
@@ -160,7 +166,8 @@ write_stats(void)
         */
 
 #if !defined(VMS)
-       /* (prr) getpriority returns -1 on error, but -1 is also a valid
+       /*
+        * (prr) getpriority returns -1 on error, but -1 is also a valid
         * return value (!), so instead we have to zero errno before the
         * call and check it for non-zero afterwards.
         */
@@ -190,9 +197,8 @@ write_stats(void)
 #else /*  not HAVE_GETCLOCK */
        GETTIMEOFDAY(&tv,(struct timezone *)NULL);
 #endif /* not HAVE_GETCLOCK */
-       if (ntp_set_tod(&tv,(struct timezone *)NULL) != 0) {
+       if (ntp_set_tod(&tv,(struct timezone *)NULL) != 0)
                msyslog(LOG_ERR, "can't sync battery time: %m");
-       }
 #if !defined(VMS)
        if (prio_set)
                setpriority(PRIO_PROCESS, 0, o_prio); /* downshift */
@@ -205,17 +211,18 @@ write_stats(void)
                    last_offset, drift_comp * 1e6, sys_jitter,
                    sys_poll);
 
-       
        record_sys_stats();
-       if (fabs(prev_drift_comp - drift_comp) > clock_phi)
+       ftemp = fabs(prev_drift_comp - drift_comp); 
+       prev_drift_comp = drift_comp;
+       if (ftemp > clock_phi)
                return;
 
-       prev_drift_comp = drift_comp;
        if (stats_drift_file != 0) {
                if (state == 4) {
-                       if ((fp = fopen(stats_temp_file, "w")) == NULL) {
+                       if ((fp = fopen(stats_temp_file, "w")) == NULL)
+                           {
                                msyslog(LOG_ERR, "can't open %s: %m",
-                                       stats_temp_file);
+                                   stats_temp_file);
                                return;
                        }
                        fprintf(fp, "%.3f\n", drift_comp * 1e6);
@@ -229,9 +236,10 @@ write_stats(void)
                        (void) rename(stats_temp_file, stats_drift_file);
 #else
                        /* we have no rename NFS of ftp in use */
-                       if ((fp = fopen(stats_drift_file, "w")) == NULL) {
+                       if ((fp = fopen(stats_drift_file, "w")) == NULL)
+                           {
                                msyslog(LOG_ERR, "can't open %s: %m",
-                                       stats_drift_file);
+                                   stats_drift_file);
                                return;
                        }
 #endif
@@ -241,9 +249,10 @@ write_stats(void)
                        {
                                $DESCRIPTOR(oldvers,";-1");
                                struct dsc$descriptor driftdsc = {
-                                       strlen(stats_drift_file),0,0,stats_drift_file };
-
-                               while(lib$delete_file(&oldvers,&driftdsc) & 1);
+                                       strlen(stats_drift_file), 0, 0,
+                                           stats_drift_file };
+                               while(lib$delete_file(&oldvers,
+                                   &driftdsc) & 1);
                        }
 #endif
                } else {
@@ -278,20 +287,23 @@ stats_config(
                    case STATS_FREQ_FILE:
                        strcpy(parameter,"STATS_FREQ_FILE");
                        break;
+
                    case STATS_STATSDIR:
                        strcpy(parameter,"STATS_STATSDIR");
                        break;
+
                    case STATS_PID_FILE:
                        strcpy(parameter,"STATS_PID_FILE");
                        break;
+
                    default:
                        strcpy(parameter,"UNKNOWN");
                        break;
                }
                value = invalue;
-
                msyslog(LOG_ERR,
-                   "ExpandEnvironmentStrings(%s) failed: %m\n", parameter);
+                   "ExpandEnvironmentStrings(%s) failed: %m\n",
+                   parameter);
        } else {
                value = newvalue;
        }
@@ -351,32 +363,36 @@ stats_config(
                msyslog(LOG_INFO,
                    "frequency initialized %.3f PPM from %s",
                        old_drift * 1e6, stats_drift_file);
+
+               leap_file("/etc/leapseconds");
+
                break;
        
            case STATS_STATSDIR:
-               /* HMS: the following test is insufficient:
+
+               /*
+                * HMS: the following test is insufficient:
                 * - value may be missing the DIR_SEP
                 * - we still need the filename after it
                 */
                if (strlen(value) >= sizeof(statsdir)) {
                        msyslog(LOG_ERR,
                            "value for statsdir too long (>%d, sigh)",
-                           (int)sizeof(statsdir)-1);
+                           (int)sizeof(statsdir) - 1);
                } else {
                        l_fp now;
                        int add_dir_sep;
                        int value_l = strlen(value);
 
-                       /* We do not want a DIR_SEP if we have no prefix */
+                       /* Add a DIR_SEP unless we already have one. */
                        if (value_l == 0)
                                add_dir_sep = 0;
                        else
-                               add_dir_sep = strcmp(DIR_SEP,
-                                       value + value_l - strlen(DIR_SEP));
+                               add_dir_sep = (DIR_SEP == value[value_l - 1]);
 
                        if (add_dir_sep)
                            snprintf(statsdir, sizeof(statsdir),
-                               "%s%s", value, DIR_SEP);
+                               "%s%c", value, DIR_SEP);
                        else
                            snprintf(statsdir, sizeof(statsdir),
                                "%s", value);
@@ -438,6 +454,7 @@ stats_config(
        }
 }
 
+
 /*
  * record_peer_stats - write peer statistics to file
  *
@@ -473,13 +490,14 @@ record_peer_stats(
        now.l_ui %= 86400;
        if (peerstats.fp != NULL) {
                fprintf(peerstats.fp,
-                   "%lu %s %s %x %.9f %.9f %.9f %.9f\n",
-                   day, ulfptoa(&now, 3), stoa(addr), status, offset,
+                   "%lu %s %s %x %.9f %.9f %.9f %.9f\n", day,
+                   ulfptoa(&now, 3), stoa(addr), status, offset,
                    delay, dispersion, skew);
                fflush(peerstats.fp);
        }
 }
 
+
 /*
  * record_loop_stats - write loop filter statistics to file
  *
@@ -517,6 +535,7 @@ record_loop_stats(
        }
 }
 
+
 /*
  * record_clock_stats - write clock statistics to file
  *
@@ -543,16 +562,16 @@ record_clock_stats(
        day = now.l_ui / 86400 + MJD_1900;
        now.l_ui %= 86400;
        if (clockstats.fp != NULL) {
-               fprintf(clockstats.fp, "%lu %s %s %s\n",
-                   day, ulfptoa(&now, 3), stoa(addr), text);
+               fprintf(clockstats.fp, "%lu %s %s %s\n", day,
+                   ulfptoa(&now, 3), stoa(addr), text);
                fflush(clockstats.fp);
        }
 }
 
+
 /*
  * record_raw_stats - write raw timestamps to file
  *
- *
  * file format
  * time (s past midnight)
  * peer ip address
@@ -580,10 +599,10 @@ record_raw_stats(
        day = now.l_ui / 86400 + MJD_1900;
        now.l_ui %= 86400;
        if (rawstats.fp != NULL) {
-                fprintf(rawstats.fp, "%lu %s %s %s %s %s %s %s\n",
-                       day, ulfptoa(&now, 3), stoa(srcadr), dstadr ? stoa(dstadr) : "-",
-                       ulfptoa(t1, 9), ulfptoa(t2, 9), ulfptoa(t3, 9),
-                       ulfptoa(t4, 9));
+                fprintf(rawstats.fp, "%lu %s %s %s %s %s %s %s\n", day,
+                   ulfptoa(&now, 3), stoa(srcadr), dstadr ? 
+                   stoa(dstadr) : "-", ulfptoa(t1, 9), ulfptoa(t2, 9),
+                   ulfptoa(t3, 9), ulfptoa(t4, 9));
                fflush(rawstats.fp);
        }
 }
@@ -670,6 +689,79 @@ record_crypto_stats(
 }
 #endif /* OPENSSL */
 
+
+/*
+ * leap_file - load leapseconds table from file
+ *
+ * Read the ERTS leapsecond file in NIST text format and extract the
+ * NTP seconds of the latest leap and TAI offset after the leap..
+ */
+static void
+leap_file(
+       char    *cp             /* file name */
+       )
+{
+       FILE    *str;           /* file handle */
+       char    buf[NTP_MAXSTRLEN];     /* file line buffer */
+       u_long  leapsec;        /* NTP time at leap */
+       int     offset;         /* TAI offset at leap (s) */
+       char    filename[MAXFILENAME]; /* name of leapseconds file */
+       char    *dp;
+       int     i;
+
+       /*
+        * Open the file and discard comment lines. If the first
+        * character of the file name is not '/', prepend the keys
+        * directory string. If the file is not found, ignore; if found
+        * with errors, report to the log and ignore.
+        */
+       if (*cp == '/')
+               strcpy(filename, cp);
+       else
+               snprintf(filename, MAXFILENAME, "%s/%s", keysdir, cp);
+       if ((str = fopen(filename, "r")) == NULL)
+               return;
+
+       /*
+        * Read and parse the leapseconds file. Empty lines and comments
+        * are ignored. Other lines must begin with two integers
+        * followed by junk or comments. The first integer is the NTP
+        * seconds at the leap, the second is the TAI offset after the
+        * leap. The second word must equal the initial insertion of ten
+        * seconds on 1 January 1972 plus one second for each succeeding
+        * insertion.
+        */
+       i = TAI_1972;
+       while (i < MAX_LEAP) {
+               dp = fgets(buf, NTP_MAXSTRLEN - 1, str);
+               if (dp == NULL)
+                       break;
+
+               if (strlen(buf) < 1)
+                       continue;
+
+               if (*buf == '#')
+                       continue;
+
+               if (sscanf(buf, "%lu %d", &leapsec, &offset) != 2)
+                       continue;
+
+               if (i != offset) 
+                       break;
+               i++;
+       }
+       fclose(str);
+       if (dp != NULL) {
+               msyslog(LOG_INFO, "Leapseconds format error in %s", cp);
+       } else {
+               sys_tai = offset;
+               sys_leapin = leapsec;
+               msyslog(LOG_INFO, "TAI offset %d s at %lu from %s",
+                   sys_tai, sys_leapin, cp);
+       }
+}
+
+
 #ifdef DEBUG_TIMING
 /*
  * record_crypto_stats - write crypto statistics to file
@@ -696,13 +788,15 @@ record_timing_stats(
        day = now.l_ui / 86400 + MJD_1900;
        now.l_ui %= 86400;
        if (timingstats.fp != NULL) {
-               fprintf(timingstats.fp, "%lu %s %s\n",
-                           day, lfptoa(&now, 3), text);
+               fprintf(timingstats.fp, "%lu %s %s\n", day, lfptoa(&now,
+                   3), text);
                if (++flshcnt % 100 == 0)
                        fflush(timingstats.fp);
        }
 }
 #endif
+
+
 /*
  * getauthkeys - read the authentication keys from the specified file
  */
@@ -734,13 +828,12 @@ getauthkeys(
 #ifndef SYS_WINNT
        memmove(key_file_name, keyfile, (unsigned)(len+1));
 #else
-       if (!ExpandEnvironmentStrings(keyfile, key_file_name, MAXPATHLEN)) 
-       {
+       if (!ExpandEnvironmentStrings(keyfile, key_file_name,
+           MAXPATHLEN)) {
                msyslog(LOG_ERR,
                    "ExpandEnvironmentStrings(KEY_FILE) failed: %m\n");
        }
 #endif /* SYS_WINNT */
-
        authreadkeys(key_file_name);
 }
 
@@ -755,6 +848,7 @@ rereadkeys(void)
            authreadkeys(key_file_name);
 }
 
+
 /*
  * sock_hash - hash an sockaddr_storage structure
  */
@@ -767,9 +861,9 @@ sock_hash(
        int i;
        int len;
        char *ch;
-
        hashVal = 0;
        len = 0;
+
        /*
         * We can't just hash the whole thing because there are hidden
         * fields in sockaddr_in6 that might be filled in by recvfrom(),
@@ -786,6 +880,7 @@ sock_hash(
                ch = (char *)&((struct sockaddr_in *)addr)->sin_addr;
                len = sizeof(struct in_addr);
                break;
+
        case AF_INET6:
                ch = (char *)&((struct sockaddr_in6 *)addr)->sin6_addr;
                len = sizeof(struct in6_addr);
@@ -794,15 +889,13 @@ sock_hash(
 
        for (i = 0; i < len ; i++)
                hashVal = 37 * hashVal + (int)*(ch + i);
-
        hashVal = hashVal % 128;  /* % MON_HASH_SIZE hardcoded */
-
        if (hashVal < 0)
                hashVal += 128;
-
        return hashVal;
 }
 
+
 #if notyet
 /*
  * ntp_exit - document explicitly that ntpd has exited
@@ -810,7 +903,7 @@ sock_hash(
 void
 ntp_exit(int retval)
 {
-  msyslog(LOG_ERR, "EXITING with return code %d", retval);
-  exit(retval);
+       msyslog(LOG_ERR, "EXITING with return code %d", retval);
+       exit(retval);
 }
 #endif
index 0bf51161cd6290ad7e48578593fa92196e7ffa53..2da45c2fdc53be2739f2d1d3ab2a8c540070f5f1 100644 (file)
@@ -231,7 +231,7 @@ int NT_set_process_priority(void);  /* Define this function */
 # define USE_PROTOTYPES                /* for ntp_types.h */
 
 /* Directory separator, usually / or \ */
-#define DIR_SEP "\\"
+#define DIR_SEP '\\'
 
 #define ULONG_CONST(a) a ## UL