2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib_int.h"
12 #include <openssl/err.h>
13 #include "internal/rand_int.h"
14 #include "internal/bio.h"
15 #include <openssl/evp.h>
16 #include "internal/evp_int.h"
17 #include "internal/conf.h"
18 #include "internal/async.h"
19 #include "internal/engine.h"
20 #include "internal/comp.h"
21 #include "internal/err.h"
22 #include "internal/err_int.h"
23 #include "internal/objects.h"
26 #include "internal/thread_once.h"
27 #include "internal/dso_conf.h"
28 #include "internal/dso.h"
29 #include "internal/store.h"
30 #include <openssl/trace.h>
32 static int stopped
= 0;
35 * Since per-thread-specific-data destructors are not universally
36 * available, i.e. not on Windows, only below CRYPTO_THREAD_LOCAL key
37 * is assumed to have destructor associated. And then an effort is made
38 * to call this single destructor on non-pthread platform[s].
40 * Initial value is "impossible". It is used as guard value to shortcut
41 * destructor for threads terminating before libcrypto is initialized or
42 * after it's de-initialized. Access to the key doesn't have to be
43 * serialized for the said threads, because they didn't use libcrypto
44 * and it doesn't matter if they pick "impossible" or derefernce real
45 * key value and pull NULL past initialization in the first thread that
46 * intends to use libcrypto.
50 CRYPTO_THREAD_LOCAL value
;
51 } destructor_key
= { -1 };
53 static void ossl_init_thread_stop(struct thread_local_inits_st
*locals
);
55 static void ossl_init_thread_destructor(void *local
)
57 ossl_init_thread_stop((struct thread_local_inits_st
*)local
);
60 static struct thread_local_inits_st
*ossl_init_get_thread_local(int alloc
)
62 struct thread_local_inits_st
*local
=
63 CRYPTO_THREAD_get_local(&destructor_key
.value
);
67 && (local
= OPENSSL_zalloc(sizeof(*local
))) != NULL
68 && !CRYPTO_THREAD_set_local(&destructor_key
.value
, local
)) {
73 CRYPTO_THREAD_set_local(&destructor_key
.value
, NULL
);
79 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP
;
80 struct ossl_init_stop_st
{
81 void (*handler
)(void);
82 OPENSSL_INIT_STOP
*next
;
85 static OPENSSL_INIT_STOP
*stop_handlers
= NULL
;
86 static CRYPTO_RWLOCK
*init_lock
= NULL
;
88 static CRYPTO_ONCE base
= CRYPTO_ONCE_STATIC_INIT
;
89 static int base_inited
= 0;
90 DEFINE_RUN_ONCE_STATIC(ossl_init_base
)
92 CRYPTO_THREAD_LOCAL key
;
94 if (ossl_trace_init() == 0)
97 OSSL_TRACE(INIT
, "ossl_init_base: setting up stop handlers\n");
98 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
99 ossl_malloc_setup_failures();
101 if (!CRYPTO_THREAD_init_local(&key
, ossl_init_thread_destructor
))
103 if ((init_lock
= CRYPTO_THREAD_lock_new()) == NULL
)
105 OPENSSL_cpuid_setup();
107 destructor_key
.value
= key
;
112 OSSL_TRACE(INIT
, "ossl_init_base failed!\n");
113 CRYPTO_THREAD_lock_free(init_lock
);
116 CRYPTO_THREAD_cleanup_local(&key
);
120 static CRYPTO_ONCE register_atexit
= CRYPTO_ONCE_STATIC_INIT
;
121 #if !defined(OPENSSL_SYS_UEFI) && defined(_WIN32)
122 static int win32atexit(void)
129 DEFINE_RUN_ONCE_STATIC(ossl_init_register_atexit
)
131 #ifdef OPENSSL_INIT_DEBUG
132 fprintf(stderr
, "OPENSSL_INIT: ossl_init_register_atexit()\n");
134 #ifndef OPENSSL_SYS_UEFI
136 /* We use _onexit() in preference because it gets called on DLL unload */
137 if (_onexit(win32atexit
) == NULL
)
140 if (atexit(OPENSSL_cleanup
) != 0)
148 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_register_atexit
,
149 ossl_init_register_atexit
)
151 #ifdef OPENSSL_INIT_DEBUG
152 fprintf(stderr
, "OPENSSL_INIT: ossl_init_no_register_atexit ok!\n");
154 /* Do nothing in this case */
158 static CRYPTO_ONCE load_crypto_nodelete
= CRYPTO_ONCE_STATIC_INIT
;
159 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_nodelete
)
161 OSSL_TRACE(INIT
, "ossl_init_load_crypto_nodelete()\n");
163 #if !defined(OPENSSL_USE_NODELETE) \
164 && !defined(OPENSSL_NO_PINSHARED)
165 # if defined(DSO_WIN32) && !defined(_WIN32_WCE)
167 HMODULE handle
= NULL
;
170 /* We don't use the DSO route for WIN32 because there is a better way */
171 ret
= GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
172 | GET_MODULE_HANDLE_EX_FLAG_PIN
,
173 (void *)&base_inited
, &handle
);
176 "ossl_init_load_crypto_nodelete: "
177 "obtained DSO reference? %s\n",
178 (ret
== TRUE
? "No!" : "Yes."));
179 return (ret
== TRUE
) ? 1 : 0;
181 # elif !defined(DSO_NONE)
183 * Deliberately leak a reference to ourselves. This will force the library
184 * to remain loaded until the atexit() handler is run at process exit.
190 if (!err_shelve_state(&err
))
193 dso
= DSO_dsobyaddr(&base_inited
, DSO_FLAG_NO_UNLOAD_ON_FREE
);
195 * In case of No!, it is uncertain our exit()-handlers can still be
196 * called. After dlclose() the whole library might have been unloaded
199 OSSL_TRACE1(INIT
, "obtained DSO reference? %s\n",
200 (dso
== NULL
? "No!" : "Yes."));
202 err_unshelve_state(err
);
210 static CRYPTO_ONCE load_crypto_strings
= CRYPTO_ONCE_STATIC_INIT
;
211 static int load_crypto_strings_inited
= 0;
212 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings
)
216 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
217 * pulling in all the error strings during static linking
219 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
220 OSSL_TRACE(INIT
, "err_load_crypto_strings_int()\n");
221 ret
= err_load_crypto_strings_int();
222 load_crypto_strings_inited
= 1;
227 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_load_crypto_strings
,
228 ossl_init_load_crypto_strings
)
230 /* Do nothing in this case */
234 static CRYPTO_ONCE add_all_ciphers
= CRYPTO_ONCE_STATIC_INIT
;
235 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers
)
238 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
239 * pulling in all the ciphers during static linking
241 #ifndef OPENSSL_NO_AUTOALGINIT
242 OSSL_TRACE(INIT
, "openssl_add_all_ciphers_int()\n");
243 openssl_add_all_ciphers_int();
248 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_ciphers
,
249 ossl_init_add_all_ciphers
)
255 static CRYPTO_ONCE add_all_digests
= CRYPTO_ONCE_STATIC_INIT
;
256 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests
)
259 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
260 * pulling in all the ciphers during static linking
262 #ifndef OPENSSL_NO_AUTOALGINIT
263 OSSL_TRACE(INIT
, "openssl_add_all_digests()\n");
264 openssl_add_all_digests_int();
269 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_digests
,
270 ossl_init_add_all_digests
)
276 static CRYPTO_ONCE add_all_macs
= CRYPTO_ONCE_STATIC_INIT
;
277 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_macs
)
280 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
281 * pulling in all the macs during static linking
283 #ifndef OPENSSL_NO_AUTOALGINIT
284 OSSL_TRACE(INIT
, "openssl_add_all_macs_int()\n");
285 openssl_add_all_macs_int();
290 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_macs
, ossl_init_add_all_macs
)
296 static CRYPTO_ONCE config
= CRYPTO_ONCE_STATIC_INIT
;
297 static int config_inited
= 0;
298 static const OPENSSL_INIT_SETTINGS
*conf_settings
= NULL
;
299 DEFINE_RUN_ONCE_STATIC(ossl_init_config
)
301 int ret
= openssl_config_int(conf_settings
);
305 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_config
, ossl_init_config
)
307 OSSL_TRACE(INIT
, "openssl_no_config_int()\n");
308 openssl_no_config_int();
313 static CRYPTO_ONCE async
= CRYPTO_ONCE_STATIC_INIT
;
314 static int async_inited
= 0;
315 DEFINE_RUN_ONCE_STATIC(ossl_init_async
)
317 OSSL_TRACE(INIT
, "async_init()\n");
324 #ifndef OPENSSL_NO_ENGINE
325 static CRYPTO_ONCE engine_openssl
= CRYPTO_ONCE_STATIC_INIT
;
326 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl
)
328 OSSL_TRACE(INIT
, "engine_load_openssl_int()\n");
329 engine_load_openssl_int();
332 # ifndef OPENSSL_NO_RDRAND
333 static CRYPTO_ONCE engine_rdrand
= CRYPTO_ONCE_STATIC_INIT
;
334 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand
)
336 OSSL_TRACE(INIT
, "engine_load_rdrand_int()\n");
337 engine_load_rdrand_int();
341 static CRYPTO_ONCE engine_dynamic
= CRYPTO_ONCE_STATIC_INIT
;
342 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic
)
344 OSSL_TRACE(INIT
, "engine_load_dynamic_int()\n");
345 engine_load_dynamic_int();
348 # ifndef OPENSSL_NO_STATIC_ENGINE
349 # ifndef OPENSSL_NO_DEVCRYPTOENG
350 static CRYPTO_ONCE engine_devcrypto
= CRYPTO_ONCE_STATIC_INIT
;
351 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto
)
353 OSSL_TRACE(INIT
, "engine_load_devcrypto_int()\n");
354 engine_load_devcrypto_int();
358 # if !defined(OPENSSL_NO_PADLOCKENG)
359 static CRYPTO_ONCE engine_padlock
= CRYPTO_ONCE_STATIC_INIT
;
360 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock
)
362 OSSL_TRACE(INIT
, "engine_load_padlock_int()\n");
363 engine_load_padlock_int();
367 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
368 static CRYPTO_ONCE engine_capi
= CRYPTO_ONCE_STATIC_INIT
;
369 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi
)
371 OSSL_TRACE(INIT
, "engine_load_capi_int()\n");
372 engine_load_capi_int();
376 # if !defined(OPENSSL_NO_AFALGENG)
377 static CRYPTO_ONCE engine_afalg
= CRYPTO_ONCE_STATIC_INIT
;
378 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg
)
380 OSSL_TRACE(INIT
, "engine_load_afalg_int()\n");
381 engine_load_afalg_int();
388 #ifndef OPENSSL_NO_COMP
389 static CRYPTO_ONCE zlib
= CRYPTO_ONCE_STATIC_INIT
;
391 static int zlib_inited
= 0;
392 DEFINE_RUN_ONCE_STATIC(ossl_init_zlib
)
394 /* Do nothing - we need to know about this for the later cleanup */
400 static void ossl_init_thread_stop(struct thread_local_inits_st
*locals
)
402 /* Can't do much about this */
407 OSSL_TRACE(INIT
, "async_delete_thread_state()\n");
408 async_delete_thread_state();
411 if (locals
->err_state
) {
412 OSSL_TRACE(INIT
, "err_delete_thread_state()\n");
413 err_delete_thread_state();
417 OSSL_TRACE(INIT
, "drbg_delete_thread_state()\n");
418 drbg_delete_thread_state();
421 OPENSSL_free(locals
);
424 void OPENSSL_thread_stop(void)
426 if (destructor_key
.sane
!= -1)
427 ossl_init_thread_stop(ossl_init_get_thread_local(0));
430 int ossl_init_thread_start(uint64_t opts
)
432 struct thread_local_inits_st
*locals
;
434 if (!OPENSSL_init_crypto(0, NULL
))
437 locals
= ossl_init_get_thread_local(1);
442 if (opts
& OPENSSL_INIT_THREAD_ASYNC
) {
444 "ossl_init_thread_start: "
445 "marking thread for async\n");
449 if (opts
& OPENSSL_INIT_THREAD_ERR_STATE
) {
451 "ossl_init_thread_start: "
452 "marking thread for err_state\n");
453 locals
->err_state
= 1;
456 if (opts
& OPENSSL_INIT_THREAD_RAND
) {
458 "ossl_init_thread_start: "
459 "marking thread for rand\n");
466 void OPENSSL_cleanup(void)
468 OPENSSL_INIT_STOP
*currhandler
, *lasthandler
;
469 CRYPTO_THREAD_LOCAL key
;
472 * TODO(3.0): This function needs looking at with a view to moving most/all
473 * of this into onfree handlers in OPENSSL_CTX.
476 /* If we've not been inited then no need to deinit */
480 /* Might be explicitly called and also by atexit */
486 * Thread stop may not get automatically called by the thread library for
487 * the very last thread in some situations, so call it directly.
489 ossl_init_thread_stop(ossl_init_get_thread_local(0));
491 currhandler
= stop_handlers
;
492 while (currhandler
!= NULL
) {
493 currhandler
->handler();
494 lasthandler
= currhandler
;
495 currhandler
= currhandler
->next
;
496 OPENSSL_free(lasthandler
);
498 stop_handlers
= NULL
;
500 CRYPTO_THREAD_lock_free(init_lock
);
504 * We assume we are single-threaded for this function, i.e. no race
505 * conditions for the various "*_inited" vars below.
508 #ifndef OPENSSL_NO_COMP
510 OSSL_TRACE(INIT
, "OPENSSL_cleanup: comp_zlib_cleanup_int()\n");
511 comp_zlib_cleanup_int();
516 OSSL_TRACE(INIT
, "OPENSSL_cleanup: async_deinit()\n");
520 if (load_crypto_strings_inited
) {
521 OSSL_TRACE(INIT
, "OPENSSL_cleanup: err_free_strings_int()\n");
522 err_free_strings_int();
525 key
= destructor_key
.value
;
526 destructor_key
.sane
= -1;
527 CRYPTO_THREAD_cleanup_local(&key
);
530 * Note that cleanup order is important:
531 * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
532 * must be called before engine_cleanup_int()
533 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
534 * before the ex data handlers are wiped during default openssl_ctx deinit.
535 * - conf_modules_free_int() can end up in ENGINE code so must be called
536 * before engine_cleanup_int()
537 * - ENGINEs and additional EVP algorithms might use added OIDs names so
538 * obj_cleanup_int() must be called last
540 OSSL_TRACE(INIT
, "OPENSSL_cleanup: rand_cleanup_int()\n");
543 OSSL_TRACE(INIT
, "OPENSSL_cleanup: rand_drbg_cleanup_int()\n");
544 rand_drbg_cleanup_int();
546 OSSL_TRACE(INIT
, "OPENSSL_cleanup: conf_modules_free_int()\n");
547 conf_modules_free_int();
549 #ifndef OPENSSL_NO_ENGINE
550 OSSL_TRACE(INIT
, "OPENSSL_cleanup: engine_cleanup_int()\n");
551 engine_cleanup_int();
553 OSSL_TRACE(INIT
, "OPENSSL_cleanup: ossl_store_cleanup_int()\n");
554 ossl_store_cleanup_int();
556 OSSL_TRACE(INIT
, "OPENSSL_cleanup: openssl_ctx_default_deinit()\n");
557 openssl_ctx_default_deinit();
559 OSSL_TRACE(INIT
, "OPENSSL_cleanup: bio_cleanup()\n");
562 OSSL_TRACE(INIT
, "OPENSSL_cleanup: evp_cleanup_int()\n");
565 OSSL_TRACE(INIT
, "OPENSSL_cleanup: obj_cleanup_int()\n");
568 OSSL_TRACE(INIT
, "OPENSSL_cleanup: err_int()\n");
571 OSSL_TRACE(INIT
, "OPENSSL_cleanup: CRYPTO_secure_malloc_done()\n");
572 CRYPTO_secure_malloc_done();
574 OSSL_TRACE(INIT
, "OPENSSL_cleanup: ossl_trace_cleanup()\n");
575 ossl_trace_cleanup();
581 * If this function is called with a non NULL settings value then it must be
582 * called prior to any threads making calls to any OpenSSL functions,
583 * i.e. passing a non-null settings value is assumed to be single-threaded.
585 int OPENSSL_init_crypto(uint64_t opts
, const OPENSSL_INIT_SETTINGS
*settings
)
588 * TODO(3.0): This function needs looking at with a view to moving most/all
589 * of this into OPENSSL_CTX.
593 if (!(opts
& OPENSSL_INIT_BASE_ONLY
))
594 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO
, ERR_R_INIT_FAIL
);
599 * When the caller specifies OPENSSL_INIT_BASE_ONLY, that should be the
600 * *only* option specified. With that option we return immediately after
601 * doing the requested limited initialization. Note that
602 * err_shelve_state() called by us via ossl_init_load_crypto_nodelete()
603 * re-enters OPENSSL_init_crypto() with OPENSSL_INIT_BASE_ONLY, but with
604 * base already initialized this is a harmless NOOP.
606 * If we remain the only caller of err_shelve_state() the recursion should
607 * perhaps be removed, but if in doubt, it can be left in place.
609 if (!RUN_ONCE(&base
, ossl_init_base
))
612 if (opts
& OPENSSL_INIT_BASE_ONLY
)
616 * Now we don't always set up exit handlers, the INIT_BASE_ONLY calls
617 * should not have the side-effect of setting up exit handlers, and
618 * therefore, this code block is below the INIT_BASE_ONLY-conditioned early
621 if ((opts
& OPENSSL_INIT_NO_ATEXIT
) != 0) {
622 if (!RUN_ONCE_ALT(®ister_atexit
, ossl_init_no_register_atexit
,
623 ossl_init_register_atexit
))
625 } else if (!RUN_ONCE(®ister_atexit
, ossl_init_register_atexit
)) {
629 if (!RUN_ONCE(&load_crypto_nodelete
, ossl_init_load_crypto_nodelete
))
632 if ((opts
& OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS
)
633 && !RUN_ONCE_ALT(&load_crypto_strings
,
634 ossl_init_no_load_crypto_strings
,
635 ossl_init_load_crypto_strings
))
638 if ((opts
& OPENSSL_INIT_LOAD_CRYPTO_STRINGS
)
639 && !RUN_ONCE(&load_crypto_strings
, ossl_init_load_crypto_strings
))
642 if ((opts
& OPENSSL_INIT_NO_ADD_ALL_CIPHERS
)
643 && !RUN_ONCE_ALT(&add_all_ciphers
, ossl_init_no_add_all_ciphers
,
644 ossl_init_add_all_ciphers
))
647 if ((opts
& OPENSSL_INIT_ADD_ALL_CIPHERS
)
648 && !RUN_ONCE(&add_all_ciphers
, ossl_init_add_all_ciphers
))
651 if ((opts
& OPENSSL_INIT_NO_ADD_ALL_DIGESTS
)
652 && !RUN_ONCE_ALT(&add_all_digests
, ossl_init_no_add_all_digests
,
653 ossl_init_add_all_digests
))
656 if ((opts
& OPENSSL_INIT_ADD_ALL_DIGESTS
)
657 && !RUN_ONCE(&add_all_digests
, ossl_init_add_all_digests
))
660 if ((opts
& OPENSSL_INIT_NO_ADD_ALL_MACS
)
661 && !RUN_ONCE_ALT(&add_all_macs
, ossl_init_no_add_all_macs
,
662 ossl_init_add_all_macs
))
665 if ((opts
& OPENSSL_INIT_ADD_ALL_MACS
)
666 && !RUN_ONCE(&add_all_macs
, ossl_init_add_all_macs
))
669 if ((opts
& OPENSSL_INIT_ATFORK
)
670 && !openssl_init_fork_handlers())
673 if ((opts
& OPENSSL_INIT_NO_LOAD_CONFIG
)
674 && !RUN_ONCE_ALT(&config
, ossl_init_no_config
, ossl_init_config
))
677 if (opts
& OPENSSL_INIT_LOAD_CONFIG
) {
679 CRYPTO_THREAD_write_lock(init_lock
);
680 conf_settings
= settings
;
681 ret
= RUN_ONCE(&config
, ossl_init_config
);
682 conf_settings
= NULL
;
683 CRYPTO_THREAD_unlock(init_lock
);
688 if ((opts
& OPENSSL_INIT_ASYNC
)
689 && !RUN_ONCE(&async
, ossl_init_async
))
692 #ifndef OPENSSL_NO_ENGINE
693 if ((opts
& OPENSSL_INIT_ENGINE_OPENSSL
)
694 && !RUN_ONCE(&engine_openssl
, ossl_init_engine_openssl
))
696 # ifndef OPENSSL_NO_RDRAND
697 if ((opts
& OPENSSL_INIT_ENGINE_RDRAND
)
698 && !RUN_ONCE(&engine_rdrand
, ossl_init_engine_rdrand
))
701 if ((opts
& OPENSSL_INIT_ENGINE_DYNAMIC
)
702 && !RUN_ONCE(&engine_dynamic
, ossl_init_engine_dynamic
))
704 # ifndef OPENSSL_NO_STATIC_ENGINE
705 # ifndef OPENSSL_NO_DEVCRYPTOENG
706 if ((opts
& OPENSSL_INIT_ENGINE_CRYPTODEV
)
707 && !RUN_ONCE(&engine_devcrypto
, ossl_init_engine_devcrypto
))
710 # if !defined(OPENSSL_NO_PADLOCKENG)
711 if ((opts
& OPENSSL_INIT_ENGINE_PADLOCK
)
712 && !RUN_ONCE(&engine_padlock
, ossl_init_engine_padlock
))
715 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
716 if ((opts
& OPENSSL_INIT_ENGINE_CAPI
)
717 && !RUN_ONCE(&engine_capi
, ossl_init_engine_capi
))
720 # if !defined(OPENSSL_NO_AFALGENG)
721 if ((opts
& OPENSSL_INIT_ENGINE_AFALG
)
722 && !RUN_ONCE(&engine_afalg
, ossl_init_engine_afalg
))
726 if (opts
& (OPENSSL_INIT_ENGINE_ALL_BUILTIN
727 | OPENSSL_INIT_ENGINE_OPENSSL
728 | OPENSSL_INIT_ENGINE_AFALG
)) {
729 ENGINE_register_all_complete();
733 #ifndef OPENSSL_NO_COMP
734 if ((opts
& OPENSSL_INIT_ZLIB
)
735 && !RUN_ONCE(&zlib
, ossl_init_zlib
))
742 int OPENSSL_atexit(void (*handler
)(void))
744 OPENSSL_INIT_STOP
*newhand
;
746 #if !defined(OPENSSL_USE_NODELETE)\
747 && !defined(OPENSSL_NO_PINSHARED)
754 handlersym
.func
= handler
;
755 # if defined(DSO_WIN32) && !defined(_WIN32_WCE)
757 HMODULE handle
= NULL
;
761 * We don't use the DSO route for WIN32 because there is a better
764 ret
= GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
765 | GET_MODULE_HANDLE_EX_FLAG_PIN
,
766 handlersym
.sym
, &handle
);
771 # elif !defined(DSO_NONE)
773 * Deliberately leak a reference to the handler. This will force the
774 * library/code containing the handler to remain loaded until we run the
775 * atexit handler. If -znodelete has been used then this is
782 dso
= DSO_dsobyaddr(handlersym
.sym
, DSO_FLAG_NO_UNLOAD_ON_FREE
);
783 /* See same code above in ossl_init_base() for an explanation. */
785 "atexit: obtained DSO reference? %s\n",
786 (dso
== NULL
? "No!" : "Yes."));
794 if ((newhand
= OPENSSL_malloc(sizeof(*newhand
))) == NULL
) {
795 CRYPTOerr(CRYPTO_F_OPENSSL_ATEXIT
, ERR_R_MALLOC_FAILURE
);
799 newhand
->handler
= handler
;
800 newhand
->next
= stop_handlers
;
801 stop_handlers
= newhand
;
806 #ifdef OPENSSL_SYS_UNIX
808 * The following three functions are for OpenSSL developers. This is
809 * where we set/reset state across fork (called via pthread_atfork when
810 * it exists, or manually by the application when it doesn't).
812 * WARNING! If you put code in either OPENSSL_fork_parent or
813 * OPENSSL_fork_child, you MUST MAKE SURE that they are async-signal-
814 * safe. See this link, for example:
815 * http://man7.org/linux/man-pages/man7/signal-safety.7.html
818 void OPENSSL_fork_prepare(void)
822 void OPENSSL_fork_parent(void)
826 void OPENSSL_fork_child(void)