From: Dave Hart Date: Wed, 25 May 2011 03:44:32 +0000 (+0000) Subject: [Bug 1695] ntpdate takes longer than necessary. X-Git-Tag: NTP_4_2_6P4_BETA2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=91bad0ea795df086e61873588ccb8dc61c8fce63;p=thirdparty%2Fntp.git [Bug 1695] ntpdate takes longer than necessary. [Bug 1832] ntpdate doesn't allow timeout > 2s. [Bug 1933] WWVB/Spectracom driver timestamps LFs, not CRs. Backport utility routines from ntp-dev: mprintf(), emalloc_zero(). bk: 4ddc7b20q46RhHddOdauhS9oGZZKbw --- diff --git a/ChangeLog b/ChangeLog index 199c4d471..9987116de 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +--- + +* [Bug 1695] ntpdate takes longer than necessary. +* [Bug 1832] ntpdate doesn't allow timeout > 2s. +* [Bug 1933] WWVB/Spectracom driver timestamps LFs, not CRs. +* Backport utility routines from ntp-dev: mprintf(), emalloc_zero(). + --- (4.2.6p4-beta1) 2011/05/16 Released by Harlan Stenn diff --git a/include/ntp_assert.h b/include/ntp_assert.h index 179d97d94..ab9b41981 100644 --- a/include/ntp_assert.h +++ b/include/ntp_assert.h @@ -7,17 +7,21 @@ * int result; * int value; * - * NTP_REQUIRE(a != NULL); + * REQUIRE(a != NULL); * ... * bar(&value); - * NTP_INSIST(value > 2); + * INSIST(value > 2); * ... * - * NTP_ENSURE(result != 12); + * ENSURE(result != 12); * return result; * } * - * open question: when would we use NTP_INVARIANT()? + * open question: when would we use INVARIANT()? + * + * For cases where the overhead for non-debug builds is deemed too high, + * use DEBUG_REQUIRE(), DEBUG_INSIST(), DEBUG_ENSURE(), and/or + * DEBUG_INVARIANT(). */ #ifndef NTP_ASSERT_H @@ -27,12 +31,16 @@ extern void calysto_assume(unsigned char cnd); /* assume this always holds */ extern void calysto_assert(unsigned char cnd); /* check whether this holds */ -#define NTP_REQUIRE(x) calysto_assert(x) -#define NTP_INSIST(x) calysto_assume(x) /* DLH calysto_assert()? */ -#define NTP_INVARIANT(x) calysto_assume(x) -#define NTP_ENSURE(x) calysto_assert(x) +#define ALWAYS_REQUIRE(x) calysto_assert(x) +#define ALWAYS_INSIST(x) calysto_assume(x) /* DLH calysto_assert()? */ +#define ALWAYS_INVARIANT(x) calysto_assume(x) +#define ALWAYS_ENSURE(x) calysto_assert(x) -# elif defined(__COVERITY__) +/* # elif defined(__COVERITY__) */ +/* + * DH: try letting coverity scan our actual assertion macros, now that + * isc_assertioncallback_t is marked __attribute__ __noreturn__. + */ /* * Coverity has special knowledge that assert(x) terminates the process @@ -42,19 +50,48 @@ extern void calysto_assert(unsigned char cnd); /* check whether this holds */ * that seems to be a reasonable trade-off. */ -#define NTP_REQUIRE(x) assert(x) -#define NTP_INSIST(x) assert(x) -#define NTP_INVARIANT(x) assert(x) -#define NTP_ENSURE(x) assert(x) +/* +#define ALWAYS_REQUIRE(x) assert(x) +#define ALWAYS_INSIST(x) assert(x) +#define ALWAYS_INVARIANT(x) assert(x) +#define ALWAYS_ENSURE(x) assert(x) +*/ # else /* neither Coverity nor Calysto */ #include "isc/assertions.h" -#define NTP_REQUIRE(x) ISC_REQUIRE(x) -#define NTP_INSIST(x) ISC_INSIST(x) -#define NTP_INVARIANT(x) ISC_INVARIANT(x) -#define NTP_ENSURE(x) ISC_ENSURE(x) +#define ALWAYS_REQUIRE(x) ISC_REQUIRE(x) +#define ALWAYS_INSIST(x) ISC_INSIST(x) +#define ALWAYS_INVARIANT(x) ISC_INVARIANT(x) +#define ALWAYS_ENSURE(x) ISC_ENSURE(x) # endif /* neither Coverity nor Calysto */ + +#define REQUIRE(x) ALWAYS_REQUIRE(x) +#define INSIST(x) ALWAYS_INSIST(x) +#define INVARIANT(x) ALWAYS_INVARIANT(x) +#define ENSURE(x) ALWAYS_ENSURE(x) + +/* + * We initially used NTP_REQUIRE() instead of REQUIRE() etc, but that + * is unneccesarily verbose, as libisc use of REQUIRE() etc shows. + */ +#define NTP_REQUIRE(x) REQUIRE(x) +#define NTP_INSIST(x) INSIST(x) +#define NTP_INVARIANT(x) INVARIANT(x) +#define NTP_ENSURE(x) ENSURE(x) + +# ifdef DEBUG +#define DEBUG_REQUIRE(x) REQUIRE(x) +#define DEBUG_INSIST(x) INSIST(x) +#define DEBUG_INVARIANT(x) INVARIANT(x) +#define DEBUG_ENSURE(x) ENSURE(x) +# else +#define DEBUG_REQUIRE(x) (void)(x) +#define DEBUG_INSIST(x) (void)(x) +#define DEBUG_INVARIANT(x) (void)(x) +#define DEBUG_ENSURE(x) (void)(x) +# endif + #endif /* NTP_ASSERT_H */ diff --git a/include/ntp_malloc.h b/include/ntp_malloc.h index 0dbcbeed4..4cde62e1a 100644 --- a/include/ntp_malloc.h +++ b/include/ntp_malloc.h @@ -4,10 +4,6 @@ #ifndef NTP_MALLOC_H #define NTP_MALLOC_H -#ifdef HAVE_CONFIG_H -# include -#endif - #ifdef HAVE_STDLIB_H # include #else @@ -16,4 +12,50 @@ # endif #endif +/* + * Deal with platform differences declaring alloca() + * This comes nearly verbatim from: + * + * http://www.gnu.org/software/autoconf/manual/autoconf.html#Particular-Functions + * + * The only modifications were to remove C++ support and guard against + * redefining alloca. + */ +#ifdef HAVE_ALLOCA_H +# include +#elif defined __GNUC__ +# ifndef alloca +# define alloca __builtin_alloca +# endif +#elif defined _AIX +# ifndef alloca +# define alloca __alloca +# endif +#elif defined _MSC_VER +# include +# ifndef alloca +# define alloca _alloca +# endif +#else +# include +void * alloca(size_t); +#endif + +#ifdef EREALLOC_IMPL +# define EREALLOC_CALLSITE /* preserve __FILE__ and __LINE__ */ +#else +# define EREALLOC_IMPL(ptr, newsz, filenm, loc) \ + realloc(ptr, (newsz)) +#endif + +#ifdef HAVE_STRINGS_H +# include +# define zero_mem(p, s) bzero(p, s) +#endif + +#ifndef zero_mem +# define zero_mem(p, s) memset(p, 0, s) +#endif +#define ZERO(var) zero_mem(&(var), sizeof(var)) + #endif /* NTP_MALLOC_H */ diff --git a/include/ntp_refclock.h b/include/ntp_refclock.h index 7c89af996..9fa4a7bce 100644 --- a/include/ntp_refclock.h +++ b/include/ntp_refclock.h @@ -177,7 +177,7 @@ struct refclockbug { struct refclockproc { struct refclockio io; /* I/O handler structure */ - caddr_t unitptr; /* pointer to unit structure */ + void * unitptr; /* pointer to unit structure */ u_char leap; /* leap/synchronization code */ u_char currentstatus; /* clock status */ u_char lastevent; /* last exception event */ diff --git a/include/ntp_stdlib.h b/include/ntp_stdlib.h index d1033ffe4..8fc237ae0 100644 --- a/include/ntp_stdlib.h +++ b/include/ntp_stdlib.h @@ -12,6 +12,7 @@ #include "l_stdlib.h" #include "ntp_rfc2553.h" #include "ntp_types.h" +#include "ntp_malloc.h" #include "ntp_string.h" #include "ntp_net.h" #include "ntp_syslog.h" @@ -33,9 +34,16 @@ # endif #endif -extern size_t mvsnprintf(char *, size_t, const char *, va_list); -extern size_t msnprintf(char *, size_t, const char *, ...) - __attribute__((__format__(__printf__, 3, 4))); +extern int mprintf(const char *, ...) + __attribute__((__format__(__printf__, 1, 2))); +extern int mfprintf(FILE *, const char *, ...) + __attribute__((__format__(__printf__, 2, 3))); +extern int mvfprintf(FILE *, const char *, va_list) + __attribute__((__format__(__printf__, 2, 0))); +extern int mvsnprintf(char *, size_t, const char *, va_list) + __attribute__((__format__(__printf__, 3, 0))); +extern int msnprintf(char *, size_t, const char *, ...) + __attribute__((__format__(__printf__, 3, 4))); extern void msyslog(int, const char *, ...) __attribute__((__format__(__printf__, 2, 3))); @@ -63,17 +71,6 @@ extern int authusekey (keyid_t, int, const u_char *); extern u_long calyearstart (u_long); extern const char *clockname (int); extern int clocktime (int, int, int, int, int, u_long, u_long *, u_int32 *); -#if !defined(_MSC_VER) || !defined(_DEBUG) -extern void * emalloc (size_t); -extern void * erealloc (void *, size_t); -extern char * estrdup (const char *); -#else -extern void * debug_erealloc (void *, size_t, const char *, int); -#define emalloc(c) debug_erealloc(NULL, (c), __FILE__, __LINE__) -#define erealloc(p, c) debug_erealloc((p), (c), __FILE__, __LINE__) -extern char * debug_estrdup (const char *, const char *, int); -#define estrdup(s) debug_estrdup((s), __FILE__, __LINE__) -#endif extern int ntp_getopt (int, char **, const char *); extern void init_auth (void); extern void init_lib (void); @@ -87,6 +84,32 @@ extern int MD5authencrypt (int, u_char *, u_int32 *, int); extern void MD5auth_setkey (keyid_t, int, const u_char *, const int); extern u_int32 addr2refid (sockaddr_u *); +/* emalloc.c */ +#ifndef EREALLOC_CALLSITE /* ntp_malloc.h defines */ +extern void * ereallocz (void *, size_t, size_t, int); +#define erealloczsite(p, n, o, z, f, l) ereallocz(p, n, o, (z)) +extern void * emalloc (size_t); +#define emalloc_zero(c) ereallocz(NULL, (c), 0, TRUE) +#define erealloc(p, c) ereallocz(p, (c), 0, FALSE) +#define erealloc_zero(p, n, o) ereallocz(p, n, (o), TRUE) +extern char * estrdup_impl (const char *); +#define estrdup(s) estrdup_impl(s) +#else +extern void * ereallocz (void *, size_t, size_t, int, + const char *, int); +#define erealloczsite ereallocz +#define emalloc(c) ereallocz(NULL, (c), 0, FALSE, \ + __FILE__, __LINE__) +#define emalloc_zero(c) ereallocz(NULL, (c), 0, TRUE, \ + __FILE__, __LINE__) +#define erealloc(p, c) ereallocz(p, (c), 0, FALSE, \ + __FILE__, __LINE__) +#define erealloc_zero(p, n, o) ereallocz(p, n, (o), TRUE, \ + __FILE__, __LINE__) +extern char * estrdup_impl (const char *, const char *, int); +#define estrdup(s) estrdup_impl((s), __FILE__, __LINE__) +#endif + extern int atoint (const char *, long *); extern int atouint (const char *, u_long *); diff --git a/include/ntp_types.h b/include/ntp_types.h index ed6a2ca4b..9ace91fe9 100644 --- a/include/ntp_types.h +++ b/include/ntp_types.h @@ -10,6 +10,13 @@ #include #include "ntp_machine.h" +#ifndef TRUE +# define TRUE 1 +#endif +#ifndef FALSE +# define FALSE 0 +#endif + /* * This is another naming conflict. * On NetBSD for MAC the macro "mac" is defined as 1 diff --git a/libntp/emalloc.c b/libntp/emalloc.c index 5c40f4ee0..c49c5c1e1 100644 --- a/libntp/emalloc.c +++ b/libntp/emalloc.c @@ -1,71 +1,12 @@ /* * emalloc - return new memory obtained from the system. Belch if none. */ +#include #include "ntp_types.h" #include "ntp_malloc.h" #include "ntp_syslog.h" #include "ntp_stdlib.h" -extern char *progname; - -#if !defined(_MSC_VER) || !defined(_DEBUG) - - -void * -erealloc( - void * prev, - size_t size - ) -{ - void * mem; - - mem = realloc(prev, size ? size : 1); - - if (NULL == mem) { - msyslog(LOG_ERR, - "fatal out of memory (%u bytes)", (u_int)size); - fprintf(stderr, - "%s: fatal out of memory (%u bytes)", progname, - (u_int)size); - exit(1); - } - - return mem; -} - - -void * -emalloc( - size_t size - ) -{ - return erealloc(NULL, size); -} - - -char * -estrdup( - const char * str - ) -{ - char * copy; - - copy = strdup(str); - - if (NULL == copy) { - msyslog(LOG_ERR, - "fatal out of memory duplicating %u bytes", - (u_int)strlen(str) + 1); - fprintf(stderr, - "%s: fatal out of memory duplicating %u bytes", - progname, (u_int)strlen(str) + 1); - exit(1); - } - - return copy; -} - -#else /* below is _MSC_VER && _DEBUG */ /* * When using the debug MS CRT allocator, each allocation stores the @@ -74,49 +15,82 @@ estrdup( * allocations to show up as coming from emalloc.c, so we preserve the * original callsite's source file and line using macros which pass * __FILE__ and __LINE__ as parameters to these routines. + * Other debug malloc implementations can be used by defining + * EREALLOC_IMPL() as ports/winnt/include/config.h does. */ void * -debug_erealloc( - void * prev, - size_t size, - const char * file, /* __FILE__ */ - int line /* __LINE__ */ +ereallocz( + void * ptr, + size_t newsz, + size_t priorsz, + int zero_init +#ifdef EREALLOC_CALLSITE /* ntp_malloc.h */ + , + const char * file, + int line +#endif ) { - void * mem; + char * mem; + size_t allocsz; - mem = _realloc_dbg(prev, size ? size : 1, - _NORMAL_BLOCK, file, line); + if (0 == newsz) + allocsz = 1; + else + allocsz = newsz; + mem = EREALLOC_IMPL(ptr, allocsz, file, line); if (NULL == mem) { + msyslog_term = TRUE; +#ifndef EREALLOC_CALLSITE + msyslog(LOG_ERR, "fatal out of memory (%lu bytes)", + (u_long)newsz); +#else msyslog(LOG_ERR, - "fatal: out of memory in %s line %d size %u", - file, line, (u_int)size); - fprintf(stderr, - "%s: fatal: out of memory in %s line %d size %u", - progname, file, line, (u_int)size); + "fatal out of memory %s line %d (%lu bytes)", + file, line, (u_long)newsz); +#endif exit(1); } + if (zero_init && newsz > priorsz) + zero_mem(mem + priorsz, newsz - priorsz); + return mem; } + char * -debug_estrdup( - const char * str, - const char * file, /* __FILE__ */ - int line /* __LINE__ */ +estrdup_impl( + const char * str +#ifdef EREALLOC_CALLSITE + , + const char * file, + int line +#endif ) { char * copy; size_t bytes; bytes = strlen(str) + 1; - copy = debug_erealloc(NULL, bytes, file, line); + copy = ereallocz(NULL, bytes, 0, FALSE +#ifdef EREALLOC_CALLSITE + , file, line +#endif + ); memcpy(copy, str, bytes); return copy; } -#endif /* _MSC_VER && _DEBUG */ + +#ifndef EREALLOC_CALLSITE +void * +emalloc(size_t newsz) +{ + return ereallocz(NULL, newsz, 0, FALSE); +} +#endif + diff --git a/libntp/msyslog.c b/libntp/msyslog.c index 688cd6f0a..ebfdd4b9f 100644 --- a/libntp/msyslog.c +++ b/libntp/msyslog.c @@ -153,7 +153,7 @@ format_errmsg( } -size_t +int mvsnprintf( char * buf, size_t bufsiz, @@ -161,26 +161,99 @@ mvsnprintf( va_list ap ) { - char nfmt[256]; - int errval; +#ifndef VSNPRINTF_PERCENT_M + char nfmt[256]; +#else + const char * nfmt = fmt; +#endif + int errval; /* * Save the error value as soon as possible */ - errval = errno; - #ifdef SYS_WINNT errval = GetLastError(); if (NO_ERROR == errval) +#endif /* SYS_WINNT */ errval = errno; + +#ifndef VSNPRINTF_PERCENT_M + format_errmsg(nfmt, sizeof(nfmt), fmt, errval); +#else + errno = errval; +#endif + return vsnprintf(buf, bufsiz, nfmt, ap); +} + + +int +mvfprintf( + FILE * fp, + const char * fmt, + va_list ap + ) +{ +#ifndef VSNPRINTF_PERCENT_M + char nfmt[256]; +#else + const char * nfmt = fmt; +#endif + int errval; + + /* + * Save the error value as soon as possible + */ +#ifdef SYS_WINNT + errval = GetLastError(); + if (NO_ERROR == errval) #endif /* SYS_WINNT */ + errval = errno; +#ifndef VSNPRINTF_PERCENT_M format_errmsg(nfmt, sizeof(nfmt), fmt, errval); +#else + errno = errval; +#endif + return vfprintf(fp, nfmt, ap); +} - return vsnprintf(buf, bufsiz, nfmt, ap); + +int +mfprintf( + FILE * fp, + const char * fmt, + ... + ) +{ + va_list ap; + int rc; + + va_start(ap, fmt); + rc = mvfprintf(fp, fmt, ap); + va_end(ap); + + return rc; +} + + +int +mprintf( + const char * fmt, + ... + ) +{ + va_list ap; + int rc; + + va_start(ap, fmt); + rc = mvfprintf(stdout, fmt, ap); + va_end(ap); + + return rc; } -size_t + +int msnprintf( char * buf, size_t bufsiz, diff --git a/libntp/strdup.c b/libntp/strdup.c index 2e26ba7a5..f7565a2fb 100644 --- a/libntp/strdup.c +++ b/libntp/strdup.c @@ -1,8 +1,9 @@ -#include "ntp_malloc.h" +#include -#if !HAVE_STRDUP +#include +#include "ntp_malloc.h" -#define NULL 0 +#ifndef HAVE_STRDUP char *strdup(const char *s); @@ -11,18 +12,19 @@ strdup( const char *s ) { - char *cp; + size_t octets; + char * cp; + + if (s) { + octets = 1 + strlen(s); + cp = malloc(octets); + if (NULL != cp) + memcpy(cp, s, octets); + else + cp = NULL; - if (s) { - cp = (char *) malloc((unsigned) (strlen(s)+1)); - if (cp) { - (void) strcpy(cp, s); - } - } else { - cp = (char *) NULL; - } - return(cp); + return(cp); } #else -int strdup_bs; +int strdup_c_nonempty_compilation_unit; #endif diff --git a/ntpd/refclock_wwvb.c b/ntpd/refclock_wwvb.c index e53104bb5..10bb63f6e 100644 --- a/ntpd/refclock_wwvb.c +++ b/ntpd/refclock_wwvb.c @@ -54,7 +54,7 @@ * hh:mm:ss = hours, minutes, seconds * i = synchronization flag (' ' = in synch, '?' = out of synch) * - * The alarm condition is indicated by other than ' ' at a, which occurs + * The alarm condition is indicated by other than ' ' at i, which occurs * during initial synchronization and when received signal is lost for * about ten hours. * @@ -69,7 +69,7 @@ * ddd = day of year * hh:mm:ss.fff = hours, minutes, seconds, milliseconds * - * The alarm condition is indicated by other than ' ' at a, which occurs + * The alarm condition is indicated by other than ' ' at i, which occurs * during initial synchronization and when received signal is lost for * about ten hours. The unlock condition is indicated by other than ' ' * at q. @@ -120,9 +120,8 @@ #define DESCRIPTION "Spectracom WWVB/GPS Receiver" /* WRU */ #define LENWWVB0 22 /* format 0 timecode length */ -#define LENWWVB1 22 /* format 1 timecode length */ #define LENWWVB2 24 /* format 2 timecode length */ -#define LENWWVB3 29 /* format 3 timecode length */ +#define LENWWVB3 29 /* format 3 timecode length */ #define MONLIN 15 /* number of monitoring lines */ /* @@ -136,7 +135,8 @@ struct wwvbunit { int tcount; /* timecode sample counter */ int pcount; /* PPS sample counter */ #endif /* HAVE_PPSAPI */ - l_fp laststamp; /* last receive timestamp */ + l_fp laststamp; /* last timestamp */ + int prev_eol_cr; /* was last EOL (not )? */ u_char lasthour; /* last hour (for monitor) */ u_char linect; /* count ignored lines (for monitor */ }; @@ -150,7 +150,7 @@ static void wwvb_receive (struct recvbuf *); static void wwvb_poll (int, struct peer *); static void wwvb_timer (int, struct peer *); #ifdef HAVE_PPSAPI -static void wwvb_control (int, struct refclockstat *, +static void wwvb_control (int, const struct refclockstat *, struct refclockstat *, struct peer *); #define WWVB_CONTROL wwvb_control #else @@ -189,27 +189,26 @@ wwvb_start( * Open serial port. Use CLK line discipline, if available. */ snprintf(device, sizeof(device), DEVICE, unit); - if (0 == (fd = refclock_open(device, SPEED232, LDISC_CLK))) + fd = refclock_open(device, SPEED232, LDISC_CLK); + if (fd <= 0) return (0); /* * Allocate and initialize unit structure */ - up = emalloc(sizeof(*up)); - memset(up, 0, sizeof(*up)); + up = emalloc_zero(sizeof(*up)); pp = peer->procptr; - pp->unitptr = (caddr_t)up; pp->io.clock_recv = wwvb_receive; - pp->io.srcclock = (caddr_t)peer; + pp->io.srcclock = (void *)peer; pp->io.datalen = 0; pp->io.fd = fd; if (!io_addclock(&pp->io)) { close(fd); pp->io.fd = -1; free(up); - pp->unitptr = NULL; return (0); } + pp->unitptr = up; /* * Initialize miscellaneous variables @@ -234,7 +233,7 @@ wwvb_shutdown( struct refclockproc *pp; pp = peer->procptr; - up = (struct wwvbunit *)pp->unitptr; + up = pp->unitptr; if (-1 != pp->io.fd) io_closeclock(&pp->io); if (NULL != up) @@ -267,9 +266,9 @@ wwvb_receive( /* * Initialize pointers and read the timecode and timestamp */ - peer = (struct peer *)rbufp->recv_srcclock; + peer = rbufp->recv_peer; pp = peer->procptr; - up = (struct wwvbunit *)pp->unitptr; + up = pp->unitptr; temp = refclock_gtlin(rbufp, pp->a_lastcode, BMAX, &trtmp); /* @@ -281,13 +280,37 @@ wwvb_receive( * reading precision is only to the millisecond. Thus, unless * you have a PPS gadget and don't have to have the year, format * 0 provides the lowest jitter. + * Save the timestamp of each in up->laststamp. Lines with + * no characters occur for every , and for some s when + * format 0 is used. Format 0 starts and ends each cycle with a + * pair, format 2 starts each cycle with its only pair. + * The preceding is the on-time character for both formats. + * The timestamp provided with non-empty lines corresponds to + * the following the timecode, which is ultimately not used + * with format 0 and is used for the following timecode for + * format 2. */ if (temp == 0) { - up->laststamp = trtmp; + if (up->prev_eol_cr) { + DPRINTF(2, ("wwvb: @ %s\n", + prettydate(&trtmp))); + } else { + up->laststamp = trtmp; + DPRINTF(2, ("wwvb: @ %s\n", + prettydate(&trtmp))); + } + up->prev_eol_cr = !up->prev_eol_cr; return; } pp->lencode = temp; pp->lastrec = up->laststamp; + up->laststamp = trtmp; + up->prev_eol_cr = TRUE; + DPRINTF(2, ("wwvb: code @ %s\n" + " using %s minus one char\n", + prettydate(&trtmp), prettydate(&pp->lastrec))); + if (L_ISZERO(&pp->lastrec)) + return; /* * We get down to business, check the timecode format and decode @@ -307,9 +330,11 @@ wwvb_receive( if (sscanf(pp->a_lastcode, "%c %3d %2d:%2d:%2d%c%cTZ=%2d", &syncchar, &pp->day, &pp->hour, &pp->minute, - &pp->second, &tmpchar, &dstchar, &tz) == 8) + &pp->second, &tmpchar, &dstchar, &tz) == 8) { pp->nsec = 0; break; + } + goto bad_format; case LENWWVB2: @@ -319,14 +344,19 @@ wwvb_receive( "%c%c %2d %3d %2d:%2d:%2d.%3ld %c", &syncchar, &qualchar, &pp->year, &pp->day, &pp->hour, &pp->minute, &pp->second, &pp->nsec, - &leapchar) == 9) + &leapchar) == 9) { pp->nsec *= 1000000; break; + } + goto bad_format; case LENWWVB3: - /* + /* * Timecode format 3: "0003I yyyymmdd hhmmss+0000SL#" + * WARNING: Undocumented, and the on-time character # is + * not yet handled correctly by this driver. It may be + * as simple as compensating for an additional 1/960 s. */ if (sscanf(pp->a_lastcode, "0003%c %4d%2d%2d %2d%2d%2d+0000%c%c", @@ -337,8 +367,10 @@ wwvb_receive( pp->nsec = 0; break; } + goto bad_format; default: + bad_format: /* * Unknown format: If dumping internal table, record @@ -423,6 +455,7 @@ wwvb_timer( register struct wwvbunit *up; struct refclockproc *pp; char pollchar; /* character sent to clock */ + l_fp now; /* * Time to poll the clock. The Spectracom clock responds to a @@ -432,13 +465,18 @@ wwvb_timer( * the clock; all others just listen in. */ pp = peer->procptr; - up = (struct wwvbunit *)pp->unitptr; + up = pp->unitptr; if (up->linect > 0) pollchar = 'R'; else pollchar = 'T'; if (write(pp->io.fd, &pollchar, 1) != 1) refclock_report(peer, CEVNT_FAULT); +#ifdef DEBUG + get_systime(&now); + if (debug) + printf("%c poll at %s\n", pollchar, prettydate(&now)); +#endif #ifdef HAVE_PPSAPI if (up->ppsapi_lit && refclock_pps(peer, &up->atom, pp->sloppyclockflag) > 0) { @@ -467,7 +505,7 @@ wwvb_poll( * are received, declare a timeout and keep going. */ pp = peer->procptr; - up = (struct wwvbunit *)pp->unitptr; + up = pp->unitptr; pp->polls++; /* @@ -517,7 +555,7 @@ wwvb_poll( static void wwvb_control( int unit, - struct refclockstat *in_st, + const struct refclockstat *in_st, struct refclockstat *out_st, struct peer *peer ) @@ -526,7 +564,7 @@ wwvb_control( struct refclockproc *pp; pp = peer->procptr; - up = (struct wwvbunit *)pp->unitptr; + up = pp->unitptr; if (!(pp->sloppyclockflag & CLK_FLAG1)) { if (!up->ppsapi_tried) diff --git a/ntpdate/ntpdate.c b/ntpdate/ntpdate.c index ec3bd7e86..7686b41a1 100644 --- a/ntpdate/ntpdate.c +++ b/ntpdate/ntpdate.c @@ -20,6 +20,7 @@ #include "ntp_syslog.h" #include "ntp_select.h" #include "ntp_stdlib.h" +#include "ntp_assert.h" #include #include "isc/net.h" @@ -119,9 +120,9 @@ volatile int debug = 0; */ int ai_fam_templ; -int nbsock; /* the number of sockets used */ +int nbsock; /* the number of sockets used */ SOCKET fd[MAX_AF]; -int fd_family[MAX_AF]; /* to remember the socket family */ +int fd_family[MAX_AF]; /* to remember the socket family */ #ifdef HAVE_POLL_H struct pollfd fdmask[MAX_AF]; #else @@ -419,18 +420,7 @@ ntpdatemain ( } else { sys_timeout = ((LFPTOFP(&tmp) * TIMER_HZ) + 0x8000) >> 16; - /* - * No less than 1s between requests to - * a server to stay within ntpd's - * default "discard minimum 1" (and 1s - * enforcement slop). That is enforced - * only if the nondefault limited - * restriction is in place, such as with - * "restrict ... limited" and "restrict - * ... kod limited". - */ - if (MINTIMEOUT < sys_timeout) - sys_timeout = MINTIMEOUT; + sys_timeout = max(sys_timeout, MINTIMEOUT); } break; case 'v': @@ -921,6 +911,17 @@ receive( * Shift this data in, then schedule another transmit. */ server_data(server, (s_fp) di, &ci, 0); + + if ((int)server->filter_nextpt >= sys_samples) { + /* + * Got all the data we need. Mark this guy + * completed and return. + */ + server->event_time = 0; + complete_servers++; + return; + } + server->event_time = current_time + sys_timeout; } @@ -1355,10 +1356,12 @@ addserver( int error; /* Service name */ char service[5]; - strcpy(service, "ntp"); + sockaddr_u addr; + + strncpy(service, "ntp", sizeof(service)); /* Get host address. Looking for UDP datagram connection. */ - memset(&hints, 0, sizeof(hints)); + ZERO(hints); hints.ai_family = ai_fam_templ; hints.ai_socktype = SOCK_DGRAM; @@ -1387,17 +1390,21 @@ addserver( return; } #ifdef DEBUG - else if (debug) { - fprintf(stderr, "host found : %s\n", stohost((sockaddr_u *)addrResult->ai_addr)); + if (debug) { + ZERO(addr); + INSIST(addrResult->ai_addrlen <= sizeof(addr)); + memcpy(&addr, addrResult->ai_addr, addrResult->ai_addrlen); + fprintf(stderr, "host found : %s\n", stohost(&addr)); } #endif /* We must get all returned server in case the first one fails */ for (ptr = addrResult; ptr != NULL; ptr = ptr->ai_next) { - if (is_reachable ((sockaddr_u *)ptr->ai_addr)) { - server = emalloc(sizeof(*server)); - memset(server, 0, sizeof(*server)); - + ZERO(addr); + INSIST(ptr->ai_addrlen <= sizeof(addr)); + memcpy(&addr, ptr->ai_addr, ptr->ai_addrlen); + if (is_reachable(&addr)) { + server = emalloc_zero(sizeof(*server)); memcpy(&server->srcadr, ptr->ai_addr, ptr->ai_addrlen); server->event_time = ++sys_numservers; if (sys_servers == NULL) @@ -1406,7 +1413,8 @@ addserver( struct server *sp; for (sp = sys_servers; sp->next_server != NULL; - sp = sp->next_server) ; + sp = sp->next_server) + /* empty */; sp->next_server = server; } } @@ -1452,15 +1460,15 @@ findserver( complete_servers++; } - server = emalloc(sizeof(*server)); - memset(server, 0, sizeof(*server)); + server = emalloc_zero(sizeof(*server)); server->srcadr = *addr; server->event_time = ++sys_numservers; for (sp = sys_servers; sp->next_server != NULL; - sp = sp->next_server) ; + sp = sp->next_server) + /* empty */; sp->next_server = server; transmit(server); } @@ -1672,7 +1680,9 @@ init_io(void) { struct addrinfo *res, *ressave; struct addrinfo hints; + sockaddr_u addr; char service[5]; + int rc; int optval = 1; int check_ntp_port_in_use = !debug && !simple_query && !unpriv_port; @@ -1685,20 +1695,20 @@ init_io(void) * Open the socket */ - strcpy(service, "ntp"); + strncpy(service, "ntp", sizeof(service)); /* * Init hints addrinfo structure */ - memset(&hints, 0, sizeof(hints)); + ZERO(hints); hints.ai_family = ai_fam_templ; hints.ai_flags = AI_PASSIVE; hints.ai_socktype = SOCK_DGRAM; - if(getaddrinfo(NULL, service, &hints, &res) != 0) { - msyslog(LOG_ERR, "getaddrinfo() failed: %m"); - exit(1); - /*NOTREACHED*/ + if (getaddrinfo(NULL, service, &hints, &res) != 0) { + msyslog(LOG_ERR, "getaddrinfo() failed: %m"); + exit(1); + /*NOTREACHED*/ } #ifdef SYS_WINNT @@ -1754,13 +1764,12 @@ init_io(void) * bind the socket to the NTP port */ if (check_ntp_port_in_use) { - if (bind(fd[nbsock], res->ai_addr, - SOCKLEN((sockaddr_u *)res->ai_addr)) < 0) { -#ifndef SYS_WINNT - if (errno == EADDRINUSE) -#else - if (WSAGetLastError() == WSAEADDRINUSE) -#endif /* SYS_WINNT */ + ZERO(addr); + INSIST(res->ai_addrlen <= sizeof(addr)); + memcpy(&addr, res->ai_addr, res->ai_addrlen); + rc = bind(fd[nbsock], &addr.sa, SOCKLEN(&addr)); + if (rc < 0) { + if (EADDRINUSE == socket_errno()) msyslog(LOG_ERR, "the NTP socket is in use, exiting"); else msyslog(LOG_ERR, "bind() fails: %m"); @@ -1877,7 +1886,7 @@ input_handler(void) register int n; register struct recvbuf *rb; struct sock_timeval tvzero; - int fromlen; + GETSOCKNAME_SOCKLEN_TYPE fromlen; l_fp ts; int i; #ifdef HAVE_POLL_H @@ -2116,7 +2125,7 @@ printserver( { register int i; char junk[5]; - char *str; + const char *str; if (!debug) { (void) fprintf(fp, "server %s, stratum %d, offset %s, delay %s\n", @@ -2179,57 +2188,6 @@ printserver( lfptoa(&pp->offset, 6)); } -#if !defined(HAVE_VSPRINTF) -int -vsprintf( - char *str, - const char *fmt, - va_list ap - ) -{ - FILE f; - int len; - - f._flag = _IOWRT+_IOSTRG; - f._ptr = str; - f._cnt = 32767; - len = _doprnt(fmt, ap, &f); - *f._ptr = 0; - return (len); -} -#endif - -#if 0 -/* override function in library since SA_RESTART makes ALL syscalls restart */ -#ifdef SA_RESTART -void -signal_no_reset( - int sig, - void (*func)() - ) -{ - int n; - struct sigaction vec; - - vec.sa_handler = func; - sigemptyset(&vec.sa_mask); - vec.sa_flags = 0; - - while (1) - { - n = sigaction(sig, &vec, NULL); - if (n == -1 && errno == EINTR) - continue; - break; - } - if (n == -1) - { - perror("sigaction"); - exit(1); - } -} -#endif -#endif #ifdef HAVE_NETINFO static ni_namelist * diff --git a/ports/winnt/include/config.h b/ports/winnt/include/config.h index c09fe5764..491ca7ca4 100644 --- a/ports/winnt/include/config.h +++ b/ports/winnt/include/config.h @@ -35,6 +35,8 @@ #include #include /* #define MALLOC_LINT */ /* defers free() */ +# define EREALLOC_IMPL(ptr, newsz, filenm, loc) \ + _realloc_dbg(ptr, newsz, _NORMAL_BLOCK, filenm, loc) #endif /*