Support _onexit() in preference to atexit() on Windows
authorMatt Caswell <matt@openssl.org>
Fri, 16 Nov 2018 17:26:23 +0000 (17:26 +0000)
committerMatt Caswell <matt@openssl.org>
Fri, 4 Jan 2019 20:23:16 +0000 (20:23 +0000)
This enables cleanup to happen on DLL unload instead of at process exit.

[extended tests]

Reviewed-by: Tim Hudson <tjh@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/7983)

crypto/init.c

index 1c50f2994ec7d49b37acc0fd4477a88d56ca4f82..46ba67d3e406b362315bf4d453bf9f1c7de10a46 100644 (file)
@@ -118,14 +118,28 @@ err:
 }
 
 static CRYPTO_ONCE register_atexit = CRYPTO_ONCE_STATIC_INIT;
+#if !defined(OPENSSL_SYS_UEFI) && defined(_WIN32)
+static int win32atexit(void)
+{
+    OPENSSL_cleanup();
+    return 0;
+}
+#endif
+
 DEFINE_RUN_ONCE_STATIC(ossl_init_register_atexit)
 {
-# ifdef OPENSSL_INIT_DEBUG
+#ifdef OPENSSL_INIT_DEBUG
     fprintf(stderr, "OPENSSL_INIT: ossl_init_register_atexit()\n");
-# endif
+#endif
 #ifndef OPENSSL_SYS_UEFI
+# ifdef _WIN32
+    /* We use _onexit() in preference because it gets called on DLL unload */
+    if (_onexit(win32atexit) == NULL)
+        return 0;
+# else
     if (atexit(OPENSSL_cleanup) != 0)
         return 0;
+# endif
 #endif
 
     return 1;