]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
[Bug 1580] Purge tty line buffers so serial refclocks do not get outdated data immedi...
authorJuergen Perlinger <perlinger@ntp.org>
Fri, 4 Jun 2010 06:34:03 +0000 (08:34 +0200)
committerJuergen Perlinger <perlinger@ntp.org>
Fri, 4 Jun 2010 06:34:03 +0000 (08:34 +0200)
bk: 4c089e5bk23LbkYfv9bbtczpXywTWQ

ntpd/ntp_refclock.c

index 9e47f58c6a37fe83629307b4c82f168ff9b6b362..249c998c460be2ef9d5aace86a2683dfb8d5074f 100644 (file)
@@ -714,6 +714,9 @@ refclock_open(
 {
        int     fd;
        int     omode;
+#ifdef O_NONBLOCK
+       char    trash[128];     /* litter bin for old input data */
+#endif
 
        /*
         * Open serial port and set default options
@@ -740,6 +743,16 @@ refclock_open(
                close(fd);
                return (0);
        }
+#ifdef O_NONBLOCK
+       /*
+        * We want to make sure there is no pending trash in the input
+        * buffer. Since we have none-blocking IO available, this is a good
+        * moment to read and dump all available outdated stuff that might have
+        * become toxic for the driver.
+        */
+       while (read(fd, trash, sizeof(trash)) > 0)
+               /*NOP*/;
+#endif
        return (fd);
 }
 
@@ -835,6 +848,15 @@ refclock_setup(
                    "refclock_setup fd %d TCSANOW: %m", fd);
                return (0);
        }
+
+       /*
+        * flush input and output buffers to discard any outdated stuff that
+        * might have become toxic for the driver. Failing to do so is logged,
+        * but we keep our fingers crossed otherwise.
+        */
+       if (tcflush(fd, TCIOFLUSH) < 0) {
+               msyslog(LOG_ERR, "refclock_setup fd %d tcflush(): %m", fd);
+       }
 #endif /* HAVE_TERMIOS */
 
 #ifdef HAVE_SYSV_TTYS