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 */
#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) {
# 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);
* 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 --*-- */
# 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);
}
/* 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
*/
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;
}
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));
}
}
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"
>
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"
>
<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>
</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>
<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>
</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>