2 * Copyright 2001-2017 The OpenSSL Project Authors. All Rights Reserved.
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
14 * Initialise a engine type for use (or up its functional reference count if
15 * it's already in use). This version is only used internally.
17 int engine_unlocked_init(ENGINE *e)
21 if ((e->funct_ref == 0) && e->init)
23 * This is the first functional reference and the engine requires
24 * initialisation so we do it now.
26 to_return = e->init(e);
29 * OK, we return a functional reference which is also a structural
34 engine_ref_debug(e, 0, 1);
35 engine_ref_debug(e, 1, 1);
41 * Free a functional reference to a engine type. This version is only used
44 int engine_unlocked_finish(ENGINE *e, int unlock_for_handlers)
49 * Reduce the functional reference count here so if it's the terminating
50 * case, we can release the lock safely and call the finish() handler
51 * without risk of a race. We get a race if we leave the count until
52 * after and something else is calling "finish" at the same time -
53 * there's a chance that both threads will together take the count from 2
54 * to 0 without either calling finish().
57 engine_ref_debug(e, 1, -1);
58 if ((e->funct_ref == 0) && e->finish) {
59 if (unlock_for_handlers)
60 CRYPTO_THREAD_unlock(global_engine_lock);
61 to_return = e->finish(e);
62 if (unlock_for_handlers)
63 CRYPTO_THREAD_write_lock(global_engine_lock);
67 REF_ASSERT_ISNT(e->funct_ref < 0);
68 /* Release the structural reference too */
69 if (!engine_free_util(e, 0)) {
70 ENGINEerr(ENGINE_F_ENGINE_UNLOCKED_FINISH, ENGINE_R_FINISH_FAILED);
76 /* The API (locked) version of "init" */
77 int ENGINE_init(ENGINE *e)
81 ENGINEerr(ENGINE_F_ENGINE_INIT, ERR_R_PASSED_NULL_PARAMETER);
84 if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
85 ENGINEerr(ENGINE_F_ENGINE_INIT, ERR_R_MALLOC_FAILURE);
88 CRYPTO_THREAD_write_lock(global_engine_lock);
89 ret = engine_unlocked_init(e);
90 CRYPTO_THREAD_unlock(global_engine_lock);
94 /* The API (locked) version of "finish" */
95 int ENGINE_finish(ENGINE *e)
101 CRYPTO_THREAD_write_lock(global_engine_lock);
102 to_return = engine_unlocked_finish(e, 1);
103 CRYPTO_THREAD_unlock(global_engine_lock);
105 ENGINEerr(ENGINE_F_ENGINE_FINISH, ENGINE_R_FINISH_FAILED);