From fe8fd924ffd272f73b782faee654d2f89404725b Mon Sep 17 00:00:00 2001
From: vertuozzo <vertuozzo@gmail.com>
Date: Tue, 15 Jun 2021 14:34:15 +0300
Subject: [PATCH] Correct stop of a node via a KILL (SIGINT, SIGTERM, etc)
 signal

---
 sources/main.c             |  6 ++--
 sources/sig_unix_handler.c | 71 +++++++++++++++++++++++++++++---------
 2 files changed, 57 insertions(+), 20 deletions(-)

diff --git a/sources/main.c b/sources/main.c
index 6136e06..f5c0ffb 100755
--- a/sources/main.c
+++ b/sources/main.c
@@ -493,9 +493,9 @@ int main( int argc, const char **argv )
 
 //failure:
 
-    #ifdef DAP_SUPPORT_PYTHON_PLUGINS
-        dap_chain_plugins_deinit();
-    #endif
+//    #ifdef DAP_SUPPORT_PYTHON_PLUGINS
+//        dap_chain_plugins_deinit();
+//    #endif
     dap_dns_server_stop();
 	dap_stream_deinit();
 	dap_stream_ctl_deinit();
diff --git a/sources/sig_unix_handler.c b/sources/sig_unix_handler.c
index a10258f..4c900b0 100644
--- a/sources/sig_unix_handler.c
+++ b/sources/sig_unix_handler.c
@@ -1,51 +1,88 @@
 #include <signal.h>
 #include <stdio.h>
 #include <string.h>
+#include <unistd.h>
+
 #include "dap_common.h"
-#include "dap_file_utils.h"
+#include "dap_events.h"
+#include "dap_chain_global_db.h"
 #include "sig_unix_handler.h"
 
 #define LOG_TAG "sig_unix_handler"
 
-static const char *l_pid_path;
+static const char *s_pid_path = NULL;
 
 static void clear_pid_file() {
-    FILE * f = fopen(l_pid_path, "w");
+    FILE * f = fopen(s_pid_path, "w");
     if (f == NULL)
         log_it(L_WARNING, "Pid file not cleared");
     else
         fclose(f);
 }
 
-_Noreturn static void sig_exit_handler(int sig_code) {
+static void sig_exit_handler(int sig_code) {
     log_it(L_DEBUG, "Got exit code: %d", sig_code);
+
     clear_pid_file();
+
+//#ifdef DAP_SUPPORT_PYTHON_PLUGINS
+//    dap_chain_plugins_deinit();
+//#endif
+    dap_chain_node_mempool_autoproc_deinit();
+    dap_chain_net_srv_xchange_deinit();
+    dap_chain_net_srv_stake_deinit();
+    dap_chain_net_deinit();
+    dap_chain_global_db_deinit();
+    dap_chain_deinit();
+    dap_stream_ctl_deinit();
+    dap_stream_deinit();
+    dap_enc_ks_deinit();
+    enc_http_deinit();
+    dap_http_deinit();
+    dap_dns_server_stop();
+    dap_server_deinit();
+    dap_events_stop_all();
+    dap_events_deinit();
+    dap_config_close( g_config );
+    dap_common_deinit();
+
+    //log_it(L_NOTICE,"Stopped Cellframe Node");
+    //fflush(stdout);
+
     exit(0);
 }
 
-/**
- * @brief sig_unix_handler_init
- * @param a_pid_path
- * @return
- */
-int sig_unix_handler_init(const char *a_pid_path)
+int sig_unix_handler_init(const char *a_pid_path) 
 {
-    char * l_pid_dir = dap_path_get_dirname(a_pid_path);
-    sleep(1);
-    dap_mkdir_with_parents(l_pid_dir);
-    DAP_DELETE(l_pid_dir);
+    //char * l_pid_dir = dap_path_get_dirname(a_pid_path);
+    //sleep(1); // Don't know why but without it it crashes O_o 
+    //dap_mkdir_with_parents(l_pid_dir);
+    //DAP_DELETE(l_pid_dir);
+
+    //log_it(L_DEBUG, "Init");
+
+    s_pid_path = strdup(a_pid_path);
 
-    l_pid_path = strdup(a_pid_path);
     signal(SIGINT, sig_exit_handler);
-    signal(SIGTERM, sig_exit_handler);
     signal(SIGHUP, sig_exit_handler);
+    signal(SIGTERM, sig_exit_handler);
+    signal(SIGQUIT, sig_exit_handler);
+    signal(SIGTSTP, sig_exit_handler);
+
     return 0;
 }
 
 int sig_unix_handler_deinit() {
-    free((char*)l_pid_path);
+    //log_it(L_DEBUG, "Deinit");
+
+    if( s_pid_path )
+	DAP_DELETE(s_pid_path);
+
     signal(SIGTERM, SIG_DFL);
     signal(SIGINT, SIG_DFL);
     signal(SIGHUP, SIG_DFL);
+    signal(SIGQUIT, SIG_DFL);
+    signal(SIGTSTP, SIG_DFL);
+
     return 0;
 }
-- 
GitLab