From 207a65ca0bea937639758b307e47ae3404c02eac Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Mon, 19 Jan 2015 18:23:57 -0600 Subject: [PATCH] Add NetBSD support. Fixes #3, thanks to @gitisihara for providing the initial patch and testing. --- .gitignore | 2 ++ ChangeLog | 13 +++++---- Makefile.am | 7 +++++ README | 1 + compat/adjfreq_netbsd.c | 60 +++++++++++++++++++++++++++++++++++++++++ compat/arc4random.h | 3 +++ configure.ac | 14 ++++++++-- 7 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 compat/adjfreq_netbsd.c diff --git a/.gitignore b/.gitignore index 6f5230e..e48d0d3 100644 --- a/.gitignore +++ b/.gitignore @@ -37,6 +37,7 @@ compat/arc4random.c compat/arc4random_freebsd.h compat/arc4random_hpux.h compat/arc4random_linux.h +compat/arc4random_netbsd.h compat/arc4random_osx.h compat/arc4random_solaris.h compat/arc4random_uniform.c @@ -46,6 +47,7 @@ compat/explicit_bzero.c compat/getentropy_freebsd.c compat/getentropy_hpux.c compat/getentropy_linux.c +compat/getentropy_netbsd.c compat/getentropy_osx.c compat/getentropy_solaris.c compat/getentropy_win.c diff --git a/ChangeLog b/ChangeLog index 7df41dd..3a7ef1e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -10,18 +10,21 @@ the GitHub mirror. * Added support for setting the process title on Linux and OS X. The different processes are now possible to tell apart by role in the process list. + * Import NetBSD support. * Various bugfixes and refinements from the community. 2015-01-08 OpenNTPD 5.7p1 - * Support for a new build infrastructure based on the LibreSSL framework. - Source code is integrated directly from the OpenBSD tree with few manual - changes, easing maintenance. + * Support for a new build infrastructure based on the LibreSSL + framework. Source code is integrated directly from the OpenBSD tree + with few manual changes, easing maintenance. * Removed support for several OSes pending test reports and updated portability code. - * Supports the Simple Network Time Protocol version 4 as described in RFC 5905 + * Supports the Simple Network Time Protocol version 4 as described in + RFC 5905 * Added route virtualization (rdomain) support. * Added ntpctl(8), which allows for querying ntpd(8) at runtime. - * Finer-grained clock adjustment via adjfreq / ntp_adjtime where available. + * Finer-grained clock adjustment via adjfreq / ntp_adjtime where + available. * Improved latency on heavily-loaded machines. diff --git a/Makefile.am b/Makefile.am index 1dc48c8..353e3bb 100644 --- a/Makefile.am +++ b/Makefile.am @@ -125,6 +125,9 @@ endif if HOST_LINUX libcompat_la_SOURCES += compat/adjfreq_linux.c endif +if HOST_NETBSD +libcompat_la_SOURCES += compat/adjfreq_netbsd.c +endif if HOST_DARWIN libcompat_la_SOURCES += compat/adjfreq_osx.c endif @@ -197,6 +200,9 @@ if !HAVE_SHA512 libcompat_la_SOURCES += compat/sha2.c endif endif +if HOST_NETBSD +libcompat_la_SOURCES += compat/getentropy_netbsd.c +endif if HOST_DARWIN libcompat_la_SOURCES += compat/getentropy_osx.c if !HAVE_SHA512 @@ -219,6 +225,7 @@ endif noinst_HEADERS = compat/arc4random.h noinst_HEADERS += compat/arc4random_freebsd.h noinst_HEADERS += compat/arc4random_linux.h +noinst_HEADERS += compat/arc4random_netbsd.h noinst_HEADERS += compat/arc4random_osx.h noinst_HEADERS += compat/arc4random_solaris.h noinst_HEADERS += compat/arc4random_win.h diff --git a/README b/README index 7fa1a2d..af37e34 100644 --- a/README +++ b/README @@ -19,6 +19,7 @@ At the time of writing the Portable version is known to build and work on: - OpenBSD (5.6) - Linux (Ubuntu 12.04, 14.04) - FreeBSD (9.x, 10.x) + - NetBSD (6.1.5) - Solaris (10.x, 11.x) - Mac OS X (10.9) diff --git a/compat/adjfreq_netbsd.c b/compat/adjfreq_netbsd.c new file mode 100644 index 0000000..71d3840 --- /dev/null +++ b/compat/adjfreq_netbsd.c @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2007 Sebastian Benoit + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include + +#include + +#include "ntp.h" +#include "ntpd.h" + +/* + * adjfreq (old)freq = nanosec. per seconds shifted left 32 bits + * timex.freq is ppm / left shifted by SHIFT_USEC (16 bits), defined in timex.h + */ + +int +adjfreq(const int64_t *freq, int64_t *oldfreq) +{ + struct timex txc; + int64_t newfreq; + + if (freq != NULL) { + txc.modes = MOD_FREQUENCY; + txc.freq = *freq / 1e3 / (1LL << 16); + + if ((ntp_adjtime(&txc)) == -1) + log_warn("ntp_adjtime (2) failed"); + + log_debug("ntp_adjtime adjusted frequency by %fppm", + ((txc.freq * 1e3) * (1LL<<16) / 1e3 / (1LL << 32))); + } + if (oldfreq != NULL) { + txc.modes = 0; + if ((ntp_adjtime(&txc)) == -1) { + log_warn("ntp_adjtime (1) failed"); + return -1; + } + newfreq = (txc.freq * 1e3) * (1LL<<16); + log_debug("ntp_adjtime returns frequency of %fppm", + newfreq / 1e3 / (1LL << 32)); + *oldfreq = newfreq; + } + + return 0; +} diff --git a/compat/arc4random.h b/compat/arc4random.h index 53b5a46..7ceac55 100644 --- a/compat/arc4random.h +++ b/compat/arc4random.h @@ -9,6 +9,9 @@ #elif defined(__linux__) #include "arc4random_linux.h" +#elif defined(__NetBSD__) +#include "arc4random_netbsd.h" + #elif defined(__APPLE__) #include "arc4random_osx.h" diff --git a/configure.ac b/configure.ac index 16acbe1..eb1028e 100644 --- a/configure.ac +++ b/configure.ac @@ -46,6 +46,9 @@ case $host_os in CFLAGS="$CFLAGS -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_POSIX_SOURCE -D_GNU_SOURCE" AC_DEFINE([SPT_TYPE], [SPT_REUSEARGV]) ;; + *netbsd*) + HOST_OS=netbsd + ;; *openbsd*) AC_DEFINE([HAVE_ATTRIBUTE__BOUNDED__], [1], [OpenBSD has __bounded__]) AC_DEFINE([HAVE_ATTRIBUTE__DEAD], [1], [OpenBSD has __dead]) @@ -61,6 +64,7 @@ esac AM_CONDITIONAL([HOST_DARWIN], [test x$HOST_OS = xdarwin]) AM_CONDITIONAL([HOST_FREEBSD], [test x$HOST_OS = xfreebsd]) AM_CONDITIONAL([HOST_LINUX], [test x$HOST_OS = xlinux]) +AM_CONDITIONAL([HOST_NETBSD], [test x$HOST_OS = xnetbsd]) AM_CONDITIONAL([HOST_SOLARIS], [test x$HOST_OS = xsolaris]) AC_CHECK_FUNC([dl_iterate_phdr],, @@ -139,9 +143,15 @@ AM_CONDITIONAL([HAVE_STRTONUM], [test "x$ac_cv_func_strtonum" = xyes]) # overrides for arc4random implementations with known issues AM_CONDITIONAL([HAVE_ARC4RANDOM], - [test "x$HOST_OS" != xdarwin -a "x$HOST_OS" != xfreebsd -a "x$ac_cv_func_arc4random" = xyes]) + [test "x$HOST_OS" != xdarwin \ + -a "x$HOST_OS" != xfreebsd \ + -a "x$HOST_OS" != xnetbsd \ + -a "x$ac_cv_func_arc4random" = xyes]) AM_CONDITIONAL([HAVE_ARC4RANDOM_UNIFORM], - [test "x$HOST_OS" != xdarwin -a "x$HOST_OS" != xfreebsd -a "x$ac_cv_func_arc4random_uniform" = xyes]) + [test "x$HOST_OS" != xdarwin \ + -a "x$HOST_OS" != xfreebsd \ + -a "x$HOST_OS" != xnetbsd \ + -a "x$ac_cv_func_arc4random_uniform" = xyes]) AC_CACHE_CHECK([whether va_copy exists], ac_cv_have_va_copy, [ AC_LINK_IFELSE([AC_LANG_PROGRAM([[