Swap the init code to use the new Thread API thread locals
[openssl.git] / crypto / init.c
1 /*
2  * Written by Matt Caswell for the OpenSSL project.
3  */
4 /* ====================================================================
5  * Copyright (c) 2016 The OpenSSL Project.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. All advertising materials mentioning features or use of this
20  *    software must display the following acknowledgment:
21  *    "This product includes software developed by the OpenSSL Project
22  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
23  *
24  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25  *    endorse or promote products derived from this software without
26  *    prior written permission. For written permission, please contact
27  *    openssl-core@openssl.org.
28  *
29  * 5. Products derived from this software may not be called "OpenSSL"
30  *    nor may "OpenSSL" appear in their names without prior written
31  *    permission of the OpenSSL Project.
32  *
33  * 6. Redistributions of any form whatsoever must retain the following
34  *    acknowledgment:
35  *    "This product includes software developed by the OpenSSL Project
36  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49  * OF THE POSSIBILITY OF SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This product includes cryptographic software written by Eric Young
53  * (eay@cryptsoft.com).  This product includes software written by Tim
54  * Hudson (tjh@cryptsoft.com).
55  *
56  */
57
58 #include <internal/threads.h>
59 #include <internal/cryptlib_int.h>
60 #include <openssl/err.h>
61 #include <openssl/rand.h>
62 #include <openssl/evp.h>
63 #include <internal/evp_int.h>
64 #include <internal/conf.h>
65 #include <internal/async.h>
66 #ifndef OPENSSL_NO_ENGINE
67 #include <internal/engine.h>
68 #endif
69 #include <openssl/comp.h>
70 #include <internal/err.h>
71 #include <stdlib.h>
72 #include <assert.h>
73
74 static int stopped = 0;
75
76 static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
77
78 static CRYPTO_THREAD_LOCAL threadstopkey;
79
80 static void ossl_init_thread_stop_wrap(void *local)
81 {
82     ossl_init_thread_stop((struct thread_local_inits_st *)local);
83 }
84
85 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
86 {
87     struct thread_local_inits_st *local =
88         CRYPTO_THREAD_get_local(&threadstopkey);
89
90     if (local == NULL && alloc) {
91         local = OPENSSL_zalloc(sizeof *local);
92         CRYPTO_THREAD_set_local(&threadstopkey, local);
93     }
94     if (!alloc) {
95         CRYPTO_THREAD_set_local(&threadstopkey, NULL);
96     }
97
98     return local;
99 }
100
101 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
102 struct ossl_init_stop_st {
103     void (*handler)(void);
104     OPENSSL_INIT_STOP *next;
105 };
106
107 static OPENSSL_INIT_STOP *stop_handlers = NULL;
108
109 static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
110 static int base_inited = 0;
111 static void ossl_init_base(void)
112 {
113 #ifdef OPENSSL_INIT_DEBUG
114     fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
115 #endif
116     /*
117      * We use a dummy thread local key here. We use the destructor to detect
118      * when the thread is going to stop (where that feature is available)
119      */
120     CRYPTO_THREAD_init_local(&threadstopkey, ossl_init_thread_stop_wrap);
121 #ifndef OPENSSL_SYS_UEFI
122     atexit(OPENSSL_cleanup);
123 #endif
124     OPENSSL_cpuid_setup();
125     base_inited = 1;
126 }
127
128 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
129 static int load_crypto_strings_inited = 0;
130 static void ossl_init_no_load_crypto_strings(void)
131 {
132     /* Do nothing in this case */
133     return;
134 }
135
136 static void ossl_init_load_crypto_strings(void)
137 {
138     /*
139      * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
140      * pulling in all the error strings during static linking
141      */
142 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
143 # ifdef OPENSSL_INIT_DEBUG
144     fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
145                     "err_load_crypto_strings_intern()\n");
146 # endif
147     err_load_crypto_strings_intern();
148 #endif
149     load_crypto_strings_inited = 1;
150 }
151
152 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
153 static void ossl_init_add_all_ciphers(void)
154 {
155     /*
156      * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
157      * pulling in all the ciphers during static linking
158      */
159 #ifndef OPENSSL_NO_AUTOALGINIT
160 # ifdef OPENSSL_INIT_DEBUG
161     fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
162                     "openssl_add_all_ciphers_internal()\n");
163 # endif
164     openssl_add_all_ciphers_internal();
165 # ifndef OPENSSL_NO_ENGINE
166 #  if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
167     ENGINE_setup_bsd_cryptodev();
168 #  endif
169 # endif
170 #endif
171 }
172
173 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
174 static void ossl_init_add_all_digests(void)
175 {
176     /*
177      * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
178      * pulling in all the ciphers during static linking
179      */
180 #ifndef OPENSSL_NO_AUTOALGINIT
181 # ifdef OPENSSL_INIT_DEBUG
182     fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
183                     "openssl_add_all_digests_internal()\n");
184 # endif
185     openssl_add_all_digests_internal();
186 # ifndef OPENSSL_NO_ENGINE
187 #  if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
188     ENGINE_setup_bsd_cryptodev();
189 #  endif
190 # endif
191 #endif
192 }
193
194 static void ossl_init_no_add_algs(void)
195 {
196     /* Do nothing */
197     return;
198 }
199
200 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
201 static int config_inited = 0;
202 static const char *config_filename;
203 static void ossl_init_config(void)
204 {
205 #ifdef OPENSSL_INIT_DEBUG
206     fprintf(stderr,
207             "OPENSSL_INIT: ossl_init_config: openssl_config_internal(%s)\n",
208             config_filename==NULL?"NULL":config_filename);
209 #endif
210     openssl_config_internal(config_filename);
211     config_inited = 1;
212 }
213 static void ossl_init_no_config(void)
214 {
215 #ifdef OPENSSL_INIT_DEBUG
216     fprintf(stderr,
217             "OPENSSL_INIT: ossl_init_config: openssl_no_config_internal()\n");
218 #endif
219     openssl_no_config_internal();
220     config_inited = 1;
221 }
222
223 #ifndef OPENSSL_NO_ASYNC
224 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
225 static int async_inited = 0;
226 static void ossl_init_async(void)
227 {
228 #ifdef OPENSSL_INIT_DEBUG
229     fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
230 #endif
231     async_init();
232     async_inited = 1;
233 }
234 #endif
235
236 #ifndef OPENSSL_NO_ENGINE
237 static int engine_inited = 0;
238 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
239 static void ossl_init_engine_openssl(void)
240 {
241 # ifdef OPENSSL_INIT_DEBUG
242     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
243                     "engine_load_openssl_internal()\n");
244 # endif
245     engine_load_openssl_internal();
246     engine_inited = 1;
247 }
248 # if !defined(OPENSSL_NO_HW) && \
249     (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
250 static CRYPTO_ONCE engine_cryptodev = CRYPTO_ONCE_STATIC_INIT;
251 static void ossl_init_engine_cryptodev(void)
252 {
253 #  ifdef OPENSSL_INIT_DEBUG
254     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
255                     "engine_load_cryptodev_internal()\n");
256 #  endif
257     engine_load_cryptodev_internal();
258     engine_inited = 1;
259 }
260 # endif
261
262 # ifndef OPENSSL_NO_RDRAND
263 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
264 static void ossl_init_engine_rdrand(void)
265 {
266 #  ifdef OPENSSL_INIT_DEBUG
267     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
268                     "engine_load_rdrand_internal()\n");
269 #  endif
270     engine_load_rdrand_internal();
271     engine_inited = 1;
272 }
273 # endif
274 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
275 static void ossl_init_engine_dynamic(void)
276 {
277 # ifdef OPENSSL_INIT_DEBUG
278     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
279                     "engine_load_dynamic_internal()\n");
280 # endif
281     engine_load_dynamic_internal();
282     engine_inited = 1;
283 }
284 # ifndef OPENSSL_NO_STATIC_ENGINE
285 #  if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
286 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
287 static void ossl_init_engine_padlock(void)
288 {
289 #   ifdef OPENSSL_INIT_DEBUG
290     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
291                     "engine_load_padlock_internal()\n");
292 #   endif
293     engine_load_padlock_internal();
294     engine_inited = 1;
295 }
296 #  endif
297 #  if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
298 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
299 static void ossl_init_engine_capi(void)
300 {
301 #   ifdef OPENSSL_INIT_DEBUG
302     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
303                     "engine_load_capi_internal()\n");
304 #   endif
305     engine_load_capi_internal();
306     engine_inited = 1;
307 }
308 #  endif
309 static CRYPTO_ONCE engine_dasync = CRYPTO_ONCE_STATIC_INIT;
310 static void ossl_init_engine_dasync(void)
311 {
312 # ifdef OPENSSL_INIT_DEBUG
313     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dasync: "
314                     "engine_load_dasync_internal()\n");
315 # endif
316     engine_load_dasync_internal();
317     engine_inited = 1;
318 }
319 #  if !defined(OPENSSL_NO_AFALGENG)
320 static OPENSSL_INIT_ONCE engine_afalg = OPENSSL_INIT_ONCE_STATIC_INIT;
321 static void ossl_init_engine_afalg(void)
322 {
323 #   ifdef OPENSSL_INIT_DEBUG
324     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
325                     "engine_load_afalg_internal()\n");
326 #   endif
327     engine_load_afalg_internal();
328     engine_inited = 1;
329 }
330 #  endif
331 # endif
332 #endif
333
334 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
335 static int zlib_inited = 0;
336 static void ossl_init_zlib(void)
337 {
338     /* Do nothing - we need to know about this for the later cleanup */
339     zlib_inited = 1;
340 }
341
342 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
343 {
344     /* Can't do much about this */
345     if (locals == NULL)
346         return;
347
348 #ifndef OPENSSL_NO_ASYNC
349     if (locals->async) {
350 #ifdef OPENSSL_INIT_DEBUG
351         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
352                         "ASYNC_cleanup_thread()\n");
353 #endif
354         ASYNC_cleanup_thread();
355     }
356 #endif
357
358     if (locals->err_state) {
359 #ifdef OPENSSL_INIT_DEBUG
360         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
361                         "ERR_remove_thread_state(NULL)\n");
362 #endif
363         ERR_remove_thread_state(NULL);
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 = ossl_init_get_thread_local(1);
378
379     if (locals == NULL)
380         return 0;
381
382     if (opts & OPENSSL_INIT_THREAD_ASYNC) {
383 #ifdef OPENSSL_INIT_DEBUG
384         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
385                         "marking thread for async\n");
386 #endif
387         locals->async = 1;
388     }
389
390     if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
391 #ifdef OPENSSL_INIT_DEBUG
392         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
393                         "marking thread for err_state\n");
394 #endif
395         locals->err_state = 1;
396     }
397
398     return 1;
399 }
400
401 void OPENSSL_cleanup(void)
402 {
403     OPENSSL_INIT_STOP *currhandler, *lasthandler;
404
405     /* If we've not been inited then no need to deinit */
406     if (!base_inited)
407         return;
408
409     /* Might be explicitly called and also by atexit */
410     if (stopped)
411         return;
412     stopped = 1;
413
414     /*
415      * Thread stop may not get automatically called by the thread library for
416      * the very last thread in some situations, so call it directly.
417      */
418     ossl_init_thread_stop(ossl_init_get_thread_local(0));
419
420     currhandler = stop_handlers;
421     while (currhandler != NULL) {
422         currhandler->handler();
423         lasthandler = currhandler;
424         currhandler = currhandler->next;
425         OPENSSL_free(lasthandler);
426     }
427     stop_handlers = NULL;
428     /*
429      * We assume we are single-threaded for this function, i.e. no race
430      * conditions for the various "*_inited" vars below.
431      */
432
433     if (zlib_inited) {
434 #ifdef OPENSSL_INIT_DEBUG
435         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
436                         "COMP_zlib_cleanup()\n");
437 #endif
438         COMP_zlib_cleanup();
439     }
440
441 #ifndef OPENSSL_NO_ASYNC
442     if (async_inited) {
443 # ifdef OPENSSL_INIT_DEBUG
444         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
445                         "async_deinit()\n");
446 # endif
447         async_deinit();
448     }
449 #endif
450
451 #ifndef OPENSSL_NO_ENGINE
452     if (engine_inited) {
453 # ifdef OPENSSL_INIT_DEBUG
454         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
455                         "ENGINE_cleanup()\n");
456 # endif
457         ENGINE_cleanup();
458     }
459 #endif
460
461     if (load_crypto_strings_inited) {
462 #ifdef OPENSSL_INIT_DEBUG
463         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
464                         "ERR_free_strings()\n");
465 #endif
466         ERR_free_strings();
467     }
468
469     CRYPTO_THREAD_cleanup_local(&threadstopkey);
470
471 #ifdef OPENSSL_INIT_DEBUG
472     fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
473                     "CRYPTO_cleanup_all_ex_data()\n");
474     fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
475                     "EVP_cleanup()\n");
476     fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
477                     "CONF_modules_free()\n");
478     fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
479                     "RAND_cleanup()\n");
480 #endif
481     CRYPTO_cleanup_all_ex_data();
482     EVP_cleanup();
483     CONF_modules_free();
484     RAND_cleanup();
485     base_inited = 0;
486 }
487
488 /*
489  * If this function is called with a non NULL settings value then it must be
490  * called prior to any threads making calls to any OpenSSL functions,
491  * i.e. passing a non-null settings value is assumed to be single-threaded.
492  */
493 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
494 {
495     static int stoperrset = 0;
496
497     if (stopped) {
498         if (!stoperrset) {
499             /*
500              * We only ever set this once to avoid getting into an infinite
501              * loop where the error system keeps trying to init and fails so
502              * sets an error etc
503              */
504             stoperrset = 1;
505             CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
506         }
507         return 0;
508     }
509
510     if (!CRYPTO_THREAD_run_once(&base, ossl_init_base))
511         return 0;
512
513     if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
514             && !CRYPTO_THREAD_run_once(&load_crypto_strings,
515                                        ossl_init_no_load_crypto_strings))
516         return 0;
517
518     if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
519             && !CRYPTO_THREAD_run_once(&load_crypto_strings,
520                                        ossl_init_load_crypto_strings))
521         return 0;
522
523     if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
524             && !CRYPTO_THREAD_run_once(&add_all_ciphers, ossl_init_no_add_algs))
525         return 0;
526
527     if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
528             && !CRYPTO_THREAD_run_once(&add_all_ciphers,
529                                        ossl_init_add_all_ciphers))
530         return 0;
531
532     if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
533             && !CRYPTO_THREAD_run_once(&add_all_digests, ossl_init_no_add_algs))
534         return 0;
535
536     if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
537             && !CRYPTO_THREAD_run_once(&add_all_digests,
538                                        ossl_init_add_all_digests))
539         return 0;
540
541     if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
542             && !CRYPTO_THREAD_run_once(&config, ossl_init_no_config))
543         return 0;
544
545     if (opts & OPENSSL_INIT_LOAD_CONFIG) {
546         int ret;
547         CRYPTO_w_lock(CRYPTO_LOCK_INIT);
548         config_filename = (settings == NULL) ? NULL : settings->config_name;
549         ret = CRYPTO_THREAD_run_once(&config, ossl_init_config);
550         CRYPTO_w_unlock(CRYPTO_LOCK_INIT);
551         if (!ret)
552             return 0;
553     }
554
555 #ifndef OPENSSL_NO_ASYNC
556     if ((opts & OPENSSL_INIT_ASYNC)
557             && !CRYPTO_THREAD_run_once(&async, ossl_init_async))
558         return 0;
559 #endif
560 #ifndef OPENSSL_NO_ENGINE
561     if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
562             && !CRYPTO_THREAD_run_once(&engine_openssl,
563                                        ossl_init_engine_openssl))
564         return 0;
565 # if !defined(OPENSSL_NO_HW) && \
566     (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
567     if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
568             && !CRYPTO_THREAD_run_once(&engine_cryptodev,
569                                        ossl_init_engine_cryptodev))
570         return 0;
571 # endif
572 # ifndef OPENSSL_NO_RDRAND
573     if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
574             && !CRYPTO_THREAD_run_once(&engine_rdrand, ossl_init_engine_rdrand))
575         return 0;
576 # endif
577     if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
578             && !CRYPTO_THREAD_run_once(&engine_dynamic,
579                                        ossl_init_engine_dynamic))
580         return 0;
581 # ifndef OPENSSL_NO_STATIC_ENGINE
582 #  if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
583     if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
584             && CRYPTO_THREAD_run_once(&engine_padlock,
585                                       ossl_init_engine_padlock))
586         return 0;
587 #  endif
588 #  if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
589     if ((opts & OPENSSL_INIT_ENGINE_CAPI)
590             && CRYPTO_THREAD_run_once(&engine_capi, ossl_init_engine_capi))
591         return 0;
592 #  endif
593     if ((opts & OPENSSL_INIT_ENGINE_DASYNC)
594             && !CRYPTO_THREAD_run_once(&engine_dasync, ossl_init_engine_dasync))
595         return 0;
596 #  if !defined(OPENSSL_NO_AFALGENG)
597     if ((opts & OPENSSL_INIT_ENGINE_AFALG)
598             && !CRYPTO_THREAD_run_once(&engine_afalg, ossl_init_engine_afalg))
599         return 0;
600 #  endif
601 # endif
602     if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
603                 | OPENSSL_INIT_ENGINE_DASYNC | OPENSSL_INIT_ENGINE_OPENSSL
604                 | OPENSSL_INIT_ENGINE_AFALG)) {
605         ENGINE_register_all_complete();
606     }
607 #endif
608
609     if ((opts & OPENSSL_INIT_ZLIB)
610             && CRYPTO_THREAD_run_once(&zlib, ossl_init_zlib))
611         return 0;
612
613     return 1;
614 }
615
616 int OPENSSL_atexit(void (*handler)(void))
617 {
618     OPENSSL_INIT_STOP *newhand;
619
620     newhand = OPENSSL_malloc(sizeof(*newhand));
621     if (newhand == NULL)
622         return 0;
623
624     newhand->handler = handler;
625     newhand->next = stop_handlers;
626     stop_handlers = newhand;
627
628     return 1;
629 }
630
631