From 987aa6a084312be8501bdda42b0e5aab3b84d52a Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Thu, 22 Mar 2018 20:50:24 -0500 Subject: add clock_gettime for macos 10.11 and earlier --- apps/openssl/Makefile.am | 6 ++++++ apps/openssl/compat/clock_gettime_osx.c | 26 ++++++++++++++++++++++++++ include/compat/sys/time.h | 4 ---- include/compat/time.h | 13 +++++++++++++ m4/check-libc.m4 | 2 ++ 5 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 apps/openssl/compat/clock_gettime_osx.c 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..e0a5f59 --- /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(clock_id_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..d43dfcb 100644 --- a/include/compat/time.h +++ b/include/compat/time.h @@ -22,6 +22,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 +int +clock_gettime(clockid_t clock_id, struct timespec *tp); +#endif + #ifndef timespecsub #define timespecsub(tsp, usp, vsp) \ do { \ diff --git a/m4/check-libc.m4 b/m4/check-libc.m4 index cacdd17..df0266a 100644 --- a/m4/check-libc.m4 +++ b/m4/check-libc.m4 @@ -131,6 +131,8 @@ AC_SEARCH_LIBS([clock_gettime],[rt posix4]) AC_CHECK_FUNC([clock_gettime]) AC_SEARCH_LIBS([dl_iterate_phdr],[dl]) AC_CHECK_FUNC([dl_iterate_phdr]) + +AM_CONDITIONAL([HAVE_CLOCK_GETTIME], [test "x$ac_cv_func_clock_gettime" = xyes]) ]) AC_DEFUN([CHECK_VA_COPY], [ -- cgit v1.2.3-55-g6feb From 7ffaf004153931ba77da7f6659feb1990f4ff760 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Thu, 22 Mar 2018 21:11:46 -0500 Subject: hide clock_gettime on win32 --- include/compat/time.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/compat/time.h b/include/compat/time.h index d43dfcb..117a441 100644 --- a/include/compat/time.h +++ b/include/compat/time.h @@ -30,10 +30,12 @@ time_t timegm(struct tm *tm); #define CLOCK_REALTIME 0 #endif +#ifndef _WIN32 #ifndef HAVE_CLOCK_GETTIME int clock_gettime(clockid_t clock_id, struct timespec *tp); #endif +#endif #ifndef timespecsub #define timespecsub(tsp, usp, vsp) \ -- cgit v1.2.3-55-g6feb From 158281bdfe1664174ea22d7f900a72d8c18887cb Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Thu, 22 Mar 2018 21:32:25 -0500 Subject: AC_CHECK_FUNC does not set the define, prefer AC_CHECK_FUNCS --- m4/check-libc.m4 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/m4/check-libc.m4 b/m4/check-libc.m4 index df0266a..1fff77e 100644 --- a/m4/check-libc.m4 +++ b/m4/check-libc.m4 @@ -126,12 +126,12 @@ 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]) ]) -- cgit v1.2.3-55-g6feb From 70e6be3d6e82cbdc7a8ca5e9f2a1e0d95ece17c2 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Thu, 22 Mar 2018 21:43:06 -0500 Subject: move clock_gettime check out --- CMakeLists.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a67405a..37ef9c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -283,8 +283,11 @@ endif() if(WIN32) set(OPENSSL_LIBS ${OPENSSL_LIBS} ws2_32) endif() +check_library_exists(rt clock_gettime "time.h" 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) set(OPENSSL_LIBS ${OPENSSL_LIBS} rt) endif() -- cgit v1.2.3-55-g6feb From 4b2a8cd1c16896ca5d4570656435aa1b69bcbd46 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Fri, 23 Mar 2018 11:10:16 -0500 Subject: adjust definition of compat clock_gettime --- apps/openssl/compat/clock_gettime_osx.c | 2 +- include/compat/time.h | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/apps/openssl/compat/clock_gettime_osx.c b/apps/openssl/compat/clock_gettime_osx.c index e0a5f59..bb385e4 100644 --- a/apps/openssl/compat/clock_gettime_osx.c +++ b/apps/openssl/compat/clock_gettime_osx.c @@ -5,7 +5,7 @@ #define ORWL_GIGA UINT64_C(1000000000) int -clock_gettime(clock_id_t clock_id, struct timespec *tp) +clock_gettime(int clock_id, struct timespec *tp) { static double orwl_timebase = 0.0; static uint64_t orwl_timestart = 0; diff --git a/include/compat/time.h b/include/compat/time.h index 117a441..fb0ab29 100644 --- a/include/compat/time.h +++ b/include/compat/time.h @@ -30,11 +30,8 @@ time_t timegm(struct tm *tm); #define CLOCK_REALTIME 0 #endif -#ifndef _WIN32 #ifndef HAVE_CLOCK_GETTIME -int -clock_gettime(clockid_t clock_id, struct timespec *tp); -#endif +int clock_gettime(int clock_id, struct timespec *tp); #endif #ifndef timespecsub -- cgit v1.2.3-55-g6feb From 6b513a1c78b8a8c4c48b9ef476628728974f3171 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Fri, 23 Mar 2018 13:45:05 -0500 Subject: add proper guard and typedef --- apps/openssl/compat/clock_gettime_osx.c | 2 +- include/compat/time.h | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/apps/openssl/compat/clock_gettime_osx.c b/apps/openssl/compat/clock_gettime_osx.c index bb385e4..5545187 100644 --- a/apps/openssl/compat/clock_gettime_osx.c +++ b/apps/openssl/compat/clock_gettime_osx.c @@ -5,7 +5,7 @@ #define ORWL_GIGA UINT64_C(1000000000) int -clock_gettime(int clock_id, struct timespec *tp) +clock_gettime(clockid_t clock_id, struct timespec *tp) { static double orwl_timebase = 0.0; static uint64_t orwl_timestart = 0; diff --git a/include/compat/time.h b/include/compat/time.h index fb0ab29..8f08c9a 100644 --- a/include/compat/time.h +++ b/include/compat/time.h @@ -3,6 +3,9 @@ * sys/time.h compatibility shim */ +#ifndef LIBCRYPTOCOMPAT_TIME_H +#define LIBCRYPTOCOMPAT_TIME_H + #ifdef _MSC_VER #if _MSC_VER >= 1900 #include <../ucrt/time.h> @@ -31,7 +34,8 @@ time_t timegm(struct tm *tm); #endif #ifndef HAVE_CLOCK_GETTIME -int clock_gettime(int clock_id, struct timespec *tp); +typedef int clockid_t; +int clock_gettime(clockid_t clock_id, struct timespec *tp); #endif #ifndef timespecsub @@ -45,3 +49,5 @@ int clock_gettime(int clock_id, struct timespec *tp); } \ } while (0) #endif + +#endif -- cgit v1.2.3-55-g6feb From 862cd768b7fd81bcbe13fd49109932309e107856 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Fri, 23 Mar 2018 13:52:12 -0500 Subject: fix clock_gettime check in cmake --- CMakeLists.txt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 37ef9c9..03c87a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -283,11 +283,14 @@ endif() if(WIN32) set(OPENSSL_LIBS ${OPENSSL_LIBS} ws2_32) endif() -check_library_exists(rt clock_gettime "time.h" HAVE_CLOCK_GETTIME) -if (HAVE_CLOCK_GETTIME) + +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) set(OPENSSL_LIBS ${OPENSSL_LIBS} rt) endif() -- cgit v1.2.3-55-g6feb From 31c98c2969f70b2db3e866352ec027f7b965c406 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Fri, 23 Mar 2018 15:35:58 -0500 Subject: always allow upstream time.h to be visible --- include/compat/time.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/compat/time.h b/include/compat/time.h index 8f08c9a..dc460ef 100644 --- a/include/compat/time.h +++ b/include/compat/time.h @@ -3,9 +3,6 @@ * sys/time.h compatibility shim */ -#ifndef LIBCRYPTOCOMPAT_TIME_H -#define LIBCRYPTOCOMPAT_TIME_H - #ifdef _MSC_VER #if _MSC_VER >= 1900 #include <../ucrt/time.h> @@ -16,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) -- cgit v1.2.3-55-g6feb