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"
31 static int stopped = 0;
34 * Since per-thread-specific-data destructors are not universally
35 * available, i.e. not on Windows, only below CRYPTO_THREAD_LOCAL key
36 * is assumed to have destructor associated. And then an effort is made
37 * to call this single destructor on non-pthread platform[s].
39 * Initial value is "impossible". It is used as guard value to shortcut
40 * destructor for threads terminating before libcrypto is initialized or
41 * after it's de-initialized. Access to the key doesn't have to be
42 * serialized for the said threads, because they didn't use libcrypto
43 * and it doesn't matter if they pick "impossible" or derefernce real
44 * key value and pull NULL past initialization in the first thread that
45 * intends to use libcrypto.
49 CRYPTO_THREAD_LOCAL value;
50 } destructor_key = { -1 };
52 static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
54 static void ossl_init_thread_destructor(void *local)
56 ossl_init_thread_stop((struct thread_local_inits_st *)local);
59 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
61 struct thread_local_inits_st *local =
62 CRYPTO_THREAD_get_local(&destructor_key.value);
66 && (local = OPENSSL_zalloc(sizeof(*local))) != NULL
67 && !CRYPTO_THREAD_set_local(&destructor_key.value, local)) {
72 CRYPTO_THREAD_set_local(&destructor_key.value, NULL);
78 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
79 struct ossl_init_stop_st {
80 void (*handler)(void);
81 OPENSSL_INIT_STOP *next;
84 static OPENSSL_INIT_STOP *stop_handlers = NULL;
85 static CRYPTO_RWLOCK *init_lock = NULL;
87 static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
88 static int base_inited = 0;
89 DEFINE_RUN_ONCE_STATIC(ossl_init_base)
91 CRYPTO_THREAD_LOCAL key;
93 #ifdef OPENSSL_INIT_DEBUG
94 fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
96 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
97 ossl_malloc_setup_failures();
99 if (!CRYPTO_THREAD_init_local(&key, ossl_init_thread_destructor))
101 if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL)
103 #ifndef OPENSSL_SYS_UEFI
104 if (atexit(OPENSSL_cleanup) != 0)
107 OPENSSL_cpuid_setup();
109 destructor_key.value = key;
114 #ifdef OPENSSL_INIT_DEBUG
115 fprintf(stderr, "OPENSSL_INIT: ossl_init_base not ok!\n");
117 CRYPTO_THREAD_lock_free(init_lock);
120 CRYPTO_THREAD_cleanup_local(&key);
124 static CRYPTO_ONCE load_crypto_nodelete = CRYPTO_ONCE_STATIC_INIT;
125 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_nodelete)
127 #ifdef OPENSSL_INIT_DEBUG
128 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_nodelete()\n");
130 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
133 HMODULE handle = NULL;
136 /* We don't use the DSO route for WIN32 because there is a better way */
137 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
138 | GET_MODULE_HANDLE_EX_FLAG_PIN,
139 (void *)&base_inited, &handle);
141 # ifdef OPENSSL_INIT_DEBUG
142 fprintf(stderr, "OPENSSL_INIT: obtained DSO reference? %s\n",
143 (ret == TRUE ? "No!" : "Yes."));
145 return (ret == TRUE) ? 1 : 0;
149 * Deliberately leak a reference to ourselves. This will force the library
150 * to remain loaded until the atexit() handler is run at process exit.
156 if (!err_shelve_state(&err))
159 dso = DSO_dsobyaddr(&base_inited, DSO_FLAG_NO_UNLOAD_ON_FREE);
160 # ifdef OPENSSL_INIT_DEBUG
161 fprintf(stderr, "OPENSSL_INIT: obtained DSO reference? %s\n",
162 (dso == NULL ? "No!" : "Yes."));
164 * In case of No!, it is uncertain our exit()-handlers can still be
165 * called. After dlclose() the whole library might have been unloaded
170 err_unshelve_state(err);
178 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
179 static int load_crypto_strings_inited = 0;
180 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
184 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
185 * pulling in all the error strings during static linking
187 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
188 # ifdef OPENSSL_INIT_DEBUG
189 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
190 "err_load_crypto_strings_int()\n");
192 ret = err_load_crypto_strings_int();
193 load_crypto_strings_inited = 1;
198 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_load_crypto_strings,
199 ossl_init_load_crypto_strings)
201 /* Do nothing in this case */
205 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
206 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
209 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
210 * pulling in all the ciphers during static linking
212 #ifndef OPENSSL_NO_AUTOALGINIT
213 # ifdef OPENSSL_INIT_DEBUG
214 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
215 "openssl_add_all_ciphers_int()\n");
217 openssl_add_all_ciphers_int();
222 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_ciphers,
223 ossl_init_add_all_ciphers)
229 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
230 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
233 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
234 * pulling in all the ciphers during static linking
236 #ifndef OPENSSL_NO_AUTOALGINIT
237 # ifdef OPENSSL_INIT_DEBUG
238 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
239 "openssl_add_all_digests()\n");
241 openssl_add_all_digests_int();
246 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_digests,
247 ossl_init_add_all_digests)
253 static CRYPTO_ONCE add_all_macs = CRYPTO_ONCE_STATIC_INIT;
254 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_macs)
257 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
258 * pulling in all the macs during static linking
260 #ifndef OPENSSL_NO_AUTOALGINIT
261 # ifdef OPENSSL_INIT_DEBUG
262 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_macs: "
263 "openssl_add_all_macs_int()\n");
265 openssl_add_all_macs_int();
270 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_macs, ossl_init_add_all_macs)
276 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
277 static int config_inited = 0;
278 static const char *appname;
279 DEFINE_RUN_ONCE_STATIC(ossl_init_config)
281 #ifdef OPENSSL_INIT_DEBUG
283 "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n",
284 appname == NULL ? "NULL" : appname);
286 openssl_config_int(appname);
290 DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_config, ossl_init_config)
292 #ifdef OPENSSL_INIT_DEBUG
294 "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
296 openssl_no_config_int();
301 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
302 static int async_inited = 0;
303 DEFINE_RUN_ONCE_STATIC(ossl_init_async)
305 #ifdef OPENSSL_INIT_DEBUG
306 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
314 #ifndef OPENSSL_NO_ENGINE
315 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
316 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
318 # ifdef OPENSSL_INIT_DEBUG
319 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
320 "engine_load_openssl_int()\n");
322 engine_load_openssl_int();
325 # ifndef OPENSSL_NO_DEVCRYPTOENG
326 static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
327 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
329 # ifdef OPENSSL_INIT_DEBUG
330 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_devcrypto: "
331 "engine_load_devcrypto_int()\n");
333 engine_load_devcrypto_int();
338 # ifndef OPENSSL_NO_RDRAND
339 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
340 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
342 # ifdef OPENSSL_INIT_DEBUG
343 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
344 "engine_load_rdrand_int()\n");
346 engine_load_rdrand_int();
350 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
351 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
353 # ifdef OPENSSL_INIT_DEBUG
354 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
355 "engine_load_dynamic_int()\n");
357 engine_load_dynamic_int();
360 # ifndef OPENSSL_NO_STATIC_ENGINE
361 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
362 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
363 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
365 # ifdef OPENSSL_INIT_DEBUG
366 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
367 "engine_load_padlock_int()\n");
369 engine_load_padlock_int();
373 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
374 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
375 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
377 # ifdef OPENSSL_INIT_DEBUG
378 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
379 "engine_load_capi_int()\n");
381 engine_load_capi_int();
385 # if !defined(OPENSSL_NO_AFALGENG)
386 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
387 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
389 # ifdef OPENSSL_INIT_DEBUG
390 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
391 "engine_load_afalg_int()\n");
393 engine_load_afalg_int();
400 #ifndef OPENSSL_NO_COMP
401 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
403 static int zlib_inited = 0;
404 DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
406 /* Do nothing - we need to know about this for the later cleanup */
412 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
414 /* Can't do much about this */
419 #ifdef OPENSSL_INIT_DEBUG
420 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
421 "async_delete_thread_state()\n");
423 async_delete_thread_state();
426 if (locals->err_state) {
427 #ifdef OPENSSL_INIT_DEBUG
428 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
429 "err_delete_thread_state()\n");
431 err_delete_thread_state();
435 #ifdef OPENSSL_INIT_DEBUG
436 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
437 "drbg_delete_thread_state()\n");
439 drbg_delete_thread_state();
442 OPENSSL_free(locals);
445 void OPENSSL_thread_stop(void)
447 if (destructor_key.sane != -1)
448 ossl_init_thread_stop(ossl_init_get_thread_local(0));
451 int ossl_init_thread_start(uint64_t opts)
453 struct thread_local_inits_st *locals;
455 if (!OPENSSL_init_crypto(0, NULL))
458 locals = ossl_init_get_thread_local(1);
463 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
464 #ifdef OPENSSL_INIT_DEBUG
465 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
466 "marking thread for async\n");
471 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
472 #ifdef OPENSSL_INIT_DEBUG
473 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
474 "marking thread for err_state\n");
476 locals->err_state = 1;
479 if (opts & OPENSSL_INIT_THREAD_RAND) {
480 #ifdef OPENSSL_INIT_DEBUG
481 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
482 "marking thread for rand\n");
490 void OPENSSL_cleanup(void)
492 OPENSSL_INIT_STOP *currhandler, *lasthandler;
493 CRYPTO_THREAD_LOCAL key;
495 /* If we've not been inited then no need to deinit */
499 /* Might be explicitly called and also by atexit */
505 * Thread stop may not get automatically called by the thread library for
506 * the very last thread in some situations, so call it directly.
508 ossl_init_thread_stop(ossl_init_get_thread_local(0));
510 currhandler = stop_handlers;
511 while (currhandler != NULL) {
512 currhandler->handler();
513 lasthandler = currhandler;
514 currhandler = currhandler->next;
515 OPENSSL_free(lasthandler);
517 stop_handlers = NULL;
519 CRYPTO_THREAD_lock_free(init_lock);
523 * We assume we are single-threaded for this function, i.e. no race
524 * conditions for the various "*_inited" vars below.
527 #ifndef OPENSSL_NO_COMP
529 #ifdef OPENSSL_INIT_DEBUG
530 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
531 "comp_zlib_cleanup_int()\n");
533 comp_zlib_cleanup_int();
538 # ifdef OPENSSL_INIT_DEBUG
539 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
545 if (load_crypto_strings_inited) {
546 #ifdef OPENSSL_INIT_DEBUG
547 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
548 "err_free_strings_int()\n");
550 err_free_strings_int();
553 key = destructor_key.value;
554 destructor_key.sane = -1;
555 CRYPTO_THREAD_cleanup_local(&key);
557 #ifdef OPENSSL_INIT_DEBUG
558 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
559 "rand_cleanup_int()\n");
560 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
561 "conf_modules_free_int()\n");
562 #ifndef OPENSSL_NO_ENGINE
563 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
564 "engine_cleanup_int()\n");
566 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
567 "crypto_cleanup_all_ex_data_int()\n");
568 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
569 "bio_sock_cleanup_int()\n");
570 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
572 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
573 "evp_cleanup_int()\n");
574 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
575 "obj_cleanup_int()\n");
576 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
580 * Note that cleanup order is important:
581 * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
582 * must be called before engine_cleanup_int()
583 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
584 * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
585 * - conf_modules_free_int() can end up in ENGINE code so must be called
586 * before engine_cleanup_int()
587 * - ENGINEs and additional EVP algorithms might use added OIDs names so
588 * obj_cleanup_int() must be called last
591 rand_drbg_cleanup_int();
592 conf_modules_free_int();
593 #ifndef OPENSSL_NO_ENGINE
594 engine_cleanup_int();
596 ossl_store_cleanup_int();
597 crypto_cleanup_all_ex_data_int();
603 CRYPTO_secure_malloc_done();
609 * If this function is called with a non NULL settings value then it must be
610 * called prior to any threads making calls to any OpenSSL functions,
611 * i.e. passing a non-null settings value is assumed to be single-threaded.
613 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
616 if (!(opts & OPENSSL_INIT_BASE_ONLY))
617 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
621 if (!RUN_ONCE(&base, ossl_init_base))
624 if (!(opts & OPENSSL_INIT_BASE_ONLY)
625 && !RUN_ONCE(&load_crypto_nodelete,
626 ossl_init_load_crypto_nodelete))
629 if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
630 && !RUN_ONCE_ALT(&load_crypto_strings,
631 ossl_init_no_load_crypto_strings,
632 ossl_init_load_crypto_strings))
635 if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
636 && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
639 if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
640 && !RUN_ONCE_ALT(&add_all_ciphers, ossl_init_no_add_all_ciphers,
641 ossl_init_add_all_ciphers))
644 if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
645 && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
648 if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
649 && !RUN_ONCE_ALT(&add_all_digests, ossl_init_no_add_all_digests,
650 ossl_init_add_all_digests))
653 if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
654 && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
657 if ((opts & OPENSSL_INIT_NO_ADD_ALL_MACS)
658 && !RUN_ONCE_ALT(&add_all_macs, ossl_init_no_add_all_macs,
659 ossl_init_add_all_macs))
662 if ((opts & OPENSSL_INIT_ADD_ALL_MACS)
663 && !RUN_ONCE(&add_all_macs, ossl_init_add_all_macs))
666 if ((opts & OPENSSL_INIT_ATFORK)
667 && !openssl_init_fork_handlers())
670 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
671 && !RUN_ONCE_ALT(&config, ossl_init_no_config, ossl_init_config))
674 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
676 CRYPTO_THREAD_write_lock(init_lock);
677 appname = (settings == NULL) ? NULL : settings->appname;
678 ret = RUN_ONCE(&config, ossl_init_config);
679 CRYPTO_THREAD_unlock(init_lock);
684 if ((opts & OPENSSL_INIT_ASYNC)
685 && !RUN_ONCE(&async, ossl_init_async))
688 #ifndef OPENSSL_NO_ENGINE
689 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
690 && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
692 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_DEVCRYPTOENG)
693 if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
694 && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
697 # ifndef OPENSSL_NO_RDRAND
698 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
699 && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
702 if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
703 && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
705 # ifndef OPENSSL_NO_STATIC_ENGINE
706 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
707 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
708 && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
711 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
712 if ((opts & OPENSSL_INIT_ENGINE_CAPI)
713 && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
716 # if !defined(OPENSSL_NO_AFALGENG)
717 if ((opts & OPENSSL_INIT_ENGINE_AFALG)
718 && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
722 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
723 | OPENSSL_INIT_ENGINE_OPENSSL
724 | OPENSSL_INIT_ENGINE_AFALG)) {
725 ENGINE_register_all_complete();
729 #ifndef OPENSSL_NO_COMP
730 if ((opts & OPENSSL_INIT_ZLIB)
731 && !RUN_ONCE(&zlib, ossl_init_zlib))
738 int OPENSSL_atexit(void (*handler)(void))
740 OPENSSL_INIT_STOP *newhand;
742 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
749 handlersym.func = handler;
752 HMODULE handle = NULL;
756 * We don't use the DSO route for WIN32 because there is a better
759 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
760 | GET_MODULE_HANDLE_EX_FLAG_PIN,
761 handlersym.sym, &handle);
768 * Deliberately leak a reference to the handler. This will force the
769 * library/code containing the handler to remain loaded until we run the
770 * atexit handler. If -znodelete has been used then this is
777 dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
778 # ifdef OPENSSL_INIT_DEBUG
780 "OPENSSL_INIT: OPENSSL_atexit: obtained DSO reference? %s\n",
781 (dso == NULL ? "No!" : "Yes."));
782 /* See same code above in ossl_init_base() for an explanation. */
791 if ((newhand = OPENSSL_malloc(sizeof(*newhand))) == NULL) {
792 CRYPTOerr(CRYPTO_F_OPENSSL_ATEXIT, ERR_R_MALLOC_FAILURE);
796 newhand->handler = handler;
797 newhand->next = stop_handlers;
798 stop_handlers = newhand;
803 #ifdef OPENSSL_SYS_UNIX
805 * The following three functions are for OpenSSL developers. This is
806 * where we set/reset state across fork (called via pthread_atfork when
807 * it exists, or manually by the application when it doesn't).
809 * WARNING! If you put code in either OPENSSL_fork_parent or
810 * OPENSSL_fork_child, you MUST MAKE SURE that they are async-signal-
811 * safe. See this link, for example:
812 * http://man7.org/linux/man-pages/man7/signal-safety.7.html
815 void OPENSSL_fork_prepare(void)
819 void OPENSSL_fork_parent(void)
823 void OPENSSL_fork_child(void)