]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
[Bug 3432] refclocks that 'write()' should check the result
authorJuergen Perlinger <perlinger@ntp.org>
Thu, 15 Mar 2018 05:29:23 +0000 (06:29 +0100)
committerJuergen Perlinger <perlinger@ntp.org>
Thu, 15 Mar 2018 05:29:23 +0000 (06:29 +0100)
 - also fixed some more compiler warnings

bk: 5aaa04b3huk-JRJiLAyoYApz6GHZhA

21 files changed:
ChangeLog
include/ntp_refclock.h
include/ntp_stdlib.h
libntp/refnumtoa.c
ntpd/ntp_refclock.c
ntpd/refclock_acts.c
ntpd/refclock_arbiter.c
ntpd/refclock_as2201.c
ntpd/refclock_dumbclock.c
ntpd/refclock_gpsdjson.c
ntpd/refclock_heath.c
ntpd/refclock_hopfser.c
ntpd/refclock_hpgps.c
ntpd/refclock_nmea.c
ntpd/refclock_oncore.c
ntpd/refclock_palisade.c
ntpd/refclock_true.c
parseutil/dcfd.c
ports/winnt/vs2015/ntpd/ntpd.vcxproj
tests/libntp/caljulian.c
tests/libntp/run-caljulian.c

index dcfa59c07f3fb687e22c57016ce0cd382df674e5..43e769d1f53eca8ad192d8fd80aeefe7eacca9b9 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+---
+* [Bug 3432] refclocks that 'write()' should check the result <perlinger@ntp.org>
+  - plus some more work on warnings for unchecked results
+
 ---
 (4.2.8p11) 2018/02/27 Released by Harlan Stenn <stenn@ntp.org>
 
index 4b807e5f369eef24e28354867979f8e65f515e7e..a385fc00cc6af43eba23744f73807ccb43e5b1c6 100644 (file)
@@ -215,6 +215,8 @@ struct refclock {
 extern int     io_addclock     (struct refclockio *);
 extern void    io_closeclock   (struct refclockio *);
 
+#define FDWRITE_ERROR  ((size_t)-1)
+
 #ifdef REFCLOCK
 extern void    refclock_buginfo(sockaddr_u *,
                                 struct refclockbug *);
@@ -232,6 +234,10 @@ extern     void    refclock_process_offset(struct refclockproc *, l_fp,
 extern void    refclock_report (struct peer *, int);
 extern int     refclock_gtlin  (struct recvbuf *, char *, int, l_fp *);
 extern int     refclock_gtraw  (struct recvbuf *, char *, int, l_fp *);
+extern size_t  refclock_write  (const struct peer *, const void *, size_t,
+                                const char * what);
+extern size_t  refclock_fdwrite(const struct peer *, int, const void *, size_t,
+                                const char * what);
 extern int     indicate_refclock_packet(struct refclockio *,
                                         struct recvbuf *);
 extern void    process_refclock_packet(struct recvbuf *);
index 889c3b25ef4214f61fa35347d75fee9580f2edb6..e4660be174fcd242431d5b3a824097a0f92c249e 100644 (file)
@@ -171,7 +171,7 @@ extern      int     sockaddr_masktoprefixlen(const sockaddr_u *);
 extern const char * socktohost (const sockaddr_u *);
 extern int     octtoint        (const char *, u_long *);
 extern u_long  ranp2           (int);
-extern const char *refnumtoa   (sockaddr_u *);
+extern const char *refnumtoa   (const sockaddr_u *);
 extern const char *refid_str   (u_int32, int);
 
 extern int     decodenetnum    (const char *, sockaddr_u *);
index e6ca55aef868155723977a7de5aede61f289bb4f..cfebefe1ed18161c05c14592f1288d7a5da52700 100644 (file)
@@ -10,7 +10,7 @@
 
 const char *
 refnumtoa(
-       sockaddr_u *num
+       const sockaddr_u *num
        )
 {
        u_int32 netnum;
index a0dbd4ca83b6ce59ab39c184ee495b9405a66327..b1af7cda16425931b080ae272ce32d7d411e8a80 100644 (file)
@@ -651,6 +651,73 @@ refclock_gtraw(
        return (bmax);
 }
 
+/*
+ * refclock_fdwrite()
+ *
+ * Write data to a clock device. Does the necessary result checks and
+ * logging, and encapsulates OS dependencies.
+ */
+#ifdef SYS_WINNT
+extern int async_write(int fd, const void * buf, unsigned int len);
+#endif
+
+size_t
+refclock_fdwrite(
+       const struct peer *     peer,
+       int                     fd,
+       const void *            buf,
+       size_t                  len,
+       const char *            what
+       )
+{
+       size_t  nret, nout;
+       int     nerr;
+       
+       nout = (INT_MAX > len) ? len : INT_MAX;
+#   ifdef SYS_WINNT
+       nret = (size_t)async_write(fd, buf, (unsigned int)nout);
+#   else
+       nret = (size_t)write(fd, buf, nout);
+#   endif
+       if (NULL != what) {
+               if (nret == FDWRITE_ERROR) {
+                       nerr = errno;
+                       msyslog(LOG_INFO,
+                               "%s: write %s failed, fd=%d, %m",
+                               refnumtoa(&peer->srcadr), what,
+                               fd);
+                       errno = nerr;
+               } else if (nret != len) {
+                       nerr = errno;
+                       msyslog(LOG_NOTICE,
+                               "%s: %s shortened, fd=%d, wrote %zu of %zu bytes",
+                               refnumtoa(&peer->srcadr), what,
+                               fd, nret, len);
+                       errno = nerr;
+               }
+       }
+       return nret;
+}
+
+size_t
+refclock_write(
+       const struct peer *     peer,
+       const void *            buf,
+       size_t                  len,
+       const char *            what
+       )
+{
+       if ( ! (peer && peer->procptr)) {
+               if (NULL != what)
+                       msyslog(LOG_INFO,
+                               "%s: write %s failed, invalid clock peer",
+                               refnumtoa(&peer->srcadr), what);
+               errno = EINVAL;
+               return FDWRITE_ERROR;
+       }
+       return refclock_fdwrite(peer, peer->procptr->io.fd,
+                               buf, len, what);
+}
 
 /*
  * indicate_refclock_packet()
index f62cc466b25c112d9bee99373a2fac38af5f6986..a5b8c41da37536cd7e914f6968f629e8fa768aae 100644 (file)
 # include <sys/ioctl.h>
 #endif /* HAVE_SYS_IOCTL_H */
 
-#ifdef SYS_WINNT
-#undef write   /* ports/winnt/include/config.h: #define write _write */
-extern int async_write(int, const void *, unsigned int);
-#define write(fd, data, octets)        async_write(fd, data, octets)
-#endif
-
 /*
  * This driver supports the US (NIST, USNO) and European (PTB, NPL,
  * etc.) modem time services, as well as Spectracom GPS and WWVB
@@ -347,8 +341,7 @@ acts_receive(
                        *up->bufptr++ = *tptr;
                        if (*tptr == '*' || *tptr == '#') {
                                up->tstamp = pp->lastrec;
-                               if (write(pp->io.fd, tptr, 1) < 0)
-                                       msyslog(LOG_ERR, "acts: write echo fails %m");
+                               refclock_write(peer, tptr, 1, "data");
                        }
                }
        }
@@ -408,10 +401,10 @@ acts_message(
                              up->retry, sys_phone[up->retry]);
                if (ioctl(pp->io.fd, TIOCMBIS, &dtr) < 0)
                        msyslog(LOG_ERR, "acts: ioctl(TIOCMBIS) failed: %m");
-               if (write(pp->io.fd, sys_phone[up->retry],
-                   strlen(sys_phone[up->retry])) < 0)
-                       msyslog(LOG_ERR, "acts: write DIAL fails %m");
-               write(pp->io.fd, "\r", 1);
+               refclock_write(peer, sys_phone[up->retry],
+                              strlen(sys_phone[up->retry]),
+                              "DIAL");
+               refclock_write(peer, "\r", 1, "CR");
                up->retry++;
                up->state = S_CONNECT;
                up->timer = ANSWER;
@@ -467,7 +460,6 @@ acts_timeout(
        struct actsunit *up;
        struct refclockproc *pp;
        int     fd;
-       int     rc;
        char    device[20];
        char    lockfile[128], pidbuf[8];
 
@@ -534,8 +526,7 @@ acts_timeout(
                 * the modem business and send 'T' for Spectrabum.
                 */
                if (sys_phone[up->retry] == NULL) {
-                       if (write(pp->io.fd, "T", 1) < 0)
-                               msyslog(LOG_ERR, "acts: write T fails %m");
+                       refclock_write(peer, "T", 1, "T");
                        up->state = S_MSG;
                        up->timer = TIMECODE;
                        return;
@@ -547,10 +538,9 @@ acts_timeout(
                 */
                mprintf_event(PEVNT_CLOCK, peer, "SETUP %s",
                              modem_setup);
-               rc = write(pp->io.fd, modem_setup, strlen(modem_setup));
-               if (rc < 0)
-                       msyslog(LOG_ERR, "acts: write SETUP fails %m");
-               write(pp->io.fd, "\r", 1);
+               refclock_write(peer, modem_setup, strlen(modem_setup),
+                              "SETUP");
+               refclock_write(peer, "\r", 1, "CR");
                up->state = S_SETUP;
                up->timer = SETUP;
                return;
index 738be508d586d194b60dd43c0839a653021a3897..a836df8e27022a2a8cb945d917957a2d8832e4ce 100644 (file)
 #include <stdio.h>
 #include <ctype.h>
 
-#ifdef SYS_WINNT
-extern int async_write(int, const void *, unsigned int);
-#undef write
-#define write(fd, data, octets)        async_write(fd, data, octets)
-#endif
-
 /*
  * This driver supports the Arbiter 1088A/B Satellite Controlled Clock.
  * The claimed accuracy of this clock is 100 ns relative to the PPS
@@ -200,7 +194,7 @@ arb_start(
 #ifdef DEBUG
        if(debug) { printf("arbiter: mode = %d.\n", peer->MODE); }
 #endif
-       write(pp->io.fd, COMMAND_HALT_BCAST, 2);
+       refclock_write(peer, COMMAND_HALT_BCAST, 2, "HALT_BCAST");
        return (1);
 }
 
@@ -285,33 +279,34 @@ arb_receive(
                 */
                if (!strncmp(tbuf, "TQ", 2)) {
                        up->qualchar = tbuf[2];
-                       write(pp->io.fd, "SR", 2);
+                       refclock_write(peer, "SR", 2, "SR");
                        return;
 
                } else if (!strncmp(tbuf, "SR", 2)) {
                        strlcpy(up->status, tbuf + 2,
                                sizeof(up->status));
                        if (pp->sloppyclockflag & CLK_FLAG4)
-                               write(pp->io.fd, "LA", 2);
+                           refclock_write(peer, "LA", 2, "LA");
                        else
-                               write(pp->io.fd, COMMAND_START_BCAST, 2);
+                           refclock_write(peer, COMMAND_START_BCAST, 2,
+                               COMMAND_START_BCAST);
                        return;
 
                } else if (!strncmp(tbuf, "LA", 2)) {
                        strlcpy(up->latlon, tbuf + 2, sizeof(up->latlon));
-                       write(pp->io.fd, "LO", 2);
+                       refclock_write(peer, "LO", 2, "LO");
                        return;
 
                } else if (!strncmp(tbuf, "LO", 2)) {
                        strlcat(up->latlon, " ", sizeof(up->latlon));
                        strlcat(up->latlon, tbuf + 2, sizeof(up->latlon));
-                       write(pp->io.fd, "LH", 2);
+                       refclock_write(peer, "LH", 2, "LH");
                        return;
 
                } else if (!strncmp(tbuf, "LH", 2)) {
                        strlcat(up->latlon, " ", sizeof(up->latlon));
                        strlcat(up->latlon, tbuf + 2, sizeof(up->latlon));
-                       write(pp->io.fd, "DB", 2);
+                       refclock_write(peer, "DB", 2, "DB");
                        return;
 
                } else if (!strncmp(tbuf, "DB", 2)) {
@@ -322,7 +317,8 @@ arb_receive(
                        if (debug)
                                printf("arbiter: %s\n", up->latlon);
 #endif
-                       write(pp->io.fd, COMMAND_START_BCAST, 2);
+                       refclock_write(peer, COMMAND_START_BCAST, 2,
+                                      COMMAND_START_BCAST);
                }
        }
 
@@ -351,7 +347,7 @@ arb_receive(
            &syncchar, &pp->year, &pp->day, &pp->hour,
            &pp->minute, &pp->second) != 6) {
                refclock_report(peer, CEVNT_BADREPLY);
-               write(pp->io.fd, COMMAND_HALT_BCAST, 2);
+               refclock_write(peer, COMMAND_HALT_BCAST, 2, COMMAND_HALT_BCAST);
                return;
        }
 
@@ -401,13 +397,15 @@ arb_receive(
            case 'F':           /* clock failure */
                pp->disp = MAXDISPERSE;
                refclock_report(peer, CEVNT_FAULT);
-               write(pp->io.fd, COMMAND_HALT_BCAST, 2);
+               refclock_write(peer, COMMAND_HALT_BCAST, 2,
+                              COMMAND_HALT_BCAST);
                return;
 
            default:
                pp->disp = MAXDISPERSE;
                refclock_report(peer, CEVNT_BADREPLY);
-               write(pp->io.fd, COMMAND_HALT_BCAST, 2);
+               refclock_write(peer, COMMAND_HALT_BCAST, 2,
+                              COMMAND_HALT_BCAST);
                return;
        }
        if (syncchar != ' ')
@@ -425,7 +423,7 @@ arb_receive(
                refclock_receive(peer);
 
        /* if (up->tcswitch >= MAXSTAGE) { */
-       write(pp->io.fd, COMMAND_HALT_BCAST, 2);
+       refclock_write(peer, COMMAND_HALT_BCAST, 2, COMMAND_HALT_BCAST);
        /* } */
 }
 
@@ -454,7 +452,7 @@ arb_poll(
        up = pp->unitptr;
        pp->polls++;
        up->tcswitch = 0;
-       if (write(pp->io.fd, "TQ", 2) != 2)
+       if (refclock_write(peer, "TQ", 2, "TQ") != 2)
                refclock_report(peer, CEVNT_FAULT);
 
        /*
index 1acf9b208cfa3ba96ef140027767b09daa4b9614..6f630359543b405efcf25d958d221e9bdcb16715 100644 (file)
@@ -340,8 +340,9 @@ as2201_receive(
                memcpy(up->lastptr, stat_command[up->index], octets);
                up->lastptr += octets - 1;
                *up->lastptr = '\0';
-               (void)write(pp->io.fd, stat_command[up->index],
-                   strlen(stat_command[up->index]));
+               refclock_write(peer, stat_command[up->index],
+                              strlen(stat_command[up->index]),
+                              "command");
                up->index++;
                if (*stat_command[up->index] == '\0')
                        up->index = 0;
index 89f0f4748413fbeadb9dd472ab4da4f5680b51aa..d649e188410a88571eb6276dbcff0ce76cdae930 100644 (file)
 #include <stdio.h>
 #include <ctype.h>
 
-#ifdef SYS_WINNT
-extern int async_write(int, const void *, unsigned int);
-#undef write
-#define write(fd, data, octets)        async_write(fd, data, octets)
-#endif
-
 /*
  * This driver supports a generic dumb clock that only outputs hh:mm:ss,
  * in local time, no less.
@@ -370,7 +364,7 @@ dumbclock_poll(
                pollchar = 'R';
        else
                pollchar = 'T';
-       if (write(pp->io.fd, &pollchar, 1) != 1)
+       if (refclock_fdwrite(peer, pp->io.fd, &pollchar, 1) != 1)
                refclock_report(peer, CEVNT_FAULT);
        else
                pp->polls++;
index c2d41ff07f173549b333f602dc7fdd5cca30cf7f..f1dcb4821d75bb127ea71cf276b72e6bdb733d7a 100644 (file)
@@ -1507,7 +1507,7 @@ process_version(
 
        /* The logon string is actually the ?WATCH command of GPSD,
         * using JSON data and selecting the GPS device name we created
-        * from our unit number. We have an old a newer version that
+        * from our unit number. We have an old and a newer version that
         * request PPS (and TOFF) transmission.
         */
        snprintf(up->buffer, sizeof(up->buffer),
index aed056c9149783cf531e4ff82c6827688ef38acf..2acc6db38250eb7e3a6382dda4bd9261cdd88a85 100644 (file)
@@ -427,7 +427,7 @@ heath_poll(
        if (ioctl(pp->io.fd, TIOCMBIC, (char *)&bits) < 0)
                refclock_report(peer, CEVNT_FAULT);
        get_systime(&pp->lastrec);
-       if (write(pp->io.fd, "T", 1) != 1)
+       if (refclock_write(peer, "T", 1, "T") != 1)
                refclock_report(peer, CEVNT_FAULT);
        ioctl(pp->io.fd, TIOCMBIS, (char *)&bits);
        if (pp->coderecv == pp->codeproc) {
index dae8b37617797ad6492fcccc23038a399bfc8307..14227c81624de3d8934768d6c26c9a50fcef964c 100644 (file)
 # include <sys/ioctl.h>
 #endif
 
-#ifdef SYS_WINNT
-extern int async_write(int, const void *, unsigned int);
-#undef write
-#define write(fd, data, octets)        async_write(fd, data, octets)
-#endif
-
 /*
  * clock definitions
  */
index 0b45fc74f2f1ea93060f3db630c0a22647a2eca3..f80d6bcd713732dd9f569b2d96e986af4de4c9c5 100644 (file)
@@ -209,8 +209,8 @@ hpgps_start(
         * and get the local timezone information
         */
        up->linecnt = 1;
-       if (write(pp->io.fd, "*IDN?\r:PTIME:TZONE?\r", 20) != 20)
-           refclock_report(peer, CEVNT_FAULT);
+       if (refclock_write(peer, "*IDN?\r:PTIME:TZONE?\r", 20, NULL) != 20)
+               refclock_report(peer, CEVNT_FAULT);
 
        return (1);
 }
@@ -349,10 +349,10 @@ hpgps_receive(
        if (strrchr(prompt,'E') > strrchr(prompt,'s')){
 #ifdef DEBUG
                if (debug)
-                   printf("hpgps: error indicated in prompt: %s\n", prompt);
+                       printf("hpgps: error indicated in prompt: %s\n", prompt);
 #endif
-               if (write(pp->io.fd, "*CLS\r\r", 6) != 6)
-                   refclock_report(peer, CEVNT_FAULT);
+               if (refclock_write(peer, "*CLS\r\r", 6, NULL) != 6)
+                       refclock_report(peer, CEVNT_FAULT);
        }
 
        /*
@@ -584,8 +584,8 @@ hpgps_receive(
         */
        if (pp->sloppyclockflag & CLK_FLAG4){
                up->linecnt = 22; 
-               if (write(pp->io.fd, ":SYSTEM:PRINT?\r", 15) != 15)
-                   refclock_report(peer, CEVNT_FAULT);
+               if (refclock_write(peer, ":SYSTEM:PRINT?\r", 15, NULL) != 15)
+                       refclock_report(peer, CEVNT_FAULT);
        }
 }
 
@@ -614,7 +614,7 @@ hpgps_poll(
            refclock_report(peer, CEVNT_TIMEOUT);
        else
            up->pollcnt--;
-       if (write(pp->io.fd, ":PTIME:TCODE?\r", 14) != 14) {
+       if (refclock_write(peer, ":PTIME:TCODE?\r", 14, NULL) != 14) {
                refclock_report(peer, CEVNT_FAULT);
        }
        else
index 19bdeeb3b6680d4395da9083ed9cb0516f380688..7c5cd69b24f1d57052f411914852f04c6d3dad83 100644 (file)
@@ -325,14 +325,7 @@ static void     save_ltc        (struct refclockproc * const, const char * const
  * support functions by defining NMEA_WRITE_SUPPORT to non-zero...
  */
 #if NMEA_WRITE_SUPPORT
-
 static void gps_send(int, const char *, struct peer *);
-# ifdef SYS_WINNT
-#  undef write /* ports/winnt/include/config.h: #define write _write */
-extern int async_write(int, const void *, unsigned int);
-#  define write(fd, data, octets)      async_write(fd, data, octets)
-# endif /* SYS_WINNT */
-
 #endif /* NMEA_WRITE_SUPPORT */
 
 static int32_t g_gpsMinBase;
@@ -1235,7 +1228,7 @@ gps_send(
                len - 2, cmd));
 
        /* send out the whole stuff */
-       if (write(fd, cmd, len) == -1)
+       if (refclock_fdwrite(peer, fd, cmd, len) != len)
                refclock_report(peer, CEVNT_FAULT);
 }
 #endif /* NMEA_WRITE_SUPPORT */
index 2c82caeb68c6aff4952417924872060480f1f490..c1dfb6ac24b68ec997bbcc40e1691c295b7348e2 100644 (file)
@@ -953,7 +953,9 @@ oncore_init_shmem(
                shmem_old_size = sbuf.st_size;
                if (shmem_old_size != 0) {
                        shmem_old = emalloc((unsigned) sbuf.st_size);
-                       read(fd, shmem_old, shmem_old_size);
+                       if (read(fd, shmem_old, shmem_old_size) != shmem_old_size)
+                               oncore_log(instance, LOG_WARNING,
+                                          "ONCORE: truncated/failed read of SHMEM file");
                }
                close(fd);
        }
@@ -3531,7 +3533,8 @@ oncore_load_almanac(
                if (!strncmp((char *) cp, "@@Cb", 4) &&
                    oncore_checksum_ok(cp, 33) &&
                    (*(cp+4) == 4 || *(cp+4) == 5)) {
-                       write(instance->ttyfd, cp, n);
+                       refclock_fdwrite(instance->peer, instance->ttyfd,
+                                        cp, n, "data");
                        oncore_print_Cb(instance, cp);
                }
        }
@@ -3754,20 +3757,22 @@ oncore_sendmsg(
 {
        int     fd;
        u_char cs = 0;
+       const struct peer * peer;
 
-       fd = instance->ttyfd;
+       fd   = instance->ttyfd;
+       peer = instance->peer;
 #ifdef ONCORE_VERBOSE_SENDMSG
        if (debug > 4) {
                oncore_log_f(instance, LOG_DEBUG, "ONCORE: Send @@%c%c %d",
                             ptr[0], ptr[1], (int)len);
        }
 #endif
-       write(fd, "@@", (size_t) 2);
-       write(fd, ptr, len);
+       refclock_fdwrite(peer, fd, "@@", (size_t)2, "data");
+       refclock_fdwrite(peer, fd, ptr, len, "data");
        while (len--)
                cs ^= *ptr++;
-       write(fd, &cs, (size_t) 1);
-       write(fd, "\r\n", (size_t) 2);
+       refclock_fdwrite(peer, fd, &cs, (size_t)1, "data");
+       refclock_fdwrite(peer, fd, "\r\n", (size_t)2, "data");
 }
 
 
index d69ce9400e686509b1caba9a1659472b2d226987..35549b0aaf53ac45a1551e47a5d94eac1d25c4ba 100644 (file)
@@ -1032,9 +1032,9 @@ palisade_poll (
                return;  /* using synchronous packet input */
 
        if(up->type == CLK_PRAECIS) {
-               if(write(peer->procptr->io.fd,"SPSTAT\r\n",8) < 0)
+               if (write(peer->procptr->io.fd,"SPSTAT\r\n",8) < 0) {
                        msyslog(LOG_ERR, "Palisade(%d) write: %m:",unit);
-               else {
+               else {
                        praecis_msg = 1;
                        return;
                }
@@ -1204,7 +1204,10 @@ HW_poll (
 
        /* Edge trigger */
        if (up->type == CLK_ACUTIME)
-               write (pp->io.fd, "", 1);
+               if (write (pp->io.fd, "", 1) != 1)
+                       msyslog(LOG_WARNING,
+                               "Palisade(%d) HW_poll: failed to send trigger: %m", 
+                               up->unit);
                
        if (ioctl(pp->io.fd, TIOCMSET, &x) < 0) { 
 #ifdef DEBUG
index 2799f3ee5dee8b85512c7e1637868a8c3d04c31a..1324f7b51cbcbce7ac7453feacefe4f9df16ed40 100644 (file)
 #include "ntp_unixtime.h"
 #include "ntp_stdlib.h"
 
-#ifdef SYS_WINNT
-extern int async_write(int, const void *, unsigned int);
-#undef write
-#define write(fd, data, octets)        async_write(fd, data, octets)
-#endif
-
 /* This should be an atom clock but those are very hard to build.
  *
  * The PCL720 from P C Labs has an Intel 8253 lookalike, as well as a bunch
@@ -640,7 +634,7 @@ true_send(
                size_t len = strlen(cmd);
 
                true_debug(peer, "Send '%s'\n", cmd);
-               if (write(pp->io.fd, cmd, (unsigned)len) != len)
+               if (refclock_write(peer, cmd, len, NULL) != len)
                        refclock_report(peer, CEVNT_FAULT);
                else
                        pp->polls++;
index 969c1e3863c73ce74da4dc1f3b7dca792eb41d64..d983ed4a5234f1c944e195ed787969895a3a1bcc 100644 (file)
@@ -989,7 +989,8 @@ read_drift(
        {
                int idrift = 0, fdrift = 0;
 
-               fscanf(df, "%4d.%03d", &idrift, &fdrift);
+               if (2 != fscanf(df, "%4d.%03d", &idrift, &fdrift))
+                       LPRINTF("read_drift: trouble reading drift file");
                fclose(df);
                LPRINTF("read_drift: %d.%03d ppm ", idrift, fdrift);
 
@@ -1172,7 +1173,10 @@ detach(
        )
 {
 #   ifdef HAVE_DAEMON
-       daemon(0, 0);
+       if (daemon(0, 0)) {
+               fprintf(stderr, "'daemon()' fails: %d(%s)\n",
+                       errno, strerror(errno));
+       }
 #   else /* not HAVE_DAEMON */
        if (fork())
            exit(0);
index 8114a3c43830b46743cf4101ab440cf822790a15..7e31a6cea3ee9c91adae6dde2791affe84d93704 100644 (file)
       <DisableSpecificWarnings Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">4307;%(DisableSpecificWarnings)</DisableSpecificWarnings>
       <DisableSpecificWarnings Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">4307;%(DisableSpecificWarnings)</DisableSpecificWarnings>
       <DisableSpecificWarnings Condition="'$(Configuration)|$(Platform)'=='Release|x64'">4307;%(DisableSpecificWarnings)</DisableSpecificWarnings>
+      <DisableSpecificWarnings Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">4307;%(DisableSpecificWarnings)</DisableSpecificWarnings>
     </ClCompile>
     <ClCompile Include="..\..\..\..\ntpd\refclock_as2201.c" />
     <ClCompile Include="..\..\..\..\ntpd\refclock_atom.c" />
index b25f8acac8e7f0e84ed93512ac9a1418f2fe4038..409a4ad14f4d321b95933afb1ce165b2fa93e5f4 100644 (file)
@@ -2,6 +2,7 @@
 
 #include "ntp_calendar.h"
 #include "ntp_stdlib.h"
+#include "lib_strbuf.h"
 
 #include "unity.h"
 #include "test-libntp.h"
@@ -9,8 +10,6 @@
 #include <string.h>
 
 
-char * CalendarToString(const struct calendar cal);
-int IsEqual(const struct calendar expected, const struct calendar actual);
 void setUp(void);
 void tearDown(void);
 void test_RegularTime(void);
@@ -19,37 +18,20 @@ void test_uLongBoundary(void);
 void test_uLongWrapped(void);
 
 
-char *
+static const char *
 CalendarToString(const struct calendar cal)
 {
-       char * str = emalloc (sizeof (char) * 100);
-       char buffer[100] ="";
-
-       *str = '\0';
-       snprintf(buffer, 100, "%u", cal.year);
-       strcat(str, buffer);
-       strcat(str, "-");
-       snprintf(buffer, 100, "%u", (u_int)cal.month);
-       strcat(str, buffer);
-       strcat(str, "-");
-       snprintf(buffer, 100, "%u", (u_int)cal.monthday);
-       strcat(str, buffer);
-       strcat(str, " (");
-       snprintf(buffer, 100, "%u", (u_int) cal.yearday);
-       strcat(str, buffer);
-       strcat(str, ") ");
-       snprintf(buffer, 100, "%u", (u_int)cal.hour);
-       strcat(str, buffer);
-       strcat(str, ":");
-       snprintf(buffer, 100, "%u", (u_int)cal.minute);
-       strcat(str, buffer);
-       strcat(str, ":");
-       snprintf(buffer, 100, "%u", (u_int)cal.second);
-       strcat(str, buffer);
+       char * str;
+
+       LIB_GETBUF(str);
+       snprintf(str, LIB_BUFLENGTH,
+                "%04hu-%02hhu-%02hhu (%hu) %02hhu:%02hhu:%02hhu",
+                cal.year, cal.month, cal.monthday, cal.yearday,
+                cal.hour, cal.minute, cal.second);
        return str;
 }
 
-int // technically boolean
+static int // technically boolean
 IsEqual(const struct calendar expected, const struct calendar actual)
 {
        if (   expected.year == actual.year
@@ -61,39 +43,28 @@ IsEqual(const struct calendar expected, const struct calendar actual)
            && expected.second == actual.second) {
                return TRUE;
        } else {
-               char *p_exp, *p_act;
-
-               p_exp = CalendarToString(expected);
-               p_act = CalendarToString(actual);
+               const char * p_exp = CalendarToString(expected);
+               const char * p_act = CalendarToString(actual);
                printf("expected: %s but was %s", p_exp, p_act);
-               free(p_exp);
-               free(p_act);
                return FALSE;
        }
 }
 
 
-void
-setUp()
+void setUp(void)
 {
     ntpcal_set_timefunc(timefunc);
     settime(1970, 1, 1, 0, 0, 0);
     init_lib();
-
-    return;
 }
 
-void
-tearDown()
+void tearDown(void)
 {
     ntpcal_set_timefunc(NULL);
-
-    return;
 }
 
 
-void
-test_RegularTime(void)
+void test_RegularTime(void)
 {
        u_long testDate = 3485080800UL; // 2010-06-09 14:00:00
        struct calendar expected = {2010,160,6,9,14,0,0};
@@ -103,12 +74,9 @@ test_RegularTime(void)
        caljulian(testDate, &actual);
 
        TEST_ASSERT_TRUE(IsEqual(expected, actual));
-
-       return;
 }
 
-void
-test_LeapYear(void)
+void test_LeapYear(void)
 {
        u_long input = 3549902400UL; // 2012-06-28 20:00:00Z
        struct calendar expected = {2012, 179, 6, 28, 20, 0, 0};
@@ -118,12 +86,9 @@ test_LeapYear(void)
        caljulian(input, &actual);
 
        TEST_ASSERT_TRUE(IsEqual(expected, actual));
-
-       return;
 }
 
-void
-test_uLongBoundary(void)
+void test_uLongBoundary(void)
 {
        u_long enc_time = 4294967295UL; // 2036-02-07 6:28:15
        struct calendar expected = {2036,0,2,7,6,28,15};
@@ -133,12 +98,9 @@ test_uLongBoundary(void)
        caljulian(enc_time, &actual);
 
        TEST_ASSERT_TRUE(IsEqual(expected, actual));
-
-       return;
 }
 
-void
-test_uLongWrapped(void)
+void test_uLongWrapped(void)
 {
        u_long enc_time = 0;
        struct calendar expected = {2036,0,2,7,6,28,16};
@@ -148,6 +110,4 @@ test_uLongWrapped(void)
        caljulian(enc_time, &actual);
 
        TEST_ASSERT_TRUE(IsEqual(expected, actual));
-
-       return;
 }
index f611dde2884e8308fbab8bfb0e274cdd6fd02697..c94098796ee832104bca717c3f92be0f8dd9e098 100644 (file)
@@ -25,6 +25,7 @@
 #include "config.h"
 #include "ntp_calendar.h"
 #include "ntp_stdlib.h"
+#include "lib_strbuf.h"
 #include "test-libntp.h"
 #include <string.h>
 
@@ -61,10 +62,10 @@ int main(int argc, char *argv[])
   progname = argv[0];
   suite_setup();
   UnityBegin("caljulian.c");
-  RUN_TEST(test_RegularTime, 16);
-  RUN_TEST(test_LeapYear, 17);
-  RUN_TEST(test_uLongBoundary, 18);
-  RUN_TEST(test_uLongWrapped, 19);
+  RUN_TEST(test_RegularTime, 15);
+  RUN_TEST(test_LeapYear, 16);
+  RUN_TEST(test_uLongBoundary, 17);
+  RUN_TEST(test_uLongWrapped, 18);
 
   return (UnityEnd());
 }