commit abb9161c903bcc849b17211664d5ed223a18a431
parent 9c60576121f28fb613cafc743dd66626d877c6a3
Author: Christian Grothoff <christian@grothoff.org>
Date: Sun, 10 Nov 2024 17:21:46 +0100
ensure we do not start if the database version is outdated (#9203)
Diffstat:
8 files changed, 65 insertions(+), 36 deletions(-)
diff --git a/src/include/sync_database_lib.h b/src/include/sync_database_lib.h
@@ -26,10 +26,14 @@
* Initialize the plugin.
*
* @param cfg configuration to use
+ * @param skip_preflight true if we should skip the usual
+ * preflight check which assures us that the DB is actually
+ * operational; only sync-dbinit should use true here.
* @return NULL on failure
*/
struct SYNC_DatabasePlugin *
-SYNC_DB_plugin_load (const struct GNUNET_CONFIGURATION_Handle *cfg);
+SYNC_DB_plugin_load (const struct GNUNET_CONFIGURATION_Handle *cfg,
+ bool skip_preflight);
/**
diff --git a/src/sync/sync-httpd.c b/src/sync/sync-httpd.c
@@ -641,7 +641,8 @@ run (void *cls,
}
if (NULL ==
- (db = SYNC_DB_plugin_load (config)))
+ (db = SYNC_DB_plugin_load (config,
+ false)))
{
global_ret = EXIT_NOTCONFIGURED;
GNUNET_SCHEDULER_shutdown ();
diff --git a/src/sync/sync-httpd_backup_post.c b/src/sync/sync-httpd_backup_post.c
@@ -32,7 +32,7 @@
* we are awaiting payment before giving up?
*/
#define CHECK_PAYMENT_GENERIC_TIMEOUT GNUNET_TIME_relative_multiply ( \
- GNUNET_TIME_UNIT_MINUTES, 30)
+ GNUNET_TIME_UNIT_MINUTES, 30)
/**
@@ -236,7 +236,7 @@ make_payment_request (const char *order_id,
TALER_MHD_add_global_headers (resp);
{
char *hdr;
- char *pfx;
+ const char *pfx;
char *hn;
struct GNUNET_Buffer hdr_buf = { 0 };
@@ -515,7 +515,8 @@ await_payment (struct BackupContext *bc,
bc->omgh = TALER_MERCHANT_merchant_order_get (SH_ctx,
SH_backend_url,
order_id,
- NULL /* our payments are NOT session-bound */,
+ NULL /* our payments are NOT session-bound */
+ ,
timeout,
&check_payment_cb,
bc);
diff --git a/src/syncdb/plugin_syncdb_postgres.c b/src/syncdb/plugin_syncdb_postgres.c
@@ -237,8 +237,7 @@ prepare_statements (void *cls)
* @return #GNUNET_OK on success
*/
static enum GNUNET_GenericReturnValue
-internal_setup (struct PostgresClosure *pg,
- bool skip_prepare)
+internal_setup (struct PostgresClosure *pg)
{
if (NULL == pg->conn)
{
@@ -267,11 +266,12 @@ internal_setup (struct PostgresClosure *pg,
#endif
struct GNUNET_PQ_Context *db_conn;
- db_conn = GNUNET_PQ_connect_with_cfg (pg->cfg,
- "syncdb-postgres",
- NULL,
- es,
- NULL);
+ db_conn = GNUNET_PQ_connect_with_cfg2 (pg->cfg,
+ "syncdb-postgres",
+ "sync-",
+ es,
+ NULL,
+ GNUNET_PQ_FLAG_CHECK_CURRENT);
if (NULL == db_conn)
return GNUNET_SYSERR;
pg->conn = db_conn;
@@ -280,8 +280,6 @@ internal_setup (struct PostgresClosure *pg,
GNUNET_PQ_reconnect_if_down (pg->conn);
if (pg->init)
return GNUNET_OK;
- if (skip_prepare)
- return GNUNET_OK;
return prepare_statements (pg);
}
@@ -308,8 +306,7 @@ postgres_preflight (void *cls)
if (! pg->init)
{
if (GNUNET_OK !=
- internal_setup (pg,
- false))
+ internal_setup (pg))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Failed to ensure DB is initialized\n");
@@ -631,7 +628,8 @@ payment_by_account_cb (void *cls,
static enum GNUNET_DB_QueryStatus
postgres_lookup_pending_payments_by_account (void *cls,
const struct
- SYNC_AccountPublicKeyP *account_pub,
+ SYNC_AccountPublicKeyP *account_pub
+ ,
SYNC_DB_PaymentPendingIterator it,
void *it_cls)
{
@@ -1283,6 +1281,10 @@ postgres_create_tables (void *cls)
* @return NULL on error, otherwise a `struct TALER_SYNCDB_Plugin`
*/
void *
+libsync_plugin_db_postgres_init (void *cls);
+
+/* make compiler happy */
+void *
libsync_plugin_db_postgres_init (void *cls)
{
struct GNUNET_CONFIGURATION_Handle *cfg = cls;
@@ -1316,15 +1318,6 @@ libsync_plugin_db_postgres_init (void *cls)
GNUNET_free (pg);
return NULL;
}
- if (GNUNET_OK !=
- internal_setup (pg,
- true))
- {
- GNUNET_free (pg->currency);
- GNUNET_free (pg->sql_dir);
- GNUNET_free (pg);
- return NULL;
- }
plugin = GNUNET_new (struct SYNC_DatabasePlugin);
plugin->cls = pg;
plugin->create_tables = &postgres_create_tables;
@@ -1350,6 +1343,10 @@ libsync_plugin_db_postgres_init (void *cls)
* @return NULL (always)
*/
void *
+libsync_plugin_db_postgres_done (void *cls);
+
+/* make compiler happy */
+void *
libsync_plugin_db_postgres_done (void *cls)
{
struct SYNC_DatabasePlugin *plugin = cls;
diff --git a/src/syncdb/sync-dbinit.c b/src/syncdb/sync-dbinit.c
@@ -57,7 +57,8 @@ run (void *cls,
struct SYNC_DatabasePlugin *plugin;
if (NULL ==
- (plugin = SYNC_DB_plugin_load (cfg)))
+ (plugin = SYNC_DB_plugin_load (cfg,
+ true)))
{
fprintf (stderr,
"Failed to initialize database plugin.\n");
diff --git a/src/syncdb/sync_db_plugin.c b/src/syncdb/sync_db_plugin.c
@@ -20,12 +20,13 @@
* @author Sree Harsha Totakura <sreeharsha@totakura.in>
*/
#include "platform.h"
-#include "sync_database_plugin.h"
+#include "sync_database_lib.h"
#include <ltdl.h>
struct SYNC_DatabasePlugin *
-SYNC_DB_plugin_load (const struct GNUNET_CONFIGURATION_Handle *cfg)
+SYNC_DB_plugin_load (const struct GNUNET_CONFIGURATION_Handle *cfg,
+ bool skip_preflight)
{
char *plugin_name;
char *lib_name;
@@ -52,6 +53,15 @@ SYNC_DB_plugin_load (const struct GNUNET_CONFIGURATION_Handle *cfg)
plugin->library_name = lib_name;
else
GNUNET_free (lib_name);
+ if ( (! skip_preflight) &&
+ (GNUNET_OK !=
+ plugin->preflight (plugin->cls)) )
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Database not ready. Try running sync-dbinit!\n");
+ SYNC_DB_plugin_unload (plugin);
+ return NULL;
+ }
return plugin;
}
@@ -80,6 +90,10 @@ static char *old_dlsearchpath;
* Setup libtool paths.
*/
void __attribute__ ((constructor))
+plugin_init (void);
+
+/* make compiler happy... */
+void __attribute__ ((constructor))
plugin_init ()
{
int err;
@@ -121,6 +135,10 @@ plugin_init ()
* Shutdown libtool.
*/
void __attribute__ ((destructor))
+plugin_fini (void);
+
+/* make compiler happy... */
+void __attribute__ ((destructor))
plugin_fini ()
{
lt_dlsetsearchpath (old_dlsearchpath);
diff --git a/src/syncdb/test_sync_db.c b/src/syncdb/test_sync_db.c
@@ -28,14 +28,15 @@
#define FAILIF(cond) \
- do { \
- if (! (cond)) { break;} \
- GNUNET_break (0); \
- goto drop; \
- } while (0)
+ do { \
+ if (! (cond)) { break;} \
+ GNUNET_break (0); \
+ goto drop; \
+ } while (0)
#define RND_BLK(ptr) \
- GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, ptr, sizeof (*ptr))
+ GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, ptr, sizeof (* \
+ ptr))
/**
* Global return value for the test. Initially -1, set to 0 upon
@@ -94,7 +95,8 @@ run (void *cls)
size_t bs;
void *b = NULL;
- if (NULL == (plugin = SYNC_DB_plugin_load (cfg)))
+ if (NULL == (plugin = SYNC_DB_plugin_load (cfg,
+ true)))
{
result = 77;
return;
diff --git a/src/util/os_installation.c b/src/util/os_installation.c
@@ -25,6 +25,7 @@
*/
#include "platform.h"
#include <gnunet/gnunet_util_lib.h>
+#include "sync_util.h"
/**
@@ -62,6 +63,10 @@ SYNC_project_data_default (void)
* Initialize libsyncutil.
*/
void __attribute__ ((constructor))
+SYNC_OS_init (void);
+
+/* make compiler happy */
+void __attribute__ ((constructor))
SYNC_OS_init ()
{
GNUNET_OS_init (&sync_pd);