From: Dave Hart Date: Wed, 21 Oct 2009 21:59:17 +0000 (+0000) Subject: [Bug 1353] ntpq "rv 0 settimeofday" always shows UNKNOWN on unix. X-Git-Tag: NTP_4_2_5P236_RC~2^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5005f7a2a09c737acd325f70e3b6d1f77b888dcf;p=thirdparty%2Fntp.git [Bug 1353] ntpq "rv 0 settimeofday" always shows UNKNOWN on unix. bk: 4adf8435J-nd7Dpcs0mQukzEf33zsQ --- diff --git a/ChangeLog b/ChangeLog index 9a62b14a6..04356b1a2 100644 --- 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 * [Bug 1343] lib/isc build breaks on systems without IPv6 headers. (4.2.5p234-RC) 2009/10/16 Released by Harlan Stenn diff --git a/include/ntp_stdlib.h b/include/ntp_stdlib.h index 840c80cc0..f50717726 100644 --- a/include/ntp_stdlib.h +++ b/include/ntp_stdlib.h @@ -1,6 +1,9 @@ /* * ntp_stdlib.h - Prototypes for NTP lib. */ +#ifndef NTP_STDLIB_H +#define NTP_STDLIB_H + #include #include @@ -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 */ diff --git a/libntp/machines.c b/libntp/machines.c index 481837af1..450138047 100644 --- a/libntp/machines.c +++ b/libntp/machines.c @@ -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 @@ -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; } diff --git a/ntpd/ntp_config.c b/ntpd/ntp_config.c index 7439013c4..bc1cb9f69 100644 --- a/ntpd/ntp_config.c +++ b/ntpd/ntp_config.c @@ -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 * -------------------------------------------- */ diff --git a/ports/winnt/libntp/SetSystemTime.c b/ports/winnt/libntp/SetSystemTime.c index b4c2288bb..3bc99382a 100644 --- a/ports/winnt/libntp/SetSystemTime.c +++ b/ports/winnt/libntp/SetSystemTime.c @@ -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; diff --git a/ports/winnt/ntpd/nt_clockstuff.c b/ports/winnt/ntpd/nt_clockstuff.c index a1aefe854..85d286bcf 100644 --- a/ports/winnt/ntpd/nt_clockstuff.c +++ b/ports/winnt/ntpd/nt_clockstuff.c @@ -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