Fix windows thread stop code
[openssl.git] / crypto / init.c
index 04191ded6223e150d5f9e147499cb183bc175560..cbe07df2a29e1eca3c2ef7319c0d2375fd7564f8 100644 (file)
 
 #include <internal/cryptlib_int.h>
 #include <openssl/err.h>
+#include <openssl/rand.h>
 #include <openssl/evp.h>
 #include <internal/evp_int.h>
 #include <internal/conf.h>
 #include <internal/async.h>
+#ifndef OPENSSL_NO_ENGINE
 #include <internal/engine.h>
+#endif
 #include <openssl/comp.h>
 #include <internal/err.h>
 #include <stdlib.h>
@@ -99,7 +102,7 @@ static void ossl_init_thread_stop_cleanup(void)
 static struct thread_local_inits_st *local = NULL;
 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
 {
-    static struct thread_local_inits_st *tmp;
+    struct thread_local_inits_st *tmp;
 
     tmp = local;
 
@@ -253,6 +256,7 @@ static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
 
 #endif
 
+typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
 struct ossl_init_stop_st {
     void (*handler)(void);
     OPENSSL_INIT_STOP *next;
@@ -268,7 +272,9 @@ static void ossl_init_base(void)
     fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
 #endif
     ossl_init_setup_thread_stop();
+#ifndef OPENSSL_SYS_UEFI
     atexit(OPENSSL_cleanup);
+#endif
     OPENSSL_cpuid_setup();
     base_inited = 1;
 }
@@ -368,6 +374,7 @@ static void ossl_init_no_config(void)
     config_inited = 1;
 }
 
+#ifndef OPENSSL_NO_ASYNC
 static OPENSSL_INIT_ONCE async = OPENSSL_INIT_ONCE_STATIC_INIT;
 static int async_inited = 0;
 static void ossl_init_async(void)
@@ -378,6 +385,7 @@ static void ossl_init_async(void)
     async_init();
     async_inited = 1;
 }
+#endif
 
 #ifndef OPENSSL_NO_ENGINE
 static int engine_inited = 0;
@@ -479,6 +487,7 @@ static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
     if (locals == NULL)
         return;
 
+#ifndef OPENSSL_NO_ASYNC
     if (locals->async) {
 #ifdef OPENSSL_INIT_DEBUG
         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
@@ -486,6 +495,7 @@ static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
 #endif
         ASYNC_cleanup_thread();
     }
+#endif
 
     if (locals->err_state) {
 #ifdef OPENSSL_INIT_DEBUG
@@ -496,7 +506,6 @@ static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
     }
 
     OPENSSL_free(locals);
-    ossl_init_thread_stop_cleanup();
 }
 
 void OPENSSL_thread_stop(void)
@@ -589,6 +598,8 @@ void OPENSSL_cleanup(void)
         ERR_free_strings();
     }
 
+    ossl_init_thread_stop_cleanup();
+
 #ifdef OPENSSL_INIT_DEBUG
     fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
                     "CRYPTO_cleanup_all_ex_data()\n");
@@ -606,30 +617,27 @@ void OPENSSL_cleanup(void)
     base_inited = 0;
 }
 
-static const OPENSSL_INIT_SETTINGS *ossl_init_get_setting(
-        const OPENSSL_INIT_SETTINGS *settings, int name)
-{
-    if (settings == NULL)
-        return NULL;
-
-    while (settings->name != OPENSSL_INIT_SET_END) {
-        if (settings->name == name)
-            return settings;
-        settings++;
-    }
-
-    return NULL;
-}
-
 /*
  * If this function is called with a non NULL settings value then it must be
  * called prior to any threads making calls to any OpenSSL functions,
  * i.e. passing a non-null settings value is assumed to be single-threaded.
  */
-void OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
-{
-    /* XXX TODO WARNING To be updated to return a value not assert. */
-    assert(!stopped);
+int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
+{
+    static int stoperrset = 0;
+
+    if (stopped) {
+        if (!stoperrset) {
+            /*
+             * We only ever set this once to avoid getting into an infinite
+             * loop where the error system keeps trying to init and fails so
+             * sets an error etc
+             */
+            stoperrset = 1;
+            CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
+        }
+        return 0;
+    }
 
     ossl_init_once_run(&base, ossl_init_base);
 
@@ -658,22 +666,16 @@ void OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
 
     if (opts & OPENSSL_INIT_LOAD_CONFIG) {
         CRYPTO_w_lock(CRYPTO_LOCK_INIT);
-        if (settings != NULL) {
-            const OPENSSL_INIT_SETTINGS *curr;
-            curr = ossl_init_get_setting(settings,
-                                         OPENSSL_INIT_SET_CONF_FILENAME);
-            config_filename = (curr == NULL) ? NULL : curr->value.type_string;
-        } else {
-            config_filename = NULL;
-        }
+        config_filename = (settings == NULL) ? NULL : settings->config_name;
         ossl_init_once_run(&config, ossl_init_config);
         CRYPTO_w_unlock(CRYPTO_LOCK_INIT);
     }
 
+#ifndef OPENSSL_NO_ASYNC
     if (opts & OPENSSL_INIT_ASYNC) {
         ossl_init_once_run(&async, ossl_init_async);
     }
-
+#endif
 #ifndef OPENSSL_NO_ENGINE
     if (opts & OPENSSL_INIT_ENGINE_OPENSSL) {
         ossl_init_once_run(&engine_openssl, ossl_init_engine_openssl);
@@ -716,6 +718,8 @@ void OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
     if (opts & OPENSSL_INIT_ZLIB) {
         ossl_init_once_run(&zlib, ossl_init_zlib);
     }
+
+    return 1;
 }
 
 int OPENSSL_atexit(void (*handler)(void))