]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
sys_macosx: don't require clock_gettime()
authorBryan Christianson <bryan@whatroute.net>
Thu, 12 Mar 2020 19:33:00 +0000 (08:33 +1300)
committerMiroslav Lichvar <mlichvar@redhat.com>
Mon, 16 Mar 2020 10:35:56 +0000 (11:35 +0100)
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.

sys_macosx.c

index 807d62ee3e786d8fb4524a0645e56a82875c31ac..701c078f286861d7a4cdd2e338e5c6218a4283e6 100644 (file)
@@ -451,6 +451,39 @@ legacy_MacOSX_Finalise(void)
 
 /* ================================================== */
 
+#if HAVE_CLOCK_GETTIME
+int
+clock_gettime(clockid_t clock_id, struct timespec *ts)
+{
+  /* Check that the system clock_gettime symbol is actually present before
+     attempting to call it. The symbol is available in macOS 10.12
+     and later. */
+
+  static int init = 0;
+  static int (*sys_clock_gettime)(clockid_t, struct timespec *) = NULL;
+  int ret = 0;
+
+  if (!init) {
+    sys_clock_gettime = dlsym(RTLD_NEXT, "clock_gettime");
+    init = 1;
+  }
+
+  if (sys_clock_gettime != NULL) {
+    ret = sys_clock_gettime(clock_id, ts);
+  } else {
+    struct timeval tv;
+
+    if (gettimeofday(&tv, NULL) < 0)
+      LOG_FATAL("gettimeofday() failed : %s", strerror(errno));
+
+    UTI_TimevalToTimespec(&tv, ts);
+  }
+  return ret;
+}
+#endif
+
+/* ================================================== */
+
 void
 SYS_MacOSX_Initialise(void)
 {