]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
[Bug 3757] Improve handling of Linux-PPS in NTPD
authorJuergen Perlinger <perlinger@ntp.org>
Sun, 13 Mar 2022 07:38:51 +0000 (08:38 +0100)
committerJuergen Perlinger <perlinger@ntp.org>
Sun, 13 Mar 2022 07:38:51 +0000 (08:38 +0100)
 cleanup

bk: 622d9f8b8MGFfoy8fR50et97s6W8ZA

include/ntp_refclock.h
ntpd/ntp_ppsdev.c
ntpd/refclock_nmea.c
ntpd/refclock_oncore.c
ports/winnt/vs2005/ntpd.vcproj
ports/winnt/vs2008/ntpd/ntpd.vcproj
ports/winnt/vs2013/ntpd/ntpd.vcxproj
ports/winnt/vs2013/ntpd/ntpd.vcxproj.filters
ports/winnt/vs2015/ntpd/ntpd.vcxproj
ports/winnt/vs2015/ntpd/ntpd.vcxproj.filters

index 7f88d958638735035cf89fa0656825f3b12d2b3b..5604cf240fec2f31d018712ecc2b90aa60116857 100644 (file)
@@ -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 */
index 17d6db8ecc03238c00ae347705e34568fbfc8e89..8d29b65fd0005aafbad3d76d3376fb46157d139a 100644 (file)
@@ -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 --*-- */
index 0b9e2ea7e4ef8920ee7af0b5b6a480a4fbd0b742..56178e61073d5e91cb4cc7f947e797c3e62794f5 100644 (file)
@@ -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
                 */
index 17c8cbf294abcfda21243579c58986a1331e72a3..a0d11d922b2845a1249d45115e41d802753d0ad3 100644 (file)
@@ -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));
        }
 }
 
index 3d730c0b47e561f089594f72ded4acd021a5669a..86501cbf6c1466cf371bba69cdf700af2e6a8523 100644 (file)
                        Name="Source Files"
                        Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
                        >
+                       <File
+                               RelativePath="..\..\..\ntpd\ntp_ppsdev.c"
+                               >
+                               <FileConfiguration
+                                       Name="Release|Win32"
+                                       >
+                                       <Tool
+                                               Name="VCCLCompilerTool"
+                                               AdditionalIncludeDirectories=""
+                                               PreprocessorDefinitions=""
+                                       />
+                               </FileConfiguration>
+                               <FileConfiguration
+                                       Name="Debug|Win32"
+                                       >
+                                       <Tool
+                                               Name="VCCLCompilerTool"
+                                               AdditionalIncludeDirectories=""
+                                               PreprocessorDefinitions=""
+                                       />
+                               </FileConfiguration>
+                       </File>
                        <File
                                RelativePath="..\..\..\ntpd\cmd_args.c"
                                >
index 107cf87245b7c879292701bb038f37032bd3e073..0bc3ee2f65cc9639d4f070133485d12ba93d7e7e 100644 (file)
                        Name="Source Files"
                        Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
                        >
+                       <File
+                               RelativePath="..\..\..\..\ntpd\ntp_ppsdev.c"
+                               >
+                       </File>
                        <File
                                RelativePath="..\..\..\..\ntpd\cmd_args.c"
                                >
index 0fd27b54a06b4b1e1db216500a946bd0ec2b90a7..9b445fb525ba6454bb79a87653344eafadcf28c3 100644 (file)
     <ClCompile Include="..\..\..\..\libparse\parse.c" />
     <ClCompile Include="..\..\..\..\libparse\parse_conf.c" />
     <ClCompile Include="..\..\..\..\libparse\trim_info.c" />
+    <ClCompile Include="..\..\..\..\ntpd\ntp_ppsdev.c" />
     <ClCompile Include="..\..\..\..\ntpd\cmd_args.c" />
     <ClCompile Include="..\..\..\..\ntpd\ntpd-opts.c" />
     <ClCompile Include="..\..\..\..\ntpd\ntpd.c" />
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
-</Project>
\ No newline at end of file
+</Project>
index ee251fb976c6df1a5364a1d9eeb3704bf38fda51..4a22b09a0f033be1ac018012d14ffbebbc83dcae 100644 (file)
@@ -24,6 +24,9 @@
     </Filter>
   </ItemGroup>
   <ItemGroup>
+    <ClCompile Include="..\..\..\..\ntpd\ntp_ppsdev.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
     <ClCompile Include="..\..\..\..\ntpd\cmd_args.c">
       <Filter>Source Files</Filter>
     </ClCompile>
     </CustomBuild>
     <CustomBuild Include="..\..\..\..\packageinfo.sh" />
   </ItemGroup>
-</Project>
\ No newline at end of file
+</Project>
index 8114a3c43830b46743cf4101ab440cf822790a15..c116eb14b50ff943a4ea5449d9f9c712f4e2fe5f 100644 (file)
     <ClCompile Include="..\..\..\..\libparse\parse.c" />
     <ClCompile Include="..\..\..\..\libparse\parse_conf.c" />
     <ClCompile Include="..\..\..\..\libparse\trim_info.c" />
+    <ClCompile Include="..\..\..\..\ntpd\ntp_ppsdev.c" />
     <ClCompile Include="..\..\..\..\ntpd\cmd_args.c" />
     <ClCompile Include="..\..\..\..\ntpd\ntpd-opts.c" />
     <ClCompile Include="..\..\..\..\ntpd\ntpd.c" />
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
-</Project>
\ No newline at end of file
+</Project>
index ee251fb976c6df1a5364a1d9eeb3704bf38fda51..4a22b09a0f033be1ac018012d14ffbebbc83dcae 100644 (file)
@@ -24,6 +24,9 @@
     </Filter>
   </ItemGroup>
   <ItemGroup>
+    <ClCompile Include="..\..\..\..\ntpd\ntp_ppsdev.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
     <ClCompile Include="..\..\..\..\ntpd\cmd_args.c">
       <Filter>Source Files</Filter>
     </ClCompile>
     </CustomBuild>
     <CustomBuild Include="..\..\..\..\packageinfo.sh" />
   </ItemGroup>
-</Project>
\ No newline at end of file
+</Project>