From 32372c60e2b91a3a3514892e1962a6768af31c9c Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Thu, 5 Mar 2020 21:49:48 +0200 Subject: [PATCH 1/3] Lock a file around the NVT sync --- src/manage_sql_nvts.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/manage_sql_nvts.c b/src/manage_sql_nvts.c index cde0acb4e..4bb6e5ddc 100644 --- a/src/manage_sql_nvts.c +++ b/src/manage_sql_nvts.c @@ -1716,6 +1716,7 @@ manage_update_nvt_cache_osp (const gchar *update_socket) { osp_connection_t *connection; gchar *db_feed_version, *scanner_feed_version; + static lockfile_t lockfile; /* Re-open DB after fork. */ @@ -1746,6 +1747,16 @@ manage_update_nvt_cache_osp (const gchar *update_socket) if ((db_feed_version == NULL) || strcmp (scanner_feed_version, db_feed_version)) { + switch (lockfile_lock_nb (&lockfile, "gvm-syncing-nvts")) + { + case 1: + g_warning ("%s: an NVT sync is already running", __func__); + return -1; + case -1: + g_warning ("%s: error getting sync lock", __func__); + return -1; + } + g_info ("OSP service has newer VT status (version %s) than in database (version %s, %i VTs). Starting update ...", scanner_feed_version, db_feed_version, sql_int ("SELECT count (*) FROM nvts;")); @@ -1771,9 +1782,11 @@ manage_sync_nvts (int (*fork_update_nvt_cache) ()) /** * @brief Update or rebuild NVT db. * + * Caller must get the lock. + * * @param[in] update 0 rebuild, else update. * - * @return 0 success, -1 error, -4 no osp update socket. + * @return 0 success, -1 error, -4 no osp update socket, -5 sync active. */ static int update_or_rebuild (int update) @@ -1844,9 +1857,20 @@ int manage_rebuild (GSList *log_config, const gchar *database) { int ret; + static lockfile_t lockfile; g_info (" Rebuilding NVTs."); + switch (lockfile_lock_nb (&lockfile, "gvm-syncing-nvts")) + { + case 1: + printf ("An NVT sync is already running.\n"); + return -5; + case -1: + printf ("Error getting sync lock.\n"); + return -1; + } + ret = manage_option_setup (log_config, database); if (ret) return ret; From a8098d22109931fdef2ab631f4ac28a332948c3e Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Thu, 5 Mar 2020 21:51:57 +0200 Subject: [PATCH 2/3] Move doc --- src/manage_sql_nvts.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/manage_sql_nvts.c b/src/manage_sql_nvts.c index 4bb6e5ddc..4888f6918 100644 --- a/src/manage_sql_nvts.c +++ b/src/manage_sql_nvts.c @@ -1786,7 +1786,7 @@ manage_sync_nvts (int (*fork_update_nvt_cache) ()) * * @param[in] update 0 rebuild, else update. * - * @return 0 success, -1 error, -4 no osp update socket, -5 sync active. + * @return 0 success, -1 error, -4 no osp update socket. */ static int update_or_rebuild (int update) @@ -1851,7 +1851,7 @@ update_or_rebuild (int update) * @param[in] database Location of manage database. * * @return 0 success, -1 error, -2 database is wrong version, - * -3 database needs to be initialised from server. + * -3 database needs to be initialised from server, -5 sync active. */ int manage_rebuild (GSList *log_config, const gchar *database) From 5a57b27bce05e296ce3a163a6f6ae2ef214c4167 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Thu, 5 Mar 2020 22:10:01 +0200 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 84b7397f2..765ffe8f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Print UUIDs in --get-users when --verbose given [#991](https://github.com/greenbone/gvmd/pull/991) - Add --get-roles [#992](https://github.com/greenbone/gvmd/pull/992) - Add --rebuild [#998](https://github.com/greenbone/gvmd/pull/998) +- Lock a file around the NVT sync [#1002](https://github.com/greenbone/gvmd/pull/1002) ### Changed - Update SCAP and CERT feed info in sync scripts [#810](https://github.com/greenbone/gvmd/pull/810)