From c0e34214629a6110393c889d0151276a141ab1ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E7=86=8F?= Date: Mon, 11 Jan 2021 15:15:25 +0800 Subject: [PATCH] bugfix ltls init #1314 (#1318) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * bugfix ltls init #1314 * 修改init失败信息 * fix destructor Co-authored-by: zixun --- lualib-src/ltls.c | 41 ++++++++++++++++++++++++++++++++--------- service/bootstrap.lua | 10 ++++++++++ 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/lualib-src/ltls.c b/lualib-src/ltls.c index 6da40c48..0e8e41b3 100644 --- a/lualib-src/ltls.c +++ b/lualib-src/ltls.c @@ -378,9 +378,11 @@ lnew_tls(lua_State* L) { return 1; } - int luaopen_ltls_c(lua_State* L) { + if(!TLS_IS_INIT) { + luaL_error(L, "ltls need init, Put enablessl = true in you config file."); + } luaL_Reg l[] = { {"newctx", lnew_ctx}, {"newtls", lnew_tls}, @@ -391,18 +393,24 @@ luaopen_ltls_c(lua_State* L) { return 1; } -void __attribute__((constructor)) ltls_init(void) { +// for ltls init +static int +ltls_init_constructor(lua_State* L) { #ifndef OPENSSL_EXTERNAL_INITIALIZATION - SSL_library_init(); - SSL_load_error_strings(); - ERR_load_BIO_strings(); - OpenSSL_add_all_algorithms(); - TLS_IS_INIT = true; + if(!TLS_IS_INIT) { + SSL_library_init(); + SSL_load_error_strings(); + ERR_load_BIO_strings(); + OpenSSL_add_all_algorithms(); + } #endif + TLS_IS_INIT = true; + return 0; } - -void __attribute__((destructor)) ltls_destory(void) { +static int +ltls_init_destructor(lua_State* L) { +#ifndef OPENSSL_EXTERNAL_INITIALIZATION if(TLS_IS_INIT) { ENGINE_cleanup(); CONF_modules_unload(1); @@ -411,4 +419,19 @@ void __attribute__((destructor)) ltls_destory(void) { sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); CRYPTO_cleanup_all_ex_data(); } +#endif + TLS_IS_INIT = false; + return 0; +} + +int +luaopen_ltls_init_c(lua_State* L) { + luaL_Reg l[] = { + {"constructor", ltls_init_constructor}, + {"destructor", ltls_init_destructor}, + {NULL, NULL}, + }; + luaL_checkversion(L); + luaL_newlib(L, l); + return 1; } diff --git a/service/bootstrap.lua b/service/bootstrap.lua index 39f0c49c..69802b49 100644 --- a/service/bootstrap.lua +++ b/service/bootstrap.lua @@ -1,5 +1,6 @@ local skynet = require "skynet" local harbor = require "skynet.harbor" +local service = require "skynet.service" require "skynet.manager" -- import skynet.launch, ... skynet.start(function() @@ -39,6 +40,15 @@ skynet.start(function() skynet.name("DATACENTER", datacenter) end skynet.newservice "service_mgr" + + local enablessl = skynet.getenv "enablessl" + if enablessl then + service.new("ltls_holder", function () + local c = require "ltls.init.c" + c.constructor() + end) + end + pcall(skynet.newservice,skynet.getenv "start" or "main") skynet.exit() end)