From 2492b0cd2bb28fe3bcf265bd516521a0f6067c87 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Tue, 20 Feb 2018 12:05:45 +0200 Subject: [PATCH 01/19] Use lockfile instead of transaction to prevent processes from creating functions simultaneously --- src/manage_sql.c | 9 +++-- src/utils.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++- src/utils.h | 16 ++++++++ 3 files changed, 124 insertions(+), 4 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index baf283a28..7c77e599b 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -13987,6 +13987,8 @@ task_average_scan_duration (task_t task) void init_manage_process (int update_nvt_cache, const gchar *database) { + lockfile_t lockfile; + if (sql_is_open ()) return; @@ -14012,14 +14014,15 @@ init_manage_process (int update_nvt_cache, const gchar *database) /* Lock to avoid an error return from Postgres when multiple processes * create a function at the same time. */ - sql_begin_exclusive (); + if (lockfile_lock (&lockfile, "gvm-create-functions")) + abort (); if (manage_create_sql_functions ()) { - sql_rollback (); + lockfile_unlock (&lockfile); g_warning ("%s: failed to create functions", __FUNCTION__); abort (); } - sql_commit (); + lockfile_unlock (&lockfile); } /** diff --git a/src/utils.c b/src/utils.c index 05e6df75c..ca218cc65 100644 --- a/src/utils.c +++ b/src/utils.c @@ -28,10 +28,15 @@ #include "utils.h" +#include #include -#include +#include #include #include +#include +#include +#include +#include #undef G_LOG_DOMAIN /** @@ -348,3 +353,99 @@ iso_time_tz (time_t *epoch_time, const char *timezone, const char **abbrev) g_free (tz); return ret; } + + +/* Locks. */ + +/** + * @brief Lock a file exclusively. + * + * Block until file is locked. + * + * @param[in] lockfile Lockfile. + * @param[in] lockfile_basename Basename of lock file. + * + * @return 0 sync complete, 1 already locked, -1 error + */ +int +lockfile_lock (lockfile_t *lockfile, const gchar *lockfile_basename) +{ + int fd; + gchar *lockfile_name; + + /* Open the lock file. */ + + lockfile_name = g_build_filename (g_get_tmp_dir (), lockfile_basename, NULL); + + fd = open (lockfile_name, O_RDWR | O_CREAT | O_APPEND, + /* "-rw-r--r--" */ + S_IWUSR | S_IRUSR | S_IROTH | S_IRGRP); + if (fd == -1) + { + g_warning ("Failed to open lock file '%s': %s", lockfile_name, + strerror (errno)); + lockfile->name = NULL; + g_free (lockfile_name); + return -1; + } + + lockfile->fd = fd; + + /* Lock the lockfile. */ + + if (flock (fd, LOCK_EX)) /* Exclusive, blocking. */ + { + lockfile->name = NULL; + g_free (lockfile_name); + if (errno == EWOULDBLOCK) + return 1; + g_debug ("%s: flock: %s", __FUNCTION__, strerror (errno)); + g_free (lockfile_name); + return -1; + } + + lockfile->name = lockfile_name; + + return 0; +} + +/** + * @brief Unlock a file. + * + * @param[in] lockfile Lockfile. + * + * @return 0 success, -1 error + */ +int +lockfile_unlock (lockfile_t *lockfile) +{ + if (lockfile->name == NULL) + return 0; + + assert (lockfile->fd); + + /* Close the lock file. */ + + if (close (lockfile->fd)) + { + g_free (lockfile->name); + lockfile->name = NULL; + g_warning ("Failed to close lock file: %s", strerror (errno)); + return -1; + } + + /* Remove the lock file. */ + + if (unlink (lockfile->name)) + { + g_free (lockfile->name); + lockfile->name = NULL; + g_warning ("Failed to remove lock file: %s", strerror (errno)); + return -1; + } + + g_free (lockfile->name); + lockfile->name = NULL; + + return 0; +} diff --git a/src/utils.h b/src/utils.h index bc26375c0..57737ce81 100644 --- a/src/utils.h +++ b/src/utils.h @@ -27,6 +27,7 @@ #define _GVMD_UTILS_H #include +#include int gvm_usleep (unsigned int); @@ -55,4 +56,19 @@ iso_time (time_t *); char * iso_time_tz (time_t *, const char *, const char **); +/** + * @brief Lockfile. + */ +typedef struct +{ + int fd; ///< File descriptor. + gchar *name; ///< Name. +} lockfile_t; + +int +lockfile_lock (lockfile_t *, const gchar *); + +int +lockfile_unlock (lockfile_t *); + #endif /* not _GVMD_UTILS_H */ From 4de729e1f82a3e86938ef083744781ab20ec1469 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Thu, 22 Feb 2018 13:13:24 +0200 Subject: [PATCH 02/19] Schedule tasks within a regular transaction, instead of an exclusive one I don't see a reason for the exclusive transaction. It should still work if someone modifies the task or schedule info. The task must be started outside the transaction anyway. --- src/manage.c | 2 +- src/manage_sql.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/manage.c b/src/manage.c index 919c2d916..8fa8edfe2 100644 --- a/src/manage.c +++ b/src/manage.c @@ -6802,7 +6802,7 @@ manage_schedule (int (*fork_connection) (gvm_connection_t *, gchar *), return ret; } - /* This iterator runs in an exclusive transaction, so this loop is atomic. */ + /* This iterator runs in a transaction. */ while (next (&schedules)) if (task_schedule_iterator_start_due (&schedules)) { diff --git a/src/manage_sql.c b/src/manage_sql.c index 7c77e599b..bb0231be8 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -48485,7 +48485,7 @@ init_task_schedule_iterator (iterator_t* iterator) get_data_t get; array_t *permissions; - ret = sql_begin_exclusive_giveup (); + ret = sql_begin_immediate_giveup (); if (ret) return ret; From 925c691937effc283c080d35077636f755041570 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Thu, 22 Feb 2018 13:44:11 +0200 Subject: [PATCH 03/19] Use a regular Postgres transaction for immediate transactions While this is not the same as SQLite (which prevents other processes from modifying the db during the transaction) it is still better than the current exclusive transaction which is not really doing anything. --- src/sql_pg.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/sql_pg.c b/src/sql_pg.c index 794d2bb17..3342d6dc7 100644 --- a/src/sql_pg.c +++ b/src/sql_pg.c @@ -639,8 +639,7 @@ sql_begin_exclusive_giveup () void sql_begin_immediate () { - /* TODO This is just an exclusive lock. */ - sql_begin_exclusive (); + sql ("BEGIN;"); } /** @@ -651,8 +650,12 @@ sql_begin_immediate () int sql_begin_immediate_giveup () { - /* TODO This is just an exclusive lock. */ - return sql_begin_exclusive_giveup (); + int ret; + + ret = sql_giveup ("BEGIN;"); + if (ret) + return ret; + return 0; } /** From 7af28f2b2fc1a0b3b1aa103332c0f2d2bcc42387 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Thu, 22 Feb 2018 21:00:40 +0200 Subject: [PATCH 04/19] Use immediate instead of exclusive for NVT update Multiple transactions are used, so other processes can already affect the database, for example between chunks of NVTs or between inserting the NVTs and the preferences. --- src/manage_sql_nvts.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/manage_sql_nvts.c b/src/manage_sql_nvts.c index af571969b..4d1c5079e 100644 --- a/src/manage_sql_nvts.c +++ b/src/manage_sql_nvts.c @@ -185,7 +185,7 @@ int chunk_count = 0; * * @return An NVT. */ -nvt_t +static nvt_t make_nvt_from_nvti (const nvti_t *nvti) { gchar *qod_str, *qod_type; @@ -199,7 +199,7 @@ make_nvt_from_nvti (const nvti_t *nvti) if (chunk_count == 0) { - sql_begin_exclusive (); + sql_begin_immediate (); chunk_count++; } else if (chunk_count == CHUNK_SIZE) @@ -1127,7 +1127,7 @@ manage_complete_nvt_cache_update (GList *nvts_list, GList *nvt_preferences_list) iterator_t configs; int count; - sql_begin_exclusive (); + sql_begin_immediate (); if (sql_is_sqlite3 ()) { sql ("DELETE FROM nvt_cves;"); @@ -1143,11 +1143,11 @@ manage_complete_nvt_cache_update (GList *nvts_list, GList *nvt_preferences_list) /* NVTs and preferences are buffered, insert them into DB. */ insert_nvts_list (nvts_list); - sql_begin_exclusive (); + sql_begin_immediate (); insert_nvt_preferences_list (nvt_preferences_list); sql_commit (); - sql_begin_exclusive (); + sql_begin_immediate (); /* Remove preferences from configs where the preference has vanished from * the associated NVT. */ From 497998ce20382d1acb212474983bb9d5193611dc Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Thu, 22 Feb 2018 21:36:57 +0200 Subject: [PATCH 05/19] Remove VACUUM lock This was originally added for SQLite. It's safe to allow access during a VACUUM in Postgres. --- src/manage_sql.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index bb0231be8..d4d41ee54 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -66702,14 +66702,7 @@ manage_optimize (GSList *log_config, const gchar *database, const gchar *name) else old_size = state.st_size; - if (sql_is_sqlite3 ()) - sql ("VACUUM;"); - else - { - sql_begin_exclusive (); - sql ("VACUUM;"); - sql_commit (); - } + sql ("VACUUM;"); ret = stat (db, &state); if (ret) From f02de6a777f1f6f46afa8e65561e4ee9ea6c9a72 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Thu, 22 Feb 2018 23:44:30 +0200 Subject: [PATCH 06/19] Convert more of --optimize to immediate These three just modify existing values. No need for exclusivity. --- src/manage_sql.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index d4d41ee54..1ed03c28b 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -66767,7 +66767,7 @@ manage_optimize (GSList *log_config, const gchar *database, const gchar *name) { int changes_iana, changes_old_format; - sql_begin_exclusive (); + sql_begin_immediate (); sql ("UPDATE results" " SET port = substr (port, 1," " strpos (port, ' (IANA:') - 1)" @@ -66789,7 +66789,7 @@ manage_optimize (GSList *log_config, const gchar *database, const gchar *name) else if (strcasecmp (name, "cleanup-result-severities") == 0) { int missing_severity_changes = 0; - sql_begin_exclusive(); + sql_begin_immediate (); if (sql_is_sqlite3 ()) sql ("UPDATE results" @@ -66823,7 +66823,7 @@ manage_optimize (GSList *log_config, const gchar *database, const gchar *name) { int changes; - sql_begin_exclusive (); + sql_begin_immediate (); changes = cleanup_schedule_times (); From 857f6c828d3a3b4e266a8a411f8e22de93cd7529 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Fri, 23 Feb 2018 11:22:24 +0200 Subject: [PATCH 07/19] Convert the rest of --optimize to immediate These rebuild options are used rarely, and there are no serious side effects of the data being accessed while it is being built, so no need for exclusivity. --- src/manage_sql.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index 1ed03c28b..3b70bb84f 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -66835,7 +66835,7 @@ manage_optimize (GSList *log_config, const gchar *database, const gchar *name) } else if (strcasecmp (name, "rebuild-permissions-cache") == 0) { - sql_begin_exclusive (); + sql_begin_immediate (); sql ("DELETE FROM permissions_get_tasks"); @@ -66850,7 +66850,7 @@ manage_optimize (GSList *log_config, const gchar *database, const gchar *name) { int changes; - sql_begin_exclusive (); + sql_begin_immediate (); reports_build_count_cache (1, &changes); @@ -66865,7 +66865,7 @@ manage_optimize (GSList *log_config, const gchar *database, const gchar *name) { int changes; - sql_begin_exclusive (); + sql_begin_immediate (); reports_build_count_cache (0, &changes); From e1abcc4faea88c9b0cb22996e2d791d9ae0fb3ae Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Fri, 23 Feb 2018 16:29:22 +0200 Subject: [PATCH 08/19] Use immediate when deleting user I don't see harm in allowing other processes to read the user data while the delete transaction is busy. There's the potential danger that another process starts a task that has been deleted already in the transaction, but this was already not working with our Postgres exclusive transaction, so it needs an alternate solution. --- src/manage_sql.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index 3b70bb84f..10148e786 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -63287,7 +63287,7 @@ delete_user (const char *user_id_arg, const char *name_arg, int ultimate, return 5; } - sql_begin_exclusive (); + sql_begin_immediate (); if (acl_user_may ("delete_user") == 0) { @@ -63334,7 +63334,7 @@ delete_user (const char *user_id_arg, const char *name_arg, int ultimate, free (uuid); } - /* Set requested and running tasks to stopped. */ + /* Fail if there are any active tasks. */ memset (&get, '\0', sizeof (get)); current_uuid = current_credentials.uuid; @@ -63368,7 +63368,8 @@ delete_user (const char *user_id_arg, const char *name_arg, int ultimate, free (current_credentials.uuid); current_credentials.uuid = current_uuid; - /* Transfer ownership of objects to the inheritor if one is given */ + /* Check if there's an inheritor. */ + if (inheritor_id && strcmp (inheritor_id, "")) { if (strcmp (inheritor_id, "self") == 0) @@ -63420,6 +63421,8 @@ delete_user (const char *user_id_arg, const char *name_arg, int ultimate, gchar *deleted_user_id, *deleted_user_name; gchar *real_inheritor_id, *real_inheritor_name; + /* Transfer ownership of objects to the inheritor. */ + if (inheritor == user) { sql_rollback (); From 77da60366df819f14526a4bb38c02f3d0b3b0ab8 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 26 Feb 2018 09:48:13 +0200 Subject: [PATCH 09/19] Remove header made static in 7af28f2 --- src/manage.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/manage.h b/src/manage.h index e16f2ff1c..101291407 100644 --- a/src/manage.h +++ b/src/manage.h @@ -1933,9 +1933,6 @@ nvts_feed_version (); void set_nvts_feed_version (const char*); -nvt_t -make_nvt_from_nvti (const nvti_t*); - gboolean find_nvt (const char*, nvt_t*); From 44d7444d5e75f936ecc826441e34ce71231f5b57 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 26 Feb 2018 14:41:24 +0200 Subject: [PATCH 10/19] Lock table for Postgres when deleting report The issue here is that exclusivity is needed to prevent other processes from resuming the report's task between the time we check that the report is not active and the time that we commit the deleted report. Locking the table, instead of trying to lock the entire database, seems like the best option at the moment. --- src/manage_sql.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index 10148e786..a1cdacc1a 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -25035,6 +25035,10 @@ delete_report_internal (report_t report) TASK_STATUS_STOP_WAITING)) return 2; + /* This needs to have exclusive access to reports because otherwise at this + * point another process (like a RESUME_TASK handler) could store the report + * ID and then start trying to access that report after we've deleted it. */ + if (report_task (report, &task)) return -1; @@ -25211,7 +25215,20 @@ delete_report (const char *report_id, int dummy) report_t report; int ret; - sql_begin_exclusive (); + if (sql_is_sqlite3 ()) + sql_begin_exclusive (); + else + { + sql_begin_immediate (); + + /* This prevents other processes (in particular a RESUME_TASK) from getting + * a reference to the report ID, and then using that reference to try access + * the deleted report. + * + * If the report is running already then delete_report_internal will + * ROLLBACK. */ + sql ("LOCK table reports IN ACCESS EXCLUSIVE MODE;"); + } if (acl_user_may ("delete_report") == 0) { From 09b75e2b0da5dfb6970c3320217b5be5a8141c33 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 26 Feb 2018 18:07:43 +0200 Subject: [PATCH 11/19] Lock table for Postgres when deleting reports Similarly to 44d7444, lock reports table when auto deleting all reports. --- src/manage_sql.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index a1cdacc1a..1264172d3 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -19146,8 +19146,23 @@ auto_delete_reports () g_debug ("%s", __FUNCTION__); - if (sql_begin_exclusive_giveup ()) - return; + if (sql_is_sqlite3 ()) + { + if (sql_begin_exclusive_giveup ()) + return; + } + else + { + sql_begin_immediate (); + + /* As in delete_report, this prevents other processes from getting the + * report ID. */ + if (sql_error ("LOCK table reports IN ACCESS EXCLUSIVE MODE NOWAIT;")) + { + sql ("ROLLBACK;"); + return; + } + } init_iterator (&tasks, "SELECT id, name," From 6e3689d05b4ad55105e362926ee437b1310c1e6f Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 26 Feb 2018 18:10:56 +0200 Subject: [PATCH 12/19] Remove advisory locks, which were not effective --- src/sql_pg.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/sql_pg.c b/src/sql_pg.c index 3342d6dc7..c96049c22 100644 --- a/src/sql_pg.c +++ b/src/sql_pg.c @@ -611,7 +611,6 @@ void sql_begin_exclusive () { sql ("BEGIN;"); - sql ("SELECT pg_advisory_xact_lock (1);"); } /** @@ -627,10 +626,7 @@ sql_begin_exclusive_giveup () ret = sql_giveup ("BEGIN;"); if (ret) return ret; - if (sql_int ("SELECT pg_try_advisory_xact_lock (1);")) - return 0; - sql_rollback (); - return 1; + return 0; } /** From 1b57e8349c2fbd69b5ded705f856e494172c9b7b Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Wed, 28 Feb 2018 10:45:43 +0200 Subject: [PATCH 13/19] Lock table for Postgres when deleting tasks Like 44d7444, lock reports table when deleting tasks. --- src/manage_sql.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index 1264172d3..4a30c0df8 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -12850,6 +12850,8 @@ manage_test_alert (const char *alert_id, gchar **script_message) (void*) TASK_STATUS_DONE, script_message); exit: + /* No one should be running this task, so we don't worry about the lock. We + * could guarantee that no one runs the task, but this is a very rare case. */ delete_task (task, 1); free (task_id); free (report_id); @@ -32018,6 +32020,8 @@ find_trash_task (const char*, task_t*); * * Stop the task beforehand with \ref stop_task_internal, if it is running. * + * This is only used for DELETE_TASK in gmp.c. + * * @param[in] task_id UUID of task. * @param[in] ultimate Whether to remove entirely, or to trashcan. * @@ -32109,6 +32113,16 @@ request_delete_task_uuid (const char *task_id, int ultimate) case 0: /* Stopped. */ { int ret; + + if (ultimate) + /* This prevents other processes (for example a START_TASK) from + * getting a reference to a report ID or the task ID, and then using + * that reference to try access the deleted report or task. + * + * If the task is running already then delete_task will lead to + * ROLLBACK. */ + sql ("LOCK table reports IN ACCESS EXCLUSIVE MODE;"); + ret = delete_task (task, ultimate); if (ret) sql_rollback (); @@ -32241,7 +32255,24 @@ delete_task_lock (task_t task, int ultimate) g_debug (" delete task %llu\n", task); - sql_begin_exclusive (); + if (sql_is_sqlite3 ()) + sql_begin_exclusive (); + else + { + sql_begin_immediate (); + + /* This prevents other processes (for example a START_TASK) from getting + * a reference to a report ID or the task ID, and then using that + * reference to try access the deleted report or task. + * + * If the task is already active then delete_report (via delete_task) + * will fail and rollback. */ + if (sql_error ("LOCK table reports IN ACCESS EXCLUSIVE MODE;")) + { + sql_rollback (); + return -1; + } + } if (sql_int ("SELECT hidden FROM tasks WHERE id = %llu;", task)) { From 37d15de7e687b01157d7aac9f5b6a7c6344d5737 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Thu, 1 Mar 2018 14:31:48 +0200 Subject: [PATCH 14/19] Lock table for Postgres when requesting task Lock tasks table, to prevent other processes from starting the task concurrently. Move the report and event work out of the locked transaction, to prevent deadlock and because there's a lot of work done there. --- src/manage_sql.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index 4a30c0df8..55032521c 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -18090,6 +18090,8 @@ set_task_run_status (task_t task, task_status_t status) /** * @brief Atomically set the run state of a task to requested. * + * Only used by run_slave_or_gmp_task and run_otp_task. + * * @param[in] task Task. * @param[out] status Old run status of task. * @@ -18099,8 +18101,22 @@ int set_task_requested (task_t task, task_status_t *status) { task_status_t run_status; + char *uuid, *name; + int update_report; - sql_begin_exclusive (); + /* Locking here prevents another process from starting the task + * concurrently. */ + if (sql_is_sqlite3 ()) + sql_begin_exclusive (); + else + { + sql_begin_immediate (); + if (sql_error ("LOCK table tasks IN ACCESS EXCLUSIVE MODE;")) + { + sql ("ROLLBACK;"); + return 1; + } + } run_status = task_run_status (task); if (run_status == TASK_STATUS_REQUESTED @@ -18118,10 +18134,49 @@ set_task_requested (task_t task, task_status_t *status) return 1; } - set_task_run_status (task, TASK_STATUS_REQUESTED); + /* This does the work of set_task_run_status, but spread across the COMMIT. */ + + update_report = 0; + if ((task == current_scanner_task) && current_report) + update_report = 1; + + sql ("UPDATE tasks SET run_status = %u WHERE id = %llu;", + TASK_STATUS_REQUESTED, + task); + + task_uuid (task, &uuid); + name = task_name (task); + g_log ("event task", G_LOG_LEVEL_MESSAGE, + "Status of task %s (%s) has changed to %s", + name, uuid, run_status_name (TASK_STATUS_REQUESTED)); + free (uuid); + free (name); sql_commit (); + /* Do these outside the transaction, because they modify reports, and + * other places LOCK reports, so there would be a danger of deadlock + * between the LOCKs on tasks and reports. + * + * Updating the report status outside the locked environment should be + * OK, because it's a very rare case that someone stops the task when + * it is in requested state. (Because current_report is set we are the + * only ones who can progress it to the running states.) */ + + if (update_report) + { + sql ("UPDATE reports SET scan_run_status = %u WHERE id = %llu;", + TASK_STATUS_REQUESTED, + current_report); + + if (current_report && setting_auto_cache_rebuild_int ()) + report_cache_counts (current_report, 0, 0, NULL); + } + + event (task, + (task == current_scanner_task) ? current_report : 0, + EVENT_TASK_RUN_STATUS_CHANGED, (void*) TASK_STATUS_REQUESTED); + *status = run_status; return 0; } From d76977d0c38807d5d00e20ffc61f0084e4aea2b2 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Thu, 1 Mar 2018 15:58:41 +0200 Subject: [PATCH 15/19] Remove transaction option, no longer used --- src/manage_sql.c | 38 +++++++------------------------------- 1 file changed, 7 insertions(+), 31 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index 55032521c..ab8b54ac6 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -203,7 +203,7 @@ static void report_severity_data (report_t, const char *, const get_data_t *, static gchar* apply_report_format (gchar *, gchar *, gchar *, gchar *, GList **); -static int cache_report_counts (report_t, int, int, severity_data_t*, int); +static int cache_report_counts (report_t, int, int, severity_data_t*); static char* task_owner_uuid (task_t); @@ -24677,32 +24677,23 @@ report_counts_from_cache (report_t report, int override, int min_qod, * @param[in] override Whether overrides were applied to the results. * @param[in] min_qod The minimum QoD of the results. * @param[in] data Severity data struct containing the message counts. - * @param[in] make_transaction True to wrap in an exclusive transaction. * * @return 0 if successful, 1 gave up, -1 error (see sql_giveup). */ static int cache_report_counts (report_t report, int override, int min_qod, - severity_data_t* data, int make_transaction) + severity_data_t* data) { - /* Try cache results. Give up if the database is locked because this could - * happen while the caller has an SQL statement open. If another process - * tries to write to the database between the statement open and - * cache_report_counts then they'll deadlock. */ int i, ret; double severity; int end_time; - // Do not cache results when using dynamic severity. + /* Do not cache results when using dynamic severity. */ + if (setting_dynamic_severity_int ()) return 0; - if (make_transaction) - { - ret = sql_begin_exclusive_giveup (); - if (ret) - return ret; - } + /* Try cache results. */ ret = sql_giveup ("DELETE FROM report_counts" " WHERE report = %llu" @@ -24713,8 +24704,6 @@ cache_report_counts (report_t report, int override, int min_qod, report, override, min_qod, current_credentials.uuid); if (ret) { - if (make_transaction) - sql_rollback (); return ret; } @@ -24732,8 +24721,6 @@ cache_report_counts (report_t report, int override, int min_qod, report, current_credentials.uuid, override, min_qod); if (ret) { - if (make_transaction) - sql_rollback (); return ret; } } @@ -24769,8 +24756,6 @@ cache_report_counts (report_t report, int override, int min_qod, min_qod, severity, data->counts[i], end_time); if (ret) { - if (make_transaction) - sql_rollback (); return ret; } } @@ -24778,15 +24763,6 @@ cache_report_counts (report_t report, int override, int min_qod, severity = severity_data_value (i); } } - if (make_transaction) - { - ret = sql_giveup ("COMMIT;"); - if (ret) - { - sql_rollback (); - return ret; - } - } return 0; } @@ -24962,10 +24938,10 @@ report_counts_id_full (report_t report, int* debugs, int* holes, int* infos, if (filter_cacheable && !cache_exists) { if (unfiltered_requested) - cache_report_counts (report, override, 0, &severity_data, 0); + cache_report_counts (report, override, 0, &severity_data); if (filtered_requested) cache_report_counts (report, override, min_qod_int, - &filtered_severity_data, 0); + &filtered_severity_data); } cleanup_severity_data (&severity_data); From 67b06350afc1119e809fe40a64999267ebbe7788 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Fri, 2 Mar 2018 11:41:23 +0200 Subject: [PATCH 16/19] Use immediate, not exclusive, to modify config Because a config may only be modified when not in use, this does not affect tasks. With Postgres at worst a second concurrent modification will have to wait for the first to commit. Two processes modifying the same config is pretty rare anyway. --- src/manage_sql.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index ab8b54ac6..bf76bf85b 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -37158,7 +37158,7 @@ manage_set_config_nvts (const gchar *config_id, const char* family, gchar *quoted_family, *quoted_selector; int new_nvt_count = 0, old_nvt_count; - sql_begin_exclusive (); + sql_begin_immediate (); if (find_config_with_permission (config_id, &config, "modify_config")) { @@ -39214,7 +39214,7 @@ manage_set_config_families (const gchar *config_id, int constraining; char *selector; - sql_begin_exclusive (); + sql_begin_immediate (); if (find_config_with_permission (config_id, &config, "modify_config")) { From 56fc2f64d98ec4a8c055ffb2cc6220892420b57f Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Fri, 2 Mar 2018 16:13:47 +0200 Subject: [PATCH 17/19] Use lockfiles to control access at startup Use a lockfile for each type of process instead of using exclusive locks, to prevent certain types of processes from running simultaneously. The three types of processes are the main process, option (helper) processes and the --migrate process. Use a fourth lock, gvm-checking, to prevent races when checking the three lock types, and also to allow the main process exclusive access when checking the db initially. --- src/gvmd.c | 289 ++++++++++++++++++++++++++++++++++++++--------- src/manage_sql.c | 9 +- src/utils.c | 92 +++++++++++++-- src/utils.h | 9 ++ 4 files changed, 338 insertions(+), 61 deletions(-) diff --git a/src/gvmd.c b/src/gvmd.c index ba77ca3b9..d4868213e 100644 --- a/src/gvmd.c +++ b/src/gvmd.c @@ -323,6 +323,33 @@ set_gnutls_priority (gnutls_session_t *session, const char *priority) g_warning ("Invalid GnuTLS priority: %s\n", errp); } +/** + * @brief Lock gvm-helping for an option. + * + * @param[in] lockfile_checking The gvm-checking lockfile. + * + * @return 0 success, -1 failed. + */ +static int +option_lock (lockfile_t *lockfile_checking) +{ + lockfile_t lockfile_helping; + + if (lockfile_lock_shared_nb (&lockfile_helping, "gvm-helping")) + { + g_critical ("%s: Error getting helping lock\n", __FUNCTION__); + return -1; + } + + if (lockfile_unlock (lockfile_checking)) + { + g_critical ("%s: Error releasing checking lock\n", __FUNCTION__); + return -1; + } + + return 0; +} + /* Forking, serving the client. */ @@ -1070,7 +1097,7 @@ handle_sigabrt_simple (int signal) * @return If this function did not exit itself, returns exit code. */ static int -update_nvt_cache (int register_cleanup, int skip_create_tables) +update_nvt_cache (int register_cleanup) { int ret; gvm_connection_t connection; @@ -1087,7 +1114,7 @@ update_nvt_cache (int register_cleanup, int skip_create_tables) 0, /* Max email include size. */ 0, /* Max email message size. */ NULL, - skip_create_tables)) + 1 /* Skip DB check (including table creation). */)) { case 0: break; @@ -1176,7 +1203,7 @@ update_nvt_cache_retry () else if (child_pid == 0) { /* Child: Try reload. */ - int ret = update_nvt_cache (0, 1); + int ret = update_nvt_cache (0); exit (ret); } @@ -1690,6 +1717,7 @@ main (int argc, char** argv) static gchar *disable = NULL; static gchar *value = NULL; GError *error = NULL; + lockfile_t lockfile_checking, lockfile_serving; GOptionContext *option_context; static GOptionEntry option_entries[] = { @@ -1907,6 +1935,141 @@ main (int argc, char** argv) manage_db_supported_version ()); #endif + /* Get exclusivity on the startup locks. + * + * The main process keeps this open until after init_gmpd, so that check_db + * has exclusive access to the db. + * + * Helper and migrator processes just keep this open long enough to check the + * other startup locks. + * + * There are 3 startup locks: + * 1 gvm-serving: the main process (exclusive) + * 2 gvm-helping: an option process, like --create-user (shared) + * 3 gvm-migrating: a --migrate process (exclusive). + * + * The locks are inherited by forked processes, and are only released when all + * associated files are closed (i.e. when all processes exit). */ + + + switch (lockfile_lock_nb (&lockfile_checking, "gvm-checking")) + { + case 0: + break; + case 1: + g_warning ("%s: Another process is busy starting up", __FUNCTION__); + return EXIT_FAILURE; + case -1: + default: + g_critical ("%s: Error trying to get checking lock\n", __FUNCTION__); + return EXIT_FAILURE; + } + + if (migrate_database) + { + lockfile_t lockfile_migrating; + + /* Migrate the database to the version supported by this manager. */ + + switch (lockfile_locked ("gvm-serving")) + { + case 1: + g_warning ("%s: Main process is running, refusing to migrate", + __FUNCTION__); + return EXIT_FAILURE; + case -1: + g_warning ("%s: Error checking serving lock", + __FUNCTION__); + return EXIT_FAILURE; + } + + switch (lockfile_locked ("gvm-helping")) + { + case 1: + g_warning ("%s: An option process is running, refusing to migrate", + __FUNCTION__); + return EXIT_FAILURE; + case -1: + g_warning ("%s: Error checking helping lock", + __FUNCTION__); + return EXIT_FAILURE; + } + + switch (lockfile_lock_nb (&lockfile_migrating, "gvm-migrating")) + { + case 1: + g_warning ("%s: A migrate is already running", __FUNCTION__); + return EXIT_FAILURE; + case -1: + g_critical ("%s: Error getting migrating lock\n", __FUNCTION__); + return EXIT_FAILURE; + } + + if (lockfile_unlock (&lockfile_checking)) + { + g_critical ("%s: Error releasing checking lock\n", __FUNCTION__); + return EXIT_FAILURE; + } + + proctitle_set ("gvmd: Migrating database"); + + g_info (" Migrating database.\n"); + + switch (manage_migrate (log_config, database)) + { + case 0: + g_info (" Migration succeeded.\n"); + return EXIT_SUCCESS; + case 1: + g_warning ("%s: databases are already at the supported version\n", + __FUNCTION__); + return EXIT_SUCCESS; + case 2: + g_warning ("%s: database migration too hard\n", + __FUNCTION__); + return EXIT_FAILURE; + case 11: + g_warning ("%s: cannot migrate SCAP database\n", + __FUNCTION__); + return EXIT_FAILURE; + case 12: + g_warning ("%s: cannot migrate CERT database\n", + __FUNCTION__); + return EXIT_FAILURE; + case -1: + g_critical ("%s: database migration failed\n", + __FUNCTION__); + return EXIT_FAILURE; + case -11: + g_critical ("%s: SCAP database migration failed\n", + __FUNCTION__); + return EXIT_FAILURE; + case -12: + g_critical ("%s: CERT database migration failed\n", + __FUNCTION__); + return EXIT_FAILURE; + default: + assert (0); + g_critical ("%s: strange return from manage_migrate\n", + __FUNCTION__); + return EXIT_FAILURE; + } + } + + /* For the main process and for option processes, refuse to start when a + * migrate is in process. */ + + if (lockfile_locked ("gvm-migrating")) + { + g_warning ("%s: A migrate is in progress", __FUNCTION__); + return EXIT_FAILURE; + } + + /* Handle non-migrate options. + * + * These can run concurrently, so they set the shared lock gvm-helping, and + * release gvm-checking, via option_lock. */ + if (backup_database) { /* Backup the database and then exit. */ @@ -1915,6 +2078,8 @@ main (int argc, char** argv) g_info (" Backing up database.\n"); + /* TODO Not sure about locking. Not used by Postgres. Perhaps remove. */ + switch (manage_backup_db (database)) { case 0: @@ -1953,6 +2118,9 @@ main (int argc, char** argv) proctitle_set ("gvmd: Optimizing"); + if (option_lock (&lockfile_checking)) + return EXIT_FAILURE; + ret = manage_optimize (log_config, database, optimize); log_config_free (); if (ret) @@ -1970,6 +2138,9 @@ main (int argc, char** argv) proctitle_set ("gvmd: Creating scanner"); + if (option_lock (&lockfile_checking)) + return EXIT_FAILURE; + if (!scanner_host) scanner_host = OPENVASSD_ADDRESS; if (!scanner_port) @@ -2011,6 +2182,9 @@ main (int argc, char** argv) proctitle_set ("gvmd: Modifying scanner"); + if (option_lock (&lockfile_checking)) + return EXIT_FAILURE; + if (scanner_type) { scanner_type_t type; @@ -2047,6 +2221,9 @@ main (int argc, char** argv) proctitle_set ("gvmd: Checking alerts"); + if (option_lock (&lockfile_checking)) + return EXIT_FAILURE; + ret = manage_check_alerts (log_config, database); log_config_free (); if (ret) @@ -2060,6 +2237,9 @@ main (int argc, char** argv) proctitle_set ("gvmd: Creating user"); + if (option_lock (&lockfile_checking)) + return EXIT_FAILURE; + ret = manage_create_user (log_config, database, create_user, password, role); log_config_free (); if (ret) @@ -2073,6 +2253,9 @@ main (int argc, char** argv) proctitle_set ("gvmd: Deleting user"); + if (option_lock (&lockfile_checking)) + return EXIT_FAILURE; + ret = manage_delete_user (log_config, database, delete_user, inheritor); log_config_free (); if (ret) @@ -2086,6 +2269,9 @@ main (int argc, char** argv) proctitle_set ("gvmd: Getting users"); + if (option_lock (&lockfile_checking)) + return EXIT_FAILURE; + ret = manage_get_users (log_config, database, role); log_config_free (); if (ret) @@ -2099,6 +2285,9 @@ main (int argc, char** argv) proctitle_set ("gvmd: Getting scanners"); + if (option_lock (&lockfile_checking)) + return EXIT_FAILURE; + ret = manage_get_scanners (log_config, database); log_config_free (); if (ret) @@ -2112,6 +2301,9 @@ main (int argc, char** argv) proctitle_set ("gvmd: Deleting scanner"); + if (option_lock (&lockfile_checking)) + return EXIT_FAILURE; + ret = manage_delete_scanner (log_config, database, delete_scanner); log_config_free (); if (ret) @@ -2125,6 +2317,9 @@ main (int argc, char** argv) proctitle_set ("gvmd: Verifying scanner"); + if (option_lock (&lockfile_checking)) + return EXIT_FAILURE; + ret = manage_verify_scanner (log_config, database, verify_scanner); log_config_free (); if (ret) @@ -2138,6 +2333,9 @@ main (int argc, char** argv) proctitle_set ("gvmd: Modifying user password"); + if (option_lock (&lockfile_checking)) + return EXIT_FAILURE; + ret = manage_set_password (log_config, database, user, new_password); log_config_free (); if (ret) @@ -2151,6 +2349,9 @@ main (int argc, char** argv) proctitle_set ("gvmd: Modifying setting"); + if (option_lock (&lockfile_checking)) + return EXIT_FAILURE; + ret = manage_modify_setting (log_config, database, user, modify_setting, value); log_config_free (); @@ -2159,61 +2360,15 @@ main (int argc, char** argv) return EXIT_SUCCESS; } - if (migrate_database) - { - /* Migrate the database to the version supported by this manager. */ - - proctitle_set ("gvmd: Migrating database"); - - g_info (" Migrating database.\n"); - - switch (manage_migrate (log_config, database)) - { - case 0: - g_info (" Migration succeeded.\n"); - return EXIT_SUCCESS; - case 1: - g_warning ("%s: databases are already at the supported version\n", - __FUNCTION__); - return EXIT_SUCCESS; - case 2: - g_warning ("%s: database migration too hard\n", - __FUNCTION__); - return EXIT_FAILURE; - case 11: - g_warning ("%s: cannot migrate SCAP database\n", - __FUNCTION__); - return EXIT_FAILURE; - case 12: - g_warning ("%s: cannot migrate CERT database\n", - __FUNCTION__); - return EXIT_FAILURE; - case -1: - g_critical ("%s: database migration failed\n", - __FUNCTION__); - return EXIT_FAILURE; - case -11: - g_critical ("%s: SCAP database migration failed\n", - __FUNCTION__); - return EXIT_FAILURE; - case -12: - g_critical ("%s: CERT database migration failed\n", - __FUNCTION__); - return EXIT_FAILURE; - default: - assert (0); - g_critical ("%s: strange return from manage_migrate\n", - __FUNCTION__); - return EXIT_FAILURE; - } - } - if (encrypt_all_credentials) { int ret; proctitle_set ("gvmd: Encrypting all credentials"); + if (option_lock (&lockfile_checking)) + return EXIT_FAILURE; + ret = manage_encrypt_all_credentials (log_config, database); log_config_free (); if (ret) @@ -2227,6 +2382,9 @@ main (int argc, char** argv) proctitle_set ("gvmd: Decrypting all credentials"); + if (option_lock (&lockfile_checking)) + return EXIT_FAILURE; + ret = manage_decrypt_all_credentials (log_config, database); log_config_free (); if (ret) @@ -2236,6 +2394,25 @@ main (int argc, char** argv) /* Run the standard manager. */ + if (lockfile_locked ("gvm-helping")) + { + g_warning ("%s: An option process is running", __FUNCTION__); + return EXIT_FAILURE; + } + + switch (lockfile_lock_nb (&lockfile_serving, "gvm-serving")) + { + case 0: + break; + case 1: + g_warning ("%s: Main process is already running", __FUNCTION__); + return EXIT_FAILURE; + case -1: + default: + g_critical ("%s: Error trying to get serving lock\n", __FUNCTION__); + return EXIT_FAILURE; + } + if (foreground == FALSE) { /* Fork into the background. */ @@ -2291,6 +2468,14 @@ main (int argc, char** argv) exit (EXIT_FAILURE); } + /* Release the checking lock, so that option processes may start. */ + + if (lockfile_unlock (&lockfile_checking)) + { + g_critical ("%s: Error releasing checking lock\n", __FUNCTION__); + return EXIT_FAILURE; + } + /* Register the `cleanup' function. */ if (atexit (&cleanup)) diff --git a/src/manage_sql.c b/src/manage_sql.c index bf76bf85b..3aaacce53 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -16434,6 +16434,8 @@ clean_auth_cache () /** * @brief Ensure that the database is in order. * + * Only called by init_manage_internal, and ultimately only by the main process. + * * @param[in] check_encryption_key Whether to check encryption key. * * @return 0 success, -1 error. @@ -16441,7 +16443,10 @@ clean_auth_cache () static int check_db (int check_encryption_key) { - sql_begin_exclusive (); + /* The file locks managed at startup ensure that this is the only Manager + * process accessing the db. Nothing else should be accessing the db, access + * should always go through Manager. */ + sql_begin_immediate (); create_tables (); check_db_sequences (); set_db_version (GVMD_DATABASE_VERSION); @@ -16763,6 +16768,8 @@ init_manage_internal (GSList *log_config, if (skip_db_check == 0) { + /* This only happens for init_manage callers with skip_db_check set, so + * there is ultimately only one caller case, the main process. */ ret = check_db (check_encryption_key); if (ret) return ret; diff --git a/src/utils.c b/src/utils.c index ca218cc65..e31e01844 100644 --- a/src/utils.c +++ b/src/utils.c @@ -358,24 +358,26 @@ iso_time_tz (time_t *epoch_time, const char *timezone, const char **abbrev) /* Locks. */ /** - * @brief Lock a file exclusively. + * @brief Lock a file. * * Block until file is locked. * * @param[in] lockfile Lockfile. * @param[in] lockfile_basename Basename of lock file. + * @param[in] operation LOCK_EX (exclusive) or LOCK_SH (shared). * - * @return 0 sync complete, 1 already locked, -1 error + * @return 0 success, 1 already locked, -1 error */ -int -lockfile_lock (lockfile_t *lockfile, const gchar *lockfile_basename) +static int +lock_internal (lockfile_t *lockfile, const gchar *lockfile_basename, + int operation) { int fd; gchar *lockfile_name; /* Open the lock file. */ - lockfile_name = g_build_filename (g_get_tmp_dir (), lockfile_basename, NULL); + lockfile_name = g_build_filename (GVM_RUN_DIR, lockfile_basename, NULL); fd = open (lockfile_name, O_RDWR | O_CREAT | O_APPEND, /* "-rw-r--r--" */ @@ -393,13 +395,13 @@ lockfile_lock (lockfile_t *lockfile, const gchar *lockfile_basename) /* Lock the lockfile. */ - if (flock (fd, LOCK_EX)) /* Exclusive, blocking. */ + if (flock (fd, operation)) /* Exclusive, blocking. */ { lockfile->name = NULL; g_free (lockfile_name); if (errno == EWOULDBLOCK) return 1; - g_debug ("%s: flock: %s", __FUNCTION__, strerror (errno)); + g_warning ("%s: flock: %s", __FUNCTION__, strerror (errno)); g_free (lockfile_name); return -1; } @@ -409,6 +411,55 @@ lockfile_lock (lockfile_t *lockfile, const gchar *lockfile_basename) return 0; } +/** + * @brief Lock a file exclusively. + * + * Block until file is locked. + * + * @param[in] lockfile Lockfile. + * @param[in] lockfile_basename Basename of lock file. + * + * @return 0 success, 1 already locked, -1 error + */ +int +lockfile_lock (lockfile_t *lockfile, const gchar *lockfile_basename) +{ + g_debug ("%s: lock '%s'", __FUNCTION__, lockfile_basename); + return lock_internal (lockfile, lockfile_basename, LOCK_EX); +} + +/** + * @brief Lock a file exclusively, without blocking. + * + * @param[in] lockfile Lockfile. + * @param[in] lockfile_basename Basename of lock file. + * + * @return 0 success, 1 already locked, -1 error + */ +int +lockfile_lock_nb (lockfile_t *lockfile, const gchar *lockfile_basename) +{ + g_debug ("%s: lock '%s'", __FUNCTION__, lockfile_basename); + return lock_internal (lockfile, lockfile_basename, LOCK_EX | LOCK_NB); +} + +/** + * @brief Lock a file with a shared lock. + * + * Block until file is locked. + * + * @param[in] lockfile Lockfile. + * @param[in] lockfile_basename Basename of lock file. + * + * @return 0 success, 1 already locked, -1 error + */ +int +lockfile_lock_shared_nb (lockfile_t *lockfile, const gchar *lockfile_basename) +{ + g_debug ("%s: lock '%s'", __FUNCTION__, lockfile_basename); + return lock_internal (lockfile, lockfile_basename, LOCK_SH | LOCK_NB); +} + /** * @brief Unlock a file. * @@ -424,6 +475,8 @@ lockfile_unlock (lockfile_t *lockfile) assert (lockfile->fd); + g_debug ("%s: unlock '%s'", __FUNCTION__, lockfile->name); + /* Close the lock file. */ if (close (lockfile->fd)) @@ -438,9 +491,11 @@ lockfile_unlock (lockfile_t *lockfile) if (unlink (lockfile->name)) { + g_warning ("Failed to remove lock file %s: %s", + lockfile->name, + strerror (errno)); g_free (lockfile->name); lockfile->name = NULL; - g_warning ("Failed to remove lock file: %s", strerror (errno)); return -1; } @@ -449,3 +504,24 @@ lockfile_unlock (lockfile_t *lockfile) return 0; } + +/** + * @brief Check if a file is locked. + * + * @param[in] lockfile_basename Basename of lock file. + * + * @return 0 free, 1 locked, -1 error + */ +int +lockfile_locked (const gchar *lockfile_basename) +{ + int ret; + lockfile_t lockfile; + + g_debug ("%s: check '%s'", __FUNCTION__, lockfile_basename); + + ret = lockfile_lock_nb (&lockfile, lockfile_basename); + if ((ret == 0) && lockfile_unlock (&lockfile)) + return -1; + return ret; +} diff --git a/src/utils.h b/src/utils.h index 57737ce81..cd188fa42 100644 --- a/src/utils.h +++ b/src/utils.h @@ -68,7 +68,16 @@ typedef struct int lockfile_lock (lockfile_t *, const gchar *); +int +lockfile_lock_nb (lockfile_t *, const gchar *); + +int +lockfile_lock_shared_nb (lockfile_t *, const gchar *); + int lockfile_unlock (lockfile_t *); +int +lockfile_locked (const gchar *); + #endif /* not _GVMD_UTILS_H */ From d4a53cdb6648f2bc0d1540a781c05eef2fc2614e Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Fri, 2 Mar 2018 16:33:56 +0200 Subject: [PATCH 18/19] Use immediate instead of exclusive locks Exclusivity is now provided via file locks in process startup. --- src/manage_migrators.c | 380 ++++++++++++++++++++--------------------- 1 file changed, 190 insertions(+), 190 deletions(-) diff --git a/src/manage_migrators.c b/src/manage_migrators.c index d57102155..3fcfaf4c9 100644 --- a/src/manage_migrators.c +++ b/src/manage_migrators.c @@ -258,7 +258,7 @@ create_tables_version_4 () int migrate_0_to_1 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 0. */ @@ -314,7 +314,7 @@ migrate_1_to_2 () { iterator_t nvts; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 1. */ @@ -362,7 +362,7 @@ migrate_1_to_2 () int migrate_2_to_3 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 2. */ @@ -416,7 +416,7 @@ migrate_3_to_4 () { iterator_t nvts; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 3. */ @@ -916,7 +916,7 @@ migrate_4_to_5_copy_data () int migrate_4_to_5 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 4. */ @@ -1044,7 +1044,7 @@ migrate_5_to_6_move_other_config (const char *predefined_config_name, int migrate_5_to_6 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 5. */ @@ -1116,7 +1116,7 @@ migrate_5_to_6 () int migrate_6_to_7 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 6. */ @@ -1149,7 +1149,7 @@ migrate_6_to_7 () int migrate_7_to_8 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 7. */ @@ -1183,7 +1183,7 @@ migrate_7_to_8 () int migrate_8_to_9 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 8. */ @@ -1290,7 +1290,7 @@ migrate_9_to_10 () { iterator_t rows; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 9. */ @@ -1361,7 +1361,7 @@ migrate_9_to_10 () int migrate_10_to_11 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 10. */ @@ -1416,7 +1416,7 @@ migrate_10_to_11 () int migrate_11_to_12 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 11. */ @@ -1499,7 +1499,7 @@ migrate_12_to_13 () { iterator_t rows; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 12. */ @@ -1578,7 +1578,7 @@ migrate_12_to_13 () int migrate_13_to_14 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 13. */ @@ -1614,7 +1614,7 @@ migrate_13_to_14 () int migrate_14_to_15 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 14. */ @@ -1651,7 +1651,7 @@ migrate_14_to_15 () int migrate_15_to_16 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 15. */ @@ -1698,7 +1698,7 @@ migrate_16_to_17 () { iterator_t rows; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 16. */ @@ -1780,7 +1780,7 @@ migrate_17_to_18_set_pref (config_t config) int migrate_17_to_18 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 17. */ @@ -1836,7 +1836,7 @@ migrate_17_to_18 () int migrate_18_to_19 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 18. */ @@ -1977,7 +1977,7 @@ migrate_19_to_20 () { iterator_t rows; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 19. */ @@ -2082,7 +2082,7 @@ migrate_19_to_20 () int migrate_20_to_21 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 20. */ @@ -2119,7 +2119,7 @@ migrate_21_to_22 () { iterator_t rows; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 21. */ @@ -2352,7 +2352,7 @@ migrate_21_to_22 () int migrate_22_to_23 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 22. */ @@ -2391,7 +2391,7 @@ migrate_22_to_23 () int migrate_23_to_24 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 23. */ @@ -2428,7 +2428,7 @@ migrate_24_to_25 () { iterator_t rows; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 24. */ @@ -2556,7 +2556,7 @@ migrate_24_to_25 () int migrate_25_to_26 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 25. */ @@ -2590,7 +2590,7 @@ migrate_25_to_26 () int migrate_26_to_27 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 26. */ @@ -2628,7 +2628,7 @@ migrate_26_to_27 () int migrate_27_to_28 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 27. */ @@ -2662,7 +2662,7 @@ migrate_27_to_28 () int migrate_28_to_29 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 28. */ @@ -2696,7 +2696,7 @@ migrate_28_to_29 () int migrate_29_to_30 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 29. */ @@ -2730,7 +2730,7 @@ migrate_29_to_30 () int migrate_30_to_31 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 30. */ @@ -2764,7 +2764,7 @@ migrate_30_to_31 () int migrate_31_to_32 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 31. */ @@ -2803,7 +2803,7 @@ migrate_31_to_32 () int migrate_32_to_33 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 32. */ @@ -2867,7 +2867,7 @@ migrate_33_to_34_set_pref (config_t config) int migrate_33_to_34 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 33. */ @@ -2905,7 +2905,7 @@ migrate_33_to_34 () int migrate_34_to_35 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 34. */ @@ -2969,7 +2969,7 @@ migrate_35_to_36 () iterator_t tasks; char *scanner_range, *quoted_scanner_range; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 35. */ @@ -3083,7 +3083,7 @@ migrate_35_to_36 () int migrate_36_to_37 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 36. */ @@ -3123,7 +3123,7 @@ migrate_37_to_38 () { gchar *old_dir, *new_dir; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 37. */ @@ -3235,7 +3235,7 @@ migrate_37_to_38 () int migrate_38_to_39 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 38. */ @@ -3302,7 +3302,7 @@ migrate_39_to_40_set_pref (config_t config) int migrate_39_to_40 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 39. */ @@ -3341,7 +3341,7 @@ migrate_39_to_40 () int migrate_40_to_41 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 40. */ @@ -3393,7 +3393,7 @@ migrate_40_to_41 () int migrate_41_to_42 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Require that the database is currently version 41. */ @@ -3446,7 +3446,7 @@ migrate_41_to_42 () int migrate_42_to_43 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Require that the database is currently version 42. */ @@ -3493,7 +3493,7 @@ migrate_42_to_43 () int migrate_43_to_44 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Require that the database is currently version 43. */ @@ -3536,7 +3536,7 @@ migrate_43_to_44 () int migrate_44_to_45 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Require that the database is currently version 44. */ @@ -3570,7 +3570,7 @@ migrate_44_to_45 () int migrate_45_to_46 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Require that the database is currently version 45. */ @@ -3604,7 +3604,7 @@ migrate_45_to_46 () int migrate_46_to_47 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Require that the database is currently version 46. */ @@ -3646,7 +3646,7 @@ migrate_46_to_47 () int migrate_47_to_48 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Require that the database is currently version 47. */ @@ -3684,7 +3684,7 @@ migrate_47_to_48 () int migrate_48_to_49 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Require that the database is currently version 48. */ @@ -3728,7 +3728,7 @@ migrate_48_to_49 () int migrate_49_to_50 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 49. */ @@ -3780,7 +3780,7 @@ migrate_49_to_50 () int migrate_50_to_51 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 50. */ @@ -3814,7 +3814,7 @@ migrate_50_to_51 () int migrate_51_to_52 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 51. */ @@ -3861,7 +3861,7 @@ migrate_51_to_52 () int migrate_52_to_53 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 52. */ @@ -3901,7 +3901,7 @@ migrate_52_to_53 () int migrate_53_to_54 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 53. */ @@ -3976,7 +3976,7 @@ migrate_54_to_55_format (const char *old_uuid, const char *new_uuid) int migrate_54_to_55 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 54. */ @@ -4737,7 +4737,7 @@ migrate_55_to_56 () { iterator_t rows; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 55. */ @@ -5018,7 +5018,7 @@ migrate_55_to_56 () int migrate_56_to_57 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 56. */ @@ -5137,7 +5137,7 @@ migrate_56_to_57 () int migrate_57_to_58 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 57. */ @@ -5194,7 +5194,7 @@ migrate_57_to_58 () int migrate_58_to_59 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 58. */ @@ -5260,7 +5260,7 @@ migrate_58_to_59 () int migrate_59_to_60 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 59. */ @@ -5296,7 +5296,7 @@ migrate_59_to_60 () int migrate_60_to_61 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 60. */ @@ -5338,7 +5338,7 @@ migrate_60_to_61 () int migrate_61_to_62 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 61. */ @@ -5384,7 +5384,7 @@ migrate_61_to_62 () int migrate_62_to_63 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 62. */ @@ -5442,7 +5442,7 @@ migrate_62_to_63 () int migrate_63_to_64 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 63. */ @@ -5483,7 +5483,7 @@ migrate_63_to_64 () int migrate_64_to_65 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 64. */ @@ -5521,7 +5521,7 @@ migrate_64_to_65 () int migrate_65_to_66 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 65. */ @@ -5562,7 +5562,7 @@ migrate_65_to_66 () int migrate_66_to_67 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 66. */ @@ -5599,7 +5599,7 @@ migrate_66_to_67 () int migrate_67_to_68 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 67. */ @@ -5646,7 +5646,7 @@ migrate_67_to_68 () int migrate_68_to_69 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 68. */ @@ -5695,7 +5695,7 @@ migrate_68_to_69 () int migrate_69_to_70 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 69. */ @@ -5737,7 +5737,7 @@ migrate_69_to_70 () int migrate_70_to_71 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 70. */ @@ -5779,7 +5779,7 @@ migrate_70_to_71 () int migrate_71_to_72 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 71. */ @@ -5828,7 +5828,7 @@ migrate_71_to_72 () int migrate_72_to_73 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 72. */ @@ -5876,7 +5876,7 @@ migrate_72_to_73 () int migrate_73_to_74 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 73. */ @@ -5921,7 +5921,7 @@ migrate_73_to_74 () int migrate_74_to_75 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 74. */ @@ -5978,7 +5978,7 @@ migrate_74_to_75 () int migrate_75_to_76 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 75. */ @@ -6016,7 +6016,7 @@ migrate_75_to_76 () int migrate_76_to_77 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 76. */ @@ -6064,7 +6064,7 @@ migrate_76_to_77 () int migrate_77_to_78 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 77. */ if (manage_db_version () != 77) @@ -6110,7 +6110,7 @@ migrate_77_to_78 () int migrate_78_to_79 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 78. */ @@ -6429,7 +6429,7 @@ migrate_79_to_80 () gchar *dir; struct stat state; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 79. */ @@ -6802,7 +6802,7 @@ migrate_79_to_80 () int migrate_80_to_81 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 80. */ @@ -6867,7 +6867,7 @@ migrate_80_to_81 () int migrate_81_to_82 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 80. */ @@ -6896,7 +6896,7 @@ migrate_81_to_82 () int migrate_82_to_83 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 82. */ @@ -6955,7 +6955,7 @@ migrate_82_to_83 () int migrate_83_to_84 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 83. */ @@ -6987,7 +6987,7 @@ migrate_83_to_84 () int migrate_84_to_85 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 84. */ @@ -7023,7 +7023,7 @@ migrate_84_to_85 () int migrate_85_to_86 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 85. */ @@ -7058,7 +7058,7 @@ migrate_85_to_86 () int migrate_86_to_87 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 86. */ @@ -7108,7 +7108,7 @@ migrate_86_to_87 () int migrate_87_to_88 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 87. */ @@ -7160,7 +7160,7 @@ migrate_87_to_88 () int migrate_88_to_89 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 87. */ @@ -7270,7 +7270,7 @@ migrate_88_to_89 () int migrate_89_to_90 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 89. */ @@ -7305,7 +7305,7 @@ migrate_89_to_90 () int migrate_90_to_91 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 90. */ @@ -7392,7 +7392,7 @@ migrate_90_to_91 () int migrate_91_to_92 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 91. */ @@ -7435,7 +7435,7 @@ migrate_91_to_92 () int migrate_92_to_93 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 92. */ @@ -7469,7 +7469,7 @@ migrate_92_to_93 () int migrate_93_to_94 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 93. */ @@ -7501,7 +7501,7 @@ migrate_93_to_94 () int migrate_94_to_95 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 94. */ @@ -7535,7 +7535,7 @@ migrate_94_to_95 () int migrate_95_to_96 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 95. */ @@ -7573,7 +7573,7 @@ migrate_95_to_96 () int migrate_96_to_97 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 96. */ @@ -7605,7 +7605,7 @@ migrate_96_to_97 () int migrate_97_to_98 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 97. */ @@ -7638,7 +7638,7 @@ migrate_97_to_98 () int migrate_98_to_99 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 98. */ @@ -7676,7 +7676,7 @@ migrate_98_to_99 () int migrate_99_to_100 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 99. */ @@ -7723,7 +7723,7 @@ migrate_99_to_100 () int migrate_100_to_101 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 100. */ @@ -7777,7 +7777,7 @@ migrate_100_to_101 () int migrate_101_to_102 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 101. */ @@ -7829,7 +7829,7 @@ migrate_101_to_102 () int migrate_102_to_103 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 102. */ @@ -7873,7 +7873,7 @@ migrate_102_to_103 () int migrate_103_to_104 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 103. */ @@ -7906,7 +7906,7 @@ migrate_103_to_104 () int migrate_104_to_105 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 104. */ @@ -7955,7 +7955,7 @@ migrate_104_to_105 () int migrate_105_to_106 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 105. */ @@ -7988,7 +7988,7 @@ migrate_105_to_106 () int migrate_106_to_107 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 106. */ @@ -8021,7 +8021,7 @@ migrate_106_to_107 () int migrate_107_to_108 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 107. */ @@ -8056,7 +8056,7 @@ migrate_107_to_108 () int migrate_108_to_109 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 108. */ @@ -8103,7 +8103,7 @@ migrate_108_to_109 () int migrate_109_to_110 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 109. */ @@ -8140,7 +8140,7 @@ migrate_109_to_110 () int migrate_110_to_111 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 110. */ @@ -8177,7 +8177,7 @@ migrate_110_to_111 () int migrate_111_to_112 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 111. */ @@ -8219,7 +8219,7 @@ migrate_111_to_112 () int migrate_112_to_113 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 112. */ @@ -8253,7 +8253,7 @@ migrate_112_to_113 () int migrate_113_to_114 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 113. */ @@ -8290,7 +8290,7 @@ migrate_113_to_114 () int migrate_114_to_115 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 114. */ @@ -8339,7 +8339,7 @@ migrate_114_to_115 () int migrate_115_to_116 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 115. */ @@ -8399,7 +8399,7 @@ migrate_116_to_117 () { int scap_loaded = manage_scap_loaded (); int cert_loaded = manage_cert_loaded (); - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 116. */ @@ -8642,7 +8642,7 @@ migrate_116_to_117 () int migrate_117_to_118 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 117. */ @@ -8763,7 +8763,7 @@ migrate_117_to_118 () int migrate_118_to_119 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 118. */ @@ -8805,7 +8805,7 @@ migrate_118_to_119 () int migrate_119_to_120 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 119. */ @@ -8847,7 +8847,7 @@ migrate_119_to_120 () int migrate_120_to_121 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 120. */ @@ -8883,7 +8883,7 @@ migrate_120_to_121 () int migrate_121_to_122 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 121. */ @@ -8922,7 +8922,7 @@ migrate_122_to_123 () int column_found = 0; iterator_t column_data; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 122. */ @@ -8969,7 +8969,7 @@ migrate_122_to_123 () int migrate_123_to_124 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 123. */ @@ -9031,7 +9031,7 @@ migrate_123_to_124 () int migrate_124_to_125 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 124. */ @@ -9068,7 +9068,7 @@ migrate_124_to_125 () int migrate_125_to_126 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 125. */ @@ -9138,7 +9138,7 @@ migrate_125_to_126 () int migrate_126_to_127 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 126. */ @@ -9176,7 +9176,7 @@ migrate_126_to_127 () int migrate_127_to_128 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 127. */ @@ -9210,7 +9210,7 @@ migrate_127_to_128 () int migrate_128_to_129 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 128. */ @@ -9266,7 +9266,7 @@ migrate_129_to_130 () char *quoted_ca_pub, *quoted_key_pub, *quoted_key_priv; GError *error = NULL; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 129. */ @@ -9342,7 +9342,7 @@ migrate_129_to_130 () int migrate_130_to_131 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 130. */ @@ -9383,7 +9383,7 @@ migrate_130_to_131 () int migrate_131_to_132 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 131. */ @@ -9482,7 +9482,7 @@ migrate_131_to_132 () int migrate_132_to_133 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 132. */ @@ -9527,7 +9527,7 @@ migrate_132_to_133 () int migrate_133_to_134 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 133. */ @@ -9572,7 +9572,7 @@ migrate_133_to_134 () int migrate_134_to_135 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 134. */ @@ -9605,7 +9605,7 @@ migrate_134_to_135 () int migrate_135_to_136 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 135. */ @@ -9647,7 +9647,7 @@ migrate_135_to_136 () int migrate_136_to_137 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 136. */ @@ -9684,7 +9684,7 @@ migrate_136_to_137 () int migrate_137_to_138 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 137. */ @@ -9761,7 +9761,7 @@ migrate_138_to_139 () { iterator_t nvts; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 138. */ @@ -9811,7 +9811,7 @@ migrate_138_to_139 () int migrate_139_to_140 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 139. */ @@ -9848,7 +9848,7 @@ migrate_139_to_140 () int migrate_140_to_141 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 140. */ @@ -9879,7 +9879,7 @@ migrate_140_to_141 () int migrate_141_to_142 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 141. */ @@ -9914,7 +9914,7 @@ migrate_141_to_142 () int migrate_142_to_143 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 142. */ @@ -9950,7 +9950,7 @@ int migrate_143_to_144 () { iterator_t nvts; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 143. */ @@ -10010,7 +10010,7 @@ migrate_143_to_144 () int migrate_144_to_145 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 144. */ @@ -10043,7 +10043,7 @@ migrate_144_to_145 () int migrate_145_to_146 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 145. */ @@ -10077,7 +10077,7 @@ migrate_145_to_146 () int migrate_146_to_147 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 146. */ @@ -10110,7 +10110,7 @@ migrate_146_to_147 () int migrate_147_to_148 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 147. */ @@ -10143,7 +10143,7 @@ migrate_147_to_148 () int migrate_148_to_149 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 148. */ @@ -10176,7 +10176,7 @@ migrate_148_to_149 () int migrate_149_to_150 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 149. */ @@ -10220,7 +10220,7 @@ migrate_149_to_150 () int migrate_150_to_151 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 150. */ @@ -10260,7 +10260,7 @@ migrate_150_to_151 () int migrate_151_to_152 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 151. */ @@ -10303,7 +10303,7 @@ migrate_151_to_152 () int migrate_152_to_153 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 152. */ @@ -10347,7 +10347,7 @@ migrate_153_to_154 () const char *primary_key_type = sql_is_sqlite3 () ? "INTEGER" : "SERIAL"; iterator_t credentials; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 153. */ @@ -10558,7 +10558,7 @@ migrate_153_to_154 () int migrate_154_to_155 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 154. */ @@ -10597,7 +10597,7 @@ migrate_154_to_155 () int migrate_155_to_156 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 155. */ @@ -10806,7 +10806,7 @@ int migrate_156_to_157 () { iterator_t slaves; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 156. */ @@ -11138,7 +11138,7 @@ migrate_156_to_157 () int migrate_157_to_158 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 157. */ @@ -11187,7 +11187,7 @@ int migrate_158_to_159 () { iterator_t scanners; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 158. */ @@ -11568,7 +11568,7 @@ migrate_158_to_159 () int migrate_159_to_160 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 159. */ @@ -11609,7 +11609,7 @@ migrate_160_to_161 () iterator_t iter; iter.crypt_ctx = NULL; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 160. */ @@ -11797,7 +11797,7 @@ migrate_160_to_161 () int migrate_161_to_162 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 161. */ @@ -11848,7 +11848,7 @@ migrate_161_to_162 () int migrate_162_to_163 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 162. */ @@ -11995,7 +11995,7 @@ migrate_162_to_163 () int migrate_163_to_164 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 163. */ @@ -12073,7 +12073,7 @@ migrate_163_to_164 () int migrate_164_to_165 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 164. */ @@ -12117,7 +12117,7 @@ int migrate_165_to_166 () { iterator_t alert_data; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 165. */ @@ -12483,7 +12483,7 @@ insert_predefined (const gchar *uuid) int migrate_166_to_167 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 166. */ @@ -12541,7 +12541,7 @@ migrate_167_to_168 () { const char *uuid; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 167. */ @@ -12652,7 +12652,7 @@ migrate_168_to_169 () const char *uuid; iterator_t users; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 168. */ @@ -12756,7 +12756,7 @@ migrate_169_to_170_add_permission (const gchar *role, const gchar *permission) int migrate_169_to_170 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 169. */ @@ -12797,7 +12797,7 @@ migrate_170_to_171 () gchar *old_dir, *new_dir; struct stat state; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 170. */ @@ -12917,7 +12917,7 @@ migrate_171_to_172 () const gchar *subdir_name; struct stat state; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 171. */ @@ -13078,7 +13078,7 @@ migrate_171_to_172 () int migrate_172_to_173 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 172. */ @@ -13130,7 +13130,7 @@ migrate_172_to_173 () int migrate_173_to_174 () { - sql_begin_exclusive (); + sql_begin_immediate (); report_format_t report_format; /* Ensure that the database is currently version 173. */ @@ -13192,7 +13192,7 @@ migrate_174_to_175 () const gchar *subdir_name; GDir *old_dir; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 174. */ @@ -13345,7 +13345,7 @@ int migrate_175_to_176 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 175. */ @@ -13380,7 +13380,7 @@ migrate_176_to_177 () { int now; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 176. */ @@ -13435,7 +13435,7 @@ int migrate_177_to_178 () { credential_t credential; - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 177. */ @@ -13507,7 +13507,7 @@ migrate_177_to_178 () int migrate_178_to_179 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 178. */ @@ -13581,7 +13581,7 @@ migrate_179_to_180_update_ref (const gchar *table, int trash) int migrate_179_to_180 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 179. */ @@ -13673,7 +13673,7 @@ migrate_179_to_180 () int migrate_180_to_181 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 180. */ @@ -13860,7 +13860,7 @@ migrate_181_to_182_move (const char *dest) int migrate_181_to_182 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 181. */ @@ -13900,7 +13900,7 @@ migrate_181_to_182 () int migrate_182_to_183 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 182. */ @@ -13961,7 +13961,7 @@ migrate_182_to_183 () int migrate_183_to_184 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 183. */ @@ -14003,7 +14003,7 @@ migrate_183_to_184 () int migrate_184_to_185 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 184. */ @@ -14060,7 +14060,7 @@ migrate_184_to_185 () int migrate_185_to_186 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 185. */ @@ -14098,7 +14098,7 @@ migrate_185_to_186 () int migrate_186_to_187 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 186. */ @@ -14135,7 +14135,7 @@ migrate_186_to_187 () int migrate_187_to_188 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 187. */ @@ -14172,7 +14172,7 @@ migrate_187_to_188 () int migrate_188_to_189 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 188. */ @@ -14271,7 +14271,7 @@ migrate_188_to_189 () int migrate_189_to_190 () { - sql_begin_exclusive (); + sql_begin_immediate (); /* Ensure that the database is currently version 189. */ From b8c1c955716540e1ff0d8e1c3838e697e99a8809 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Fri, 2 Mar 2018 16:40:34 +0200 Subject: [PATCH 19/19] Disable unused exclusive functions for Postgres --- src/sql_pg.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/sql_pg.c b/src/sql_pg.c index c96049c22..707193467 100644 --- a/src/sql_pg.c +++ b/src/sql_pg.c @@ -610,7 +610,9 @@ sql_explain_internal (const char* sql, va_list args) void sql_begin_exclusive () { - sql ("BEGIN;"); + /* No longer used under Postgres. */ + assert (0); + abort (); } /** @@ -621,12 +623,10 @@ sql_begin_exclusive () int sql_begin_exclusive_giveup () { - int ret; - - ret = sql_giveup ("BEGIN;"); - if (ret) - return ret; - return 0; + /* No longer used under Postgres. */ + assert (0); + abort (); + return -1; } /**