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 <tom.cosgrove@arm.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24173)
This commit is contained in:
Richard Levitte 2024-04-17 13:45:32 +02:00 committed by Tomas Mraz
parent 34f35473c0
commit f352c808ed
2 changed files with 44 additions and 14 deletions

View file

@ -12,30 +12,59 @@
/* system-specific variants defining OSSL_sleep() */
#if defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__)
#include <unistd.h>
# 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 <unistd.h>
void OSSL_sleep(uint64_t millis)
{
# ifdef OPENSSL_SYS_VXWORKS
struct timespec ts;
ts.tv_sec = (long int) (millis / 1000);
ts.tv_nsec = (long int) (millis % 1000) * 1000000ul;
nanosleep(&ts, NULL);
# elif defined(__TANDEM) && !defined(_REENTRANT)
# include <cextdecs.h(PROCESS_DELAY_)>
/* 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);
/*
* 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);
# endif
}
# elif defined(__TANDEM) && !defined(_REENTRANT)
# include <cextdecs.h(PROCESS_DELAY_)>
void OSSL_sleep(uint64_t millis)
{
/* HPNS does not support usleep for non threaded apps */
PROCESS_DELAY_(millis * 1000);
}
# else
/* nanosleep is defined by POSIX.1-2001 */
# include <time.h>
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 <windows.h>

View file

@ -76,6 +76,7 @@ mlock
mmap
mprotect
munmap
nanosleep
opendir
openlog
poll