diff --git a/CMakeLists.txt b/CMakeLists.txt index a67405a..03c87a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -283,6 +283,12 @@ endif() if(WIN32) set(OPENSSL_LIBS ${OPENSSL_LIBS} ws2_32) endif() + +check_function_exists(clock_gettime HAVE_CLOCK_GETTIME) +if(HAVE_CLOCK_GETTIME) + add_definitions(-DHAVE_CLOCK_GETTIME) +endif() + if(CMAKE_SYSTEM_NAME MATCHES "Linux") check_library_exists(rt clock_gettime "time.h" HAVE_CLOCK_GETTIME) if (HAVE_CLOCK_GETTIME) diff --git a/apps/openssl/Makefile.am b/apps/openssl/Makefile.am index 9b9eb10..f100adb 100644 --- a/apps/openssl/Makefile.am +++ b/apps/openssl/Makefile.am @@ -74,6 +74,12 @@ openssl_SOURCES += compat/poll_win.c endif endif +if !HAVE_CLOCK_GETTIME +if HOST_DARWIN +openssl_SOURCES += compat/clock_gettime_osx.c +endif +endif + if !HAVE_STRTONUM openssl_SOURCES += compat/strtonum.c endif diff --git a/apps/openssl/compat/clock_gettime_osx.c b/apps/openssl/compat/clock_gettime_osx.c new file mode 100644 index 0000000..5545187 --- /dev/null +++ b/apps/openssl/compat/clock_gettime_osx.c @@ -0,0 +1,26 @@ +#include + +#include +#define ORWL_NANO (+1.0E-9) +#define ORWL_GIGA UINT64_C(1000000000) + +int +clock_gettime(clockid_t clock_id, struct timespec *tp) +{ + static double orwl_timebase = 0.0; + static uint64_t orwl_timestart = 0; + + if (!orwl_timestart) { + mach_timebase_info_data_t tb = { 0 }; + mach_timebase_info(&tb); + orwl_timebase = tb.numer; + orwl_timebase /= tb.denom; + orwl_timestart = mach_absolute_time(); + } + + double diff = (mach_absolute_time() - orwl_timestart) * orwl_timebase; + tp->tv_sec = diff * ORWL_NANO; + tp->tv_nsec = diff - (tp->tv_sec * ORWL_GIGA); + + return 0; +} diff --git a/include/compat/sys/time.h b/include/compat/sys/time.h index 3d31985..76428c1 100644 --- a/include/compat/sys/time.h +++ b/include/compat/sys/time.h @@ -13,10 +13,6 @@ int gettimeofday(struct timeval *tp, void *tzp); #include_next #endif -#ifndef CLOCK_MONOTONIC -#define CLOCK_MONOTONIC CLOCK_REALTIME -#endif - #ifndef timersub #define timersub(tvp, uvp, vvp) \ do { \ diff --git a/include/compat/time.h b/include/compat/time.h index df65530..dc460ef 100644 --- a/include/compat/time.h +++ b/include/compat/time.h @@ -13,6 +13,9 @@ #include_next #endif +#ifndef LIBCRYPTOCOMPAT_TIME_H +#define LIBCRYPTOCOMPAT_TIME_H + #ifdef _WIN32 struct tm *__gmtime_r(const time_t * t, struct tm * tm); #define gmtime_r(tp, tm) __gmtime_r(tp, tm) @@ -22,6 +25,19 @@ struct tm *__gmtime_r(const time_t * t, struct tm * tm); time_t timegm(struct tm *tm); #endif +#ifndef CLOCK_MONOTONIC +#define CLOCK_MONOTONIC CLOCK_REALTIME +#endif + +#ifndef CLOCK_REALTIME +#define CLOCK_REALTIME 0 +#endif + +#ifndef HAVE_CLOCK_GETTIME +typedef int clockid_t; +int clock_gettime(clockid_t clock_id, struct timespec *tp); +#endif + #ifndef timespecsub #define timespecsub(tsp, usp, vsp) \ do { \ @@ -33,3 +49,5 @@ time_t timegm(struct tm *tm); } \ } while (0) #endif + +#endif diff --git a/m4/check-libc.m4 b/m4/check-libc.m4 index cacdd17..1fff77e 100644 --- a/m4/check-libc.m4 +++ b/m4/check-libc.m4 @@ -126,11 +126,13 @@ AM_CONDITIONAL([HAVE_ARC4RANDOM_BUF], -a "x$ac_cv_func_arc4random_buf" = xyes]) # Check for getentropy fallback dependencies -AC_CHECK_FUNC([getauxval]) -AC_SEARCH_LIBS([clock_gettime],[rt posix4]) -AC_CHECK_FUNC([clock_gettime]) +AC_CHECK_FUNCS([getauxval]) AC_SEARCH_LIBS([dl_iterate_phdr],[dl]) -AC_CHECK_FUNC([dl_iterate_phdr]) +AC_CHECK_FUNCS([dl_iterate_phdr]) + +AC_SEARCH_LIBS([clock_gettime],[rt posix4]) +AC_CHECK_FUNCS([clock_gettime]) +AM_CONDITIONAL([HAVE_CLOCK_GETTIME], [test "x$ac_cv_func_clock_gettime" = xyes]) ]) AC_DEFUN([CHECK_VA_COPY], [