Skip to content

Commit 2ae62ab

Browse files
authored
Merge pull request #339 from jjnicola/wait-newkb
Initialize the host kb from hosts_new()
2 parents 7e7465a + 0a045c2 commit 2ae62ab

File tree

4 files changed

+41
-14
lines changed

4 files changed

+41
-14
lines changed

src/attack.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,16 +1111,16 @@ attack_network (struct scan_globals *globals, kb_t *network_kb)
11111111
struct attack_start_args args;
11121112
char *host_str;
11131113

1114-
rc = kb_new (&host_kb, prefs_get ("db_address"));
1115-
if (rc)
1114+
host_str = gvm_host_value_str (host);
1115+
rc = hosts_new (globals, host_str, &host_kb);
1116+
if (rc == -1)
11161117
{
1117-
report_kb_failure (global_socket, rc);
1118+
g_free (host_str);
11181119
goto scan_stop;
11191120
}
1120-
host_str = gvm_host_value_str (host);
1121-
if (hosts_new (globals, host_str, host_kb) < 0)
1121+
if (rc == -2)
11221122
{
1123-
g_free (host_str);
1123+
report_kb_failure (global_socket, rc);
11241124
goto scan_stop;
11251125
}
11261126

src/hosts.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@
3434
#include <string.h> /* for strlen() */
3535
#include <sys/wait.h> /* for waitpid() */
3636
#include <unistd.h> /* for close() */
37+
#include <gvm/base/prefs.h> /* for prefs_get() */
3738

3839
#undef G_LOG_DOMAIN
3940
/**
4041
* @brief GLib log domain.
4142
*/
4243
#define G_LOG_DOMAIN "sd main"
4344

45+
#define KB_RETRY_DELAY 60
4446
/**
4547
* @brief Host information, implemented as doubly linked list.
4648
*/
@@ -208,22 +210,35 @@ hosts_init (int soc, int max_hosts)
208210
}
209211

210212
int
211-
hosts_new (struct scan_globals *globals, char *name, kb_t kb)
213+
hosts_new (struct scan_globals *globals, char *name, kb_t *kb)
212214
{
213215
struct host *h;
216+
int rc = 0;
214217

215-
while (hosts_num () >= g_max_hosts)
218+
do
216219
{
217-
if (hosts_read (globals) < 0)
218-
return -1;
220+
rc = kb_new (kb, prefs_get ("db_address"));
221+
if (rc < 0 && rc != -2)
222+
return -2;
223+
else if (rc == -2)
224+
sleep(KB_RETRY_DELAY);
225+
226+
if (hosts_num () > 0)
227+
if (hosts_read (globals) < 0)
228+
return -1;
229+
230+
if (hosts_num () >= g_max_hosts)
231+
kb_delete(*kb);
219232
}
233+
while (hosts_num () >= g_max_hosts || rc == -2);
234+
220235
if (global_scan_stop)
221236
return 0;
222237

223238
h = g_malloc0 (sizeof (struct host));
224239
h->name = g_strdup (name);
225240
h->pid = 0;
226-
h->host_kb = kb;
241+
h->host_kb = *kb;
227242
if (hosts != NULL)
228243
hosts->prev = h;
229244
h->next = hosts;

src/hosts.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ int
3232
hosts_init (int, int);
3333

3434
int
35-
hosts_new (struct scan_globals *, char *, kb_t);
35+
hosts_new (struct scan_globals *, char *, kb_t *);
3636

3737
int
3838
hosts_set_pid (char *, pid_t);

src/openvassd.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
#define PROCTITLE_RELOADING "openvassd: Reloading"
8686
#define PROCTITLE_SERVING "openvassd: Serving %s"
8787

88+
#define KB_RETRY_DELAY 60
8889
/**
8990
* Globals that should not be touched (used in utils module).
9091
*/
@@ -610,13 +611,23 @@ stop_all_scans (void)
610611
void
611612
check_kb_status ()
612613
{
613-
int waitredis = 5, waitkb = 5, ret = 0;
614-
614+
int waitredis = 5, waitkb = 5, ret = 0, log_flag = 1;
615615
kb_t kb_access_aux;
616616

617617
while (waitredis != 0)
618618
{
619619
ret = kb_new (&kb_access_aux, prefs_get ("db_address"));
620+
if (ret == -2)
621+
{
622+
if (log_flag)
623+
{
624+
g_warning ("No redis DB available, It will retry every %ds...",
625+
KB_RETRY_DELAY);
626+
log_flag = 0;
627+
}
628+
sleep (KB_RETRY_DELAY);
629+
continue;
630+
}
620631
if (ret)
621632
{
622633
g_message ("Redis connection lost. Trying to reconnect.");
@@ -629,6 +640,7 @@ check_kb_status ()
629640
kb_delete (kb_access_aux);
630641
break;
631642
}
643+
log_flag = 1;
632644
}
633645

634646
if (waitredis == 0)

0 commit comments

Comments
 (0)