diff --git a/configure.ac b/configure.ac index 7ad5908..4237714 100644 --- a/configure.ac +++ b/configure.ac @@ -58,6 +58,12 @@ AC_SUBST(LTLDFLAGS_OSMOCTRL) dnl checks for header files AC_HEADER_STDC AC_CHECK_HEADERS(execinfo.h sys/select.h sys/socket.h sys/timerfd.h syslog.h ctype.h netinet/tcp.h netinet/in.h) + +dnl check for Mach / Darwin / Mac OS X / macOS specifics +dnl used in src/timer_clockgettime.c +AC_CHECK_HEADERS(mach/clock.h mach/mach.h) +AC_CHECK_TYPES([clockid_t], [], [], [[#include ]]) + # for src/conv.c AC_FUNC_ALLOCA AC_SEARCH_LIBS([dlopen], [dl dld], [LIBRARY_DLOPEN="$LIBS";LIBS=""]) @@ -129,7 +135,7 @@ ARG_ENABLE_DETECT_TLS_GCC_ARM_BUG dnl Generate the output -AC_CONFIG_HEADER(config.h) +AC_CONFIG_HEADER(include/osmocom/config.h) PKG_CHECK_MODULES(TALLOC, [talloc >= 2.0.1]) diff --git a/include/Makefile.am b/include/Makefile.am index a82d6ac..fea6446 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -1,6 +1,7 @@ BUILT_SOURCES = osmocom/gsm/gsm0503.h nobase_include_HEADERS = \ + osmocom/config.h \ osmocom/codec/ecu.h \ osmocom/codec/codec.h \ osmocom/codec/gsm610_bits.h \ diff --git a/include/osmocom/core/timer.h b/include/osmocom/core/timer.h index 1979766..88c750d 100644 --- a/include/osmocom/core/timer.h +++ b/include/osmocom/core/timer.h @@ -46,6 +46,12 @@ #include #include #include +#include + +/* define this only if not already */ +#ifndef HAVE_CLOCKID_T +typedef int clockid_t; +#endif /* convert absolute time (in seconds) to elapsed days/hours/minutes */ #define OSMO_SEC2MIN(sec) ((sec % (60 * 60)) / 60) diff --git a/src/backtrace.c b/src/backtrace.c index a18bde0..76d2fcc 100644 --- a/src/backtrace.c +++ b/src/backtrace.c @@ -28,7 +28,7 @@ #include #include #include -#include "config.h" +#include #ifdef HAVE_EXECINFO_H #include diff --git a/src/conv.c b/src/conv.c index a2c13de..7f16453 100644 --- a/src/conv.c +++ b/src/conv.c @@ -28,7 +28,7 @@ * * \file conv.c */ -#include "config.h" +#include #ifdef HAVE_ALLOCA_H #include #endif diff --git a/src/conv_acc.c b/src/conv_acc.c index c16e436..0b46572 100644 --- a/src/conv_acc.c +++ b/src/conv_acc.c @@ -26,7 +26,7 @@ #include #include -#include "config.h" +#include #include diff --git a/src/conv_acc_sse.c b/src/conv_acc_sse.c index 63d8722..a42f8f7 100644 --- a/src/conv_acc_sse.c +++ b/src/conv_acc_sse.c @@ -24,7 +24,7 @@ */ #include -#include "config.h" +#include #include #include diff --git a/src/conv_acc_sse_avx.c b/src/conv_acc_sse_avx.c index 5ac3c16..22a1fe0 100644 --- a/src/conv_acc_sse_avx.c +++ b/src/conv_acc_sse_avx.c @@ -24,7 +24,7 @@ */ #include -#include "config.h" +#include #include #include diff --git a/src/ctrl/control_if.c b/src/ctrl/control_if.c index ce2e367..5d17959 100644 --- a/src/ctrl/control_if.c +++ b/src/ctrl/control_if.c @@ -26,7 +26,7 @@ * */ -#include "config.h" +#include #include #include diff --git a/src/gsm/auth_core.c b/src/gsm/auth_core.c index 9e750a0..0e4644d 100644 --- a/src/gsm/auth_core.c +++ b/src/gsm/auth_core.c @@ -20,7 +20,7 @@ * */ -#include "config.h" +#include #include #include diff --git a/src/gsm/gprs_cipher_core.c b/src/gsm/gprs_cipher_core.c index 7f2b1a5..11daf0a 100644 --- a/src/gsm/gprs_cipher_core.c +++ b/src/gsm/gprs_cipher_core.c @@ -23,7 +23,7 @@ * */ -#include "config.h" +#include #include #include diff --git a/src/gsm/gsm0411_utils.c b/src/gsm/gsm0411_utils.c index ccefe54..b773032 100644 --- a/src/gsm/gsm0411_utils.c +++ b/src/gsm/gsm0411_utils.c @@ -27,7 +27,7 @@ * */ -#include "../../config.h" +#include #include #include diff --git a/src/gsm/gsm0808_utils.c b/src/gsm/gsm0808_utils.c index 364a04f..4c6f8b7 100644 --- a/src/gsm/gsm0808_utils.c +++ b/src/gsm/gsm0808_utils.c @@ -19,7 +19,7 @@ * */ -#include "config.h" +#include #include #include diff --git a/src/gsm/gsm_utils.c b/src/gsm/gsm_utils.c index 365920f..51eae89 100644 --- a/src/gsm/gsm_utils.c +++ b/src/gsm/gsm_utils.c @@ -96,7 +96,7 @@ #include #include -#include "../../config.h" +#include #if (!EMBEDDED) /* FIXME: this can be removed once we bump glibc requirements to 2.25: */ diff --git a/src/gsm/ipa.c b/src/gsm/ipa.c index 1563d0a..585a217 100644 --- a/src/gsm/ipa.c +++ b/src/gsm/ipa.c @@ -24,7 +24,7 @@ * */ -#include "config.h" +#include #include #include diff --git a/src/gsm/mncc.c b/src/gsm/mncc.c index 938cf9a..417f332 100644 --- a/src/gsm/mncc.c +++ b/src/gsm/mncc.c @@ -20,7 +20,7 @@ * */ -#include "../config.h" +#include #ifdef HAVE_SYS_SOCKET_H diff --git a/src/gsmtap_util.c b/src/gsmtap_util.c index 2fb18a4..eace8e9 100644 --- a/src/gsmtap_util.c +++ b/src/gsmtap_util.c @@ -23,7 +23,7 @@ * */ -#include "../config.h" +#include #include #include diff --git a/src/logging.c b/src/logging.c index 1c3544f..946a4d7 100644 --- a/src/logging.c +++ b/src/logging.c @@ -29,7 +29,7 @@ * * \file logging.c */ -#include "../config.h" +#include #include #include diff --git a/src/logging_gsmtap.c b/src/logging_gsmtap.c index bd64271..70c40f9 100644 --- a/src/logging_gsmtap.c +++ b/src/logging_gsmtap.c @@ -31,7 +31,7 @@ * @{ * \file logging_gsmtap.c */ -#include "../config.h" +#include #include #include diff --git a/src/logging_syslog.c b/src/logging_syslog.c index f980689..9d9c254 100644 --- a/src/logging_syslog.c +++ b/src/logging_syslog.c @@ -26,7 +26,7 @@ * @{ * \file logging_syslog.c */ -#include "../config.h" +#include #ifdef HAVE_SYSLOG_H diff --git a/src/macaddr.c b/src/macaddr.c index de9d07a..83b1c2b 100644 --- a/src/macaddr.c +++ b/src/macaddr.c @@ -28,7 +28,7 @@ * @{ * \file macaddr.c */ -#include "config.h" +#include #include #include diff --git a/src/panic.c b/src/panic.c index 072f458..716bee7 100644 --- a/src/panic.c +++ b/src/panic.c @@ -31,7 +31,7 @@ #include #include -#include "../config.h" +#include static osmo_panic_handler_t osmo_panic_handler = (void*)0; diff --git a/src/plugin.c b/src/plugin.c index 40de4f8..071061e 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -27,7 +27,7 @@ * @{ * \file plugin.c */ -#include "../config.h" +#include #if HAVE_DLFCN_H diff --git a/src/select.c b/src/select.c index 7ce135f..3f368f1 100644 --- a/src/select.c +++ b/src/select.c @@ -39,7 +39,7 @@ #include #include -#include "../config.h" +#include #ifdef HAVE_SYS_SELECT_H #include diff --git a/src/sercomm.c b/src/sercomm.c index 2639bf8..15d2eed 100644 --- a/src/sercomm.c +++ b/src/sercomm.c @@ -26,7 +26,7 @@ * * \file sercomm.c */ -#include "config.h" +#include #include #include diff --git a/src/socket.c b/src/socket.c index c50af55..5779bd3 100644 --- a/src/socket.c +++ b/src/socket.c @@ -21,7 +21,7 @@ * */ -#include "../config.h" +#include /*! \addtogroup socket * @{ diff --git a/src/stats.c b/src/stats.c index b5adbf2..84fd626 100644 --- a/src/stats.c +++ b/src/stats.c @@ -62,7 +62,7 @@ * * \file stats.c */ -#include "config.h" +#include #if !defined(EMBEDDED) #include diff --git a/src/stats_statsd.c b/src/stats_statsd.c index c3f739e..db6f76b 100644 --- a/src/stats_statsd.c +++ b/src/stats_statsd.c @@ -25,7 +25,7 @@ * @{ * \file stats_statsd.c */ -#include "config.h" +#include #if !defined(EMBEDDED) #include diff --git a/src/timer_clockgettime.c b/src/timer_clockgettime.c index 7b17fd1..e333fd0 100644 --- a/src/timer_clockgettime.c +++ b/src/timer_clockgettime.c @@ -52,8 +52,100 @@ #include #include +#include #include +/* Hack to allow fakeclock to work on Mach / Darwin / Mac OS X */ + +#if defined(HAVE_MACH_CLOCK_H) && defined(HAVE_MACH_MACH_H) + +#include +#include +#include + +/* + * the following defines are ignored in the replacement + * "clock_gettime" function, but must be defined to unique values that + * are in the clockid_t enum from + */ + +#ifdef CLOCK_REALTIME +#undef CLOCK_REALTIME +#endif +#define CLOCK_REALTIME 0 + +#ifdef CLOCK_REALTIME_COARSE +#undef CLOCK_REALTIME_COARSE +#endif +#define CLOCK_REALTIME_COARSE 9 + +#ifdef CLOCK_MONOTONIC +#undef CLOCK_MONOTONIC +#endif +#define CLOCK_MONOTONIC 6 + +#ifdef CLOCK_MONOTONIC_COARSE +#undef CLOCK_MONOTONIC_COARSE +#endif +#define CLOCK_MONOTONIC_COARSE 5 + +#ifdef CLOCK_MONOTONIC_RAW +#undef CLOCK_MONOTONIC_RAW +#endif +#define CLOCK_MONOTONIC_RAW 4 + +#ifdef CLOCK_BOOTTIME +#undef CLOCK_BOOTTIME +#endif +#define CLOCK_BOOTTIME 8 + +#ifdef CLOCK_PROCESS_CPUTIME_ID +#undef CLOCK_PROCESS_CPUTIME_ID +#endif +#define CLOCK_PROCESS_CPUTIME_ID 12 + +#ifdef CLOCK_THREAD_CPUTIME_ID +#undef CLOCK_THREAD_CPUTIME_ID +#endif +#define CLOCK_THREAD_CPUTIME_ID 16 + +static int mach_clock_gettime (clockid_t clk_id, struct timespec* tp) +{ + kern_return_t ret; + clock_serv_t cclock; + mach_timespec_t mts; + + ret = host_get_clock_service (mach_host_self (), CALENDAR_CLOCK, &cclock); + if (ret != 0) { + errno = EINVAL; + return -1; + } + + ret = clock_get_time (cclock, &mts); + if (ret != 0) { + goto clock_gettime_out; + } + + tp->tv_sec = mts.tv_sec; + tp->tv_nsec = mts.tv_nsec; + +clock_gettime_out: + if (mach_port_deallocate (mach_task_self (), cclock) != 0 || ret != 0) { + errno = EINVAL; + return -1; + } else { + return 0; + } +} + +#define USE_CLOCK_GETTIME mach_clock_gettime + +#else + +#define USE_CLOCK_GETTIME clock_gettime + +#endif + /*! An internal structure to handle overriden time for each clock type. */ struct fakeclock { bool override; @@ -102,7 +194,7 @@ int osmo_clock_gettime(clockid_t clk_id, struct timespec *tp) { struct fakeclock* c = clkid_to_fakeclock(clk_id); if (!c || !c->override) - return clock_gettime(clk_id, tp); + return USE_CLOCK_GETTIME(clk_id, tp); *tp = c->time; return 0; diff --git a/src/utils.c b/src/utils.c index ea1de0f..6e1a837 100644 --- a/src/utils.c +++ b/src/utils.c @@ -409,7 +409,7 @@ char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len) __attribute__((weak, alias("osmo_hexdump_nospc"))); #endif -#include "../config.h" +#include #ifdef HAVE_CTYPE_H #include /*! Convert an entire string to lower case diff --git a/src/vty/fsm_vty.c b/src/vty/fsm_vty.c index 9bde241..0f17127 100644 --- a/src/vty/fsm_vty.c +++ b/src/vty/fsm_vty.c @@ -23,7 +23,7 @@ #include #include -#include "../../config.h" +#include #include #include diff --git a/src/vty/logging_vty.c b/src/vty/logging_vty.c index b785be4..b52a668 100644 --- a/src/vty/logging_vty.c +++ b/src/vty/logging_vty.c @@ -24,7 +24,7 @@ #include #include -#include "../../config.h" +#include #include #include diff --git a/src/vty/stats_vty.c b/src/vty/stats_vty.c index 296519c..53fbd17 100644 --- a/src/vty/stats_vty.c +++ b/src/vty/stats_vty.c @@ -25,7 +25,7 @@ #include #include -#include "../../config.h" +#include #include #include diff --git a/tests/socket/socket_test.c b/tests/socket/socket_test.c index 37e0281..8447ac1 100644 --- a/tests/socket/socket_test.c +++ b/tests/socket/socket_test.c @@ -33,7 +33,7 @@ #include #include -#include "../config.h" +#include void *ctx = NULL; diff --git a/tests/timer/timer_test.c b/tests/timer/timer_test.c index d2b0204..c83ccea 100644 --- a/tests/timer/timer_test.c +++ b/tests/timer/timer_test.c @@ -33,7 +33,7 @@ #include #include -#include "../config.h" +#include static void main_timer_fired(void *data); static void secondary_timer_fired(void *data);