]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
[Bug 1353] ntpq "rv 0 settimeofday" always shows UNKNOWN on unix.
authorDave Hart <hart@ntp.org>
Wed, 21 Oct 2009 21:59:17 +0000 (21:59 +0000)
committerDave Hart <hart@ntp.org>
Wed, 21 Oct 2009 21:59:17 +0000 (21:59 +0000)
bk: 4adf8435J-nd7Dpcs0mQukzEf33zsQ

ChangeLog
include/ntp_stdlib.h
libntp/machines.c
ntpd/ntp_config.c
ports/winnt/libntp/SetSystemTime.c
ports/winnt/ntpd/nt_clockstuff.c

index 9a62b14a670c77b23d172abac1979d97483273f5..04356b1a2ca34e6c2daa00e484a9e747ab7d6ce1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,4 @@
+* [Bug 1353] ntpq "rv 0 settimeofday" always shows UNKNOWN on unix.
 (4.2.5p235-RC) 2009/10/18 Released by Harlan Stenn <stenn@ntp.org>
 * [Bug 1343] lib/isc build breaks on systems without IPv6 headers.
 (4.2.5p234-RC) 2009/10/16 Released by Harlan Stenn <stenn@ntp.org>
index 840c80cc0dfc3f7e04c32fcb751eada504e86332..f50717726f2cd695c89c6bc59e4272879e9599b1 100644 (file)
@@ -1,6 +1,9 @@
 /*
  * ntp_stdlib.h - Prototypes for NTP lib.
  */
+#ifndef NTP_STDLIB_H
+#define NTP_STDLIB_H
+
 #include <sys/types.h>
 #include <sys/socket.h>
 
@@ -140,7 +143,8 @@ extern int  ipv4_works;
 extern int     ipv6_works;
 
 /* machines.c */
-extern const char *set_tod_using;
+typedef void (*pset_tod_using)(const char *);
+extern pset_tod_using  set_tod_using;
 
 /* lib/isc/win32/strerror.c
  *
@@ -161,3 +165,5 @@ extern double       sys_tick;               /* adjtime() resolution */
 
 /* version.c */
 extern const char *Version;            /* version declaration */
+
+#endif /* NTP_STDLIB_H */
index 481837af1ac35b1acbe0c8f5afed4716cdc27fe1..450138047db63c0361d9d156ac53775b28c51f1a 100644 (file)
@@ -12,6 +12,7 @@
 #include "ntp_syslog.h"
 #include "ntp_stdlib.h"
 #include "ntp_unixtime.h"
+#include "lib_strbuf.h"
 
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
@@ -409,7 +410,20 @@ return 0;
 }
 #endif /* MPE */
 
-const char *set_tod_using = "UNKNOWN";
+#define SET_TOD_UNDETERMINED   0
+#define SET_TOD_CLOCK_SETTIME  1
+#define SET_TOD_SETTIMEOFDAY   2
+#define SET_TOD_STIME          3
+
+const char * const set_tod_used[] = {
+       "undetermined",
+       "clock_settime",
+       "settimeofday",
+       "stime"
+};
+
+pset_tod_using set_tod_using = NULL;
+
 
 int
 ntp_set_tod(
@@ -417,7 +431,9 @@ ntp_set_tod(
        void *tzp
        )
 {
+       static int tod;
        int rc = -1;
+       int saved_errno = 0;
 
 #ifdef DEBUG
        if (debug)
@@ -425,29 +441,31 @@ ntp_set_tod(
 #endif
 
 #ifdef HAVE_CLOCK_SETTIME
-       if (rc) {
+       if (rc && (SET_TOD_CLOCK_SETTIME == tod || !tod)) {
                struct timespec ts;
 
-               set_tod_using = "clock_settime";
                /* Convert timeval to timespec */
                ts.tv_sec = tvp->tv_sec;
                ts.tv_nsec = 1000 *  tvp->tv_usec;
 
                errno = 0;
                rc = clock_settime(CLOCK_REALTIME, &ts);
+               saved_errno = errno;
 #ifdef DEBUG
                if (debug) {
-                       printf("ntp_set_tod: %s: %d: %s\n",
-                              set_tod_using, rc, strerror(errno));
+                       printf("ntp_set_tod: clock_settime: %d: %s\n",
+                              rc, strerror(saved_errno));
                }
 #endif
+               if (!tod && !rc)
+                       tod = SET_TOD_CLOCK_SETTIME;
+
        }
 #endif /* HAVE_CLOCK_SETTIME */
 #ifdef HAVE_SETTIMEOFDAY
-       if (rc) {
+       if (rc && (SET_TOD_SETTIMEOFDAY == tod || !tod)) {
                struct timeval adjtv;
 
-               set_tod_using = "settimeofday";
                /*
                 * Some broken systems don't reset adjtime() when the
                 * clock is stepped.
@@ -456,37 +474,52 @@ ntp_set_tod(
                adjtime(&adjtv, NULL);
                errno = 0;
                rc = SETTIMEOFDAY(tvp, tzp);
+               saved_errno = errno;
 #ifdef DEBUG
                if (debug) {
-                       printf("ntp_set_tod: %s: %d: %s\n",
-                              set_tod_using, rc, strerror(errno));
+                       printf("ntp_set_tod: settimeofday: %d: %s\n",
+                              rc, strerror(saved_errno));
                }
 #endif
+               if (!tod && !rc)
+                       tod = SET_TOD_SETTIMEOFDAY;
        }
 #endif /* HAVE_SETTIMEOFDAY */
 #ifdef HAVE_STIME
-       if (rc) {
+       if (rc && (SET_TOD_STIME == tod || !tod)) {
                long tp = tvp->tv_sec;
 
-               set_tod_using = "stime";
                errno = 0;
                rc = stime(&tp); /* lie as bad as SysVR4 */
+               saved_errno = errno;
 #ifdef DEBUG
                if (debug) {
-                       printf("ntp_set_tod: %s: %d: %s\n",
-                              set_tod_using, rc, strerror(errno));
+                       printf("ntp_set_tod: stime: %d: %s\n",
+                              rc, strerror(saved_errno));
                }
 #endif
+               if (!tod && !rc)
+                       tod = SET_TOD_STIME;
        }
 #endif /* HAVE_STIME */
-       if (rc)
-           set_tod_using = "Failed!";
+
 #ifdef DEBUG
        if (debug) {
                printf("ntp_set_tod: Final result: %s: %d: %s\n",
-                       set_tod_using, rc, strerror(errno));
+                       set_tod_used[tod], rc, strerror(saved_errno));
        }
 #endif
+       /*
+        * Say how we're setting the time of day
+        */
+       if (!rc && NULL != set_tod_using) {
+               (*set_tod_using)(set_tod_used[tod]);
+               set_tod_using = NULL;
+       }
+
+       if (rc)
+               errno = saved_errno;
+
        return rc;
 }
 
index 7439013c47b5682de63df4ef8d732b7e5da2bfd8..bc1cb9f6944f423112b3e3cec8e9c8341a744aac 100644 (file)
@@ -303,6 +303,7 @@ do {                                        \
        }                               \
 } while (0)
 
+void ntpd_set_tod_using(const char *);
 static unsigned long get_pfxmatch(char **s,struct masks *m);
 static unsigned long get_match(char *s,struct masks *m);
 static unsigned long get_logmask(char *s);
@@ -3776,11 +3777,19 @@ getconfig(
        set_sys_var(line, strlen(line)+1, RO);
 
        /*
-        * Say how we're setting the time of day
+        * Set up for the first time step to install a variable showing
+        * which syscall is being used to step.
         */
-       snprintf(line, sizeof(line), "settimeofday=\"%s\"",
-           set_tod_using);
-       set_sys_var(line, strlen(line)+1, RO);
+       set_tod_using = &ntpd_set_tod_using;
+
+       /*
+        * On Windows, the variable has already been set, on the rest,
+        * initialize it to "UNKNOWN".
+        */
+#ifndef SYS_WINNT
+       strncpy(line, "settimeofday=\"UNKNOWN\"", sizeof(line));
+       set_sys_var(line, strlen(line) + 1, RO);
+#endif
 
        /*
         * Initialize the loop.
@@ -3936,6 +3945,18 @@ save_and_apply_config_tree(void)
 }
 
 
+void
+ntpd_set_tod_using(
+       const char *which
+       )
+{
+       char line[128];
+
+       snprintf(line, sizeof(line), "settimeofday=\"%s\"", which);
+       set_sys_var(line, strlen(line) + 1, RO);
+}
+
+
 /* FUNCTIONS COPIED FROM THE OLDER ntp_config.c
  * --------------------------------------------
  */
index b4c2288bb32253c24afbe1bbb2d80039e4aa1cce..3bc99382a5ce8ba851fcb4115aca69da828b534f 100644 (file)
@@ -2,7 +2,7 @@
 #include "clockstuff.h"
 #include "ntp_stdlib.h"
 
-const char *set_tod_using = "SetSystemTime";
+pset_tod_using         set_tod_using = NULL;
 
 time_stepped_callback  step_callback = NULL;
 
index a1aefe8540b44069232f05060493c56ff02fb540..85d286bcf9f212fd1039791d82e6e73dd00fc62f 100644 (file)
@@ -533,6 +533,7 @@ adj_systime(
 void 
 init_winnt_time(void)
 {
+       static const char settod[] = "settimeofday=\"SetSystemTime\"";
        char szMsgPath[MAX_PATH+1];
        HANDLE hToken = INVALID_HANDLE_VALUE;
        TOKEN_PRIVILEGES tkp;
@@ -615,6 +616,11 @@ init_winnt_time(void)
        CloseHandle(hToken);
        hToken = INVALID_HANDLE_VALUE;
 
+       /*
+        * Say how we're setting the time of day
+        */
+       set_sys_var(settod, sizeof(settod), RO);
+
        /*
         * ntpd on Windows has always raised its priority, without
         * requiring -N as on Unix.  Since Windows ntpd doesn't share