SKALE-4005 throw exceptions during init process

parent 4f7a8449
...@@ -27,7 +27,6 @@ ...@@ -27,7 +27,6 @@
#include <jsonrpccpp/server/connectors/httpserver.h> #include <jsonrpccpp/server/connectors/httpserver.h>
#include "CSRManagerServer.h" #include "CSRManagerServer.h"
#include "ExitHandler.h"
#include "SGXException.h" #include "SGXException.h"
#include "sgxwallet_common.h" #include "sgxwallet_common.h"
...@@ -111,7 +110,7 @@ Json::Value CSRManagerServer::signByHash(const string &hash, int status) { ...@@ -111,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)
...@@ -120,20 +119,17 @@ int CSRManagerServer::initCSRManagerServer() { ...@@ -120,20 +119,17 @@ 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");
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_starting_server); throw SGXException(CSR_MANAGER_SERVER_FAILED_TO_START, "CSRManager server could not start listening.");
return 1;
} 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() { int CSRManagerServer::exitServer() {
spdlog::info("Stoping CSRManager server"); spdlog::info("Stoping CSRManager server");
if (cs && !cs->StopListening()) { if (cs && !cs->StopListening()) {
spdlog::error("CSRManager server could not be stopped"); spdlog::error("CSRManager server could not be stopped. Will forcefully terminate the app");
exit(-104);
} else { } else {
spdlog::info("CSRManager server stopped"); spdlog::info("CSRManager server stopped");
} }
......
...@@ -49,7 +49,7 @@ class CSRManagerServer : public abstractCSRManagerServer { ...@@ -49,7 +49,7 @@ 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(); static int exitServer();
}; };
......
...@@ -13,8 +13,6 @@ void ExitHandler::exitHandler( int s, ExitHandler::exit_code_t ec ) { ...@@ -13,8 +13,6 @@ void ExitHandler::exitHandler( int s, ExitHandler::exit_code_t ec ) {
g_ec = ec; g_ec = ec;
} }
s_shouldExit = true; s_shouldExit = true;
// HACK wait for loop in main to recieve exit call
std::this_thread::sleep_for( std::chrono::seconds( 20 ) );
} }
volatile bool ExitHandler::s_shouldExit = false; volatile bool ExitHandler::s_shouldExit = false;
......
...@@ -9,16 +9,7 @@ public: ...@@ -9,16 +9,7 @@ public:
enum exit_code_t { enum exit_code_t {
ec_success = 0, ec_success = 0,
ec_failure = 1, // same as EXIT_FAILURE in stdlib.h, generic failure in main()
ec_termninated_by_signal = 196,
ec_error_starting_server = 197, // error starting one of the http(s) servers
ec_rotation_complete = 0, // must be zero, exit requested after rotation complete
ec_error_creating_database = 198, // error initing LevelDB
ec_error_initing_sek = 199, // error while initing or validating SEK
ec_creating_certificate = 200, // error creating SSL certificate to initialize server
ec_initing_enclave = 201, // error starting secure enclave
ec_initing_user_space = 202, // error or exception while initializing user space ec_initing_user_space = 202, // error or exception while initializing user space
ec_cannot_start_zeromq = 203, // error starting ZMQ server
}; };
private: private:
......
...@@ -30,7 +30,6 @@ ...@@ -30,7 +30,6 @@
#include <jsonrpccpp/client.h> #include <jsonrpccpp/client.h>
#include "sgxwallet_common.h" #include "sgxwallet_common.h"
#include "ExitHandler.h"
#include "SGXException.h" #include "SGXException.h"
#include "LevelDB.h" #include "LevelDB.h"
...@@ -275,8 +274,8 @@ void LevelDB::initDataFolderAndDBs() { ...@@ -275,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.");
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_creating_database); 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;
...@@ -289,8 +288,8 @@ void LevelDB::initDataFolderAndDBs() { ...@@ -289,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.");
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_creating_database); throw SGXException(ERROR_CREATING_SGX_DATA_FOLDER, "Could not create sgx_data folder.");
} }
} }
......
...@@ -33,7 +33,6 @@ ...@@ -33,7 +33,6 @@
#include "common.h" #include "common.h"
#include "sgxwallet.h" #include "sgxwallet.h"
#include "ExitHandler.h"
#include "SGXException.h" #include "SGXException.h"
#include "BLSCrypto.h" #include "BLSCrypto.h"
#include "LevelDB.h" #include "LevelDB.h"
...@@ -91,7 +90,7 @@ void validate_SEK() { ...@@ -91,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");
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_initing_sek); throw SGXException(CORRUPT_DATABASE, "Corrupt test key is LevelDB");
} }
sgx_status_t status = SGX_SUCCESS; sgx_status_t status = SGX_SUCCESS;
...@@ -109,7 +108,7 @@ void validate_SEK() { ...@@ -109,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");
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_initing_sek); throw SGXException(INVALID_SEK, "Invalid storage key. Recover using backup key");
} }
} }
...@@ -182,6 +181,7 @@ void gen_SEK() { ...@@ -182,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 {
...@@ -202,8 +202,6 @@ void gen_SEK() { ...@@ -202,8 +202,6 @@ void gen_SEK() {
setSEK(encrypted_SEK_ptr); setSEK(encrypted_SEK_ptr);
validate_SEK();
} }
void setSEK(shared_ptr <string> hex_encrypted_SEK) { void setSEK(shared_ptr <string> hex_encrypted_SEK) {
...@@ -229,10 +227,7 @@ void setSEK(shared_ptr <string> hex_encrypted_SEK) { ...@@ -229,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"
...@@ -244,13 +239,13 @@ void enter_SEK() { ...@@ -244,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");
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_initing_sek); 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);
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_initing_sek); throw SGXException(FILE_NOT_FOUND, "File does not exist: " BACKUP_PATH);
} }
ifstream sek_file(BACKUP_PATH); ifstream sek_file(BACKUP_PATH);
...@@ -266,7 +261,7 @@ void enter_SEK() { ...@@ -266,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");
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_initing_sek); 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);
...@@ -286,10 +281,10 @@ void enter_SEK() { ...@@ -286,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();
......
...@@ -31,7 +31,6 @@ ...@@ -31,7 +31,6 @@
#include "sgxwallet_common.h" #include "sgxwallet_common.h"
#include "ExitHandler.h"
#include "SGXException.h" #include "SGXException.h"
#include "LevelDB.h" #include "LevelDB.h"
...@@ -108,7 +107,7 @@ Json::Value SGXInfoServer::isKeyExist(const string& key) { ...@@ -108,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)
...@@ -116,21 +115,17 @@ int SGXInfoServer::initInfoServer(uint32_t _logLevel, bool _autoSign, bool _chec ...@@ -116,21 +115,17 @@ 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);
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_starting_server); throw SGXException(SGX_INFO_SERVER_FAILED_TO_START, "Info server could not start listening.");
return 1;
} else { } else {
spdlog::info("Info server started on port {}", BASE_PORT + 4); spdlog::info("Info server started on port {}", BASE_PORT + 4);
} }
return 0;
} }
int SGXInfoServer::exitServer() { int SGXInfoServer::exitServer() {
spdlog::info("Stoping SGXInfo server"); spdlog::info("Stoping SGXInfo server");
if (server && !server->StopListening()) { if (server && !server->StopListening()) {
spdlog::error("SGXInfo server could not be stopped"); spdlog::error("SGXInfo server could not be stopped. Will forcefully terminate the app");
exit(-105);
} else { } else {
spdlog::info("SGXInfo server stopped"); spdlog::info("SGXInfo server stopped");
} }
......
...@@ -59,7 +59,7 @@ public: ...@@ -59,7 +59,7 @@ 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(); static int exitServer();
......
...@@ -32,7 +32,6 @@ ...@@ -32,7 +32,6 @@
#include "sgxwallet_common.h" #include "sgxwallet_common.h"
#include "ExitHandler.h"
#include "SGXException.h" #include "SGXException.h"
#include "LevelDB.h" #include "LevelDB.h"
...@@ -163,7 +162,7 @@ Json::Value SGXRegistrationServer::GetCertificate(const string &hash) { ...@@ -163,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,
...@@ -173,21 +172,17 @@ int SGXRegistrationServer::initRegistrationServer(bool _autoSign) { ...@@ -173,21 +172,17 @@ 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);
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_starting_server); throw SGXException(REGISTRATION_SERVER_FAILED_TO_START, "Registration server could not start listening.");
return 1;
} else { } else {
spdlog::info("Registration server started on port {}", BASE_PORT + 1); spdlog::info("Registration server started on port {}", BASE_PORT + 1);
} }
return 0;
} }
int SGXRegistrationServer::exitServer() { int SGXRegistrationServer::exitServer() {
spdlog::info("Stoping registration server"); spdlog::info("Stoping registration server");
if (server && !server->StopListening()) { if (server && !server->StopListening()) {
spdlog::error("Registration server could not be stopped"); spdlog::error("Registration server could not be stopped. Will forcefully terminate the app");
exit(-102);
} else { } else {
spdlog::info("Registration server stopped"); spdlog::info("Registration server stopped");
} }
......
...@@ -60,7 +60,7 @@ public: ...@@ -60,7 +60,7 @@ 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(); static int exitServer();
}; };
......
...@@ -32,12 +32,9 @@ ...@@ -32,12 +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 "ExitHandler.h"
#include "SGXException.h" #include "SGXException.h"
#include "LevelDB.h" #include "LevelDB.h"
#include "BLSCrypto.h" #include "BLSCrypto.h"
...@@ -142,7 +139,7 @@ void SGXWalletServer::createCertsIfNeeded() { ...@@ -142,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");
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_creating_certificate); throw SGXException(FAIL_TO_CREATE_CERTIFICATE, "ROOT CA CERTIFICATE GENERATION FAILED");
} }
} }
...@@ -159,7 +156,7 @@ void SGXWalletServer::createCertsIfNeeded() { ...@@ -159,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");
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_creating_certificate); throw SGXException(FAIL_TO_CREATE_CERTIFICATE, "SERVER CERTIFICATE GENERATION FAILED");
} }
} }
...@@ -169,12 +166,12 @@ void SGXWalletServer::createCertsIfNeeded() { ...@@ -169,12 +166,12 @@ 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");
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_creating_certificate); 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);
...@@ -194,14 +191,13 @@ int SGXWalletServer::initHttpsServer(bool _checkCerts) { ...@@ -194,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");
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_starting_server); 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__);
...@@ -213,18 +209,15 @@ int SGXWalletServer::initHttpServer() { //without ssl ...@@ -213,18 +209,15 @@ 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");
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_starting_server); throw SGXException(SGX_SERVER_FAILED_TO_START, "Http server could not start listening.");
} }
return 0;
} }
int SGXWalletServer::exitServer() { int SGXWalletServer::exitServer() {
spdlog::info("Stoping sgx server"); spdlog::info("Stoping sgx server");
if (server && !server->StopListening()) { if (server && !server->StopListening()) {
spdlog::error("Sgx server could not be stopped"); spdlog::error("Sgx server could not be stopped. Will forcefully terminate the app");
exit(-103);
} else { } else {
spdlog::info("Sgx server stopped"); spdlog::info("Sgx server stopped");
} }
......
...@@ -176,9 +176,9 @@ public: ...@@ -176,9 +176,9 @@ public:
static void printDB(); static void printDB();
static int initHttpServer(); static void initHttpServer();
static int initHttpsServer(bool _checkCerts); static void initHttpsServer(bool _checkCerts);
static int exitServer(); static int exitServer();
......
...@@ -71,7 +71,7 @@ void systemHealthCheck() { ...@@ -71,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");
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_initing_user_space); 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);
...@@ -85,7 +85,7 @@ void systemHealthCheck() { ...@@ -85,7 +85,7 @@ 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);
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_initing_user_space); throw SGXException(WRONG_ULIMIT, errStr);
} }
} }
...@@ -101,8 +101,6 @@ void initUserSpace() { ...@@ -101,8 +101,6 @@ void initUserSpace() {
systemHealthCheck(); systemHealthCheck();
#endif #endif
} }
...@@ -114,7 +112,7 @@ uint64_t initEnclave() { ...@@ -114,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);
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_initing_enclave); throw SGXException(COULD_NOT_INIT_ENCLAVE, "SGX is not supported or not enabled");
} }
#endif #endif
...@@ -145,7 +143,7 @@ uint64_t initEnclave() { ...@@ -145,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);
} }
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_initing_enclave); 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");
...@@ -212,15 +210,9 @@ void initAll(uint32_t _logLevel, bool _checkCert, ...@@ -212,15 +210,9 @@ void initAll(uint32_t _logLevel, bool _checkCert,
spdlog::info("Inited JSON-RPC server over HTTP"); spdlog::info("Inited JSON-RPC server over HTTP");
} }
if (SGXRegistrationServer::initRegistrationServer(_autoSign)) { SGXRegistrationServer::initRegistrationServer(_autoSign);
return; CSRManagerServer::initCSRManagerServer();
} SGXInfoServer::initInfoServer(_logLevel, _checkCert, _autoSign, _generateTestKeys);
if (CSRManagerServer::initCSRManagerServer()) {
return;
}
if (SGXInfoServer::initInfoServer(_logLevel, _checkCert, _autoSign, _generateTestKeys)) {
return;
}
ZMQServer::initZMQServer(_checkZMQSig); ZMQServer::initZMQServer(_checkZMQSig);
sgxServerInited = true; sgxServerInited = true;
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
#include "common.h" #include "common.h"
#include "ExitHandler.h" #include "SGXException.h"
#include "ZMQServer.h" #include "ZMQServer.h"
#include "sgxwallet_common.h" #include "sgxwallet_common.h"
...@@ -79,7 +79,7 @@ void ZMQServer::run() { ...@@ -79,7 +79,7 @@ void ZMQServer::run() {
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);
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_cannot_start_zeromq); throw SGXException(ZMQ_COULD_NOT_BIND_FRONT_END, "Server task could not bind.");
} }
spdlog::info("Bound port ..."); spdlog::info("Bound port ...");
...@@ -89,7 +89,7 @@ void ZMQServer::run() { ...@@ -89,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());
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_cannot_start_zeromq); throw SGXException(ZMQ_COULD_NOT_BIND_BACK_END, "Could not bind to zmq backend.");
} }
...@@ -104,7 +104,7 @@ void ZMQServer::run() { ...@@ -104,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());
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_cannot_start_zeromq); throw SGXException(ZMQ_COULD_NOT_CREATE_WORKERS, "Could not create zmq server workers.");
}; };
...@@ -124,7 +124,7 @@ void ZMQServer::run() { ...@@ -124,7 +124,7 @@ void ZMQServer::run() {
return; return;
} }
spdlog::info("Error, exiting zmq server ..."); spdlog::info("Error, exiting zmq server ...");
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_cannot_start_zeromq); throw SGXException(ZMQ_COULD_NOT_CREATE_PROXY, "Error, exiting zmq server.");
} }
} }
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
@date 2020 @date 2020
*/ */
#include <csignal>
#include <stdbool.h> #include <stdbool.h>
#include "ExitHandler.h" #include "ExitHandler.h"
...@@ -84,6 +85,11 @@ void SGXWallet::serializeKeys(const vector<string>& _ecdsaKeyNames, const vector ...@@ -84,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;
...@@ -95,18 +101,20 @@ int main(int argc, char *argv[]) { ...@@ -95,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();
ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure); exit(-22);
case 'c': case 'c':
checkClientCertOption = false; checkClientCertOption = false;
break; break;
......
...@@ -26,6 +26,8 @@ class SGXWallet { ...@@ -26,6 +26,8 @@ class SGXWallet {
public: public:
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 -107
#define COULD_NOT_INIT_ENCLAVE -108
#define FAIL_TO_VERIFY_CERTIFICATE -109
#define SGX_SERVER_FAILED_TO_START -110
#define CORRUPT_DATABASE -111
#define INVALID_SEK -112
#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