From: Juergen Perlinger Date: Sun, 13 Mar 2022 07:38:51 +0000 (+0100) Subject: [Bug 3757] Improve handling of Linux-PPS in NTPD X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4531395fdaa216efc7855650480ab106948e142e;p=thirdparty%2Fntp.git [Bug 3757] Improve handling of Linux-PPS in NTPD cleanup bk: 622d9f8b8MGFfoy8fR50et97s6W8ZA --- diff --git a/include/ntp_refclock.h b/include/ntp_refclock.h index 7f88d9586..5604cf240 100644 --- a/include/ntp_refclock.h +++ b/include/ntp_refclock.h @@ -250,12 +250,10 @@ extern int refclock_ppsaugment( const struct refclock_atom*, l_fp *rcvtime , double rcvfudge, double ppsfudge); -#ifdef _WIN32 -#define ppsdev_open(ttyfd, ppspath, mode, flags) (ttyfd) -#else -extern int ppsdev_open(int ttyfd, const char *ppspath, - int mode, int flags); -#endif +extern int ppsdev_reopen(int ttyfd, int ppsfd, const char *ppspath, + int mode, int flags); +extern void ppsdev_close(int ttyfd, int ppsfd); + #endif /* REFCLOCK */ #endif /* NTP_REFCLOCK_H */ diff --git a/ntpd/ntp_ppsdev.c b/ntpd/ntp_ppsdev.c index 17d6db8ec..8d29b65fd 100644 --- a/ntpd/ntp_ppsdev.c +++ b/ntpd/ntp_ppsdev.c @@ -337,16 +337,23 @@ findMatchingPpsDev( #endif /* linux PPS device matcher */ /* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */ - int -ppsdev_open( - int ttyfd , - const char *ppspath, - int omode , - int oflags ) +ppsdev_reopen( + int ttyfd , /* current tty FD, or -1 */ + int ppsfd , /* current pps FD, or -1 */ + const char *ppspath, /* path to pps device, or NULL */ + int omode , /* open mode for pps device */ + int oflags ) /* openn flags for pps device */ { int retfd = -1; + /* avoid 'unused' warnings: we might not use all args, no + * thanks to conditional compiling:) + */ + (void)ppspath; + (void)omode; + (void)oflags; + # if defined(__unix__) && !defined(_WIN32) if (-1 == retfd) { if (ppspath && *ppspath) { @@ -358,7 +365,7 @@ ppsdev_open( # endif # if defined(WITH_PPSDEV_MATCH) - if (-1 == retfd) { + if ((-1 == retfd) && (-1 != ttyfd)) { char *xpath = findMatchingPpsDev(ttyfd); if (xpath && *xpath) { retfd = open(xpath, omode, oflags); @@ -375,10 +382,29 @@ ppsdev_open( * based on COM Events. So, if everything else fails, simply * try the FD given for the TTY/COMport... */ + if (-1 == retfd) + retfd = ppsfd; if (-1 == retfd) retfd = ttyfd; + + /* Close the old pps FD, but only if the new pps FD is neither + * the tty FD nor the existing pps FD! + */ + if ((retfd != ttyfd) && (retfd != ppsfd)) + ppsdev_close(ttyfd, ppsfd); return retfd; } +void +ppsdev_close( + int ttyfd, /* current tty FD, or -1 */ + int ppsfd) /* current pps FD, or -1 */ +{ + /* The pps fd might be the same as the tty fd. We close the pps + * channel only if it's valid and _NOT_ the tty itself: + */ + if ((-1 != ppsfd) && (ttyfd != ppsfd)) + close(ppsfd); +} /* --*-- that's all folks --*-- */ diff --git a/ntpd/refclock_nmea.c b/ntpd/refclock_nmea.c index 0b9e2ea7e..56178e610 100644 --- a/ntpd/refclock_nmea.c +++ b/ntpd/refclock_nmea.c @@ -472,8 +472,7 @@ nmea_shutdown( # ifdef HAVE_PPSAPI if (up->ppsapi_lit) time_pps_destroy(up->atom.handle); - if ((up->ppsapi_fd != -1) && (up->ppsapi_fd != pp->io.fd)) - close(up->ppsapi_fd); + ppsdev_close(pp->io.fd, up->ppsapi_fd); # endif free(up); } @@ -519,36 +518,20 @@ nmea_control( /* Light up the PPSAPI interface if not yet attempted. */ if ((CLK_FLAG1 & pp->sloppyclockflag) && !up->ppsapi_tried) { - int ppsfd; + const char *ppsname = device; up->ppsapi_tried = TRUE; /* get FD for the pps device; might be the tty itself! */ devlen = snprintf(device, sizeof(device), PPSDEV, unit); - if (devlen < sizeof(device)) { - ppsfd = ppsdev_open( - pp->io.fd, device, - PPSOPENMODE, (S_IRUSR|S_IWUSR)); - } else { - ppsfd = pp->io.fd; + if (devlen >= sizeof(device)) { msyslog(LOG_ERR, "%s PPS device name too long", refnumtoa(&peer->srcadr)); + ppsname = NULL; } - /* Now do a dance to juggle it into place: */ - if (-1 == up->ppsapi_fd) { - /* no previous FD -- that one is easy. */ - up->ppsapi_fd = ppsfd; - } else if (ppsfd != pp->io.fd) { - /* new distinct pps FD -- take it! */ - if (up->ppsapi_fd != pp->io.fd) - close(up->ppsapi_fd); - up->ppsapi_fd = ppsfd; - } - /* If neither condition above is met, we have to keep - * the existing pps handle: It is either a device we - * could not open again since we dropped privs, or it is - * the tty handle because there was nothing else to open - * right from the beginning. - * - * note: the PPS I/O handle remains valid until + up->ppsapi_fd = ppsdev_reopen( + pp->io.fd, up->ppsapi_fd, + ppsname, PPSOPENMODE, (S_IRUSR|S_IWUSR)); + /* note 1: the pps fd might be the same as the tty fd + * note 2: the current PPS fd remains valid until * - the clock is shut down * - flag1 is set again after being cleared */ diff --git a/ntpd/refclock_oncore.c b/ntpd/refclock_oncore.c index 17c8cbf29..a0d11d922 100644 --- a/ntpd/refclock_oncore.c +++ b/ntpd/refclock_oncore.c @@ -3431,7 +3431,7 @@ oncore_check_leap_sec( if (instance->saw_Gj < 0) { /* -1 DONT have Gj use Bj */ if ((instance->BEHa[4] == 6) || (instance->BEHa[4] == 12)) oncore_sendmsg(instance, oncore_cmd_Bj, sizeof(oncore_cmd_Bj)); - oncore_sendmsg(instance, oncore_cmd_Bl, sizeof(oncore_cmd_Bl)); + oncore_sendmsg(instance, oncore_cmd_Bl, sizeof(oncore_cmd_Bl)); return; } @@ -3874,7 +3874,7 @@ oncore_set_traim( oncore_sendmsg(instance, oncore_cmd_Enx, sizeof(oncore_cmd_Enx)); else /* chan == 12 */ oncore_sendmsg(instance, oncore_cmd_Ge0, sizeof(oncore_cmd_Ge0)); - oncore_sendmsg(instance, oncore_cmd_Hn0, sizeof(oncore_cmd_Hn0)); + oncore_sendmsg(instance, oncore_cmd_Hn0, sizeof(oncore_cmd_Hn0)); } } diff --git a/ports/winnt/vs2005/ntpd.vcproj b/ports/winnt/vs2005/ntpd.vcproj index 3d730c0b4..86501cbf6 100644 --- a/ports/winnt/vs2005/ntpd.vcproj +++ b/ports/winnt/vs2005/ntpd.vcproj @@ -208,6 +208,28 @@ Name="Source Files" Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" > + + + + + + + + diff --git a/ports/winnt/vs2008/ntpd/ntpd.vcproj b/ports/winnt/vs2008/ntpd/ntpd.vcproj index 107cf8724..0bc3ee2f6 100644 --- a/ports/winnt/vs2008/ntpd/ntpd.vcproj +++ b/ports/winnt/vs2008/ntpd/ntpd.vcproj @@ -307,6 +307,10 @@ Name="Source Files" Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" > + + diff --git a/ports/winnt/vs2013/ntpd/ntpd.vcxproj b/ports/winnt/vs2013/ntpd/ntpd.vcxproj index 0fd27b54a..9b445fb52 100644 --- a/ports/winnt/vs2013/ntpd/ntpd.vcxproj +++ b/ports/winnt/vs2013/ntpd/ntpd.vcxproj @@ -260,6 +260,7 @@ + @@ -513,4 +514,4 @@ - \ No newline at end of file + diff --git a/ports/winnt/vs2013/ntpd/ntpd.vcxproj.filters b/ports/winnt/vs2013/ntpd/ntpd.vcxproj.filters index ee251fb97..4a22b09a0 100644 --- a/ports/winnt/vs2013/ntpd/ntpd.vcxproj.filters +++ b/ports/winnt/vs2013/ntpd/ntpd.vcxproj.filters @@ -24,6 +24,9 @@ + + Source Files + Source Files @@ -556,4 +559,4 @@ - \ No newline at end of file + diff --git a/ports/winnt/vs2015/ntpd/ntpd.vcxproj b/ports/winnt/vs2015/ntpd/ntpd.vcxproj index 8114a3c43..c116eb14b 100644 --- a/ports/winnt/vs2015/ntpd/ntpd.vcxproj +++ b/ports/winnt/vs2015/ntpd/ntpd.vcxproj @@ -260,6 +260,7 @@ + @@ -513,4 +514,4 @@ - \ No newline at end of file + diff --git a/ports/winnt/vs2015/ntpd/ntpd.vcxproj.filters b/ports/winnt/vs2015/ntpd/ntpd.vcxproj.filters index ee251fb97..4a22b09a0 100644 --- a/ports/winnt/vs2015/ntpd/ntpd.vcxproj.filters +++ b/ports/winnt/vs2015/ntpd/ntpd.vcxproj.filters @@ -24,6 +24,9 @@ + + Source Files + Source Files @@ -556,4 +559,4 @@ - \ No newline at end of file +