From 0ee16ccaf02932de92cf952c95b1f04a97caeb97 Mon Sep 17 00:00:00 2001
From: Oleh Nikolaiev <oleg@skalelabs.com>
Date: Thu, 1 Apr 2021 12:56:32 +0300
Subject: [PATCH] SKALE-4005 add unique exit codes

---
 CSRManagerServer.cpp      |  2 +-
 ExitHandler.h             | 10 ++++++----
 LevelDB.cpp               |  4 ++--
 SEKManager.cpp            | 10 +++++-----
 SGXInfoServer.cpp         |  2 +-
 SGXRegistrationServer.cpp |  2 +-
 SGXWalletServer.cpp       | 14 +++++---------
 ServerInit.cpp            | 14 +++++++-------
 sgxwall.cpp               | 22 +++++++---------------
 sgxwall.h                 |  1 -
 10 files changed, 35 insertions(+), 46 deletions(-)

diff --git a/CSRManagerServer.cpp b/CSRManagerServer.cpp
index 8ed5664..78e2b7f 100644
--- a/CSRManagerServer.cpp
+++ b/CSRManagerServer.cpp
@@ -120,7 +120,7 @@ int CSRManagerServer::initCSRManagerServer() {
 
     if (!cs->StartListening()) {
         spdlog::info("CSR manager server could not start listening");
-        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
+        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_starting_server);
         exit(-1);
     } else {
         spdlog::info("CSR manager server started on port {}", BASE_PORT + 2);
diff --git a/ExitHandler.h b/ExitHandler.h
index 06540fb..3012f54 100644
--- a/ExitHandler.h
+++ b/ExitHandler.h
@@ -11,11 +11,13 @@ public:
         ec_success = 0,
         ec_failure = 1,  // same as EXIT_FAILURE in stdlib.h, generic failure in main()
         ec_termninated_by_signal = 196,
-        ec_compute_snapshot_error = 197,  // snapshot computation error
+        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_consensus_terminate_request = 198,  // exit requested by consensus
-        ec_web3_request = 199,                 // programmatic shutdown via Web3 call, when enabled
-        ec_state_root_mismatch = 200,  // current state root is not equal to arrived from consensus
+        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
     };
 
 private:
diff --git a/LevelDB.cpp b/LevelDB.cpp
index 2810fa9..79f13e8 100644
--- a/LevelDB.cpp
+++ b/LevelDB.cpp
@@ -276,7 +276,7 @@ void LevelDB::initDataFolderAndDBs() {
 
     if (getcwd(cwd, sizeof(cwd)) == NULL) {
         spdlog::error("could not get current workin directory");
-        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
+        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_creating_database);
         exit(-2);
     }
 
@@ -291,7 +291,7 @@ void LevelDB::initDataFolderAndDBs() {
         }
         else{
             spdlog::error("Couldnt create creating sgx_data folder");
-            ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
+            ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_creating_database);
             exit(-3);
         }
     }
diff --git a/SEKManager.cpp b/SEKManager.cpp
index 1785ffa..3b0d45c 100644
--- a/SEKManager.cpp
+++ b/SEKManager.cpp
@@ -91,7 +91,7 @@ void validate_SEK() {
     if (!hex2carray(test_key_ptr->c_str(), &len, encr_test_key.data(),
                     BUF_LEN)) {
         spdlog::error("Corrupt test key is LevelDB");
-        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
+        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_initing_sek);
         exit(-4);
     }
 
@@ -110,7 +110,7 @@ void validate_SEK() {
         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("Then run sgxwallet using backup flag");
-        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
+        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_initing_sek);
         exit(-5);
     }
 }
@@ -259,14 +259,14 @@ void enter_SEK() {
     shared_ptr <string> test_key_ptr = LevelDB::getLevelDb()->readString("TEST_KEY");
     if (test_key_ptr == nullptr) {
         spdlog::error("Error: corrupt or empty LevelDB database");
-        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
+        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_initing_sek);
         exit(-7);
     }
 
 
     if (!experimental::filesystem::is_regular_file(BACKUP_PATH)) {
         spdlog::error("File does not exist: "  BACKUP_PATH);
-        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
+        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_initing_sek);
         exit(-8);
     }
 
@@ -283,7 +283,7 @@ void enter_SEK() {
 
     while (!checkHex(sek, 16)) {
         spdlog::error("Invalid hex in key");
-        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
+        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_initing_sek);
         exit(-9);
     }
 
diff --git a/SGXInfoServer.cpp b/SGXInfoServer.cpp
index 625d382..7fb8a51 100644
--- a/SGXInfoServer.cpp
+++ b/SGXInfoServer.cpp
@@ -116,7 +116,7 @@ int SGXInfoServer::initInfoServer(uint32_t _logLevel, bool _autoSign, bool _chec
 
     if (!server->StartListening()) {
         spdlog::error("Info server could not start listening on port {}", BASE_PORT + 4);
-        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
+        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_starting_server);
         exit(-10);
     } else {
         spdlog::info("Info server started on port {}", BASE_PORT + 4);
diff --git a/SGXRegistrationServer.cpp b/SGXRegistrationServer.cpp
index 064564c..9cf814c 100644
--- a/SGXRegistrationServer.cpp
+++ b/SGXRegistrationServer.cpp
@@ -173,7 +173,7 @@ int SGXRegistrationServer::initRegistrationServer(bool _autoSign) {
 
     if (!server->StartListening()) {
         spdlog::error("Registration server could not start listening on port {}", BASE_PORT + 1);
-        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
+        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_starting_server);
         exit(-10);
     } else {
         spdlog::info("Registration server started on port {}", BASE_PORT + 1);
diff --git a/SGXWalletServer.cpp b/SGXWalletServer.cpp
index 3c4349f..aaf6208 100644
--- a/SGXWalletServer.cpp
+++ b/SGXWalletServer.cpp
@@ -142,7 +142,7 @@ void SGXWalletServer::createCertsIfNeeded() {
             spdlog::info("ROOT CA CERTIFICATE IS SUCCESSFULLY GENERATED");
         } else {
             spdlog::error("ROOT CA CERTIFICATE GENERATION FAILED");
-            ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
+            ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_creating_certificate);
             exit(-11);
         }
     }
@@ -160,7 +160,7 @@ void SGXWalletServer::createCertsIfNeeded() {
             spdlog::info("SERVER CERTIFICATE IS SUCCESSFULLY GENERATED");
         } else {
             spdlog::info("SERVER CERTIFICATE GENERATION FAILED");
-            ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
+            ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_creating_certificate);
             exit(-12);
         }
     }
@@ -171,7 +171,7 @@ void SGXWalletServer::createCertsIfNeeded() {
         spdlog::info("SERVER CERTIFICATE IS SUCCESSFULLY VERIFIED");
     } else {
         spdlog::info("SERVER CERTIFICATE VERIFICATION FAILED");
-        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
+        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_creating_certificate);
         exit(-12);
     }
 }
@@ -182,10 +182,6 @@ int SGXWalletServer::initHttpsServer(bool _checkCerts) {
     spdlog::info("Entering {}", __FUNCTION__);
     spdlog::info("Initing server, number of threads: {}", NUM_THREADS);
 
-
-
-
-
     string certPath = string(SGXDATA_FOLDER) + "cert_data/SGXServerCert.crt";
     string keyPath = string(SGXDATA_FOLDER) + "cert_data/SGXServerCert.key";
     string rootCAPath = string(SGXDATA_FOLDER) + "cert_data/rootCA.pem";
@@ -201,7 +197,7 @@ int SGXWalletServer::initHttpsServer(bool _checkCerts) {
 
     if (!server->StartListening()) {
         spdlog::error("SGX Server could not start listening");
-        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
+        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_starting_server);
         exit(-13);
     } else {
         spdlog::info("SGX Server started on port {}", BASE_PORT);
@@ -221,7 +217,7 @@ int SGXWalletServer::initHttpServer() { //without ssl
                                           JSONRPC_SERVER_V2); // hybrid server (json-rpc 1.0 & 2.0)
     if (!server->StartListening()) {
         spdlog::error("Server could not start listening");
-        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
+        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_error_starting_server);
         exit(-14);
     }
     return 0;
diff --git a/ServerInit.cpp b/ServerInit.cpp
index e0f1ed5..dea128d 100644
--- a/ServerInit.cpp
+++ b/ServerInit.cpp
@@ -71,7 +71,7 @@ void systemHealthCheck() {
         ulimit = exec("/bin/bash -c \"ulimit -n\"");
     } catch (...) {
         spdlog::error("Execution of '/bin/bash -c ulimit -n' failed");
-        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
+        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_initing_user_space);
         exit(-15);
     }
     int noFiles = strtol(ulimit.c_str(), NULL, 10);
@@ -86,7 +86,7 @@ void systemHealthCheck() {
                 "and setting 'DefaultLimitNOFILE=65535'\n"
                 "After that, restart sgxwallet";
         spdlog::error(errStr);
-        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
+        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_initing_user_space);
         exit(-16);
     }
 }
@@ -119,7 +119,7 @@ uint64_t initEnclave() {
     support = get_sgx_support();
     if (!SGX_OK(support)) {
         sgx_support_perror(support);
-        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
+        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_initing_enclave);
         exit(-17);
     }
 #endif
@@ -151,7 +151,7 @@ uint64_t initEnclave() {
             } else {
                 spdlog::error("sgx_create_enclave_search failed {} {}", ENCLAVE_NAME, status);
             }
-            ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
+            ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_initing_enclave);
             exit(-21);
         }
 
@@ -227,18 +227,18 @@ void initAll(uint32_t _logLevel, bool _checkCert,
         sgxServerInited = true;
     } catch (SGXException &_e) {
         spdlog::error(_e.getMessage());
-        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
+        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_initing_user_space);
         exit(-18);
     } catch (exception &_e) {
         spdlog::error(_e.what());
-        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
+        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_initing_user_space);
         exit(-19);
     }
     catch (...) {
         exception_ptr p = current_exception();
         printf("Exception %s \n", p.__cxa_exception_type()->name());
         spdlog::error("Unknown exception");
-        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
+        ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_initing_user_space);
         exit(-22);
     }
 };
diff --git a/sgxwall.cpp b/sgxwall.cpp
index d05827a..76711b8 100644
--- a/sgxwall.cpp
+++ b/sgxwall.cpp
@@ -42,12 +42,6 @@
 #include "sgxwallet.h"
 
 
-void SGXWallet::usage() {
-    cerr << "usage: sgxwallet\n";
-    ExitHandler::exitHandler(SIGTERM, ExitHandler::ec_failure);
-    exit(-21);
-}
-
 void SGXWallet::printUsage() {
     cerr << "\nAvailable flags:\n";
     cerr << "\nDebug flags:\n\n";
@@ -211,16 +205,14 @@ int main(int argc, char *argv[]) {
 
 
 
-    while (true) {
+    while ( !ExitHandler::shouldExit() ) {
         sleep(10);
-        if ( ExitHandler::shouldExit() ) {
-            ExitHandler::exit_code_t exitCode = ExitHandler::requestedExitCode();
-            spdlog::info("Will exit with exit code {}", exitCode);
-            exitAll();
-            spdlog::info("Exiting with exit code {}", exitCode);
-            return exitCode;
-        }
     }
 
-    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;
 }
diff --git a/sgxwall.h b/sgxwall.h
index de5d049..5f5e7d6 100644
--- a/sgxwall.h
+++ b/sgxwall.h
@@ -26,7 +26,6 @@ class SGXWallet {
 
 public:
 
-    static void usage();
     static void printUsage();
 
     static void serializeKeys( const vector<string>& _ecdsaKeyNames,
-- 
2.18.1