From 5a04f61a2ecd76ceda404c5e58865d862d696800 Mon Sep 17 00:00:00 2001 From: Johannes Helmold Date: Mon, 23 Dec 2024 13:01:56 +0100 Subject: [PATCH 1/2] Fix: Optimized "gvmd --get-users" and "gvmd --get-roles" command. 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. --- src/gvmd.c | 18 +++++++++++++++++- src/manage.h | 2 ++ src/manage_sql.c | 7 +++++-- src/utils.c | 27 +++++++++++++++++++++++++++ src/utils.h | 6 ++++++ 5 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/gvmd.c b/src/gvmd.c index f5c0100b0..3a2a3a6aa 100644 --- a/src/gvmd.c +++ b/src/gvmd.c @@ -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; @@ -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; @@ -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; diff --git a/src/manage.h b/src/manage.h index ce073b277..3d8dfa9d1 100644 --- a/src/manage.h +++ b/src/manage.h @@ -144,6 +144,8 @@ manage_reset_currents (); /* Commands. */ +#define MAX_LOCK_RETRIES 16 + /** * @brief A command. */ diff --git a/src/manage_sql.c b/src/manage_sql.c index 99635d030..7d26fa904 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -17275,11 +17275,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 (); diff --git a/src/utils.c b/src/utils.c index 32588497b..115a3efc1 100644 --- a/src/utils.c +++ b/src/utils.c @@ -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. */ @@ -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. */ diff --git a/src/utils.h b/src/utils.h index 10fcf59d8..f09447429 100644 --- a/src/utils.h +++ b/src/utils.h @@ -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 *); From 6157b4fdf3d0ee282ac2893b866bde95b2541ba6 Mon Sep 17 00:00:00 2001 From: Johannes Helmold Date: Mon, 23 Dec 2024 13:31:42 +0100 Subject: [PATCH 2/2] Amended some formatting. --- src/utils.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/utils.h b/src/utils.h index f09447429..ed97ff74f 100644 --- a/src/utils.h +++ b/src/utils.h @@ -85,8 +85,7 @@ lockfile_unlock (lockfile_t *); int lockfile_locked (const gchar *); -void -set_skip_update_nvti_cache (gboolean); +void set_skip_update_nvti_cache (gboolean); gboolean skip_update_nvti_cache ();