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 --feed-lock-timeout option (bp #1472) #1474

Merged
merged 4 commits into from
Apr 9, 2021
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 @@ -29,6 +29,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Improve GMP docs around users [#1363](https://github.com/greenbone/gvmd/pull/1363)
- Cache report counts when Dynamic Severity is enabled [#1389](https://github.com/greenbone/gvmd/pull/1389)
- Detection entry detection while importing reports [#1405](https://github.com/greenbone/gvmd/pull/1405)
- Add --feed-lock-timeout option [#1472](https://github.com/greenbone/gvmd/pull/1472)

### Changed
- Move EXE credential generation to a Python script [#1260](https://github.com/greenbone/gvmd/pull/1260) [#1262](https://github.com/greenbone/gvmd/pull/1262)
Expand Down
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 (no retry).
.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 (no retry).
</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 (no retry).</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 (no retry).",
"<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
74 changes: 73 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_timeout 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,48 @@ 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 lock_status;
gboolean log_timeout;
time_t timeout_end;

/* Try to lock the file */

log_timeout = TRUE;
timeout_end = time (NULL) + feed_lock_timeout;
do
{
lock_status = feed_lockfile_lock (lockfile);
if (lock_status == 1 /* already locked, but no error */
&& 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 (lock_status) /* error */
{
return lock_status;
}
} while (lock_status); /* lock is acquired when lock_status is 0 */

return 0;
}

/**
* @brief Releases the feed lock and clears the contents.
*
Expand Down Expand Up @@ -6345,7 +6417,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