Skip to content

Commit

Permalink
Add --feed-lock-timeout option
Browse files Browse the repository at this point in the history
This new option sets a number of seconds to retry for if the feed is
locked in contexts (like migration or rebuilds) that do not retry on
their own (like automatic syncs).
This is intended to help automated upgrades of GVM modules where
ospd-openvas needs to be restarted, which locks the feed while the
VTs are reloaded.
  • Loading branch information
timopollmeier committed Apr 1, 2021
1 parent 9caec37 commit 5410313
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 6 deletions.
6 changes: 3 additions & 3 deletions doc/gvmd.8
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ Disable task scheduling.
\fB--feed-lock-path=\fIPATH\fB\f1
Sets the path to the feed lock file.
.TP
\fB--feed-lock-timeout=\fITIMEOUT\fB\f1
Sets the number of seconds to retry for if the feed is locked in contexts (like migration or rebuilds) that do not retry on their own (like automatic syncs). Defaults to 0.
.TP
\fB-f, --foreground\f1
Run in foreground.
.TP
Expand Down Expand Up @@ -182,9 +185,6 @@ Time out tasks that are more than TIME minutes overdue. -1 to disable, 0 for min
\fB--secinfo-commit-size=\fINUMBER\fB\f1
During CERT and SCAP sync, commit updates to the database every NUMBER items, 0 for unlimited.
.TP
\fB--slave-commit-size=\fINUMBER\fB\f1
During slave updates, commit after every NUMBER updated results and hosts, 0 for unlimited.
.TP
\fB-c, --unix-socket=\fIFILENAME\fB\f1
Listen on UNIX socket at FILENAME.
.TP
Expand Down
10 changes: 10 additions & 0 deletions doc/gvmd.8.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<p>Sets the path to the feed lock file.</p>
</optdesc>
</option>
<option>
<p><opt>--feed-lock-timeout=<arg>TIMEOUT</arg></opt></p>
<optdesc>
<p>
Sets the number of seconds to retry for if the feed is locked
in contexts (like migration or rebuilds) that do not retry
on their own (like automatic syncs). Defaults to 0.
</p>
</optdesc>
</option>
<option>
<p><opt>-f, --foreground</opt></p>
<optdesc>
Expand Down
6 changes: 6 additions & 0 deletions doc/gvmd.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ <h2>Options</h2>



<p><b>--feed-lock-timeout=<em>TIMEOUT</em></b></p>

<p>Sets the number of seconds to retry for if the feed is locked in contexts (like migration or rebuilds) that do not retry on their own (like automatic syncs). Defaults to 0.</p>



<p><b>-f, --foreground</b></p>

<p>Run in foreground.</p>
Expand Down
10 changes: 10 additions & 0 deletions src/gvmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,7 @@ gvmd (int argc, char** argv)
static gchar *disable = NULL;
static gchar *value = NULL;
static gchar *feed_lock_path = NULL;
static int feed_lock_timeout = 0;
GError *error = NULL;
lockfile_t lockfile_checking, lockfile_serving;
GOptionContext *option_context;
Expand Down Expand Up @@ -1833,6 +1834,12 @@ gvmd (int argc, char** argv)
&feed_lock_path,
"Sets the path to the feed lock file.",
"<path>" },
{ "feed-lock-timeout", '\0', 0, G_OPTION_ARG_INT,
&feed_lock_timeout,
"Sets the number of seconds to retry for if the feed is locked"
" in contexts (like migration or rebuilds) that do not retry"
" on their own (like automatic syncs). Defaults to 0.",
"<timeout>" },
{ "foreground", 'f', 0, G_OPTION_ARG_NONE,
&foreground,
"Run in foreground.",
Expand Down Expand Up @@ -2081,6 +2088,9 @@ gvmd (int argc, char** argv)

/* Set feed lock path */
set_feed_lock_path (feed_lock_path);

/* Set feed lock timeout */
set_feed_lock_timeout (feed_lock_timeout);

/* Set schedule_timeout */

Expand Down
76 changes: 75 additions & 1 deletion src/manage.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@
*/
static gchar *feed_lock_path = NULL;

/**
* @brief Number of seconds to wait for the feed lock to be released.
*/
static int feed_lock_timeout = 0;

/**
* @brief Path to the relay mapper executable, NULL to disable relays.
*/
Expand Down Expand Up @@ -5937,6 +5942,31 @@ set_feed_lock_path (const char *new_path)
feed_lock_path = g_strdup (GVM_FEED_LOCK_PATH);
}

/**
* @brief Get the feed lock timeout.
*
* @return The current timeout in seconds.
*/
int
get_feed_lock_timeout ()
{
return feed_lock_timeout;
}

/**
* @brief Set the feed lock timeout.
*
* @param new_path The new timeout in seconds.
*/
void
set_feed_lock_timeout (int new_timeout)
{
if (new_timeout < 0)
feed_lock_timeout = 0;
else
feed_lock_timeout = new_timeout;
}

/**
* @brief Write start time to sync lock file.
*
Expand Down Expand Up @@ -5995,6 +6025,50 @@ feed_lockfile_lock (lockfile_t *lockfile)
return 0;
}

/**
* @brief Acquires the feed lock and writes the current time to the lockfile.
*
* @param[out] lockfile Lockfile data struct.
*
* @return 0 success, 1 already locked, -1 error
*/
int
feed_lockfile_lock_timeout (lockfile_t *lockfile)
{
int ret;
gboolean log_timeout;
time_t timeout_end;

/* Try to lock the file */

log_timeout = TRUE;
timeout_end = time (NULL) + feed_lock_timeout;
do
{
ret = 1; // lockfile_lock_path_nb (lockfile, get_feed_lock_path ());
if (ret == 1 && timeout_end > time (NULL))
{
if (log_timeout)
{
log_timeout = FALSE;
g_message ("%s: Feed is currently locked by another process,"
" will retry until %s.",
__func__, iso_time (&timeout_end));
}
gvm_sleep (1);
}
else if (ret)
{
return ret;
}
} while (ret);

/* Write the file contents (timestamp) */
write_sync_start (lockfile->fd);

return 0;
}

/**
* @brief Releases the feed lock and clears the contents.
*
Expand Down Expand Up @@ -6345,7 +6419,7 @@ gvm_migrate_secinfo (int feed_type)
return -1;
}

ret = feed_lockfile_lock (&lockfile);
ret = feed_lockfile_lock_timeout (&lockfile);
if (ret == 1)
return 1;
else if (ret)
Expand Down
9 changes: 9 additions & 0 deletions src/manage.h
Original file line number Diff line number Diff line change
Expand Up @@ -3693,12 +3693,21 @@ get_feed_lock_path ();
void
set_feed_lock_path (const char *);

int
get_feed_lock_timeout ();

void
set_feed_lock_timeout (int);

void
write_sync_start (int);

int
feed_lockfile_lock (lockfile_t *);

int
feed_lockfile_lock_timeout (lockfile_t*);

int
feed_lockfile_unlock (lockfile_t *);

Expand Down
2 changes: 1 addition & 1 deletion src/manage_sql_nvts.c
Original file line number Diff line number Diff line change
Expand Up @@ -2203,7 +2203,7 @@ manage_rebuild (GSList *log_config, const db_conn_info_t *database)

g_info (" Rebuilding NVTs.");

switch (feed_lockfile_lock (&lockfile))
switch (feed_lockfile_lock_timeout (&lockfile))
{
case 1:
printf ("A feed sync is already running.\n");
Expand Down
2 changes: 1 addition & 1 deletion src/manage_sql_secinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -4944,7 +4944,7 @@ rebuild_scap ()
int ret = -1;
lockfile_t lockfile;

ret = feed_lockfile_lock (&lockfile);
ret = feed_lockfile_lock_timeout (&lockfile);
if (ret == 1)
return 2;
else if (ret)
Expand Down

0 comments on commit 5410313

Please sign in to comment.