From f352c808edaaced8ba6a95cb440825094f2bb842 Mon Sep 17 00:00:00 2001 From: Richard Levitte Date: Wed, 17 Apr 2024 13:45:32 +0200 Subject: [PATCH] For Unix, refactor OSSL_sleep() to use nanosleep() instead of usleep() usleep() is obsolete since POSIX.1-2001 and removed in POSIX.1-2008, in favor of nanosleep(), which has been present since POSIX.1-2001. The exceptions for DJGPP and TANDEM are preserved. Also, just in case nanosleep() turns out to be unavailable on any Unix machinery that we are unaware of, we allow a revert to using usleep() by defining OPENSSL_USE_USLEEP. Reviewed-by: Tom Cosgrove Reviewed-by: Tomas Mraz (Merged from https://github.com/openssl/openssl/pull/24173) --- crypto/sleep.c | 55 ++++++++++++++++++++------ util/platform_symbols/unix-symbols.txt | 1 + 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/crypto/sleep.c b/crypto/sleep.c index 73467fb859330..dbd0f7802576b 100644 --- a/crypto/sleep.c +++ b/crypto/sleep.c @@ -12,30 +12,59 @@ /* system-specific variants defining OSSL_sleep() */ #if defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__) -#include +# if defined(OPENSSL_USE_USLEEP) \ + || defined(__DJGPP__) \ + || (defined(__TANDEM) && defined(_REENTRANT)) + +/* + * usleep() was made obsolete by POSIX.1-2008, and nanosleep() + * should be used instead. However, nanosleep() isn't implemented + * on the platforms given above, so we still use it for those. + * Also, OPENSSL_USE_USLEEP can be defined to enable the use of + * usleep, if it turns out that nanosleep() is unavailable. + */ + +# include void OSSL_sleep(uint64_t millis) { -# ifdef OPENSSL_SYS_VXWORKS - struct timespec ts; + unsigned int s = (unsigned int)(millis / 1000); + unsigned int us = (unsigned int)((millis % 1000) * 1000); + + if (s > 0) + sleep(s); + /* + * On NonStop with the PUT thread model, thread context switch is + * cooperative, with usleep() being a "natural" context switch point. + * We avoid checking us > 0 here, to allow that context switch to + * happen. + */ + usleep(us); +} - ts.tv_sec = (long int) (millis / 1000); - ts.tv_nsec = (long int) (millis % 1000) * 1000000ul; - nanosleep(&ts, NULL); # elif defined(__TANDEM) && !defined(_REENTRANT) -# include +# include +void OSSL_sleep(uint64_t millis) +{ /* HPNS does not support usleep for non threaded apps */ PROCESS_DELAY_(millis * 1000); +} + # else - unsigned int s = (unsigned int)(millis / 1000); - unsigned int us = (unsigned int)((millis % 1000) * 1000); - if (s > 0) - sleep(s); - usleep(us); -# endif +/* nanosleep is defined by POSIX.1-2001 */ +# include +void OSSL_sleep(uint64_t millis) +{ + struct timespec ts; + + ts.tv_sec = (long int) (millis / 1000); + ts.tv_nsec = (long int) (millis % 1000) * 1000000ul; + nanosleep(&ts, NULL); } + +# endif #elif defined(_WIN32) && !defined(OPENSSL_SYS_UEFI) # include diff --git a/util/platform_symbols/unix-symbols.txt b/util/platform_symbols/unix-symbols.txt index 20f1a78ad9dcc..e0af15b034de3 100644 --- a/util/platform_symbols/unix-symbols.txt +++ b/util/platform_symbols/unix-symbols.txt @@ -76,6 +76,7 @@ mlock mmap mprotect munmap +nanosleep opendir openlog poll