diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-07-10 14:33:10 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-07-10 14:33:10 +0100 |
commit | 32700f14d269267ac9b70a2eb675ba04e4aa154f (patch) | |
tree | 6a1b84127cba6710821f4aaed4f8a839659f59e1 /src | |
parent | 39ebf65fa5605de25cb54460119c88ce0402db85 (diff) | |
download | rspamd-32700f14d269267ac9b70a2eb675ba04e4aa154f.tar.gz rspamd-32700f14d269267ac9b70a2eb675ba04e4aa154f.zip |
More fixes to sqlite3 locking.
Diffstat (limited to 'src')
-rw-r--r-- | src/libstat/backends/sqlite3_backend.c | 1 | ||||
-rw-r--r-- | src/libutil/sqlite_utils.c | 24 |
2 files changed, 20 insertions, 5 deletions
diff --git a/src/libstat/backends/sqlite3_backend.c b/src/libstat/backends/sqlite3_backend.c index 4ded6198d..90c184ae3 100644 --- a/src/libstat/backends/sqlite3_backend.c +++ b/src/libstat/backends/sqlite3_backend.c @@ -187,7 +187,6 @@ rspamd_sqlite3_opendb (const gchar *path, const ucl_object_t *opts, { struct rspamd_stat_sqlite3_db *bk; - bk = g_slice_alloc0 (sizeof (*bk)); bk->sqlite = rspamd_sqlite3_open_or_create (path, create_tables_sql, err); diff --git a/src/libutil/sqlite_utils.c b/src/libutil/sqlite_utils.c index c07223c6e..37f2ccb8b 100644 --- a/src/libutil/sqlite_utils.c +++ b/src/libutil/sqlite_utils.c @@ -213,7 +213,6 @@ rspamd_sqlite3_open_or_create (const gchar *path, const gchar *create_sql, GError **err) { sqlite3 *sqlite; - sqlite3_stmt *stmt; gint rc, flags, lock_fd; gchar lock_path[PATH_MAX], dbdir[PATH_MAX], *pdir; static const char sqlite_wal[] = "PRAGMA journal_mode=\"wal\";", @@ -239,13 +238,17 @@ rspamd_sqlite3_open_or_create (const gchar *path, const return NULL; } - if (access (path, R_OK) == -1) { + rspamd_snprintf (lock_path, sizeof (lock_path), "%s.lock", path); + + if (access (path, R_OK) == -1 || access (lock_path, R_OK) != -1) { flags |= SQLITE_OPEN_CREATE; rspamd_snprintf (lock_path, sizeof (lock_path), "%s.lock", path); lock_fd = open (lock_path, O_WRONLY|O_CREAT|O_EXCL, 00600); if (lock_fd == -1 && (errno == EEXIST || errno == EBUSY)) { + msg_debug ("checking %s to wait for db being created", lock_path); + if (!rspamd_sqlite3_wait (lock_path)) { g_set_error (err, rspamd_sqlite3_quark (), errno, "cannot create sqlite file %s: %s", @@ -258,6 +261,8 @@ rspamd_sqlite3_open_or_create (const gchar *path, const create = FALSE; } else { + msg_debug ("locking %s to block creating", lock_path); + g_assert (rspamd_file_lock (lock_fd, FALSE)); create = TRUE; } @@ -279,8 +284,14 @@ rspamd_sqlite3_open_or_create (const gchar *path, const } if (create) { + if (sqlite3_exec (sqlite, sqlite_wal, NULL, NULL, NULL) != SQLITE_OK) { + msg_warn ("WAL mode is not supported (%s), locking issues might occur", + sqlite3_errmsg (sqlite)); + } + if (sqlite3_exec (sqlite, exclusive_lock_sql, NULL, NULL, NULL) != SQLITE_OK) { - msg_warn ("cannot exclusively lock database to create schema"); + msg_warn ("cannot exclusively lock database to create schema: %s", + sqlite3_errmsg (sqlite)); } if (sqlite3_exec (sqlite, create_sql, NULL, NULL, NULL) != SQLITE_OK) { @@ -295,13 +306,17 @@ rspamd_sqlite3_open_or_create (const gchar *path, const return NULL; } + msg_debug ("removing lock from %s", lock_path); + sqlite3_close (sqlite); rspamd_file_unlock (lock_fd, FALSE); unlink (lock_path); close (lock_fd); /* Reopen in normal mode */ + msg_debug ("reopening %s in normal mode", path); flags &= ~SQLITE_OPEN_CREATE; + if ((rc = sqlite3_open_v2 (path, &sqlite, flags, NULL)) != SQLITE_OK) { #if SQLITE_VERSION_NUMBER >= 3008000 @@ -319,7 +334,8 @@ rspamd_sqlite3_open_or_create (const gchar *path, const } if (sqlite3_exec (sqlite, sqlite_wal, NULL, NULL, NULL) != SQLITE_OK) { - msg_warn ("WAL mode is not supported, locking issues might occur"); + msg_warn ("WAL mode is not supported (%s), locking issues might occur", + sqlite3_errmsg (sqlite)); } return sqlite; |