Skip to content

Commit

Permalink
Fix: Optimized "gvmd --get-users" and "gvmd --get-roles" command.
Browse files Browse the repository at this point in the history
Optimized the "gvmd --get-users" and the "gvmd --get-roles"
command by skipping the update of the nvti cache in those
cases.
Also added a retry loop around "lockfile_lock_nb (...)" so that
commands like "gvmd --get-users" don't fail if another gvmd
process is starting up.
  • Loading branch information
jhelmold authored and a-h-abdelsalam committed Jan 17, 2025
1 parent 3dffad6 commit a9d72d6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/gvmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2561,7 +2561,19 @@ gvmd (int argc, char** argv, char *env[])
* associated files are closed (i.e. when all processes exit). */


switch (lockfile_lock_nb (&lockfile_checking, "gvm-checking"))
int lock_ret;
int retries = 0;

lock_ret = lockfile_lock_nb (&lockfile_checking, "gvm-checking");

while (lock_ret == 1 && retries < MAX_LOCK_RETRIES)
{
gvm_sleep (4);
lock_ret = lockfile_lock_nb (&lockfile_checking, "gvm-checking");
retries++;
}

switch (lock_ret)
{
case 0:
break;
Expand Down Expand Up @@ -2991,7 +3003,9 @@ gvmd (int argc, char** argv, char *env[])
if (option_lock (&lockfile_checking))
return EXIT_FAILURE;

set_skip_update_nvti_cache (TRUE);
ret = manage_get_roles (log_config, &database, verbose);
set_skip_update_nvti_cache (FALSE);
log_config_free ();
if (ret)
return EXIT_FAILURE;
Expand All @@ -3007,7 +3021,9 @@ gvmd (int argc, char** argv, char *env[])
if (option_lock (&lockfile_checking))
return EXIT_FAILURE;

set_skip_update_nvti_cache (TRUE);
ret = manage_get_users (log_config, &database, role, verbose);
set_skip_update_nvti_cache (FALSE);
log_config_free ();
if (ret)
return EXIT_FAILURE;
Expand Down
2 changes: 2 additions & 0 deletions src/manage.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ manage_reset_currents ();

/* Commands. */

#define MAX_LOCK_RETRIES 16

/**
* @brief A command.
*/
Expand Down
7 changes: 5 additions & 2 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -17264,11 +17264,14 @@ init_manage_internal (GSList *log_config,

/* Load the NVT cache into memory. */

if (nvti_cache == NULL)
if (nvti_cache == NULL && !skip_update_nvti_cache ())
update_nvti_cache ();

if (skip_update_nvti_cache ())
avoid_db_check_inserts = TRUE;

if (skip_db_check == 0)
/* Requires NVT cache. */
/* Requires NVT cache if avoid_db_check_inserts == FALSE */
check_db_configs (avoid_db_check_inserts);

sql_close ();
Expand Down
27 changes: 27 additions & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
*/
#define G_LOG_DOMAIN "md manage"

/* Flag if to skip the update of the nvti cache or not. */
static gboolean skip_upd_nvti_cache = FALSE;


/* Sleep. */

Expand Down Expand Up @@ -753,6 +756,30 @@ lockfile_locked (const gchar *lockfile_basename)
return ret;
}

/**
* @brief Set Flag if to run update_nvti_cache () or not.
* The default value of the flag is FALSE.
*
* @param[in] skip_upd_nvti_c Value for the flag if to
* skip the cache update or not.
*/
void
set_skip_update_nvti_cache (gboolean skip_upd_nvti_c)
{
skip_upd_nvti_cache = skip_upd_nvti_c;
}

/**
* @brief Check if to run update_nvti_cache () or not.
*
* @return TRUE skip update, FALSE don't skip update
*/
gboolean
skip_update_nvti_cache ()
{
return skip_upd_nvti_cache;
}


/* UUIDs. */

Expand Down
6 changes: 6 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ lockfile_unlock (lockfile_t *);
int
lockfile_locked (const gchar *);

void
set_skip_update_nvti_cache (gboolean);

gboolean
skip_update_nvti_cache ();

int
is_uuid (const char *);

Expand Down

0 comments on commit a9d72d6

Please sign in to comment.