From 04f3b3eeee7c2d9a85064700f0b6c18abee27be7 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Thu, 1 Jan 2015 06:32:40 -0600 Subject: [PATCH] use per-OS files for adjfreq emulation This makes it easier to see what is supported by each OS, and to improve them individually. --- Makefile.am | 61 ++++++++++++--------- compat/{bsd-adjfreq.c => adjfreq_freebsd.c} | 33 +---------- compat/adjfreq_linux.c | 59 ++++++++++++++++++++ compat/adjfreq_osx.c | 19 +++++++ compat/adjfreq_solaris.c | 59 ++++++++++++++++++++ configure.ac | 2 +- 6 files changed, 175 insertions(+), 58 deletions(-) rename compat/{bsd-adjfreq.c => adjfreq_freebsd.c} (80%) create mode 100644 compat/adjfreq_linux.c create mode 100644 compat/adjfreq_osx.c create mode 100644 compat/adjfreq_solaris.c diff --git a/Makefile.am b/Makefile.am index fc7e426..b174056 100644 --- a/Makefile.am +++ b/Makefile.am @@ -49,32 +49,33 @@ libcompat_la_CFLAGS = $(CFLAGS) $(USER_CFLAGS) libcompat_la_SOURCES = libcompat_la_LIBADD = $(PLATFORM_LDADD) -if !HAVE_SETPROCTITLE -libcompat_la_SOURCES += compat/setproctitle.c +if !HAVE_ADJFREQ +if HOST_FREEBSD +libcompat_la_SOURCES += compat/adjfreq_freebsd.c endif - -if !HAVE_STRLCAT -libcompat_la_SOURCES += compat/strlcat.c +if HOST_LINUX +libcompat_la_SOURCES += compat/adjfreq_linux.c endif - -if !HAVE_STRLCPY -libcompat_la_SOURCES += compat/strlcpy.c +if HOST_DARWIN +libcompat_la_SOURCES += compat/adjfreq_osx.c +endif +if HOST_SOLARIS +libcompat_la_SOURCES += compat/adjfreq_solaris.c endif - -if !HAVE_STRTONUM -libcompat_la_SOURCES += compat/strtonum.c endif if !HAVE_ASPRINTF libcompat_la_SOURCES += compat/bsd-asprintf.c endif -if !HAVE_REALLOCARRAY -libcompat_la_SOURCES += compat/reallocarray.c +if !HAVE_CLOCK_GETRES +libcompat_la_SOURCES += compat/clock_getres.c endif -if !HAVE_MD5 -libcompat_la_SOURCES += compat/md5.c +if !HAVE_CLOCK_GETTIME +if HOST_DARWIN +libcompat_la_SOURCES += compat/mach-clock_gettime.c +endif endif if !HAVE_IMSG @@ -82,12 +83,28 @@ libcompat_la_SOURCES += compat/imsg.c libcompat_la_SOURCES += compat/imsg-buffer.c endif -if !HAVE_ADJFREQ -libcompat_la_SOURCES += compat/bsd-adjfreq.c +if !HAVE_MD5 +libcompat_la_SOURCES += compat/md5.c endif -if !HAVE_CLOCK_GETRES -libcompat_la_SOURCES += compat/clock_getres.c +if !HAVE_REALLOCARRAY +libcompat_la_SOURCES += compat/reallocarray.c +endif + +if !HAVE_SETPROCTITLE +libcompat_la_SOURCES += compat/setproctitle.c +endif + +if !HAVE_STRLCAT +libcompat_la_SOURCES += compat/strlcat.c +endif + +if !HAVE_STRLCPY +libcompat_la_SOURCES += compat/strlcpy.c +endif + +if !HAVE_STRTONUM +libcompat_la_SOURCES += compat/strtonum.c endif if !HAVE_SETRESGID @@ -98,12 +115,6 @@ if !HAVE_SETRESUID libcompat_la_SOURCES += compat/bsd-setresuid.c endif -if !HAVE_CLOCK_GETTIME -if HOST_DARWIN -libcompat_la_SOURCES += compat/mach-clock_gettime.c -endif -endif - if !HAVE_ARC4RANDOM libcompat_la_SOURCES += compat/arc4random.c diff --git a/compat/bsd-adjfreq.c b/compat/adjfreq_freebsd.c similarity index 80% rename from compat/bsd-adjfreq.c rename to compat/adjfreq_freebsd.c index b2f5d1f..562464c 100644 --- a/compat/bsd-adjfreq.c +++ b/compat/adjfreq_freebsd.c @@ -14,39 +14,14 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -/* RCSID("$Id$"); */ - #include -#include - -#ifdef HAVE_SYS_TIMEX_H # include -#endif -#ifdef adjfreq -# undef adjfreq -#endif +#include #include "ntp.h" #include "ntpd.h" -#ifndef NTP_ADJTIME - -#include - -/* - * Some sad but very popular platforms do not appear to provide a mechanism to - * adjust the time frequency at all! - */ -int -adjfreq(const int64_t *freq, int64_t *oldfreq) -{ - errno = ENOSYS; - return -1; -} - -#else - /* * 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 @@ -59,11 +34,7 @@ adjfreq(const int64_t *freq, int64_t *oldfreq) int64_t newfreq; if (freq != NULL) { -#if defined(__linux__) - txc.modes = ADJ_FREQUENCY; -#else txc.modes = MOD_FREQUENCY; -#endif txc.freq = *freq / 1e3 / (1LL << 16); if ((ntp_adjtime(&txc)) == -1) @@ -86,5 +57,3 @@ adjfreq(const int64_t *freq, int64_t *oldfreq) return 0; } - -#endif diff --git a/compat/adjfreq_linux.c b/compat/adjfreq_linux.c new file mode 100644 index 0000000..21ee40f --- /dev/null +++ b/compat/adjfreq_linux.c @@ -0,0 +1,59 @@ +/* + * 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 "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 = ADJ_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/adjfreq_osx.c b/compat/adjfreq_osx.c new file mode 100644 index 0000000..f8f0be4 --- /dev/null +++ b/compat/adjfreq_osx.c @@ -0,0 +1,19 @@ +/* + * This file is in the public domain. + * + * OS X does not appear to provide a mechanism to adjust the time frequency, or + * at least not one that is easy to discover. Always fail here until a suitable + * implementation is found. + */ + +#include +#include + +#include + +int +adjfreq(const int64_t *freq, int64_t *oldfreq) +{ + errno = ENOSYS; + return -1; +} diff --git a/compat/adjfreq_solaris.c b/compat/adjfreq_solaris.c new file mode 100644 index 0000000..562464c --- /dev/null +++ b/compat/adjfreq_solaris.c @@ -0,0 +1,59 @@ +/* + * 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 "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/configure.ac b/configure.ac index 26557cc..71229ec 100644 --- a/configure.ac +++ b/configure.ac @@ -145,7 +145,7 @@ if test "x$ac_cv_have___va_copy" = "xyes" ; then AC_DEFINE([HAVE___VA_COPY], [1], [Define if __va_copy exists]) fi -AC_CHECK_HEADERS([sys/timex.h md5.h sha2.h]) +AC_CHECK_HEADERS([md5.h sha2.h]) AC_CHECK_HEADERS([sys/sensors.h], AM_CONDITIONAL(HAVE_SENSORS, true), AM_CONDITIONAL(HAVE_SENSORS, false)