[ntpsec-main commit] Add OS X Support.

Chris Johns chrisj at ntpsec.org
Sat Nov 14 06:01:14 UTC 2015


Module:    ntpsec-main
Branch:    master
Commit:    ba562708d43692f551146f440ff7385728868c74
Changeset: http://git.ntpsec.org//commit/?id=ba562708d43692f551146f440ff7385728868c74

Author:    Chris Johns <chrisj at ntpsec.org>
Date:      Tue Nov  3 15:01:36 2015 +1100

Add OS X Support.

The support includes threading.

---

 include/ntp_syscall.h  |  4 ++--
 include/ntp_unixtime.h |  8 ++++++++
 include/ntp_worker.h   |  6 ++++++
 libntp/machines.c      |  2 +-
 libntp/work_thread.c   | 23 ++++++++++++++++++++++-
 ntpd/keyword-gen.c     |  1 +
 ntpd/ntp_timer.c       | 16 ++++++++++++++++
 ntpd/ntpd.c            |  2 ++
 ntpdig/main.c          |  1 -
 ntpfrob/jitter.c       |  2 +-
 ntpfrob/main.c         |  3 +++
 ntpkeygen/ntpkeygen.c  |  2 ++
 ntptime/wscript        | 27 ++++++++++++++-------------
 pylib/configure.py     | 18 ++++++++++++++----
 util/sht.c             |  9 +++++++++
 util/wscript           |  5 +++--
 16 files changed, 104 insertions(+), 25 deletions(-)

diff --git a/include/ntp_syscall.h b/include/ntp_syscall.h
index a0d843e..3a82fb7 100644
--- a/include/ntp_syscall.h
+++ b/include/ntp_syscall.h
@@ -16,7 +16,7 @@
 # include <sys/timex.h>
 #endif
 
-#ifndef HAVE_NTP_GETTIME
+#ifndef HAVE_STRUCT_NTPTIMEVAL
 struct ntptimeval
 {
 	struct timeval	time;		/* current time (ro) */
@@ -25,6 +25,6 @@ struct ntptimeval
 };
 
 int ntp_gettime(struct ntptimeval *);
-#endif	/* !HAVE_NTP_GETTIME */
+#endif	/* !HAVE_STRUCT_NTPTIMEVAL */
 
 #endif	/* GUARD_NTP_SYSCALL_H */
diff --git a/include/ntp_unixtime.h b/include/ntp_unixtime.h
index 250a151..0db35c1 100644
--- a/include/ntp_unixtime.h
+++ b/include/ntp_unixtime.h
@@ -20,6 +20,14 @@
 typedef int clockid_t;
 #endif
 
+#ifndef HAVE_CLOCK_GETTIME
+/*
+ * Pacify platforms that don't have a real clock_gettime(2),
+ * notably Mac OS X.
+ */
+int clock_gettime(clockid_t clock_id, struct timespec *tp);
+#endif
+
 /*
  * Time of day conversion constant.  Ntp's time scale starts in 1900,
  * Unix in 1970.  The value is 1970 - 1900 in seconds, 0x83aa7e80 or
diff --git a/include/ntp_worker.h b/include/ntp_worker.h
index 487198e..4f40782 100644
--- a/include/ntp_worker.h
+++ b/include/ntp_worker.h
@@ -43,7 +43,13 @@ typedef struct blocking_pipe_header_tag {
 # ifdef USE_WORK_THREAD
 #  ifdef USE_WORK_PIPE
 typedef pthread_t *	thr_ref;
+#    ifdef __MACH__
+#include <mach/task.h>
+#include <mach/semaphore.h>
+typedef semaphore_t* sem_ref;
+#    else /* !__MACH__ */
 typedef sem_t *		sem_ref;
+#    endif /* !__MACH__ */
 #  else
 typedef HANDLE		thr_ref;
 typedef HANDLE		sem_ref;
diff --git a/libntp/machines.c b/libntp/machines.c
index a7dfbc4..476775b 100644
--- a/libntp/machines.c
+++ b/libntp/machines.c
@@ -26,7 +26,7 @@
 int _getch(void);	/* Declare the one function rather than include conio.h */
 #else
 
-#ifndef HAVE_NTP_GETTIME
+#if !defined(HAVE_NTP_GETTIME) && defined(HAVE_NTP_ADJTIME)
 int ntp_gettime(struct ntptimeval *ntv)
 {
 	struct timex tntx;
diff --git a/libntp/work_thread.c b/libntp/work_thread.c
index 1c12e0d..868396f 100644
--- a/libntp/work_thread.c
+++ b/libntp/work_thread.c
@@ -38,6 +38,28 @@
 #define DEVOLATILE(type, var) ((type)(uintptr_t)(volatile void *)(var))
 #endif
 
+#ifdef __MACH__
+#include <mach/clock.h>
+#include <mach/mach.h>
+#undef sem_init
+#define sem_init(s,p,c) semaphore_create(mach_task_self(),s,SYNC_POLICY_FIFO,c)
+#undef sem_destroy
+#define sem_destroy(s) semaphore_destroy(mach_task_self(),*s)
+#undef sem_wait
+#define sem_wait(s) semaphore_wait(*s)
+#undef sem_post
+#define sem_post(s) semaphore_signal(*s)
+#undef sem_timedwait
+static int sem_timedwait_osx(sem_ref sem, const struct timespec *abs_timeout)
+{
+	mach_timespec_t wait_time;
+	wait_time.tv_sec = abs_timeout->tv_sec;
+	wait_time.tv_nsec = abs_timeout->tv_nsec;
+	return semaphore_timedwait(*sem, wait_time);
+}
+#define sem_timedwait sem_timedwait_osx
+#endif
+
 #ifdef SYS_WINNT
 # define thread_exit(c)	_endthreadex(c)
 # define tickle_sem	SetEvent
@@ -78,7 +100,6 @@ exit_worker(
 	thread_exit(exitcode);	/* see #define thread_exit */
 }
 
-
 int
 worker_sleep(
 	blocking_child *	c,
diff --git a/ntpd/keyword-gen.c b/ntpd/keyword-gen.c
index 0a213f1..c85c979 100644
--- a/ntpd/keyword-gen.c
+++ b/ntpd/keyword-gen.c
@@ -273,6 +273,7 @@ int main(int argc, char **argv)
 		exit(1);
 	}
 	debug = true;
+	init_lib();
 
 	populate_symb(argv[1]);
 
diff --git a/ntpd/ntp_timer.c b/ntpd/ntp_timer.c
index 365bb1a..0e0d64c 100644
--- a/ntpd/ntp_timer.c
+++ b/ntpd/ntp_timer.c
@@ -90,9 +90,14 @@ static	void alarming (int);
 #endif /* SYS_WINNT */
 
 #if !defined SYS_WINNT || defined(SYS_CYGWIN32)
+#  ifdef HAVE_TIMER_CREATE
 static timer_t timer_id;
 typedef struct itimerspec intervaltimer;
 #  define	itv_frac	tv_nsec
+#  else
+typedef struct itimerval intervaltimer;
+#   define	itv_frac	tv_usec
+#  endif
 intervaltimer itimer;
 #endif
 
@@ -110,8 +115,13 @@ set_timer_or_die(
 	const char *	setfunc;
 	int		rc;
 
+#ifdef HAVE_TIMER_CREATE
 	setfunc = "timer_settime";
 	rc = timer_settime(timer_id, 0, &itimer, NULL);
+#else
+	setfunc = "setitimer";
+	rc = setitimer(ITIMER_REAL, &itimer, NULL);
+#endif
 	if (-1 == rc) {
 		msyslog(LOG_ERR, "interval timer %s failed, %m",
 			setfunc);
@@ -129,7 +139,11 @@ reinit_timer(void)
 {
 #if !defined(SYS_WINNT)
 	ZERO(itimer);
+#ifdef HAVE_TIMER_CREATE
 	timer_gettime(timer_id, &itimer);
+#else
+	getitimer(ITIMER_REAL, &itimer);
+#endif
 	if (itimer.it_value.tv_sec < 0 ||
 	    itimer.it_value.tv_sec > (1 << EVENT_TIMEOUT))
 		itimer.it_value.tv_sec = (1 << EVENT_TIMEOUT);
@@ -172,10 +186,12 @@ init_timer(void)
 	 * seconds from now and they continue on every 2**EVENT_TIMEOUT
 	 * seconds.
 	 */
+#ifdef HAVE_TIMER_CREATE
 	if (TC_ERR == timer_create(CLOCK_REALTIME, NULL, &timer_id)) {
 		msyslog(LOG_ERR, "timer_create failed, %m");
 		exit(1);
 	}
+#endif
 	signal_no_reset(SIGALRM, alarming);
 	itimer.it_interval.tv_sec = 
 		itimer.it_value.tv_sec = (1 << EVENT_TIMEOUT);
diff --git a/ntpd/ntpd.c b/ntpd/ntpd.c
index 0c45336..bf79fa9 100644
--- a/ntpd/ntpd.c
+++ b/ntpd/ntpd.c
@@ -494,9 +494,11 @@ set_process_priority(void)
 			else
 				sched.sched_priority = config_priority;
 		}
+#ifdef HAVE_SCHED_SETSCHEDULER
 		if ( sched_setscheduler(0, SCHED_FIFO, &sched) == -1 )
 			msyslog(LOG_ERR, "sched_setscheduler(): %m");
 		else
+#endif
 			priority_done = PRIORITY_OK;
 	}
 # ifdef HAVE_RTPRIO
diff --git a/ntpdig/main.c b/ntpdig/main.c
index 9f7ed5e..82e1691 100644
--- a/ntpdig/main.c
+++ b/ntpdig/main.c
@@ -15,7 +15,6 @@
 #include "log.h"
 #include "libntp.h"
 
-
 bool shutting_down;
 bool time_derived;
 bool time_adjusted;
diff --git a/ntpfrob/jitter.c b/ntpfrob/jitter.c
index c70f2f8..705b2f3 100644
--- a/ntpfrob/jitter.c
+++ b/ntpfrob/jitter.c
@@ -17,12 +17,12 @@
 #include <stdlib.h>
 #include <stdbool.h>
 
+#include "ntp_unixtime.h"
 #include "ntp_fp.h"
 
 #include "ntpfrob.h"
 
 #define NBUF	800002
-#define JAN_1970 2208988800UL		/* Unix base epoch */
 #define NSAMPLES 10
 
 char progname[10];
diff --git a/ntpfrob/main.c b/ntpfrob/main.c
index 597f1df..3d2411d 100644
--- a/ntpfrob/main.c
+++ b/ntpfrob/main.c
@@ -10,11 +10,14 @@
 #include "config.h"
 #include "ntpfrob.h"
 
+void init_lib(void);
+
 int
 main(int argc, char **argv)
 {
 	int ch;
 	iomode mode = plain_text;
+	init_lib();
 	while ((ch = getopt(argc, argv, "a:Acejp:r")) != EOF) {
 		switch (ch) {
 		case 'A':
diff --git a/ntpkeygen/ntpkeygen.c b/ntpkeygen/ntpkeygen.c
index 9ad98a7..032e006 100644
--- a/ntpkeygen/ntpkeygen.c
+++ b/ntpkeygen/ntpkeygen.c
@@ -377,6 +377,8 @@ main(
 
 	progname = argv[0];
 
+	init_lib();
+
 #ifdef SYS_WINNT
 	/* Initialize before OpenSSL checks */
 	InitWin32Sockets();
diff --git a/ntptime/wscript b/ntptime/wscript
index 7b8d3da..5aaa04e 100644
--- a/ntptime/wscript
+++ b/ntptime/wscript
@@ -1,20 +1,21 @@
 from waflib import Utils
 
 def build(ctx):
-	srcnode = ctx.srcnode.abspath()
-	bldnode = ctx.bldnode.abspath()
+	if ctx.env.PLATFORM_TARGET not in ['osx']:
+		srcnode = ctx.srcnode.abspath()
+		bldnode = ctx.bldnode.abspath()
 
-	ctx(
-		target		= "ntptime",
-		features	= "c cprogram bld_include src_include libisc_include",
-		source		= ["ntptime.c"],
-		includes	= [
-					"%s/ntptime/" % bldnode,
-					"%s/ntptime/" % srcnode,
-				],
-		use		= "ntp opts isc M RT PTHREAD",
-		install_path = "${PREFIX}/bin/"
-	)
+		ctx(
+			target		= "ntptime",
+			features	= "c cprogram bld_include src_include libisc_include",
+			source		= ["ntptime.c"],
+			includes	= [
+						"%s/ntptime/" % bldnode,
+						"%s/ntptime/" % srcnode,
+					  ],
+			use		= "ntp opts isc M RT PTHREAD",
+			install_path = "${PREFIX}/bin/"
+		)
 
 	ctx.manpage(8, "ntptime-man.txt")
 
diff --git a/pylib/configure.py b/pylib/configure.py
index 6ca41a0..2ae41a2 100644
--- a/pylib/configure.py
+++ b/pylib/configure.py
@@ -58,17 +58,18 @@ def cmd_configure(ctx):
 
 
 	# XXX: hack
-	if ctx.env.PLATFORM_TARGET == "freebsd":
+	if ctx.env.PLATFORM_TARGET in ["freebsd", "osx"]:
 		ctx.env.PLATFORM_INCLUDES = ["/usr/local/include"]
 		ctx.env.PLATFORM_LIBPATH = ["/usr/local/lib"]
 	elif ctx.env.PLATFORM_TARGET == "netbsd":
 		ctx.env.PLATFORM_LIBPATH = ["/usr/pkg/lib"]
-	elif ctx.env.PLATFORM_TARGET == "osx":
-		ctx.env.PLATFORM_INCLUDES = ["/opt/local/include"]
-		ctx.env.PLATFORM_LIBPATH = ["/opt/local/lib"]
 	elif ctx.env.PLATFORM_TARGET == "win":
 		ctx.load("msvc")
 
+	# OS X needs this for IPV6
+	if ctx.env.PLATFORM_TARGET == "osx":
+		ctx.define("__APPLE_USE_RFC_3542", 1)
+
 	# Wipe out and override flags with those from the commandline
 	for flag in ctx.env.OPT_STORE:
 		opt = flag.replace("--", "").upper() # XXX: find a better way.
@@ -130,6 +131,13 @@ def cmd_configure(ctx):
 	for (f, h) in net_types:
 		ctx.check_type(f, h)
 
+	structures = (
+		("struct timex", "sys/timex.h"),
+		("struct ntptimeval", "sys/timex.h"),
+		)
+	for (s, h) in structures:
+		ctx.check_type(s, h)
+
 	structure_fields = (
 		("time_tick", "timex", ["sys/time.h", "sys/timex.h"]),
 		("modes", "timex", ["sys/time.h", "sys/timex.h"]),
@@ -205,11 +213,13 @@ def cmd_configure(ctx):
 		('pthread_attr_setstacksize', ["pthread.h"], "PTHREAD"),
 		('res_init', ["resolv.h"]),
 		("rtprio", ["sys/rtprio.h"]),		# Sun/BSD
+		('sched_setscheduler', ["sched.h"]),
 		('settimeofday', ["sys/time.h"], "RT"),	# BSD - remove?
 		('strlcpy', ["string.h"]),
 		('strlcat', ["string.h"]),
 		('sysconf', ["unistd.h"]),
 		('timegm', ["time.h"]),
+		('timer_create', ["time.h"]),
 		('updwtmpx', ["utmpx.h"]),		# glibc
 		)
 	for ft in functions:
diff --git a/util/sht.c b/util/sht.c
index f2ba41b..dee409c 100644
--- a/util/sht.c
+++ b/util/sht.c
@@ -22,6 +22,11 @@
 #endif
 #include <assert.h>
 
+#include "ntp_stdlib.h"
+#include "ntp_unixtime.h"
+
+char	*progname;
+
 struct shmTime {
 	int    mode; /* 0 - if valid set
 		      *       use values, 
@@ -120,6 +125,10 @@ main (
 	int unit;
 	char *argp;
 
+	progname = argv[0];
+
+	init_lib();
+
 	if (argc<=1) {
 	  usage:
 		printf ("usage: %s [uu:]{r[c][l]|w|snnn}\n",argv[0]);
diff --git a/util/wscript b/util/wscript
index 901f703..96461c8 100644
--- a/util/wscript
+++ b/util/wscript
@@ -5,8 +5,9 @@ def build(ctx):
 	bldnode = ctx.bldnode.abspath()
 
 	util = ['hist', 'propdelay', 'sht']
-	if ctx.env.PLATFORM_TARGET != "netbsd":
-		util.append('tg2')
+
+	if ctx.env.PLATFORM_TARGET not in ['netbsd', 'osx']:
+		util += ['tg2']
 
 	for name in util:
 		ctx(



More information about the vc mailing list