From 4a0320f107f294d1b870d44b10ae4b5ebd73f924 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Mon, 17 Aug 2020 22:47:23 +0200
Subject: Made sure the server has everything required to run.

---
 plugins/pychrysalide/pychrysa.c |  4 +--
 src/core/core.c                 | 64 +++++++++++++++++++++++------------------
 src/core/core.h                 |  4 +--
 src/hub.c                       |  6 ++++
 src/main.c                      | 10 +++----
 5 files changed, 51 insertions(+), 37 deletions(-)

diff --git a/plugins/pychrysalide/pychrysa.c b/plugins/pychrysalide/pychrysa.c
index 8e6cfbc..d1958ee 100644
--- a/plugins/pychrysalide/pychrysa.c
+++ b/plugins/pychrysalide/pychrysa.c
@@ -360,7 +360,7 @@ static void PyExit_pychrysalide(void)
 
     exit_all_plugins();
 
-    unload_all_basic_components();
+    unload_all_core_components(true);
 
 #ifdef TRACK_GOBJECT_LEAKS
     dump_remaining_gtypes();
@@ -498,7 +498,7 @@ PyMODINIT_FUNC PyInit_pychrysalide(void)
     if (!set_version_for_gtk_namespace("3.0"))
         goto exit;
 
-    if (!load_all_basic_components())
+    if (!load_all_core_components(true))
     {
         PyErr_SetString(PyExc_SystemError, "unable to load all basic components.");
         goto exit;
diff --git a/src/core/core.c b/src/core/core.c
index 7be81b2..a72122c 100644
--- a/src/core/core.c
+++ b/src/core/core.c
@@ -49,7 +49,7 @@
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : -                                                            *
+*  Paramètres  : cs = précise si l'appel est réalisé du côté client.          *
 *                                                                             *
 *  Description : Charge les éléments de base du programme.                    *
 *                                                                             *
@@ -59,7 +59,7 @@
 *                                                                             *
 ******************************************************************************/
 
-bool load_all_basic_components(void)
+bool load_all_core_components(bool cs)
 {
     static bool result = false;             /* Bilan à retourner           */
     char *cfgdir;                           /* Répertoire de configuration */
@@ -79,33 +79,37 @@ bool load_all_basic_components(void)
         result &= (ensure_path_exists(cfgdir) == 0);
         free(cfgdir);
 
-        g_boxed_type_register_static("vmpa_t",
-                                     (GBoxedCopyFunc)dup_vmpa,
-                                     (GBoxedFreeFunc)delete_vmpa);
-
         ERR_load_crypto_strings();
         SSL_load_error_strings();
         SSL_library_init();
 
-        result &= init_chrysalide_dynamic_types();
+        if (result) result = init_global_works();
+
+        if (result) result = load_hard_coded_collection_definitions();
+
+        if (cs)
+        {
+            g_boxed_type_register_static("vmpa_t",
+                                         (GBoxedCopyFunc)dup_vmpa,
+                                         (GBoxedFreeFunc)delete_vmpa);
 
-        result &= load_main_config_parameters();
+            if (result) result = load_main_config_parameters();
 
-        result &= g_generic_config_read(get_main_configuration());
+            if (result) result = g_generic_config_read(get_main_configuration());
 
-        result &= init_global_works();
+            explorer = g_content_explorer_new();
+            set_current_content_explorer(explorer);
 
-        explorer = g_content_explorer_new();
-        set_current_content_explorer(explorer);
+            resolver = g_content_resolver_new();
+            set_current_content_resolver(resolver);
 
-        resolver = g_content_resolver_new();
-        set_current_content_resolver(resolver);
+            if (result) result = init_segment_content_hash_table();
 
-        result &= init_segment_content_hash_table();
+            register_arch_gtypes();
 
-        register_arch_gtypes();
+            if (result) result = init_chrysalide_dynamic_types();
 
-        result &= load_hard_coded_collection_definitions();
+        }
 
     }
 
@@ -116,7 +120,7 @@ bool load_all_basic_components(void)
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : -                                                            *
+*  Paramètres  : cs = précise si l'appel est réalisé du côté client.          *
 *                                                                             *
 *  Description : Décharge les éléments de base du programme.                  *
 *                                                                             *
@@ -126,25 +130,29 @@ bool load_all_basic_components(void)
 *                                                                             *
 ******************************************************************************/
 
-void unload_all_basic_components(void)
+void unload_all_core_components(bool cs)
 {
-    unload_collection_definitions();
+    if (cs)
+    {
+        exit_chrysalide_dynamic_types();
 
-    unload_demanglers_definitions();
+        unload_demanglers_definitions();
 
-    unload_processors_definitions();
+        unload_processors_definitions();
 
-    set_current_content_explorer(NULL);
+        set_current_content_resolver(NULL);
 
-    set_current_content_resolver(NULL);
+        set_current_content_explorer(NULL);
 
-    exit_global_works();
+        g_generic_config_write(get_main_configuration());
 
-    g_generic_config_write(get_main_configuration());
+        unload_main_config_parameters();
 
-    unload_main_config_parameters();
+    }
 
-    exit_chrysalide_dynamic_types();
+    unload_collection_definitions();
+
+    exit_global_works();
 
     ERR_free_strings();
 
diff --git a/src/core/core.h b/src/core/core.h
index 627531c..0221f56 100644
--- a/src/core/core.h
+++ b/src/core/core.h
@@ -30,10 +30,10 @@
 
 
 /* Charge les éléments de base du programme. */
-bool load_all_basic_components(void);
+bool load_all_core_components(bool);
 
 /* Décharge les éléments de base du programme. */
-void unload_all_basic_components(void);
+void unload_all_core_components(bool);
 
 
 
diff --git a/src/hub.c b/src/hub.c
index b77bdb8..54b84ce 100644
--- a/src/hub.c
+++ b/src/hub.c
@@ -41,6 +41,7 @@
 #include "gleak.h"
 #include "analysis/db/auth.h"
 #include "analysis/db/server.h"
+#include "core/core.h"
 #include "core/global.h"
 #include "core/logs.h"
 #include "core/paths.h"
@@ -360,6 +361,9 @@ int main(int argc, char **argv)
 
     set_log_verbosity(verbosity);
 
+    if (!load_all_core_components(false))
+        goto done;
+
     /* Traitement des commandes */
 
     switch (command)
@@ -387,6 +391,8 @@ int main(int argc, char **argv)
 
     }
 
+    unload_all_core_components(false);
+
 #ifdef TRACK_GOBJECT_LEAKS
     remember_gtypes_for_leaks();
 #endif
diff --git a/src/main.c b/src/main.c
index 1b50615..7d49ba6 100644
--- a/src/main.c
+++ b/src/main.c
@@ -303,8 +303,8 @@ int main(int argc, char **argv)
 
     set_log_verbosity(verbosity);
 
-    if (!load_all_basic_components())
-        return EXIT_FAILURE;
+    if (!load_all_core_components(true))
+        goto done;
 
     /* Création de l'interface */
 
@@ -449,14 +449,14 @@ int main(int argc, char **argv)
 
  failed_to_load_editor:
 
-    unload_all_basic_components();
-
- done:
+    unload_all_core_components(true);
 
 #ifdef TRACK_GOBJECT_LEAKS
     dump_remaining_gtypes();
 #endif
 
+ done:
+
     return result;
 
 }
-- 
cgit v0.11.2-87-g4458