[chrony-dev] clock_gettime() on macOS

[ Thread Index | Date Index | More chrony.tuxfamily.org/chrony-dev Archives ]


We need to make sure that chrony compiled on macOS 10.12 (or later) will still run on versions earlier than macOS 10.12

This patch allows that but I’m not sure it’s structured correctly for chrony and I don’t like the duplication of the gettimeofday() fallback.
Another option would be to modify configure so that HAVE_CLOCK_GETTIME is never set on macOS but that doesn’t feel right either.

Suggestions how to better structure it?

diff --git a/local.c b/local.c
index 82f8fb0..c999607 100644
--- a/local.c
+++ b/local.c
@@ -355,8 +355,17 @@ void
 LCL_ReadRawTime(struct timespec *ts)
 {
 #if HAVE_CLOCK_GETTIME
+  #ifdef MACOSX
+  /* macOS only has clock_gettime on 10.12 and later. */
+  /* We need a run time check to see if it is available */
+  /* in case chrony was compiled on 10.12 but we are running on an earlier version */
+  extern void SYS_MacOSX_ReadRawTime(struct timespec *ts);
+  SYS_MacOSX_ReadRawTime(ts);
+
+  #else
   if (clock_gettime(CLOCK_REALTIME, ts) < 0)
     LOG_FATAL(LOGF_Local, "clock_gettime() failed : %s", strerror(errno));
+  #endif
 #else
   struct timeval tv;
 
diff --git a/sys_macosx.c b/sys_macosx.c
index 35f03d6..6d9797a 100644
--- a/sys_macosx.c
+++ b/sys_macosx.c
@@ -444,5 +444,25 @@ SYS_MacOSX_Finalise(void)
 }
 
 /* ================================================== */
+#if HAVE_CLOCK_GETTIME
+/* make sure clock_gettime() is a 'lazy' import */
+extern int clock_gettime(clockid_t __clock_id, struct timespec *__tp) __attribute__((weak_import));
+void SYS_MacOSX_ReadRawTime(struct timespec *ts)
+{
+  if (clock_gettime == NULL) {
+    struct timeval tv;
+
+    if (gettimeofday(&tv, NULL) < 0)
+      LOG_FATAL(LOGF_Local, "gettimeofday() failed : %s", strerror(errno));
+
+    UTI_TimevalToTimespec(&tv, ts);
+  } else {
+    if (clock_gettime(CLOCK_REALTIME, ts) < 0)
+      LOG_FATAL(LOGF_Local, "clock_gettime() failed : %s", strerror(errno));
+  }
+}
+#endif
+
+/* ================================================== */
 
 #endif

Bryan Christianson
bryan@xxxxxxxxxxxxx




--
To unsubscribe email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "unsubscribe" in the subject.
For help email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "help" in the subject.
Trouble?  Email listmaster@xxxxxxxxxxxxxxxxxxxx.


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/