Unverified Commit 3255c3e4 authored by Oleh Nikolaiev's avatar Oleh Nikolaiev Committed by GitHub

Merge pull request #290 from skalenetwork/bug/SKALE-4005-graceful-exit

Bug/skale 4005 graceful exit
parents df089ab5 c433c730
...@@ -110,7 +110,7 @@ Json::Value CSRManagerServer::signByHash(const string &hash, int status) { ...@@ -110,7 +110,7 @@ Json::Value CSRManagerServer::signByHash(const string &hash, int status) {
return signByHashImpl(hash, status); return signByHashImpl(hash, status);
} }
int CSRManagerServer::initCSRManagerServer() { void CSRManagerServer::initCSRManagerServer() {
hs3 = make_shared<jsonrpc::HttpServer>(BASE_PORT + 2); hs3 = make_shared<jsonrpc::HttpServer>(BASE_PORT + 2);
hs3->BindLocalhost(); hs3->BindLocalhost();
cs = make_shared<CSRManagerServer>(*hs3, JSONRPC_SERVER_V2); // server (json-rpc 2.0) cs = make_shared<CSRManagerServer>(*hs3, JSONRPC_SERVER_V2); // server (json-rpc 2.0)
...@@ -119,9 +119,20 @@ int CSRManagerServer::initCSRManagerServer() { ...@@ -119,9 +119,20 @@ int CSRManagerServer::initCSRManagerServer() {
if (!cs->StartListening()) { if (!cs->StartListening()) {
spdlog::info("CSR manager server could not start listening"); spdlog::info("CSR manager server could not start listening");
exit(-1); throw SGXException(CSR_MANAGER_SERVER_FAILED_TO_START, "CSRManager server could not start listening.");
} else { } else {
spdlog::info("CSR manager server started on port {}", BASE_PORT + 2); spdlog::info("CSR manager server started on port {}", BASE_PORT + 2);
} }
return 0;
}; };
int CSRManagerServer::exitServer() {
spdlog::info("Stoping CSRManager server");
if (cs && !cs->StopListening()) {
spdlog::error("CSRManager server could not be stopped. Will forcefully terminate the app");
} else {
spdlog::info("CSRManager server stopped");
}
return 0;
}
...@@ -49,7 +49,9 @@ class CSRManagerServer : public abstractCSRManagerServer { ...@@ -49,7 +49,9 @@ class CSRManagerServer : public abstractCSRManagerServer {
virtual Json::Value getUnsignedCSRs(); virtual Json::Value getUnsignedCSRs();
virtual Json::Value signByHash(const string& hash, int status); virtual Json::Value signByHash(const string& hash, int status);
static int initCSRManagerServer(); static void initCSRManagerServer();
static int exitServer();
}; };
......
#include <chrono>
#include <thread>
#include "ExitHandler.h"
void ExitHandler::exitHandler( int s ) {
exitHandler( s, ec_success );
}
void ExitHandler::exitHandler( int s, ExitHandler::exit_code_t ec ) {
m_signal = s;
if ( ec != ec_success ) {
g_ec = ec;
}
s_shouldExit = true;
}
volatile bool ExitHandler::s_shouldExit = false;
volatile int ExitHandler::m_signal = -1;
volatile ExitHandler::exit_code_t ExitHandler::g_ec = ExitHandler::ec_success;
#ifndef EXITHANDLER_H
#define EXITHANDLER_H
#include <signal.h>
class ExitHandler {
public:
enum exit_code_t {
ec_success = 0,
ec_initing_user_space = 202, // error or exception while initializing user space
};
private:
static volatile bool s_shouldExit;
static volatile int m_signal;
static volatile exit_code_t g_ec;
ExitHandler() = delete;
public:
static void exitHandler( int s );
static void exitHandler( int s, ExitHandler::exit_code_t ec );
static bool shouldExit() { return s_shouldExit; }
static int getSignal() { return m_signal; }
static exit_code_t requestedExitCode() { return g_ec; }
};
#endif // EXITHANDLER_H
...@@ -274,8 +274,8 @@ void LevelDB::initDataFolderAndDBs() { ...@@ -274,8 +274,8 @@ void LevelDB::initDataFolderAndDBs() {
char cwd[PATH_MAX]; char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) == NULL) { if (getcwd(cwd, sizeof(cwd)) == NULL) {
spdlog::error("could not get current workin directory"); spdlog::error("Could not get current working directory.");
exit(-2); throw SGXException(COULD_NOT_GET_WORKING_DIRECTORY, "Could not get current working directory.");
} }
sgx_data_folder = string(cwd) + "/" + SGXDATA_FOLDER; sgx_data_folder = string(cwd) + "/" + SGXDATA_FOLDER;
...@@ -288,8 +288,8 @@ void LevelDB::initDataFolderAndDBs() { ...@@ -288,8 +288,8 @@ void LevelDB::initDataFolderAndDBs() {
spdlog::info("Successfully created sgx_data folder"); spdlog::info("Successfully created sgx_data folder");
} }
else{ else{
spdlog::error("Couldnt create creating sgx_data folder"); spdlog::error("Could not create sgx_data folder.");
exit(-3); throw SGXException(ERROR_CREATING_SGX_DATA_FOLDER, "Could not create sgx_data folder.");
} }
} }
......
...@@ -70,7 +70,7 @@ bin_PROGRAMS = sgxwallet testw sgx_util ...@@ -70,7 +70,7 @@ bin_PROGRAMS = sgxwallet testw sgx_util
## have to be explicitly listed ## have to be explicitly listed
COMMON_SRC = SGXException.cpp ZMQClient.cpp BLSSignRspMessage.cpp ECDSASignRspMessage.cpp ECDSASignReqMessage.cpp BLSSignReqMessage.cpp ZMQMessage.cpp ZMQServer.cpp ServerWorker.cpp InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp \ COMMON_SRC = SGXException.cpp ExitHandler.cpp ZMQClient.cpp BLSSignRspMessage.cpp ECDSASignRspMessage.cpp ECDSASignReqMessage.cpp BLSSignReqMessage.cpp ZMQMessage.cpp ZMQServer.cpp ServerWorker.cpp InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp \
SGXWalletServer.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp BLSCrypto.cpp \ SGXWalletServer.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp BLSCrypto.cpp \
DKGCrypto.cpp ServerInit.cpp BLSPrivateKeyShareSGX.cpp LevelDB.cpp ServerDataChecker.cpp SEKManager.cpp \ DKGCrypto.cpp ServerInit.cpp BLSPrivateKeyShareSGX.cpp LevelDB.cpp ServerDataChecker.cpp SEKManager.cpp \
third_party/intel/sgx_stub.c third_party/intel/sgx_detect_linux.c third_party/intel/create_enclave.c third_party/intel/oc_alloc.c \ third_party/intel/sgx_stub.c third_party/intel/sgx_detect_linux.c third_party/intel/create_enclave.c third_party/intel/oc_alloc.c \
...@@ -116,12 +116,12 @@ nodist_testw_SOURCES=${nodist_sgxwallet_SOURCES} ...@@ -116,12 +116,12 @@ nodist_testw_SOURCES=${nodist_sgxwallet_SOURCES}
EXTRA_testw_DEPENDENCIES=${EXTRA_sgxwallet_DEPENDENCIES} EXTRA_testw_DEPENDENCIES=${EXTRA_sgxwallet_DEPENDENCIES}
testw_LDADD= ${sgxwallet_LDADD} testw_LDADD= ${sgxwallet_LDADD}
sgx_util_SOURCES= SGXException.cpp InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp sgx_util.cpp stubclient.cpp LevelDB.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp sgx_util_SOURCES= SGXException.cpp ExitHandler.cpp InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp sgx_util.cpp stubclient.cpp LevelDB.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp
sgx_util_LDADD=-LlibBLS/deps/deps_inst/x86_or_x64/lib -Lleveldb/build -LlibBLS/build \ sgx_util_LDADD=-LlibBLS/deps/deps_inst/x86_or_x64/lib -Lleveldb/build -LlibBLS/build \
-LlibBLS/build/libff/libff \ -LlibBLS/build/libff/libff \
-Llibzmq/build/lib/ \ -Llibzmq/build/lib/ \
-l:libzmq.a \ -l:libzmq.a \
-l:libbls.a -l:libleveldb.a \ -l:libbls.a -l:libleveldb.a \
-l:libff.a -lgmp -ljsonrpccpp-stub -ljsonrpccpp-server -ljsonrpccpp-client -ljsonrpccpp-common -ljsoncpp -lmicrohttpd -lgnutls -lgcrypt -lidn2 -lcurl -lssl -lcrypto -lz -lpthread -ldl -l:libff.a -lgmp -ljsonrpccpp-stub -ljsonrpccpp-server -ljsonrpccpp-client -ljsonrpccpp-common -ljsoncpp -lmicrohttpd -lgnutls -lgcrypt -lidn2 -lcurl -lssl -lcrypto -lz -lpthread -ldl
...@@ -90,7 +90,7 @@ void validate_SEK() { ...@@ -90,7 +90,7 @@ void validate_SEK() {
if (!hex2carray(test_key_ptr->c_str(), &len, encr_test_key.data(), if (!hex2carray(test_key_ptr->c_str(), &len, encr_test_key.data(),
BUF_LEN)) { BUF_LEN)) {
spdlog::error("Corrupt test key is LevelDB"); spdlog::error("Corrupt test key is LevelDB");
exit(-4); throw SGXException(CORRUPT_DATABASE, "Corrupt test key is LevelDB");
} }
sgx_status_t status = SGX_SUCCESS; sgx_status_t status = SGX_SUCCESS;
...@@ -108,7 +108,7 @@ void validate_SEK() { ...@@ -108,7 +108,7 @@ void validate_SEK() {
spdlog::error("Invalid storage key. You need to recover using backup key"); spdlog::error("Invalid storage key. You need to recover using backup key");
spdlog::error("Set the correct backup key into sgx_datasgxwallet_backup_key.txt"); spdlog::error("Set the correct backup key into sgx_datasgxwallet_backup_key.txt");
spdlog::error("Then run sgxwallet using backup flag"); spdlog::error("Then run sgxwallet using backup flag");
exit(-5); throw SGXException(INVALID_SEK, "Invalid storage key. Recover using backup key");
} }
} }
...@@ -181,6 +181,7 @@ void gen_SEK() { ...@@ -181,6 +181,7 @@ void gen_SEK() {
if (!autoconfirm) { if (!autoconfirm) {
sleep(10);
string confirm_str = "I confirm"; string confirm_str = "I confirm";
string buffer; string buffer;
do { do {
...@@ -201,21 +202,6 @@ void gen_SEK() { ...@@ -201,21 +202,6 @@ void gen_SEK() {
setSEK(encrypted_SEK_ptr); setSEK(encrypted_SEK_ptr);
validate_SEK();
}
static std::atomic<int> isSgxWalletExiting(0);
void safeExit() {
// this is to make sure exit is only called once if called from multiple threads
auto previousValue = isSgxWalletExiting.exchange(1);
if (previousValue != 1)
exit(-6);
} }
void setSEK(shared_ptr <string> hex_encrypted_SEK) { void setSEK(shared_ptr <string> hex_encrypted_SEK) {
...@@ -241,10 +227,7 @@ void setSEK(shared_ptr <string> hex_encrypted_SEK) { ...@@ -241,10 +227,7 @@ void setSEK(shared_ptr <string> hex_encrypted_SEK) {
HANDLE_TRUSTED_FUNCTION_ERROR(status, err_status, errMsg.data()); HANDLE_TRUSTED_FUNCTION_ERROR(status, err_status, errMsg.data());
validate_SEK(); validate_SEK();
} }
#include "experimental/filesystem" #include "experimental/filesystem"
...@@ -256,13 +239,13 @@ void enter_SEK() { ...@@ -256,13 +239,13 @@ void enter_SEK() {
shared_ptr <string> test_key_ptr = LevelDB::getLevelDb()->readString("TEST_KEY"); shared_ptr <string> test_key_ptr = LevelDB::getLevelDb()->readString("TEST_KEY");
if (test_key_ptr == nullptr) { if (test_key_ptr == nullptr) {
spdlog::error("Error: corrupt or empty LevelDB database"); spdlog::error("Error: corrupt or empty LevelDB database");
exit(-7); throw SGXException(CORRUPT_DATABASE, "Could not find TEST_KEY in database.");
} }
if (!experimental::filesystem::is_regular_file(BACKUP_PATH)) { if (!experimental::filesystem::is_regular_file(BACKUP_PATH)) {
spdlog::error("File does not exist: " BACKUP_PATH); spdlog::error("File does not exist: " BACKUP_PATH);
exit(-8); throw SGXException(FILE_NOT_FOUND, "File does not exist: " BACKUP_PATH);
} }
ifstream sek_file(BACKUP_PATH); ifstream sek_file(BACKUP_PATH);
...@@ -278,7 +261,7 @@ void enter_SEK() { ...@@ -278,7 +261,7 @@ void enter_SEK() {
while (!checkHex(sek, 16)) { while (!checkHex(sek, 16)) {
spdlog::error("Invalid hex in key"); spdlog::error("Invalid hex in key");
exit(-9); throw SGXException(SET_SEK_INVALID_SEK_HEX, "Invalid hex in key");
} }
auto encrypted_SEK = check_and_set_SEK(sek); auto encrypted_SEK = check_and_set_SEK(sek);
...@@ -298,10 +281,10 @@ void enter_SEK() { ...@@ -298,10 +281,10 @@ void enter_SEK() {
} }
void initSEK() { void initSEK() {
shared_ptr <string> encrypted_SEK_ptr = LevelDB::getLevelDb()->readString("SEK");
if (enterBackupKey) { if (enterBackupKey) {
enter_SEK(); enter_SEK();
} else { } else {
shared_ptr <string> encrypted_SEK_ptr = LevelDB::getLevelDb()->readString("SEK");
if (encrypted_SEK_ptr == nullptr) { if (encrypted_SEK_ptr == nullptr) {
spdlog::warn("SEK was not created yet. Going to create SEK"); spdlog::warn("SEK was not created yet. Going to create SEK");
gen_SEK(); gen_SEK();
......
...@@ -47,8 +47,6 @@ EXTERNC void initSEK(); ...@@ -47,8 +47,6 @@ EXTERNC void initSEK();
EXTERNC void setSEK(); EXTERNC void setSEK();
EXTERNC void safeExit();
......
...@@ -107,7 +107,7 @@ Json::Value SGXInfoServer::isKeyExist(const string& key) { ...@@ -107,7 +107,7 @@ Json::Value SGXInfoServer::isKeyExist(const string& key) {
RETURN_SUCCESS(result) RETURN_SUCCESS(result)
} }
int SGXInfoServer::initInfoServer(uint32_t _logLevel, bool _autoSign, bool _checkCerts, bool _generateTestKeys) { void SGXInfoServer::initInfoServer(uint32_t _logLevel, bool _autoSign, bool _checkCerts, bool _generateTestKeys) {
httpServer = make_shared<HttpServer>(BASE_PORT + 4); httpServer = make_shared<HttpServer>(BASE_PORT + 4);
server = make_shared<SGXInfoServer>(*httpServer, JSONRPC_SERVER_V2, _logLevel, _autoSign, _checkCerts, _generateTestKeys); // hybrid server (json-rpc 1.0 & 2.0) server = make_shared<SGXInfoServer>(*httpServer, JSONRPC_SERVER_V2, _logLevel, _autoSign, _checkCerts, _generateTestKeys); // hybrid server (json-rpc 1.0 & 2.0)
...@@ -115,12 +115,22 @@ int SGXInfoServer::initInfoServer(uint32_t _logLevel, bool _autoSign, bool _chec ...@@ -115,12 +115,22 @@ int SGXInfoServer::initInfoServer(uint32_t _logLevel, bool _autoSign, bool _chec
if (!server->StartListening()) { if (!server->StartListening()) {
spdlog::error("Info server could not start listening on port {}", BASE_PORT + 4); spdlog::error("Info server could not start listening on port {}", BASE_PORT + 4);
exit(-10); throw SGXException(SGX_INFO_SERVER_FAILED_TO_START, "Info server could not start listening.");
} else { } else {
spdlog::info("Info server started on port {}", BASE_PORT + 4); spdlog::info("Info server started on port {}", BASE_PORT + 4);
} }
}
int SGXInfoServer::exitServer() {
spdlog::info("Stoping SGXInfo server");
if (server && !server->StopListening()) {
spdlog::error("SGXInfo server could not be stopped. Will forcefully terminate the app");
} else {
spdlog::info("SGXInfo server stopped");
}
return 0; return 0;
} }
shared_ptr<SGXInfoServer> SGXInfoServer::getServer() { shared_ptr<SGXInfoServer> SGXInfoServer::getServer() {
......
...@@ -59,7 +59,9 @@ public: ...@@ -59,7 +59,9 @@ public:
virtual Json::Value isKeyExist(const string& key); virtual Json::Value isKeyExist(const string& key);
static int initInfoServer(uint32_t _logLevel, bool _autoSign, bool _checkCerts, bool _generateTestKeys); static void initInfoServer(uint32_t _logLevel, bool _autoSign, bool _checkCerts, bool _generateTestKeys);
static int exitServer();
}; };
......
...@@ -162,7 +162,7 @@ Json::Value SGXRegistrationServer::GetCertificate(const string &hash) { ...@@ -162,7 +162,7 @@ Json::Value SGXRegistrationServer::GetCertificate(const string &hash) {
} }
int SGXRegistrationServer::initRegistrationServer(bool _autoSign) { void SGXRegistrationServer::initRegistrationServer(bool _autoSign) {
httpServer = make_shared<HttpServer>(BASE_PORT + 1); httpServer = make_shared<HttpServer>(BASE_PORT + 1);
server = make_shared<SGXRegistrationServer>(*httpServer, server = make_shared<SGXRegistrationServer>(*httpServer,
JSONRPC_SERVER_V2, JSONRPC_SERVER_V2,
...@@ -172,12 +172,22 @@ int SGXRegistrationServer::initRegistrationServer(bool _autoSign) { ...@@ -172,12 +172,22 @@ int SGXRegistrationServer::initRegistrationServer(bool _autoSign) {
if (!server->StartListening()) { if (!server->StartListening()) {
spdlog::error("Registration server could not start listening on port {}", BASE_PORT + 1); spdlog::error("Registration server could not start listening on port {}", BASE_PORT + 1);
exit(-10); throw SGXException(REGISTRATION_SERVER_FAILED_TO_START, "Registration server could not start listening.");
} else { } else {
spdlog::info("Registration server started on port {}", BASE_PORT + 1); spdlog::info("Registration server started on port {}", BASE_PORT + 1);
} }
}
int SGXRegistrationServer::exitServer() {
spdlog::info("Stoping registration server");
if (server && !server->StopListening()) {
spdlog::error("Registration server could not be stopped. Will forcefully terminate the app");
} else {
spdlog::info("Registration server stopped");
}
return 0; return 0;
} }
......
...@@ -60,9 +60,10 @@ public: ...@@ -60,9 +60,10 @@ public:
virtual Json::Value GetCertificate(const string &hash); virtual Json::Value GetCertificate(const string &hash);
static int initRegistrationServer(bool _autoSign = false); static void initRegistrationServer(bool _autoSign = false);
static int exitServer();
}; };
#endif // SGXD_SGXREGISTRATIONSERVER_H #endif // SGXD_SGXREGISTRATIONSERVER_H
\ No newline at end of file
...@@ -21,7 +21,9 @@ ...@@ -21,7 +21,9 @@
@date 2019 @date 2019
*/ */
#include <chrono>
#include <iostream> #include <iostream>
#include <thread>
#include "abstractstubserver.h" #include "abstractstubserver.h"
#include <jsonrpccpp/server/connectors/httpserver.h> #include <jsonrpccpp/server/connectors/httpserver.h>
...@@ -30,11 +32,9 @@ ...@@ -30,11 +32,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "sgxwallet_common.h" #include "sgxwallet_common.h"
#include "sgxwallet.h" #include "sgxwallet.h"
#include "SGXException.h" #include "SGXException.h"
#include "LevelDB.h" #include "LevelDB.h"
#include "BLSCrypto.h" #include "BLSCrypto.h"
...@@ -139,7 +139,7 @@ void SGXWalletServer::createCertsIfNeeded() { ...@@ -139,7 +139,7 @@ void SGXWalletServer::createCertsIfNeeded() {
spdlog::info("ROOT CA CERTIFICATE IS SUCCESSFULLY GENERATED"); spdlog::info("ROOT CA CERTIFICATE IS SUCCESSFULLY GENERATED");
} else { } else {
spdlog::error("ROOT CA CERTIFICATE GENERATION FAILED"); spdlog::error("ROOT CA CERTIFICATE GENERATION FAILED");
exit(-11); throw SGXException(FAIL_TO_CREATE_CERTIFICATE, "ROOT CA CERTIFICATE GENERATION FAILED");
} }
} }
...@@ -156,7 +156,7 @@ void SGXWalletServer::createCertsIfNeeded() { ...@@ -156,7 +156,7 @@ void SGXWalletServer::createCertsIfNeeded() {
spdlog::info("SERVER CERTIFICATE IS SUCCESSFULLY GENERATED"); spdlog::info("SERVER CERTIFICATE IS SUCCESSFULLY GENERATED");
} else { } else {
spdlog::info("SERVER CERTIFICATE GENERATION FAILED"); spdlog::info("SERVER CERTIFICATE GENERATION FAILED");
exit(-12); throw SGXException(FAIL_TO_CREATE_CERTIFICATE, "SERVER CERTIFICATE GENERATION FAILED");
} }
} }
...@@ -166,20 +166,16 @@ void SGXWalletServer::createCertsIfNeeded() { ...@@ -166,20 +166,16 @@ void SGXWalletServer::createCertsIfNeeded() {
spdlog::info("SERVER CERTIFICATE IS SUCCESSFULLY VERIFIED"); spdlog::info("SERVER CERTIFICATE IS SUCCESSFULLY VERIFIED");
} else { } else {
spdlog::info("SERVER CERTIFICATE VERIFICATION FAILED"); spdlog::info("SERVER CERTIFICATE VERIFICATION FAILED");
exit(-12); throw SGXException(FAIL_TO_VERIFY_CERTIFICATE, "SERVER CERTIFICATE VERIFICATION FAILED");
} }
} }
int SGXWalletServer::initHttpsServer(bool _checkCerts) { void SGXWalletServer::initHttpsServer(bool _checkCerts) {
COUNT_STATISTICS COUNT_STATISTICS
spdlog::info("Entering {}", __FUNCTION__); spdlog::info("Entering {}", __FUNCTION__);
spdlog::info("Initing server, number of threads: {}", NUM_THREADS); spdlog::info("Initing server, number of threads: {}", NUM_THREADS);
string certPath = string(SGXDATA_FOLDER) + "cert_data/SGXServerCert.crt"; string certPath = string(SGXDATA_FOLDER) + "cert_data/SGXServerCert.crt";
string keyPath = string(SGXDATA_FOLDER) + "cert_data/SGXServerCert.key"; string keyPath = string(SGXDATA_FOLDER) + "cert_data/SGXServerCert.key";
string rootCAPath = string(SGXDATA_FOLDER) + "cert_data/rootCA.pem"; string rootCAPath = string(SGXDATA_FOLDER) + "cert_data/rootCA.pem";
...@@ -195,14 +191,13 @@ int SGXWalletServer::initHttpsServer(bool _checkCerts) { ...@@ -195,14 +191,13 @@ int SGXWalletServer::initHttpsServer(bool _checkCerts) {
if (!server->StartListening()) { if (!server->StartListening()) {
spdlog::error("SGX Server could not start listening"); spdlog::error("SGX Server could not start listening");
exit(-13); throw SGXException(SGX_SERVER_FAILED_TO_START, "Https server could not start listening.");
} else { } else {
spdlog::info("SGX Server started on port {}", BASE_PORT); spdlog::info("SGX Server started on port {}", BASE_PORT);
} }
return 0;
} }
int SGXWalletServer::initHttpServer() { //without ssl void SGXWalletServer::initHttpServer() { //without ssl
COUNT_STATISTICS COUNT_STATISTICS
spdlog::info("Entering {}", __FUNCTION__); spdlog::info("Entering {}", __FUNCTION__);
...@@ -214,9 +209,20 @@ int SGXWalletServer::initHttpServer() { //without ssl ...@@ -214,9 +209,20 @@ int SGXWalletServer::initHttpServer() { //without ssl
JSONRPC_SERVER_V2); // hybrid server (json-rpc 1.0 & 2.0) JSONRPC_SERVER_V2); // hybrid server (json-rpc 1.0 & 2.0)
if (!server->StartListening()) { if (!server->StartListening()) {
spdlog::error("Server could not start listening"); spdlog::error("Server could not start listening");
exit(-14); throw SGXException(SGX_SERVER_FAILED_TO_START, "Http server could not start listening.");
} }
return 0; }
int SGXWalletServer::exitServer() {
spdlog::info("Stoping sgx server");
if (server && !server->StopListening()) {
spdlog::error("Sgx server could not be stopped. Will forcefully terminate the app");
} else {
spdlog::info("Sgx server stopped");
}
return 0;
} }
Json::Value Json::Value
......
...@@ -39,8 +39,6 @@ using namespace std; ...@@ -39,8 +39,6 @@ using namespace std;
#define TOSTRING(x) STRINGIFY(x) #define TOSTRING(x) STRINGIFY(x)
class SGXWalletServer : public AbstractStubServer { class SGXWalletServer : public AbstractStubServer {
static shared_ptr<SGXWalletServer> server; static shared_ptr<SGXWalletServer> server;
static shared_ptr<HttpServer> httpServer; static shared_ptr<HttpServer> httpServer;
...@@ -178,9 +176,11 @@ public: ...@@ -178,9 +176,11 @@ public:
static void printDB(); static void printDB();
static int initHttpServer(); static void initHttpServer();
static void initHttpsServer(bool _checkCerts);
static int initHttpsServer(bool _checkCerts); static int exitServer();
static void createCertsIfNeeded(); static void createCertsIfNeeded();
}; };
......
...@@ -42,6 +42,7 @@ ...@@ -42,6 +42,7 @@
#include <unistd.h> #include <unistd.h>
#include "ExitHandler.h"
#include "BLSPrivateKeyShareSGX.h" #include "BLSPrivateKeyShareSGX.h"
#include "sgxwallet_common.h" #include "sgxwallet_common.h"
#include "third_party/intel/create_enclave.h" #include "third_party/intel/create_enclave.h"
...@@ -70,7 +71,7 @@ void systemHealthCheck() { ...@@ -70,7 +71,7 @@ void systemHealthCheck() {
ulimit = exec("/bin/bash -c \"ulimit -n\""); ulimit = exec("/bin/bash -c \"ulimit -n\"");
} catch (...) { } catch (...) {
spdlog::error("Execution of '/bin/bash -c ulimit -n' failed"); spdlog::error("Execution of '/bin/bash -c ulimit -n' failed");
exit(-15); throw SGXException(EXECUTION_ULIMIT_FAILED, "Execution of '/bin/bash -c ulimit -n' failed.");
} }
int noFiles = strtol(ulimit.c_str(), NULL, 10); int noFiles = strtol(ulimit.c_str(), NULL, 10);
...@@ -84,13 +85,10 @@ void systemHealthCheck() { ...@@ -84,13 +85,10 @@ void systemHealthCheck() {
"and setting 'DefaultLimitNOFILE=65535'\n" "and setting 'DefaultLimitNOFILE=65535'\n"
"After that, restart sgxwallet"; "After that, restart sgxwallet";
spdlog::error(errStr); spdlog::error(errStr);
exit(-16); throw SGXException(WRONG_ULIMIT, errStr);
} }
} }
void initUserSpace() { void initUserSpace() {
libff::inhibit_profiling_counters = true; libff::inhibit_profiling_counters = true;
...@@ -103,8 +101,6 @@ void initUserSpace() { ...@@ -103,8 +101,6 @@ void initUserSpace() {
systemHealthCheck(); systemHealthCheck();
#endif #endif
} }
...@@ -116,7 +112,7 @@ uint64_t initEnclave() { ...@@ -116,7 +112,7 @@ uint64_t initEnclave() {
support = get_sgx_support(); support = get_sgx_support();
if (!SGX_OK(support)) { if (!SGX_OK(support)) {
sgx_support_perror(support); sgx_support_perror(support);
exit(-17); throw SGXException(COULD_NOT_INIT_ENCLAVE, "SGX is not supported or not enabled");
} }
#endif #endif
...@@ -147,7 +143,7 @@ uint64_t initEnclave() { ...@@ -147,7 +143,7 @@ uint64_t initEnclave() {
} else { } else {
spdlog::error("sgx_create_enclave_search failed {} {}", ENCLAVE_NAME, status); spdlog::error("sgx_create_enclave_search failed {} {}", ENCLAVE_NAME, status);
} }
exit(-21); throw SGXException(COULD_NOT_INIT_ENCLAVE, "Error initing enclave. Please re-check your enviroment.");
} }
spdlog::info("Enclave created and started successfully"); spdlog::info("Enclave created and started successfully");
...@@ -222,15 +218,24 @@ void initAll(uint32_t _logLevel, bool _checkCert, ...@@ -222,15 +218,24 @@ void initAll(uint32_t _logLevel, bool _checkCert,
sgxServerInited = true; sgxServerInited = true;
} catch (SGXException &_e) { } catch (SGXException &_e) {
spdlog::error(_e.getMessage()); spdlog::error(_e.getMessage());
exit(-18); ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_initing_user_space);
} catch (exception &_e) { } catch (exception &_e) {
spdlog::error(_e.what()); spdlog::error(_e.what());
exit(-19); ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_initing_user_space);
} }
catch (...) { catch (...) {
exception_ptr p = current_exception(); exception_ptr p = current_exception();
printf("Exception %s \n", p.__cxa_exception_type()->name()); printf("Exception %s \n", p.__cxa_exception_type()->name());
spdlog::error("Unknown exception"); spdlog::error("Unknown exception");
exit(-22); ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_initing_user_space);
} }
}; };
void exitAll() {
SGXWalletServer::exitServer();
SGXRegistrationServer::exitServer();
CSRManagerServer::exitServer();
SGXInfoServer::exitServer();
ZMQServer::exitZMQServer();
}
...@@ -34,6 +34,8 @@ ...@@ -34,6 +34,8 @@
EXTERNC void initAll(uint32_t _logLevel, bool _checkCert, bool _checkZMQSig, bool _autoSign, bool _generateTestKeys); EXTERNC void initAll(uint32_t _logLevel, bool _checkCert, bool _checkZMQSig, bool _autoSign, bool _generateTestKeys);
void exitAll();
EXTERNC void initUserSpace(); EXTERNC void initUserSpace();
EXTERNC uint64_t initEnclave(); EXTERNC uint64_t initEnclave();
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include "common.h" #include "common.h"
#include "SGXException.h"
#include "ZMQServer.h" #include "ZMQServer.h"
#include "sgxwallet_common.h" #include "sgxwallet_common.h"
...@@ -71,14 +72,14 @@ void ZMQServer::run() { ...@@ -71,14 +72,14 @@ void ZMQServer::run() {
auto port = BASE_PORT + 5; auto port = BASE_PORT + 5;
spdlog::info("Starting zmq server on port {} ...", port); spdlog::info("Starting zmq server on port {} ...", port);
try { try {
CHECK_STATE(frontend); CHECK_STATE(frontend);
frontend->bind("tcp://*:" + to_string(port)); frontend->bind("tcp://*:" + to_string(port));
} catch (...) { } catch (...) {
spdlog::error("Server task could not bind to port:{}", port); spdlog::error("Server task could not bind to port:{}", port);
exit(ZMQ_COULD_NOT_BIND_FRONT_END); throw SGXException(ZMQ_COULD_NOT_BIND_FRONT_END, "Server task could not bind.");
} }
spdlog::info("Bound port ..."); spdlog::info("Bound port ...");
...@@ -88,7 +89,7 @@ void ZMQServer::run() { ...@@ -88,7 +89,7 @@ void ZMQServer::run() {
backend->bind("inproc://backend"); backend->bind("inproc://backend");
} catch (exception &e) { } catch (exception &e) {
spdlog::error("Could not bind to zmq backend: {}", e.what()); spdlog::error("Could not bind to zmq backend: {}", e.what());
exit(ZMQ_COULD_NOT_BIND_BACK_END); throw SGXException(ZMQ_COULD_NOT_BIND_BACK_END, "Could not bind to zmq backend.");
} }
...@@ -103,7 +104,7 @@ void ZMQServer::run() { ...@@ -103,7 +104,7 @@ void ZMQServer::run() {
} }
} catch (std::exception &e) { } catch (std::exception &e) {
spdlog::error("Could not create zmq server workers:{} ", e.what()); spdlog::error("Could not create zmq server workers:{} ", e.what());
exit(ZMQ_COULD_NOT_CREATE_WORKERS); throw SGXException(ZMQ_COULD_NOT_CREATE_WORKERS, "Could not create zmq server workers.");
}; };
...@@ -123,7 +124,7 @@ void ZMQServer::run() { ...@@ -123,7 +124,7 @@ void ZMQServer::run() {
return; return;
} }
spdlog::info("Error, exiting zmq server ..."); spdlog::info("Error, exiting zmq server ...");
exit(ZMQ_COULD_NOT_CREATE_PROXY); throw SGXException(ZMQ_COULD_NOT_CREATE_PROXY, "Error, exiting zmq server.");
} }
} }
...@@ -206,4 +207,4 @@ ZMQServer::~ZMQServer() { ...@@ -206,4 +207,4 @@ ZMQServer::~ZMQServer() {
spdlog::info("Deleting ZMQ context"); spdlog::info("Deleting ZMQ context");
ctx_ = nullptr; ctx_ = nullptr;
spdlog::info("Deleted ZMQ context"); spdlog::info("Deleted ZMQ context");
} }
\ No newline at end of file
...@@ -2,7 +2,7 @@ version: '3' ...@@ -2,7 +2,7 @@ version: '3'
services: services:
sgxwallet: sgxwallet:
image: skalenetwork/sgxwallet_signed:latest image: skalenetwork/sgxwallet_signed:latest
restart: always restart: unless-stopped
ports: ports:
- "1026:1026" - "1026:1026"
- "1027:1027" - "1027:1027"
...@@ -20,7 +20,6 @@ services: ...@@ -20,7 +20,6 @@ services:
options: options:
max-size: "10m" max-size: "10m"
max-file: "4" max-file: "4"
restart: unless-stopped
command: -s -y -d command: -s -y -d
healthcheck: healthcheck:
test: ["CMD", "ls", "/dev/isgx", "/dev/mei0"] test: ["CMD", "ls", "/dev/isgx", "/dev/mei0"]
......
...@@ -2,7 +2,7 @@ version: '3' ...@@ -2,7 +2,7 @@ version: '3'
services: services:
sgxwallet: sgxwallet:
image: skalenetwork/sgxwallet_sim:develop-latest image: skalenetwork/sgxwallet_sim:develop-latest
restart: always restart: unless-stopped
ports: ports:
- "1026:1026" - "1026:1026"
- "1027:1027" - "1027:1027"
...@@ -17,5 +17,4 @@ services: ...@@ -17,5 +17,4 @@ services:
options: options:
max-size: "10m" max-size: "10m"
max-file: "4" max-file: "4"
restart: unless-stopped
command: -s -y command: -s -y
...@@ -21,15 +21,17 @@ ...@@ -21,15 +21,17 @@
@date 2020 @date 2020
*/ */
#include <csignal>
#include <stdbool.h> #include <stdbool.h>
#include "ExitHandler.h"
#include "BLSCrypto.h" #include "BLSCrypto.h"
#include "ServerInit.h" #include "ServerInit.h"
#include "SEKManager.h" #include "SEKManager.h"
#include "SGXWalletServer.h" #include "SGXWalletServer.h"
#include <fstream> #include <fstream>
#include "TestUtils.h" #include "TestUtils.h"
...@@ -41,11 +43,6 @@ ...@@ -41,11 +43,6 @@
#include "sgxwallet.h" #include "sgxwallet.h"
void SGXWallet::usage() {
cerr << "usage: sgxwallet\n";
exit(-21);
}
void SGXWallet::printUsage() { void SGXWallet::printUsage() {
cerr << "\nAvailable flags:\n"; cerr << "\nAvailable flags:\n";
cerr << "\nDebug flags:\n\n"; cerr << "\nDebug flags:\n\n";
...@@ -88,6 +85,11 @@ void SGXWallet::serializeKeys(const vector<string>& _ecdsaKeyNames, const vector ...@@ -88,6 +85,11 @@ void SGXWallet::serializeKeys(const vector<string>& _ecdsaKeyNames, const vector
fs.close(); fs.close();
} }
void SGXWallet::signalHandler( int signalNo ) {
spdlog::info("Received exit signal {}.", signalNo);
ExitHandler::exitHandler( signalNo );
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
bool enterBackupKeyOption = false; bool enterBackupKeyOption = false;
...@@ -99,18 +101,20 @@ int main(int argc, char *argv[]) { ...@@ -99,18 +101,20 @@ int main(int argc, char *argv[]) {
bool autoSignClientCertOption = false; bool autoSignClientCertOption = false;
bool generateTestKeys = false; bool generateTestKeys = false;
std::signal(SIGABRT, SGXWallet::signalHandler);
int opt; int opt;
if (argc > 1 && strlen(argv[1]) == 1) { if (argc > 1 && strlen(argv[1]) == 1) {
SGXWallet::printUsage(); SGXWallet::printUsage();
exit(-22); exit(-21);
} }
while ((opt = getopt(argc, argv, "cshd0abyvVnT")) != -1) { while ((opt = getopt(argc, argv, "cshd0abyvVnT")) != -1) {
switch (opt) { switch (opt) {
case 'h': case 'h':
SGXWallet::printUsage(); SGXWallet::printUsage();
exit(-24); exit(-22);
case 'c': case 'c':
checkClientCertOption = false; checkClientCertOption = false;
break; break;
...@@ -178,7 +182,7 @@ int main(int argc, char *argv[]) { ...@@ -178,7 +182,7 @@ int main(int argc, char *argv[]) {
ifstream is("sgx_data/4node.json"); ifstream is("sgx_data/4node.json");
if (generateTestKeys && !is.good()) { if (generateTestKeys && !is.good() && !!ExitHandler::shouldExit()) {
cerr << "Generating test keys ..." << endl; cerr << "Generating test keys ..." << endl;
HttpClient client(RPC_ENDPOINT); HttpClient client(RPC_ENDPOINT);
...@@ -206,9 +210,14 @@ int main(int argc, char *argv[]) { ...@@ -206,9 +210,14 @@ int main(int argc, char *argv[]) {
while (true) { while ( !ExitHandler::shouldExit() ) {
sleep(10); sleep(10);
} }
return 0; ExitHandler::exit_code_t exitCode = ExitHandler::requestedExitCode();
int signal = ExitHandler::getSignal();
spdlog::info("Will exit with exit code {}", exitCode);
exitAll();
spdlog::info("Exiting with exit code {} and signal", exitCode, signal);
return exitCode;
} }
...@@ -26,7 +26,8 @@ class SGXWallet { ...@@ -26,7 +26,8 @@ class SGXWallet {
public: public:
static void usage(); static void signalHandler( int signalNo );
static void printUsage(); static void printUsage();
static void serializeKeys( const vector<string>& _ecdsaKeyNames, static void serializeKeys( const vector<string>& _ecdsaKeyNames,
......
...@@ -172,8 +172,18 @@ extern bool autoconfirm; ...@@ -172,8 +172,18 @@ extern bool autoconfirm;
#define ZMQ_COULD_NOT_BIND_BACK_END -99 #define ZMQ_COULD_NOT_BIND_BACK_END -99
#define ZMQ_COULD_NOT_CREATE_WORKERS -100 #define ZMQ_COULD_NOT_CREATE_WORKERS -100
#define ZMQ_COULD_NOT_CREATE_PROXY -101 #define ZMQ_COULD_NOT_CREATE_PROXY -101
#define REGISTRATION_SERVER_FAILED_TO_START -102
#define CSR_MANAGER_SERVER_FAILED_TO_START -103
#define SGX_INFO_SERVER_FAILED_TO_START -104
#define COULD_NOT_GET_WORKING_DIRECTORY -105
#define ERROR_CREATING_SGX_DATA_FOLDER -106
#define EXECUTION_ULIMIT_FAILED -107
#define WRONG_ULIMIT -108
#define COULD_NOT_INIT_ENCLAVE -109
#define FAIL_TO_VERIFY_CERTIFICATE -110
#define SGX_SERVER_FAILED_TO_START -111
#define CORRUPT_DATABASE -112
#define INVALID_SEK -113
#define SGX_ENCLAVE_ERROR -666 #define SGX_ENCLAVE_ERROR -666
...@@ -181,7 +191,7 @@ extern bool autoconfirm; ...@@ -181,7 +191,7 @@ extern bool autoconfirm;
#define BASE_PORT 1026 #define BASE_PORT 1026
#define WALLETDB_NAME "sgxwallet.db"//"test_sgxwallet.db" #define WALLETDB_NAME "sgxwallet.db"
#define ENCLAVE_NAME "secure_enclave.signed.so" #define ENCLAVE_NAME "secure_enclave.signed.so"
#define SGXDATA_FOLDER "sgx_data/" #define SGXDATA_FOLDER "sgx_data/"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment