summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/openssl.c46
1 files changed, 14 insertions, 32 deletions
diff --git a/src/openssl.c b/src/openssl.c
index cea38a5..1b11207 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -1318,17 +1318,14 @@ static const EVP_MD *auxL_optdigest(lua_State *L, int index, EVP_PKEY *key, cons
1318 * Prevent loader from unlinking us if we've registered a callback with 1318 * Prevent loader from unlinking us if we've registered a callback with
1319 * OpenSSL by taking another reference to ourselves. 1319 * OpenSSL by taking another reference to ourselves.
1320 */ 1320 */
1321/* dl_anchor must not be called from multiple threads at once */
1321static int dl_anchor(void) { 1322static int dl_anchor(void) {
1322#if HAVE_DLADDR 1323#if HAVE_DLADDR
1323 extern int luaopen__openssl(lua_State *); 1324 extern int luaopen__openssl(lua_State *);
1324 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1325 static void *anchor; 1325 static void *anchor;
1326 Dl_info info; 1326 Dl_info info;
1327 int error = 0; 1327 int error = 0;
1328 1328
1329 if ((error = pthread_mutex_lock(&mutex)))
1330 return error;
1331
1332 if (anchor) 1329 if (anchor)
1333 goto epilog; 1330 goto epilog;
1334 1331
@@ -1338,8 +1335,6 @@ static int dl_anchor(void) {
1338 if (!(anchor = dlopen(info.dli_fname, RTLD_NOW|RTLD_LOCAL))) 1335 if (!(anchor = dlopen(info.dli_fname, RTLD_NOW|RTLD_LOCAL)))
1339 goto dlerr; 1336 goto dlerr;
1340epilog: 1337epilog:
1341 (void)pthread_mutex_unlock(&mutex);
1342
1343 return error; 1338 return error;
1344dlerr: 1339dlerr:
1345 error = auxL_EDYLD; 1340 error = auxL_EDYLD;
@@ -1910,14 +1905,11 @@ static int compat_X509_up_ref(X509 *crt) {
1910 */ 1905 */
1911#endif 1906#endif
1912 1907
1908/* compat_init must not be called from multiple threads at once */
1913static int compat_init(void) { 1909static int compat_init(void) {
1914 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1915 static int store_index = -1, ssl_ctx_index = -1, done; 1910 static int store_index = -1, ssl_ctx_index = -1, done;
1916 int error = 0; 1911 int error = 0;
1917 1912
1918 if ((error = pthread_mutex_lock(&mutex)))
1919 return error;
1920
1921 if (done) 1913 if (done)
1922 goto epilog; 1914 goto epilog;
1923 1915
@@ -1988,8 +1980,6 @@ epilog:
1988 compat.tmp.store = NULL; 1980 compat.tmp.store = NULL;
1989 } 1981 }
1990 1982
1991 (void)pthread_mutex_unlock(&mutex);
1992
1993 return error; 1983 return error;
1994sslerr: 1984sslerr:
1995 error = auxL_EOPENSSL; 1985 error = auxL_EOPENSSL;
@@ -2115,15 +2105,12 @@ static void ex_onfree(void *parent NOTUSED, void *_data, CRYPTO_EX_DATA *ad NOTU
2115 free(data); 2105 free(data);
2116} /* ex_onfree() */ 2106} /* ex_onfree() */
2117 2107
2108/* ex_init must not be called from multiple threads at once */
2118static int ex_init(void) { 2109static int ex_init(void) {
2119 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
2120 static int done; 2110 static int done;
2121 struct ex_type *type; 2111 struct ex_type *type;
2122 int error = 0; 2112 int error = 0;
2123 2113
2124 if ((error = pthread_mutex_lock(&mutex)))
2125 return error;
2126
2127 if (done) 2114 if (done)
2128 goto epilog; 2115 goto epilog;
2129 2116
@@ -2144,8 +2131,6 @@ static int ex_init(void) {
2144 2131
2145 done = 1; 2132 done = 1;
2146epilog: 2133epilog:
2147 (void)pthread_mutex_unlock(&mutex);
2148
2149 return error; 2134 return error;
2150sslerr: 2135sslerr:
2151 error = auxL_EOPENSSL; 2136 error = auxL_EOPENSSL;
@@ -10435,14 +10420,11 @@ static unsigned long mt_gettid(void) {
10435#endif 10420#endif
10436} /* mt_gettid() */ 10421} /* mt_gettid() */
10437 10422
10423/* mt_init must not be called from multiple threads at once */
10438static int mt_init(void) { 10424static int mt_init(void) {
10439 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
10440 static int done, bound; 10425 static int done, bound;
10441 int error = 0; 10426 int error = 0;
10442 10427
10443 if ((error = pthread_mutex_lock(&mutex)))
10444 return error;
10445
10446 if (done) 10428 if (done)
10447 goto epilog; 10429 goto epilog;
10448 10430
@@ -10485,8 +10467,6 @@ static int mt_init(void) {
10485 10467
10486 done = 1; 10468 done = 1;
10487epilog: 10469epilog:
10488 pthread_mutex_unlock(&mutex);
10489
10490 return error; 10470 return error;
10491} /* mt_init() */ 10471} /* mt_init() */
10492 10472
@@ -10496,12 +10476,11 @@ static void initall(lua_State *L) {
10496 static int initssl; 10476 static int initssl;
10497 int error; 10477 int error;
10498 10478
10499 if ((error = mt_init()))
10500 auxL_error(L, error, "openssl.init");
10501
10502 pthread_mutex_lock(&mutex); 10479 pthread_mutex_lock(&mutex);
10503 10480
10504 if (!initssl) { 10481 error = mt_init();
10482
10483 if (!error && !initssl) {
10505 initssl = 1; 10484 initssl = 1;
10506 10485
10507 SSL_load_error_strings(); 10486 SSL_load_error_strings();
@@ -10515,12 +10494,15 @@ static void initall(lua_State *L) {
10515 OPENSSL_config(NULL); 10494 OPENSSL_config(NULL);
10516 } 10495 }
10517 10496
10518 pthread_mutex_unlock(&mutex); 10497 if (!error)
10498 error = compat_init();
10519 10499
10520 if ((error = compat_init())) 10500 if (!error)
10521 auxL_error(L, error, "openssl.init"); 10501 error = ex_init();
10502
10503 pthread_mutex_unlock(&mutex);
10522 10504
10523 if ((error = ex_init())) 10505 if (error)
10524 auxL_error(L, error, "openssl.init"); 10506 auxL_error(L, error, "openssl.init");
10525 10507
10526 ex_newstate(L); 10508 ex_newstate(L);