]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
For Unix, refactor OSSL_sleep() to use nanosleep() instead of usleep()
authorRichard Levitte <levitte@openssl.org>
Wed, 17 Apr 2024 11:45:32 +0000 (13:45 +0200)
committerTomas Mraz <tomas@openssl.org>
Wed, 22 May 2024 07:59:32 +0000 (09:59 +0200)
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)

crypto/sleep.c
util/platform_symbols/unix-symbols.txt

index 73467fb85933004ead97121bd582e248daca89b7..dbd0f7802576b7942f4fd81c5d3e908e0bb3287b 100644 (file)
 
 /* 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>
 
index 20f1a78ad9dccf3402f68f1c98773130c4d674ae..e0af15b034de377205ac2efbdd2dc910b7b7b2e7 100644 (file)
@@ -76,6 +76,7 @@ mlock
 mmap
 mprotect
 munmap
+nanosleep
 opendir
 openlog
 poll