]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/init.c
e_os.h removal from other headers and source files.
[thirdparty/openssl.git] / crypto / init.c
1 /*
2 * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (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
8 */
9
10 #include "internal/cryptlib_int.h"
11 #include <openssl/err.h>
12 #include "internal/rand_int.h"
13 #include "internal/bio.h"
14 #include <openssl/evp.h>
15 #include "internal/evp_int.h"
16 #include "internal/conf.h"
17 #include "internal/async.h"
18 #include "internal/engine.h"
19 #include "internal/comp.h"
20 #include "internal/err.h"
21 #include "internal/err_int.h"
22 #include "internal/objects.h"
23 #include <stdlib.h>
24 #include <assert.h>
25 #include "internal/thread_once.h"
26 #include "internal/dso.h"
27 #include "internal/store.h"
28 #include "e_os.h"
29
30 static int stopped = 0;
31
32 static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
33
34 static CRYPTO_THREAD_LOCAL threadstopkey;
35
36 static void ossl_init_thread_stop_wrap(void *local)
37 {
38 ossl_init_thread_stop((struct thread_local_inits_st *)local);
39 }
40
41 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
42 {
43 struct thread_local_inits_st *local =
44 CRYPTO_THREAD_get_local(&threadstopkey);
45
46 if (local == NULL && alloc) {
47 local = OPENSSL_zalloc(sizeof *local);
48 if (local != NULL && !CRYPTO_THREAD_set_local(&threadstopkey, local)) {
49 OPENSSL_free(local);
50 return NULL;
51 }
52 }
53 if (!alloc) {
54 CRYPTO_THREAD_set_local(&threadstopkey, NULL);
55 }
56
57 return local;
58 }
59
60 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
61 struct ossl_init_stop_st {
62 void (*handler)(void);
63 OPENSSL_INIT_STOP *next;
64 };
65
66 static OPENSSL_INIT_STOP *stop_handlers = NULL;
67 static CRYPTO_RWLOCK *init_lock = NULL;
68
69 static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
70 static int base_inited = 0;
71 DEFINE_RUN_ONCE_STATIC(ossl_init_base)
72 {
73 #ifdef OPENSSL_INIT_DEBUG
74 fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
75 #endif
76 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
77 ossl_malloc_setup_failures();
78 #endif
79 /*
80 * We use a dummy thread local key here. We use the destructor to detect
81 * when the thread is going to stop (where that feature is available)
82 */
83 CRYPTO_THREAD_init_local(&threadstopkey, ossl_init_thread_stop_wrap);
84 #ifndef OPENSSL_SYS_UEFI
85 atexit(OPENSSL_cleanup);
86 #endif
87 if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL)
88 return 0;
89 OPENSSL_cpuid_setup();
90
91 /*
92 * BIG FAT WARNING!
93 * Everything needed to be initialized in this function before threads
94 * come along MUST happen before base_inited is set to 1, or we will
95 * see race conditions.
96 */
97 base_inited = 1;
98
99 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
100 # ifdef DSO_WIN32
101 {
102 HMODULE handle = NULL;
103 BOOL ret;
104
105 /* We don't use the DSO route for WIN32 because there is a better way */
106 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
107 | GET_MODULE_HANDLE_EX_FLAG_PIN,
108 (void *)&base_inited, &handle);
109
110 return (ret == TRUE) ? 1 : 0;
111 }
112 # else
113 /*
114 * Deliberately leak a reference to ourselves. This will force the library
115 * to remain loaded until the atexit() handler is run at process exit.
116 */
117 {
118 DSO *dso = NULL;
119
120 ERR_set_mark();
121 dso = DSO_dsobyaddr(&base_inited, DSO_FLAG_NO_UNLOAD_ON_FREE);
122 DSO_free(dso);
123 ERR_pop_to_mark();
124 }
125 # endif
126 #endif
127
128 return 1;
129 }
130
131 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
132 static int load_crypto_strings_inited = 0;
133 DEFINE_RUN_ONCE_STATIC(ossl_init_no_load_crypto_strings)
134 {
135 /* Do nothing in this case */
136 return 1;
137 }
138
139 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
140 {
141 int ret = 1;
142 /*
143 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
144 * pulling in all the error strings during static linking
145 */
146 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
147 # ifdef OPENSSL_INIT_DEBUG
148 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
149 "err_load_crypto_strings_int()\n");
150 # endif
151 ret = err_load_crypto_strings_int();
152 load_crypto_strings_inited = 1;
153 #endif
154 return ret;
155 }
156
157 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
158 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
159 {
160 /*
161 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
162 * pulling in all the ciphers during static linking
163 */
164 #ifndef OPENSSL_NO_AUTOALGINIT
165 # ifdef OPENSSL_INIT_DEBUG
166 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
167 "openssl_add_all_ciphers_int()\n");
168 # endif
169 openssl_add_all_ciphers_int();
170 #endif
171 return 1;
172 }
173
174 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
175 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
176 {
177 /*
178 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
179 * pulling in all the ciphers during static linking
180 */
181 #ifndef OPENSSL_NO_AUTOALGINIT
182 # ifdef OPENSSL_INIT_DEBUG
183 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
184 "openssl_add_all_digests()\n");
185 # endif
186 openssl_add_all_digests_int();
187 #endif
188 return 1;
189 }
190
191 DEFINE_RUN_ONCE_STATIC(ossl_init_no_add_algs)
192 {
193 /* Do nothing */
194 return 1;
195 }
196
197 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
198 static int config_inited = 0;
199 static const char *appname;
200 DEFINE_RUN_ONCE_STATIC(ossl_init_config)
201 {
202 #ifdef OPENSSL_INIT_DEBUG
203 fprintf(stderr,
204 "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n",
205 appname == NULL ? "NULL" : appname);
206 #endif
207 openssl_config_int(appname);
208 config_inited = 1;
209 return 1;
210 }
211 DEFINE_RUN_ONCE_STATIC(ossl_init_no_config)
212 {
213 #ifdef OPENSSL_INIT_DEBUG
214 fprintf(stderr,
215 "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
216 #endif
217 openssl_no_config_int();
218 config_inited = 1;
219 return 1;
220 }
221
222 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
223 static int async_inited = 0;
224 DEFINE_RUN_ONCE_STATIC(ossl_init_async)
225 {
226 #ifdef OPENSSL_INIT_DEBUG
227 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
228 #endif
229 if (!async_init())
230 return 0;
231 async_inited = 1;
232 return 1;
233 }
234
235 #ifndef OPENSSL_NO_ENGINE
236 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
237 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
238 {
239 # ifdef OPENSSL_INIT_DEBUG
240 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
241 "engine_load_openssl_int()\n");
242 # endif
243 engine_load_openssl_int();
244 return 1;
245 }
246 # ifndef OPENSSL_NO_DEVCRYPTOENG
247 static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
248 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
249 {
250 # ifdef OPENSSL_INIT_DEBUG
251 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_devcrypto: "
252 "engine_load_devcrypto_int()\n");
253 # endif
254 engine_load_devcrypto_int();
255 return 1;
256 }
257 # endif
258
259 # ifndef OPENSSL_NO_RDRAND
260 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
261 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
262 {
263 # ifdef OPENSSL_INIT_DEBUG
264 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
265 "engine_load_rdrand_int()\n");
266 # endif
267 engine_load_rdrand_int();
268 return 1;
269 }
270 # endif
271 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
272 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
273 {
274 # ifdef OPENSSL_INIT_DEBUG
275 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
276 "engine_load_dynamic_int()\n");
277 # endif
278 engine_load_dynamic_int();
279 return 1;
280 }
281 # ifndef OPENSSL_NO_STATIC_ENGINE
282 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
283 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
284 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
285 {
286 # ifdef OPENSSL_INIT_DEBUG
287 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
288 "engine_load_padlock_int()\n");
289 # endif
290 engine_load_padlock_int();
291 return 1;
292 }
293 # endif
294 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
295 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
296 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
297 {
298 # ifdef OPENSSL_INIT_DEBUG
299 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
300 "engine_load_capi_int()\n");
301 # endif
302 engine_load_capi_int();
303 return 1;
304 }
305 # endif
306 # if !defined(OPENSSL_NO_AFALGENG)
307 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
308 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
309 {
310 # ifdef OPENSSL_INIT_DEBUG
311 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
312 "engine_load_afalg_int()\n");
313 # endif
314 engine_load_afalg_int();
315 return 1;
316 }
317 # endif
318 # endif
319 #endif
320
321 #ifndef OPENSSL_NO_COMP
322 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
323
324 static int zlib_inited = 0;
325 DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
326 {
327 /* Do nothing - we need to know about this for the later cleanup */
328 zlib_inited = 1;
329 return 1;
330 }
331 #endif
332
333 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
334 {
335 /* Can't do much about this */
336 if (locals == NULL)
337 return;
338
339 if (locals->async) {
340 #ifdef OPENSSL_INIT_DEBUG
341 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
342 "ASYNC_cleanup_thread()\n");
343 #endif
344 ASYNC_cleanup_thread();
345 }
346
347 if (locals->err_state) {
348 #ifdef OPENSSL_INIT_DEBUG
349 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
350 "err_delete_thread_state()\n");
351 #endif
352 err_delete_thread_state();
353 }
354
355 OPENSSL_free(locals);
356 }
357
358 void OPENSSL_thread_stop(void)
359 {
360 ossl_init_thread_stop(
361 (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
362 }
363
364 int ossl_init_thread_start(uint64_t opts)
365 {
366 struct thread_local_inits_st *locals;
367
368 if (!OPENSSL_init_crypto(0, NULL))
369 return 0;
370
371 locals = ossl_init_get_thread_local(1);
372
373 if (locals == NULL)
374 return 0;
375
376 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
377 #ifdef OPENSSL_INIT_DEBUG
378 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
379 "marking thread for async\n");
380 #endif
381 locals->async = 1;
382 }
383
384 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
385 #ifdef OPENSSL_INIT_DEBUG
386 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
387 "marking thread for err_state\n");
388 #endif
389 locals->err_state = 1;
390 }
391
392 return 1;
393 }
394
395 void OPENSSL_cleanup(void)
396 {
397 OPENSSL_INIT_STOP *currhandler, *lasthandler;
398
399 /* If we've not been inited then no need to deinit */
400 if (!base_inited)
401 return;
402
403 /* Might be explicitly called and also by atexit */
404 if (stopped)
405 return;
406 stopped = 1;
407
408 /*
409 * Thread stop may not get automatically called by the thread library for
410 * the very last thread in some situations, so call it directly.
411 */
412 ossl_init_thread_stop(ossl_init_get_thread_local(0));
413
414 currhandler = stop_handlers;
415 while (currhandler != NULL) {
416 currhandler->handler();
417 lasthandler = currhandler;
418 currhandler = currhandler->next;
419 OPENSSL_free(lasthandler);
420 }
421 stop_handlers = NULL;
422
423 CRYPTO_THREAD_lock_free(init_lock);
424
425 /*
426 * We assume we are single-threaded for this function, i.e. no race
427 * conditions for the various "*_inited" vars below.
428 */
429
430 #ifndef OPENSSL_NO_COMP
431 if (zlib_inited) {
432 #ifdef OPENSSL_INIT_DEBUG
433 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
434 "comp_zlib_cleanup_int()\n");
435 #endif
436 comp_zlib_cleanup_int();
437 }
438 #endif
439
440 if (async_inited) {
441 # ifdef OPENSSL_INIT_DEBUG
442 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
443 "async_deinit()\n");
444 # endif
445 async_deinit();
446 }
447
448 if (load_crypto_strings_inited) {
449 #ifdef OPENSSL_INIT_DEBUG
450 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
451 "err_free_strings_int()\n");
452 #endif
453 err_free_strings_int();
454 }
455
456 CRYPTO_THREAD_cleanup_local(&threadstopkey);
457
458 #ifdef OPENSSL_INIT_DEBUG
459 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
460 "rand_cleanup_int()\n");
461 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
462 "conf_modules_free_int()\n");
463 #ifndef OPENSSL_NO_ENGINE
464 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
465 "engine_cleanup_int()\n");
466 #endif
467 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
468 "crypto_cleanup_all_ex_data_int()\n");
469 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
470 "bio_sock_cleanup_int()\n");
471 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
472 "bio_cleanup()\n");
473 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
474 "evp_cleanup_int()\n");
475 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
476 "obj_cleanup_int()\n");
477 fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
478 "err_cleanup()\n");
479 #endif
480 /*
481 * Note that cleanup order is important:
482 * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
483 * must be called before engine_cleanup_int()
484 * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
485 * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
486 * - conf_modules_free_int() can end up in ENGINE code so must be called
487 * before engine_cleanup_int()
488 * - ENGINEs and additional EVP algorithms might use added OIDs names so
489 * obj_cleanup_int() must be called last
490 */
491 rand_cleanup_int();
492 rand_cleanup_drbg_int();
493 conf_modules_free_int();
494 #ifndef OPENSSL_NO_ENGINE
495 engine_cleanup_int();
496 #endif
497 ossl_store_cleanup_int();
498 crypto_cleanup_all_ex_data_int();
499 bio_cleanup();
500 evp_cleanup_int();
501 obj_cleanup_int();
502 err_cleanup();
503
504 base_inited = 0;
505 }
506
507 /*
508 * If this function is called with a non NULL settings value then it must be
509 * called prior to any threads making calls to any OpenSSL functions,
510 * i.e. passing a non-null settings value is assumed to be single-threaded.
511 */
512 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
513 {
514 static int stoperrset = 0;
515
516 if (stopped) {
517 if (!stoperrset) {
518 /*
519 * We only ever set this once to avoid getting into an infinite
520 * loop where the error system keeps trying to init and fails so
521 * sets an error etc
522 */
523 stoperrset = 1;
524 CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
525 }
526 return 0;
527 }
528
529 if (!base_inited && !RUN_ONCE(&base, ossl_init_base))
530 return 0;
531
532 if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
533 && !RUN_ONCE(&load_crypto_strings,
534 ossl_init_no_load_crypto_strings))
535 return 0;
536
537 if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
538 && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
539 return 0;
540
541 if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
542 && !RUN_ONCE(&add_all_ciphers, ossl_init_no_add_algs))
543 return 0;
544
545 if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
546 && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
547 return 0;
548
549 if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
550 && !RUN_ONCE(&add_all_digests, ossl_init_no_add_algs))
551 return 0;
552
553 if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
554 && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
555 return 0;
556
557 if ((opts & OPENSSL_INIT_ATFORK)
558 && !openssl_init_fork_handlers())
559 return 0;
560
561 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
562 && !RUN_ONCE(&config, ossl_init_no_config))
563 return 0;
564
565 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
566 int ret;
567 CRYPTO_THREAD_write_lock(init_lock);
568 appname = (settings == NULL) ? NULL : settings->appname;
569 ret = RUN_ONCE(&config, ossl_init_config);
570 CRYPTO_THREAD_unlock(init_lock);
571 if (!ret)
572 return 0;
573 }
574
575 if ((opts & OPENSSL_INIT_ASYNC)
576 && !RUN_ONCE(&async, ossl_init_async))
577 return 0;
578
579 #ifndef OPENSSL_NO_ENGINE
580 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
581 && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
582 return 0;
583 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_DEVCRYPTOENG)
584 if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
585 && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
586 return 0;
587 # endif
588 # ifndef OPENSSL_NO_RDRAND
589 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
590 && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
591 return 0;
592 # endif
593 if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
594 && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
595 return 0;
596 # ifndef OPENSSL_NO_STATIC_ENGINE
597 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
598 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
599 && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
600 return 0;
601 # endif
602 # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
603 if ((opts & OPENSSL_INIT_ENGINE_CAPI)
604 && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
605 return 0;
606 # endif
607 # if !defined(OPENSSL_NO_AFALGENG)
608 if ((opts & OPENSSL_INIT_ENGINE_AFALG)
609 && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
610 return 0;
611 # endif
612 # endif
613 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
614 | OPENSSL_INIT_ENGINE_OPENSSL
615 | OPENSSL_INIT_ENGINE_AFALG)) {
616 ENGINE_register_all_complete();
617 }
618 #endif
619
620 #ifndef OPENSSL_NO_COMP
621 if ((opts & OPENSSL_INIT_ZLIB)
622 && !RUN_ONCE(&zlib, ossl_init_zlib))
623 return 0;
624 #endif
625
626 return 1;
627 }
628
629 int OPENSSL_atexit(void (*handler)(void))
630 {
631 OPENSSL_INIT_STOP *newhand;
632
633 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
634 {
635 union {
636 void *sym;
637 void (*func)(void);
638 } handlersym;
639
640 handlersym.func = handler;
641 # ifdef DSO_WIN32
642 {
643 HMODULE handle = NULL;
644 BOOL ret;
645
646 /*
647 * We don't use the DSO route for WIN32 because there is a better
648 * way
649 */
650 ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
651 | GET_MODULE_HANDLE_EX_FLAG_PIN,
652 handlersym.sym, &handle);
653
654 if (!ret)
655 return 0;
656 }
657 # else
658 /*
659 * Deliberately leak a reference to the handler. This will force the
660 * library/code containing the handler to remain loaded until we run the
661 * atexit handler. If -znodelete has been used then this is
662 * unnecessary.
663 */
664 {
665 DSO *dso = NULL;
666
667 ERR_set_mark();
668 dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
669 DSO_free(dso);
670 ERR_pop_to_mark();
671 }
672 # endif
673 }
674 #endif
675
676 newhand = OPENSSL_malloc(sizeof(*newhand));
677 if (newhand == NULL)
678 return 0;
679
680 newhand->handler = handler;
681 newhand->next = stop_handlers;
682 stop_handlers = newhand;
683
684 return 1;
685 }
686
687 #ifdef OPENSSL_SYS_UNIX
688 /*
689 * The following three functions are for OpenSSL developers. This is
690 * where we set/reset state across fork (called via pthread_atfork when
691 * it exists, or manually by the application when it doesn't).
692 *
693 * WARNING! If you put code in either OPENSSL_fork_parent or
694 * OPENSSL_fork_child, you MUST MAKE SURE that they are async-signal-
695 * safe. See this link, for example:
696 * http://man7.org/linux/man-pages/man7/signal-safety.7.html
697 */
698
699 void OPENSSL_fork_prepare(void)
700 {
701 }
702
703 void OPENSSL_fork_parent(void)
704 {
705 }
706
707 void OPENSSL_fork_child(void)
708 {
709 rand_fork();
710 }
711 #endif