Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize the host kb from hosts_new(). #318

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/attack.c
Original file line number Diff line number Diff line change
Expand Up @@ -1121,16 +1121,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;
}

Expand Down
25 changes: 20 additions & 5 deletions src/hosts.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@
#include <string.h> /* for strlen() */
#include <sys/wait.h> /* for waitpid() */
#include <unistd.h> /* for close() */
#include <gvm/base/prefs.h> /* for prefs_get() */

#undef G_LOG_DOMAIN
/**
* @brief GLib log domain.
*/
#define G_LOG_DOMAIN "sd main"

#define KB_RETRY_DELAY 60
/**
* @brief Host information, implemented as doubly linked list.
*/
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/hosts.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 14 additions & 2 deletions src/openvassd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*/
Expand Down Expand Up @@ -604,13 +605,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.");
Expand All @@ -623,6 +634,7 @@ check_kb_status ()
kb_delete (kb_access_aux);
break;
}
log_flag = 1;
}

if (waitredis == 0)
Expand Down