From b810e05df0312b0547d9e34689b17b326516aa0d Mon Sep 17 00:00:00 2001
From: Marek Vavrusa <marek@vavrusa.com>
Date: Fri, 3 Feb 2012 11:50:57 +0100
Subject: [PATCH] Bugfixes for OS X.

---
 configure.ac                  |  4 ++--
 src/common/fdset.c            | 25 ++++++++++++++++++-------
 src/common/prng.c             |  4 +++-
 src/knot/server/tcp-handler.c | 20 ++++++++++++++++----
 4 files changed, 39 insertions(+), 14 deletions(-)

diff --git a/configure.ac b/configure.ac
index 0278aca3fb..2f0e1b9d04 100644
--- a/configure.ac
+++ b/configure.ac
@@ -111,7 +111,7 @@ AC_SEARCH_LIBS([pow], [m])
 AC_SEARCH_LIBS([pthread_create], [pthread], [], [AC_MSG_ERROR([pthreads not found])])
 AC_SEARCH_LIBS([rcu_set_pointer_sym], [urcu], [], [AC_MSG_ERROR([liburcu not found])])
 AC_SEARCH_LIBS([dlopen], [dl])
-AC_SEARCH_LIBS([clock_gettime], [rt], [], [AC_MSG_ERROR([librt not found])])
+AC_SEARCH_LIBS([clock_gettime], [rt])
 AC_SEARCH_LIBS([OpenSSL_add_all_digests], [crypto],[], [AC_MSG_ERROR([libcrypto not found])])
 #AC_SEARCH_LIBS([ldns_rr_list_pop_rrset], [ldns], [], [AC_MSG_ERROR([libldns not found])])
 
@@ -137,7 +137,7 @@ AC_DEFINE([DSFMT_MEXP], [521], [DSFMT parameters.])
 # Checks for library functions.
 AC_FUNC_FORK
 AC_FUNC_MMAP
-AC_CHECK_FUNCS([gethostbyname gettimeofday memmove memset munmap regcomp pselect select socket sqrt strcasecmp strchr strdup strerror strncasecmp strtol strtoul poll epoll_wait kqueue setgroups])
+AC_CHECK_FUNCS([gethostbyname gettimeofday clock_gettime memalign memmove memset munmap regcomp pselect select socket sqrt strcasecmp strchr strdup strerror strncasecmp strtol strtoul poll epoll_wait kqueue setgroups])
 
 AC_CONFIG_FILES([Makefile
 		 samples/Makefile
diff --git a/src/common/fdset.c b/src/common/fdset.c
index e9094281ac..8eb99710c9 100644
--- a/src/common/fdset.c
+++ b/src/common/fdset.c
@@ -26,6 +26,17 @@
 #include "common/fdset.h"
 #include <config.h>
 
+/* Workarounds for clock_gettime() not available on some platforms. */
+#ifdef HAVE_CLOCK_GETTIME
+#define time_now(x) clock_gettime(CLOCK_MONOTONIC, (x))
+typedef struct timespec timev_t;
+#elif HAVE_GETTIMEOFDAY
+#define time_now(x) gettimeofday((x), NULL)
+typedef struct timeval timev_t;
+#else
+#error Neither clock_gettime() nor gettimeofday() found. At least one is required.
+#endif
+
 struct fdset_backend_t _fdset_backend = {
 };
 
@@ -142,10 +153,10 @@ int fdset_set_watchdog(fdset_t* fdset, int fd, int interval)
 	}
 	
 	/* Find if exists. */
-	struct timespec *ts = NULL;
-	ts = (struct timespec*)skip_find(base->atimes, (void*)((size_t)fd));
+	timev_t *ts = NULL;
+	ts = (timev_t*)skip_find(base->atimes, (void*)((size_t)fd));
 	if (ts == NULL) {
-		ts = malloc(sizeof(struct timespec));
+		ts = malloc(sizeof(timev_t));
 		if (ts == NULL) {
 			return -1;
 		}
@@ -153,7 +164,7 @@ int fdset_set_watchdog(fdset_t* fdset, int fd, int interval)
 	}
 	
 	/* Update clock. */
-	if (clock_gettime(CLOCK_MONOTONIC, ts) < 0) {
+	if (time_now(ts) < 0) {
 		return -1;
 	}
 	
@@ -169,8 +180,8 @@ int fdset_sweep(fdset_t* fdset, void(*cb)(fdset_t*, int))
 	}
 	
 	/* Get time threshold. */
-	struct timespec now;
-	if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) {
+	timev_t now;
+	if (time_now(&now) < 0) {
 		return -1;
 	}
 	
@@ -181,7 +192,7 @@ int fdset_sweep(fdset_t* fdset, void(*cb)(fdset_t*, int))
 		const skip_node_t* pnext = skip_next(n);
 		
 		/* Evaluate */
-		struct timespec *ts = (struct timespec*)n->value;
+		timev_t *ts = (timev_t*)n->value;
 		if (ts->tv_sec <= now.tv_sec) {
 			cb(fdset, (int)(((ssize_t)n->key)));
 			++sweeped;
diff --git a/src/common/prng.c b/src/common/prng.c
index d1005cd67b..623741ee54 100644
--- a/src/common/prng.c
+++ b/src/common/prng.c
@@ -20,7 +20,9 @@
 #include <pthread.h>
 #include <time.h>
 #include <sys/time.h>
+#ifdef HAVE_MEMALIGN
 #include <malloc.h>
+#endif
 
 #include "prng.h"
 #include "dSFMT.h"
@@ -80,7 +82,7 @@ double tls_rand()
 		}
 
 		/* Initialize PRNG state. */
-#ifdef __APPLE__
+#ifndef HAVE_MEMALIGN
 		s = malloc(sizeof(dsfmt_t));
 #else
 		s = memalign(16, sizeof(dsfmt_t));
diff --git a/src/knot/server/tcp-handler.c b/src/knot/server/tcp-handler.c
index 74e0ab52db..b151e878cd 100644
--- a/src/knot/server/tcp-handler.c
+++ b/src/knot/server/tcp-handler.c
@@ -37,6 +37,18 @@
 #include "knot/other/error.h"
 #include "libknot/util/wire.h"
 
+/* Workarounds for clock_gettime() not available on some platforms. */
+#ifdef HAVE_CLOCK_GETTIME
+#define time_now(x) clock_gettime(CLOCK_MONOTONIC, (x))
+typedef struct timespec timev_t;
+#elif HAVE_GETTIMEOFDAY
+#define time_now(x) gettimeofday((x), NULL)
+typedef struct timeval timev_t;
+#else
+#error Neither clock_gettime() nor gettimeofday() found. At least one is required.
+#endif
+
+
 /* Defines */
 #define TCP_BUFFER_SIZE 65535 /*! Do not change, as it is used for maximum DNS/TCP packet size. */
 
@@ -488,8 +500,8 @@ int tcp_loop_worker(dthread_t *thread)
 	}
 	
 	/* Next sweep time. */
-	struct timespec next_sweep;
-	clock_gettime(CLOCK_MONOTONIC, &next_sweep);
+	timev_t next_sweep;
+	time_now(&next_sweep);
 	next_sweep.tv_sec += TCP_SWEEP_INTERVAL;
 
 	/* Accept clients. */
@@ -550,8 +562,8 @@ int tcp_loop_worker(dthread_t *thread)
 		}
 		
 		/* Sweep inactive. */
-		struct timespec now;
-		if (clock_gettime(CLOCK_MONOTONIC, &now) == 0) {
+		timev_t now;
+		if (time_now(&now) == 0) {
 			if (now.tv_sec >= next_sweep.tv_sec) {
 				fdset_sweep(w->fdset, &tcp_sweep);
 				memcpy(&next_sweep, &now, sizeof(next_sweep));
-- 
GitLab