]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
[Bug 1988] Better sntp send failed error message needed.
authorDave Hart <hart@ntp.org>
Sun, 14 Aug 2011 23:08:50 +0000 (23:08 +0000)
committerDave Hart <hart@ntp.org>
Sun, 14 Aug 2011 23:08:50 +0000 (23:08 +0000)
[Bug 1990] sntp output should include stratum.

bk: 4e485582OhAYfWIeUUzpFnhX6xCVVw

ChangeLog
include/ntp_syslog.h
libntp/msyslog.c
libntp/socktoa.c
sntp/log.c
sntp/main.c
sntp/networking.c
sntp/networking.h
sntp/utilities.c
sntp/utilities.h

index ea02bec0367b5ea9812b8c7f630fb658f9885b5d..5249ff04fed661e1b58e2344560ac91c22f9e10f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,5 @@
+* [Bug 1988] Better sntp send failed error message needed.
+* [Bug 1990] sntp output should include stratum.
 (4.2.7p203) 2011/08/13 Released by Harlan Stenn <stenn@ntp.org>
 * [Bug 1986] Require Visual C++ 2005 or later compilers in Windows port.
 * Actually use long long for (u_)int64 by correcting spelling of
index 38f1df0c9c1a4e0c1ce46d66a8bd67e4a05c2942..58c30446044b34210cafa0a96e296917f2472090 100644 (file)
@@ -18,6 +18,8 @@ extern void msyslog();
 
 extern int     syslogit;
 extern int     msyslog_term;   /* duplicate to stdout/err */
+extern int     msyslog_term_pid;
+extern int     msyslog_include_timestamp;
 extern FILE *  syslog_file;    /* if syslogit is FALSE, log to 
                                   this file and not syslog */
 extern char *  syslog_fname;
index 2953569b577a725258c5bb46f96d0d16ff9d8de0..f1b67c958c19c4f5065c0a5903f316f955b9e1ff 100644 (file)
 #endif
 
 
-int    syslogit = 1;
+int    syslogit = TRUE;
 int    msyslog_term = FALSE;   /* duplicate to stdout/err */
+int    msyslog_term_pid = TRUE;
+int    msyslog_include_timestamp = TRUE;
 FILE * syslog_file;
 char * syslog_fname;
 char * syslog_abs_fname;
@@ -162,6 +164,7 @@ addto_syslog(
        FILE *          term_file;
        int             log_to_term;
        int             log_to_file;
+       int             pid;
        const char *    nl_or_empty;
        const char *    human_time;
 
@@ -194,7 +197,14 @@ addto_syslog(
                return;
 
        /* syslog() adds the timestamp, name, and pid */
-       human_time = humanlogtime();
+       if (msyslog_include_timestamp)
+               human_time = humanlogtime();
+       else    /* suppress gcc pot. uninit. warning */
+               human_time = NULL;
+       if (msyslog_term_pid || log_to_file)
+               pid = getpid();
+       else    /* suppress gcc pot. uninit. warning */
+               pid = -1;
 
        /* syslog() adds trailing \n if not present */
        if ('\n' != msg[strlen(msg) - 1])
@@ -206,14 +216,19 @@ addto_syslog(
                term_file = (level <= LOG_ERR)
                                ? stderr
                                : stdout;
-               fprintf(term_file, "%s %s[%d]: %s%s", human_time, prog,
-                       (int)getpid(), msg, nl_or_empty);
+               if (msyslog_include_timestamp)
+                       fprintf(term_file, "%s ", human_time);
+               if (msyslog_term_pid)
+                       fprintf(term_file, "%s[%d]: ", prog, pid);
+               fprintf(term_file, "%s%s", msg, nl_or_empty);
                fflush(term_file);
        }
 
        if (log_to_file) {
-               fprintf(syslog_file, "%s %s[%d]: %s%s", human_time,
-                       prog, (int)getpid(), msg, nl_or_empty);
+               if (msyslog_include_timestamp)
+                       fprintf(syslog_file, "%s ", human_time);
+               fprintf(syslog_file, "%s[%d]: %s%s", prog, pid, msg,
+                       nl_or_empty);
                fflush(syslog_file);
        }
 }
index 2d002c842fce9d272f7024b3d30276f59d745554..c095cc63b46022a1d265404b919c48c2ce33f0b1 100644 (file)
@@ -35,15 +35,17 @@ socktoa(
        const sockaddr_u *sock
        )
 {
+       int             saved_errno;
        char *          res;
        char *          addr;
        u_long          scope;
 
+       saved_errno = socket_errno();
        LIB_GETBUF(res);
 
-       if (NULL == sock)
+       if (NULL == sock) {
                strlcpy(res, "(null)", LIB_BUFLENGTH);
-       else {
+       else {
                switch(AF(sock)) {
 
                case AF_INET:
@@ -71,6 +73,8 @@ socktoa(
                                 AF(sock));
                }
        }
+       errno = saved_errno;
+
        return res;
 }
 
@@ -80,9 +84,11 @@ sockporttoa(
        const sockaddr_u *sock
        )
 {
+       int             saved_errno;
        const char *    atext;
        char *          buf;
 
+       saved_errno = socket_errno();
        atext = socktoa(sock);
        LIB_GETBUF(buf);
        snprintf(buf, LIB_BUFLENGTH,
@@ -90,6 +96,7 @@ sockporttoa(
                     ? "[%s]:%hu"
                     : "%s:%hu",
                 atext, SRCPORT(sock));
+       errno = saved_errno;
 
        return buf;
 }
index 50741eb75d0802685a887d5ba447830730e0b71e..d7034951528e4d277801770019b290ee53328d8b 100644 (file)
@@ -13,6 +13,8 @@ sntp_init_logging(
 {
        msyslog_term = TRUE;
        init_logging(prog, 0, FALSE);
+       msyslog_term_pid = FALSE;
+       msyslog_include_timestamp = FALSE;
 }
 
 
@@ -22,7 +24,6 @@ open_logfile(
        )
 {
        change_logfile(logfile, FALSE);
-       msyslog(LOG_NOTICE, "%s\n", Version);
        atexit(cleanup_log);
 }
 
index 2e408f506a50a9a2366990b1c4087516a810dfd6..98cda4aed8bb7818b5cdc2c29b564fb374bcc592 100644 (file)
@@ -146,7 +146,6 @@ sntp_main (
        argv += optct;
 
        debug = DESC(DEBUG_LEVEL).optOccCt;
-       TRACE(1, ("%s\n", Version));
 
        TRACE(2, ("init_lib() done, %s%s\n",
                  (ipv4_works)
@@ -163,6 +162,8 @@ sntp_main (
        if (HAVE_OPT(LOGFILE))
                open_logfile(OPT_ARG(LOGFILE));
 
+       msyslog(LOG_INFO, "%s\n", Version);
+
        if (0 == argc && !HAVE_OPT(BROADCAST) && !HAVE_OPT(CONCURRENT)) {
                printf("%s: Must supply at least one of -b hostname, -c hostname, or hostname.\n",
                       progname);
@@ -659,6 +660,7 @@ xmt(
        struct timeval  tv_xmt;
        struct pkt      x_pkt;
        int             pkt_len;
+       int             sent;
 
        if (0 != gettimeofday(&tv_xmt, NULL)) {
                msyslog(LOG_ERR,
@@ -670,20 +672,18 @@ xmt(
        pkt_len = generate_pkt(&x_pkt, &tv_xmt, dctx->key_id,
                               dctx->key);
 
-       /* The current sendpkt does not return status */
-       sendpkt(sock, dst, &x_pkt, pkt_len);
-       /* Save the packet we sent... */
-       memcpy(&spkt->x_pkt, &x_pkt, min(sizeof(spkt->x_pkt), pkt_len));
-       spkt->stime = tv_xmt.tv_sec - JAN_1970;
-
-       TRACE(2, ("xmt: %lx.%6.6u %s %s\n", (u_long)tv_xmt.tv_sec,
-                 (u_int)tv_xmt.tv_usec, dctx->name, stoa(dst)));
+       sent = sendpkt(sock, dst, &x_pkt, pkt_len);
+       if (sent) {
+               /* Save the packet we sent... */
+               memcpy(&spkt->x_pkt, &x_pkt, min(sizeof(spkt->x_pkt),
+                      pkt_len));
+               spkt->stime = tv_xmt.tv_sec - JAN_1970;
 
-       /*
-       ** If the send fails:
-       ** - decrement n_pending_ntp
-       ** - restart the loop
-       */
+               TRACE(2, ("xmt: %lx.%6.6u %s %s\n", (u_long)tv_xmt.tv_sec,
+                         (u_int)tv_xmt.tv_usec, dctx->name, stoa(dst)));
+       } else {
+               dec_pending_ntp(dctx->name, dst);
+       }
 
        return;
 }
@@ -729,8 +729,8 @@ void dec_pending_ntp(
                check_exit_conditions();
        } else {
                INSIST(0 == n_pending_ntp);
-               TRACE(1, ("n_pending_ntp reached zero before dec for %s %s\n",
-                         name, stoa(server)));
+               TRACE(1, ("n_pending_ntp reached zero before dec for %s\n",
+                         hostnameaddr(name, server)));
        }
 }
 
@@ -743,8 +743,8 @@ void timeout_query(
 
        spkt->done = TRUE;
        server = &spkt->addr;
-       msyslog(LOG_NOTICE, "%s %s no response after %d seconds",
-               spkt->dctx->name, stoa(server), ucst_timeout);
+       msyslog(LOG_INFO, "%s no response after %d seconds",
+               hostnameaddr(spkt->dctx->name, server), ucst_timeout);
        dec_pending_ntp(spkt->dctx->name, server);
 }
 
@@ -1104,10 +1104,13 @@ handle_pkt(
        const char *    hostname
        )
 {
+       char            disptxt[32];
        const char *    addrtxt;
        struct timeval  tv_dst;
+       int             cnt;
        int             sw_case;
        int             digits;
+       int             stratum;
        char *          ref;
        char *          ts_str;
        double          offset;
@@ -1190,13 +1193,27 @@ handle_pkt(
                        digits = 6;
 
                ts_str = tv_to_str(&tv_dst);
-               printf("%s %+.*f", ts_str, digits, offset);
-               if (root_dispersion > 0.)
-                       printf(" +/- %f ", root_dispersion);
-               printf(" %s %s%s\n", hostname, stoa(host),
-                      (time_adjusted) 
-                          ? " [excess]"
-                          : "");
+               stratum = rpkt->stratum;
+               if (0 == stratum)
+                               stratum = 16;
+
+               if (root_dispersion > 0) {
+                       cnt = snprintf(disptxt, sizeof(disptxt),
+                                      " +/- %f", root_dispersion);
+                       if (cnt >= sizeof(disptxt))
+                               snprintf(disptxt, sizeof(disptxt),
+                                        "ERROR %d >= %d", cnt,
+                                        (int)sizeof(disptxt));
+               } else {
+                       disptxt[0] = '\0';
+               }
+
+               msyslog(LOG_INFO, "%s %+.*f%s %s s%d%s\n", ts_str,
+                       digits, offset, disptxt,
+                       hostnameaddr(hostname, host), stratum,
+                       (time_adjusted)
+                           ? " [excess]"
+                           : "");
                free(ts_str);
 
                if (p_SNTP_PRETEND_TIME)
index 6cf02bae062191e47eecaf5ffefb25de502c9d55..ec37a4b674be22469f835527e806b9754b0e2526 100644 (file)
@@ -4,7 +4,7 @@
 
 
 /* Send a packet */
-void
+int
 sendpkt (
        SOCKET rsock,
        sockaddr_u *dest,
@@ -19,23 +19,20 @@ sendpkt (
                printf("sntp sendpkt: Packet data:\n");
                pkt_output(pkt, len, stdout);
        }
-
-       if (debug) {
-               printf("sntp sendpkt: Sending packet to %s ...\n", sptoa(dest));
-       }
 #endif
+       TRACE(1, ("sntp sendpkt: Sending packet to %s ...\n",
+                 sptoa(dest)));
 
-       cc = sendto(rsock, (void *)pkt, len, 0, &dest->sa, SOCKLEN(dest));
+       cc = sendto(rsock, (void *)pkt, len, 0, &dest->sa, 
+                   SOCKLEN(dest));
        if (cc == SOCKET_ERROR) {
-#ifdef DEBUG
-               printf("sntp sendpkt: Socket error: %i. Couldn't send packet!\n", cc);
-#endif
-               if (errno != EWOULDBLOCK && errno != ENOBUFS) {
-                       /* oh well */
-               }
-       } else {
-               TRACE(3, ("Packet sent.\n"));
+               msyslog(LOG_ERR, "Send to %s failed, %m\n",
+                       sptoa(dest));
+               return FALSE;
        }
+       TRACE(1, ("Packet sent.\n"));
+
+       return TRUE;
 }
 
 
index 35c03108b548082bb10b044f01d01f2bdd165c3e..3e63891c08c4ad13ac11bba14cf5deda35eec579 100644 (file)
@@ -32,9 +32,8 @@
 #define KOD_RATE -5        /* KOD packet with code RATE, reduce poll intervall */
 #define BROADCAST_FAILED -6
 
-
-/* From ntpdate.c */
-void sendpkt(SOCKET rsock, sockaddr_u *dest, struct pkt *pkt, int len);
+/* prototypes */
+int sendpkt(SOCKET rsock, sockaddr_u *dest, struct pkt *pkt, int len);
 int recvdata(SOCKET rsock, sockaddr_u *sender, void *rdata,
             int rdata_len);
 int recvpkt(SOCKET rsock, struct pkt *rpkt, unsigned int rsize,
index 84e5b84d1d647f5a83ac0cc99549786735a9b145..591c4f7ade9ca940407250db9643ea843d457e45 100644 (file)
@@ -174,3 +174,36 @@ tv_to_str(
        return buf;
 }
 
+
+/*
+ *
+ * hostnameaddr()
+ *
+ * Formats the hostname and resulting numeric IP address into a string,
+ * avoiding duplication if the "hostname" was in fact a numeric address.
+ *
+ */
+const char *
+hostnameaddr(
+       const char *            hostname,
+       const sockaddr_u *      addr
+       )
+{
+       const char *    addrtxt;
+       char *          result;
+       int             cnt;
+
+       addrtxt = stoa(addr);
+       LIB_GETBUF(result);
+       if (strcmp(hostname, addrtxt))
+               cnt = snprintf(result, LIB_BUFLENGTH, "%s %s",
+                              hostname, addrtxt);
+       else
+               cnt = snprintf(result, LIB_BUFLENGTH, "%s", addrtxt);
+       if (cnt >= LIB_BUFLENGTH)
+               snprintf(result, LIB_BUFLENGTH,
+                        "hostnameaddr ERROR have %d (%d needed)",
+                        LIB_BUFLENGTH, cnt + 1);
+
+       return result;
+}
index c35cf39cd848cc05f4d1955b73ab0d7a5990ad36..a022c7d49769c582752a1510f205102088272247 100644 (file)
@@ -4,22 +4,24 @@
 #include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <ntp_stdlib.h>
-#include <ntp_fp.h>
-#include <ntp.h>
+
+#include "ntp.h"
+#include "ntp_stdlib.h"
+#include "lib_strbuf.h"
 
 #define HLINE "--------------------------------------------------------------------------------\n"
 #define PHLINE fprintf(output, HLINE);
 #define STDLINE printf(HLINE);
 
 
-void pkt_output (struct pkt *dpkg, int pkt_length, FILE *output);
-void l_fp_output (l_fp *ts, FILE *output);
-void l_fp_output_bin (l_fp *ts, FILE *output);
-void l_fp_output_dec (l_fp *ts, FILE *output);
+void pkt_output(struct pkt *dpkg, int pkt_length, FILE *output);
+void l_fp_output(l_fp *ts, FILE *output);
+void l_fp_output_bin(l_fp *ts, FILE *output);
+void l_fp_output_dec(l_fp *ts, FILE *output);
 
-char *addrinfo_to_str (const struct addrinfo *addr);
-char *ss_to_str (sockaddr_u *saddr);
-char *tv_to_str (const struct timeval *tv);
+char *addrinfo_to_str(const struct addrinfo *addr);
+char *ss_to_str(sockaddr_u *saddr);
+char *tv_to_str(const struct timeval *tv);
+const char * hostnameaddr(const char *, const sockaddr_u *);
 
-#endif
+#endif /* UTILITIES_H */