From b7ddb81efbcee8e590328d6e971e45a3637d1635 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Mon, 29 Apr 2019 11:14:43 -0400 Subject: [PATCH 1/2] Initialize the host kb from hosts_new(). If there is no more availabe redis kb, it will wait until a host finishes and release a db for the next one. This avoid to hanging in the kb initialization and keeps working the results forwarding. --- src/attack.c | 12 ++++++------ src/hosts.c | 25 ++++++++++++++++++++----- src/hosts.h | 2 +- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/attack.c b/src/attack.c index 51b5515fb..968536bdf 100644 --- a/src/attack.c +++ b/src/attack.c @@ -1131,16 +1131,16 @@ attack_network (struct scan_globals *globals, kb_t *network_kb) struct attack_start_args args; char *host_str; - rc = kb_new (&host_kb, prefs_get ("db_address")); - if (rc) + host_str = gvm_host_value_str (host); + rc = hosts_new (globals, host_str, &host_kb); + if (rc == -1) { - report_kb_failure (global_socket, rc); + g_free (host_str); goto scan_stop; } - host_str = gvm_host_value_str (host); - if (hosts_new (globals, host_str, host_kb) < 0) + if (rc == -2) { - g_free (host_str); + report_kb_failure (global_socket, rc); goto scan_stop; } diff --git a/src/hosts.c b/src/hosts.c index 66217ef6d..1b076cb07 100644 --- a/src/hosts.c +++ b/src/hosts.c @@ -34,6 +34,7 @@ #include /* for strlen() */ #include /* for waitpid() */ #include /* for close() */ +#include /* for prefs_get() */ #undef G_LOG_DOMAIN /** @@ -41,6 +42,7 @@ */ #define G_LOG_DOMAIN "sd main" +#define KB_RETRY_DELAY 60 /** * @brief Host information, implemented as doubly linked list. */ @@ -208,22 +210,35 @@ hosts_init (int soc, int max_hosts) } int -hosts_new (struct scan_globals *globals, char *name, kb_t kb) +hosts_new (struct scan_globals *globals, char *name, kb_t *kb) { struct host *h; + int rc = 0; - while (hosts_num () >= g_max_hosts) + do { - if (hosts_read (globals) < 0) - return -1; + rc = kb_new (kb, prefs_get ("db_address")); + if (rc < 0 && rc != -2) + return -2; + else if (rc == -2) + sleep(KB_RETRY_DELAY); + + if (hosts_num () > 0) + if (hosts_read (globals) < 0) + return -1; + + if (hosts_num () >= g_max_hosts) + kb_delete(*kb); } + while (hosts_num () >= g_max_hosts || rc == -2); + if (global_scan_stop) return 0; h = g_malloc0 (sizeof (struct host)); h->name = g_strdup (name); h->pid = 0; - h->host_kb = kb; + h->host_kb = *kb; if (hosts != NULL) hosts->prev = h; h->next = hosts; diff --git a/src/hosts.h b/src/hosts.h index 00e776787..334636008 100644 --- a/src/hosts.h +++ b/src/hosts.h @@ -32,7 +32,7 @@ int hosts_init (int, int); int -hosts_new (struct scan_globals *, char *, kb_t); +hosts_new (struct scan_globals *, char *, kb_t *); int hosts_set_pid (char *, pid_t); From e3fe84acf4a236c2e4514b30de20f4df7020435f Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Mon, 13 May 2019 06:32:32 -0400 Subject: [PATCH 2/2] Add a delay in kb_check_status(), in case there is no DB available. Because this delay was removed from the function in gvm-libs. --- src/openvassd.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/openvassd.c b/src/openvassd.c index f8f55e499..e41f12cdb 100644 --- a/src/openvassd.c +++ b/src/openvassd.c @@ -85,6 +85,7 @@ #define PROCTITLE_RELOADING "openvassd: Reloading" #define PROCTITLE_SERVING "openvassd: Serving %s" +#define KB_RETRY_DELAY 60 /** * Globals that should not be touched (used in utils module). */ @@ -615,13 +616,23 @@ stop_all_scans (void) void check_kb_status () { - int waitredis = 5, waitkb = 5, ret = 0; - + int waitredis = 5, waitkb = 5, ret = 0, log_flag = 1; kb_t kb_access_aux; while (waitredis != 0) { ret = kb_new (&kb_access_aux, prefs_get ("db_address")); + if (ret == -2) + { + if (log_flag) + { + g_warning ("No redis DB available, It will retry every %ds...", + KB_RETRY_DELAY); + log_flag = 0; + } + sleep (KB_RETRY_DELAY); + continue; + } if (ret) { g_message ("Redis connection lost. Trying to reconnect."); @@ -634,6 +645,7 @@ check_kb_status () kb_delete (kb_access_aux); break; } + log_flag = 1; } if (waitredis == 0)