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

Add currently_syncing for NVTs in GMP get_feeds (20.08) #1210

Merged
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 @@ -36,6 +36,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Add time placeholders for SCP path [#1164](https://github.com/greenbone/gvmd/pull/1164)
- Expand detection information of results [#1182](https://github.com/greenbone/gvmd/pull/1182)
- Add filter columns for special NVT tags [#1199](https://github.com/greenbone/gvmd/pull/1199)
- Add currently_syncing for NVTs in GMP get_feeds [#1210](https://github.com/greenbone/gvmd/pull/1210)

### Changed
- Update SCAP and CERT feed info in sync scripts [#810](https://github.com/greenbone/gvmd/pull/810)
Expand Down
162 changes: 104 additions & 58 deletions src/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -12349,6 +12349,88 @@ feed_type_name (int feed_type)
}
}

/**
* @brief Gets the status and timestamp of a feed lockfile.
*
* @param[in] lockfile_name Path to the lockfile.
* @param[out] timestamp Optional output o timestamp string.
*
* @return 0 lockfile was not locked, 1 lockfile was locked.
*/
static int
get_feed_lock_status (const char *lockfile_name, gchar **timestamp)
{
int lockfile;
int ret;

if (timestamp)
*timestamp = NULL;
ret = 0;

lockfile = open (lockfile_name,
O_RDWR | O_CREAT | O_APPEND,
/* "-rw-r--r--" */
S_IWUSR | S_IRUSR | S_IROTH | S_IRGRP);
if (lockfile == -1)
g_warning ("%s: failed to open lock file '%s': %s", __func__,
lockfile_name, strerror (errno));
else
{
if (flock (lockfile, LOCK_EX | LOCK_NB)) /* Exclusive, Non blocking. */
{
if (errno == EWOULDBLOCK)
{
gchar *content;
GError *file_error;

/* File is locked, must be a sync in process. */

ret = 1;

if (!g_file_get_contents (lockfile_name, &content, NULL,
&file_error))
{
if (g_error_matches (file_error, G_FILE_ERROR,
G_FILE_ERROR_NOENT)
|| g_error_matches (file_error, G_FILE_ERROR,
G_FILE_ERROR_ACCES))
{
g_error_free (file_error);
}
else
{
g_warning ("%s: %s", __func__, file_error->message);
g_error_free (file_error);
}
}
else
{
gchar **lines;

lines = g_strsplit (content, "\n", 2);
g_free (content);
if (timestamp)
*timestamp = g_strdup(lines[0]);
g_strfreev (lines);
}
}
else
{
g_warning ("%s: flock: %s", __func__, strerror (errno));
}
}
else
/* Got the lock, so no sync is in progress. */
flock (lockfile, LOCK_UN);
}

if (close (lockfile))
g_warning ("%s: failed to close lock file '%s': %s", __func__,
lockfile_name, strerror (errno));

return ret;
}

/**
* @brief Get NVT feed.
*
Expand All @@ -12373,6 +12455,8 @@ get_nvt_feed (gmp_parser_t *gmp_parser, GError **error)
{
gchar **ident = g_strsplit (feed_identification, "|", 6);
gchar *selftest_result = NULL;
const char *lockfile_name;
gchar *timestamp;

if (ident[0] == NULL || ident[1] == NULL
|| ident[2] == NULL || ident[3] == NULL)
Expand Down Expand Up @@ -12403,6 +12487,19 @@ get_nvt_feed (gmp_parser_t *gmp_parser, GError **error)
g_free (selftest_result);
}

/* Note: Checking the feed lockfile assumes that the default scanner
* is running locally.
*/
lockfile_name = get_feed_lock_path ();
if (get_feed_lock_status (lockfile_name, &timestamp))
{
SENDF_TO_CLIENT_OR_FAIL ("<currently_syncing>"
"<timestamp>%s</timestamp>"
"</currently_syncing>",
timestamp);
g_free (timestamp);
}

SEND_TO_CLIENT_OR_FAIL ("</feed>");
}

Expand Down Expand Up @@ -12561,7 +12658,7 @@ get_feed (gmp_parser_t *gmp_parser, GError **error, int feed_type)
{
gchar *feed_name, *feed_description, *feed_version;
const char *lockfile_name;
int lockfile;
gchar *timestamp;

if (feed_type == NVT_FEED)
{
Expand All @@ -12585,66 +12682,15 @@ get_feed (gmp_parser_t *gmp_parser, GError **error, int feed_type)

lockfile_name = get_feed_lock_path ();

lockfile = open (lockfile_name,
O_RDWR | O_CREAT | O_APPEND,
/* "-rw-r--r--" */
S_IWUSR | S_IRUSR | S_IROTH | S_IRGRP);
if (lockfile == -1)
g_warning ("%s: failed to open lock file '%s': %s", __func__,
lockfile_name, strerror (errno));
else
if (get_feed_lock_status (lockfile_name, &timestamp))
{
if (flock (lockfile, LOCK_EX | LOCK_NB)) /* Exclusive, Non blocking. */
{
if (errno == EWOULDBLOCK)
{
gchar *content;
GError *file_error;

/* File is locked, must be a sync in process. */

error = NULL;
if (!g_file_get_contents (lockfile_name, &content, NULL,
&file_error))
{
if (g_error_matches (file_error, G_FILE_ERROR, G_FILE_ERROR_NOENT)
|| g_error_matches (file_error, G_FILE_ERROR,
G_FILE_ERROR_ACCES))
{
g_error_free (file_error);
}
else
{
g_warning ("%s: %s", __func__, file_error->message);
g_error_free (file_error);
}
}
else
{
gchar **lines;

lines = g_strsplit (content, "\n", 2);
g_free (content);
if (lines[0])
SENDF_TO_CLIENT_OR_FAIL ("<currently_syncing>"
"<timestamp>%s</timestamp>"
"</currently_syncing>",
lines[0]);
g_strfreev (lines);
}
}
else
g_warning ("%s: flock: %s", __func__, strerror (errno));
}
else
/* Got the lock, so no sync is in progress. */
flock (lockfile, LOCK_UN);
SENDF_TO_CLIENT_OR_FAIL ("<currently_syncing>"
"<timestamp>%s</timestamp>"
"</currently_syncing>",
timestamp);
g_free (timestamp);
}

if (close (lockfile))
g_warning ("%s: failed to close lock file '%s': %s", __func__,
lockfile_name, strerror (errno));

g_free (feed_name);
g_free (feed_version);
g_free (feed_description);
Expand Down