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