Skip to content

Commit

Permalink
For Unix, refactor OSSL_sleep() to use nanosleep() instead of usleep()
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from openssl#24173)
  • Loading branch information
levitte authored and t8m committed May 22, 2024
1 parent 34f3547 commit f352c80
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
55 changes: 42 additions & 13 deletions crypto/sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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 <cextdecs.h(PROCESS_DELAY_)>

# 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
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 <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>

Expand Down
1 change: 1 addition & 0 deletions util/platform_symbols/unix-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ mlock
mmap
mprotect
munmap
nanosleep
opendir
openlog
poll
Expand Down

0 comments on commit f352c80

Please sign in to comment.