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

Lock a file around the NVT sync #1002

Merged
merged 4 commits into from
Mar 10, 2020
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 25 additions & 1 deletion src/manage_sql_nvts.c
Original file line number Diff line number Diff line change
Expand Up @@ -1717,6 +1717,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. */

Expand Down Expand Up @@ -1747,6 +1748,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;"));

Expand All @@ -1772,6 +1783,8 @@ 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.
Expand Down Expand Up @@ -1840,15 +1853,26 @@ 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)
{
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;
Expand Down