[chrony-dev] [PATCH] macOS - weak linkage of clock_gettime (fixed missing include)

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


Earlier versions of macOS do not provide clock_gettime(). This patch checks for clock_gettime() at run-time and falls back to gettimeofday() if the symbol is not present.
---
 local.c      |  8 ++++++++
 sys_macosx.c | 25 +++++++++++++++++++++++++
 sys_macosx.h |  1 +
 3 files changed, 34 insertions(+)

diff --git a/local.c b/local.c
index b4baaac..d54b69b 100644
--- a/local.c
+++ b/local.c
@@ -40,6 +40,10 @@
 #include "util.h"
 #include "logging.h"
 
+#ifdef MACOSX
+#include "sys_macosx.h"
+#endif
+
 /* ================================================== */
 
 /* Variable to store the current frequency, in ppm */
@@ -357,6 +361,10 @@ void
 LCL_ReadRawTime(struct timespec *ts)
 {
 #if HAVE_CLOCK_GETTIME
+#ifdef MACOSX
+  SYS_MacOSX_GetTime(CLOCK_REALTIME, ts);
+  return;
+#endif
   if (clock_gettime(CLOCK_REALTIME, ts) < 0)
     LOG_FATAL("clock_gettime() failed : %s", strerror(errno));
 #else
diff --git a/sys_macosx.c b/sys_macosx.c
index 807d62e..717ff5d 100644
--- a/sys_macosx.c
+++ b/sys_macosx.c
@@ -451,6 +451,31 @@ legacy_MacOSX_Finalise(void)
 
 /* ================================================== */
 
+void
+SYS_MacOSX_GetTime(clockid_t clock_id, struct timespec *ts)
+{
+  static int init = 0;
+  static int (*sys_clock_gettime)(clockid_t, struct timespec *) = NULL;
+
+  if (!init) {
+    sys_clock_gettime = dlsym(RTLD_NEXT, "clock_gettime");
+    init = 1;
+  }
+  if (sys_clock_gettime != NULL) {
+    if (sys_clock_gettime(CLOCK_REALTIME, ts) < 0)
+      LOG_FATAL("clock_gettime() failed : %s", strerror(errno));
+    } else {
+      struct timeval tv;
+
+    if (gettimeofday(&tv, NULL) < 0)
+      LOG_FATAL("gettimeofday() failed : %s", strerror(errno));
+
+      UTI_TimevalToTimespec(&tv, ts);
+  }
+}
+
+/* ================================================== */
+
 void
 SYS_MacOSX_Initialise(void)
 {
diff --git a/sys_macosx.h b/sys_macosx.h
index 5555616..c3c44b2 100644
--- a/sys_macosx.h
+++ b/sys_macosx.h
@@ -34,5 +34,6 @@ void SYS_MacOSX_SetScheduler(int SchedPriority);
 void SYS_MacOSX_DropRoot(uid_t uid, gid_t gid);
 void SYS_MacOSX_Initialise(void);
 void SYS_MacOSX_Finalise(void);
+void SYS_MacOSX_GetTime(clockid_t clock_id, struct timespec *ts);
 
 #endif
-- 
2.21.1 (Apple Git-122.3)


-- 
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/