diff options
author | Martin Schanzenbach <schanzen@gnunet.org> | 2022-09-23 01:06:59 +0900 |
---|---|---|
committer | Martin Schanzenbach <schanzen@gnunet.org> | 2022-09-23 01:06:59 +0900 |
commit | 127ad07a3abaee00fb206aaf8b980f258d2933c8 (patch) | |
tree | 7618a15a9330c300946bc6c3fe6668a6626876cd | |
parent | d78b7864d1754f2b2c5d3c47107808dfacc68e05 (diff) | |
download | gnunet-127ad07a3abaee00fb206aaf8b980f258d2933c8.tar.gz gnunet-127ad07a3abaee00fb206aaf8b980f258d2933c8.zip |
NAMESTORE: Use a per client database connection
Each connecting namestore client will now get a new database connection
through any of the plugins. This will allow us to properly use locking
in databases where available.
-rw-r--r-- | src/namestore/gnunet-service-namestore.c | 146 | ||||
-rw-r--r-- | src/namestore/plugin_namestore_flat.c | 16 | ||||
-rw-r--r-- | src/namestore/plugin_namestore_postgres.c | 16 | ||||
-rw-r--r-- | src/namestore/plugin_namestore_sqlite.c | 32 |
4 files changed, 107 insertions, 103 deletions
diff --git a/src/namestore/gnunet-service-namestore.c b/src/namestore/gnunet-service-namestore.c index 6d3cc45ec..a173e8927 100644 --- a/src/namestore/gnunet-service-namestore.c +++ b/src/namestore/gnunet-service-namestore.c | |||
@@ -132,6 +132,11 @@ struct NamestoreClient | |||
132 | struct GNUNET_SERVICE_Client *client; | 132 | struct GNUNET_SERVICE_Client *client; |
133 | 133 | ||
134 | /** | 134 | /** |
135 | * Database handle | ||
136 | */ | ||
137 | struct GNUNET_NAMESTORE_PluginFunctions *GSN_database; | ||
138 | |||
139 | /** | ||
135 | * Message queue for transmission to @e client | 140 | * Message queue for transmission to @e client |
136 | */ | 141 | */ |
137 | struct GNUNET_MQ_Handle *mq; | 142 | struct GNUNET_MQ_Handle *mq; |
@@ -353,11 +358,6 @@ static struct GNUNET_STATISTICS_Handle *statistics; | |||
353 | static struct GNUNET_NAMECACHE_Handle *namecache; | 358 | static struct GNUNET_NAMECACHE_Handle *namecache; |
354 | 359 | ||
355 | /** | 360 | /** |
356 | * Database handle | ||
357 | */ | ||
358 | static struct GNUNET_NAMESTORE_PluginFunctions *GSN_database; | ||
359 | |||
360 | /** | ||
361 | * Name of the database plugin | 361 | * Name of the database plugin |
362 | */ | 362 | */ |
363 | static char *db_lib_name; | 363 | static char *db_lib_name; |
@@ -436,7 +436,6 @@ cleanup_task (void *cls) | |||
436 | GNUNET_NAMECACHE_disconnect (namecache); | 436 | GNUNET_NAMECACHE_disconnect (namecache); |
437 | namecache = NULL; | 437 | namecache = NULL; |
438 | } | 438 | } |
439 | GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, GSN_database)); | ||
440 | GNUNET_free (db_lib_name); | 439 | GNUNET_free (db_lib_name); |
441 | db_lib_name = NULL; | 440 | db_lib_name = NULL; |
442 | if (NULL != monitor_nc) | 441 | if (NULL != monitor_nc) |
@@ -560,11 +559,13 @@ cache_nick (const struct GNUNET_IDENTITY_PrivateKey *zone, | |||
560 | /** | 559 | /** |
561 | * Return the NICK record for the zone (if it exists). | 560 | * Return the NICK record for the zone (if it exists). |
562 | * | 561 | * |
562 | * @param nc the namestore client | ||
563 | * @param zone private key for the zone to look for nick | 563 | * @param zone private key for the zone to look for nick |
564 | * @return NULL if no NICK record was found | 564 | * @return NULL if no NICK record was found |
565 | */ | 565 | */ |
566 | static struct GNUNET_GNSRECORD_Data * | 566 | static struct GNUNET_GNSRECORD_Data * |
567 | get_nick_record (const struct GNUNET_IDENTITY_PrivateKey *zone) | 567 | get_nick_record (const struct NamestoreClient *nc, |
568 | const struct GNUNET_IDENTITY_PrivateKey *zone) | ||
568 | { | 569 | { |
569 | struct GNUNET_IDENTITY_PublicKey pub; | 570 | struct GNUNET_IDENTITY_PublicKey pub; |
570 | struct GNUNET_GNSRECORD_Data *nick; | 571 | struct GNUNET_GNSRECORD_Data *nick; |
@@ -588,11 +589,11 @@ get_nick_record (const struct GNUNET_IDENTITY_PrivateKey *zone) | |||
588 | } | 589 | } |
589 | 590 | ||
590 | nick = NULL; | 591 | nick = NULL; |
591 | res = GSN_database->lookup_records (GSN_database->cls, | 592 | res = nc->GSN_database->lookup_records (nc->GSN_database->cls, |
592 | zone, | 593 | zone, |
593 | GNUNET_GNS_EMPTY_LABEL_AT, | 594 | GNUNET_GNS_EMPTY_LABEL_AT, |
594 | &lookup_nick_it, | 595 | &lookup_nick_it, |
595 | &nick); | 596 | &nick); |
596 | if ((GNUNET_OK != res) || (NULL == nick)) | 597 | if ((GNUNET_OK != res) || (NULL == nick)) |
597 | { | 598 | { |
598 | #if ! defined(GNUNET_CULL_LOGGING) | 599 | #if ! defined(GNUNET_CULL_LOGGING) |
@@ -735,7 +736,7 @@ send_lookup_response (struct NamestoreClient *nc, | |||
735 | char *name_tmp; | 736 | char *name_tmp; |
736 | char *rd_ser; | 737 | char *rd_ser; |
737 | 738 | ||
738 | nick = get_nick_record (zone_key); | 739 | nick = get_nick_record (nc, zone_key); |
739 | GNUNET_assert (-1 != GNUNET_GNSRECORD_records_get_size (rd_count, rd)); | 740 | GNUNET_assert (-1 != GNUNET_GNSRECORD_records_get_size (rd_count, rd)); |
740 | 741 | ||
741 | if ((NULL != nick) && (0 != strcmp (name, GNUNET_GNS_EMPTY_LABEL_AT))) | 742 | if ((NULL != nick) && (0 != strcmp (name, GNUNET_GNS_EMPTY_LABEL_AT))) |
@@ -936,7 +937,7 @@ refresh_block (struct NamestoreClient *nc, | |||
936 | rd_clean[rd_count_clean++] = rd[i]; | 937 | rd_clean[rd_count_clean++] = rd[i]; |
937 | } | 938 | } |
938 | 939 | ||
939 | nick = get_nick_record (zone_key); | 940 | nick = get_nick_record (nc, zone_key); |
940 | res_count = rd_count_clean; | 941 | res_count = rd_count_clean; |
941 | res = (struct GNUNET_GNSRECORD_Data *) rd_clean; /* fixme: a bit unclean... */ | 942 | res = (struct GNUNET_GNSRECORD_Data *) rd_clean; /* fixme: a bit unclean... */ |
942 | if ((NULL != nick) && (0 != strcmp (name, GNUNET_GNS_EMPTY_LABEL_AT))) | 943 | if ((NULL != nick) && (0 != strcmp (name, GNUNET_GNS_EMPTY_LABEL_AT))) |
@@ -1168,6 +1169,7 @@ client_disconnect_cb (void *cls, | |||
1168 | for (cop = cop_head; NULL != cop; cop = cop->next) | 1169 | for (cop = cop_head; NULL != cop; cop = cop->next) |
1169 | if (nc == cop->nc) | 1170 | if (nc == cop->nc) |
1170 | cop->nc = NULL; | 1171 | cop->nc = NULL; |
1172 | GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, nc->GSN_database)); | ||
1171 | GNUNET_free (nc); | 1173 | GNUNET_free (nc); |
1172 | } | 1174 | } |
1173 | 1175 | ||
@@ -1186,12 +1188,35 @@ client_connect_cb (void *cls, | |||
1186 | struct GNUNET_MQ_Handle *mq) | 1188 | struct GNUNET_MQ_Handle *mq) |
1187 | { | 1189 | { |
1188 | struct NamestoreClient *nc; | 1190 | struct NamestoreClient *nc; |
1191 | char *database; | ||
1192 | char *db_lib_name; | ||
1189 | 1193 | ||
1190 | (void) cls; | 1194 | (void) cls; |
1191 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n", client); | 1195 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n", client); |
1192 | nc = GNUNET_new (struct NamestoreClient); | 1196 | nc = GNUNET_new (struct NamestoreClient); |
1193 | nc->client = client; | 1197 | nc->client = client; |
1194 | nc->mq = mq; | 1198 | nc->mq = mq; |
1199 | /* Loading database plugin */ | ||
1200 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (GSN_cfg, | ||
1201 | "namestore", | ||
1202 | "database", | ||
1203 | &database)) | ||
1204 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n"); | ||
1205 | GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database); | ||
1206 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Loading %s\n", db_lib_name); | ||
1207 | nc->GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg); | ||
1208 | GNUNET_free (database); | ||
1209 | if (NULL == nc->GSN_database) | ||
1210 | { | ||
1211 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1212 | "Could not load database backend `%s'\n", | ||
1213 | db_lib_name); | ||
1214 | GNUNET_free (db_lib_name); | ||
1215 | GNUNET_free (nc); | ||
1216 | return NULL; | ||
1217 | } | ||
1218 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Loaded %s\n", db_lib_name); | ||
1219 | GNUNET_free (db_lib_name); | ||
1195 | return nc; | 1220 | return nc; |
1196 | } | 1221 | } |
1197 | 1222 | ||
@@ -1402,12 +1427,12 @@ handle_record_lookup (void *cls, const struct LabelLookupMessage *ll_msg) | |||
1402 | rlc.res_rd_count = 0; | 1427 | rlc.res_rd_count = 0; |
1403 | rlc.res_rd = NULL; | 1428 | rlc.res_rd = NULL; |
1404 | rlc.rd_ser_len = 0; | 1429 | rlc.rd_ser_len = 0; |
1405 | rlc.nick = get_nick_record (&ll_msg->zone); | 1430 | rlc.nick = get_nick_record (nc, &ll_msg->zone); |
1406 | res = GSN_database->lookup_records (GSN_database->cls, | 1431 | res = nc->GSN_database->lookup_records (nc->GSN_database->cls, |
1407 | &ll_msg->zone, | 1432 | &ll_msg->zone, |
1408 | conv_name, | 1433 | conv_name, |
1409 | &lookup_it, | 1434 | &lookup_it, |
1410 | &rlc); | 1435 | &rlc); |
1411 | env = | 1436 | env = |
1412 | GNUNET_MQ_msg_extra (llr_msg, | 1437 | GNUNET_MQ_msg_extra (llr_msg, |
1413 | name_len + rlc.rd_ser_len, | 1438 | name_len + rlc.rd_ser_len, |
@@ -1591,11 +1616,11 @@ handle_record_store (void *cls, const struct RecordStoreMessage *rp_msg) | |||
1591 | "Creating %u records for name `%s'\n", | 1616 | "Creating %u records for name `%s'\n", |
1592 | (unsigned int) rd_count, | 1617 | (unsigned int) rd_count, |
1593 | conv_name); | 1618 | conv_name); |
1594 | if ((GNUNET_NO == GSN_database->lookup_records (GSN_database->cls, | 1619 | if ((GNUNET_NO == nc->GSN_database->lookup_records (nc->GSN_database->cls, |
1595 | &rp_msg->private_key, | 1620 | &rp_msg->private_key, |
1596 | conv_name, | 1621 | conv_name, |
1597 | &get_block_exp_existing, | 1622 | &get_block_exp_existing, |
1598 | &existing_block_exp)) && | 1623 | &existing_block_exp)) && |
1599 | (rd_count == 0)) | 1624 | (rd_count == 0)) |
1600 | { | 1625 | { |
1601 | /* This name does not exist, so cannot be removed */ | 1626 | /* This name does not exist, so cannot be removed */ |
@@ -1676,11 +1701,11 @@ handle_record_store (void *cls, const struct RecordStoreMessage *rp_msg) | |||
1676 | /* remove nick record from cache, in case we have one there */ | 1701 | /* remove nick record from cache, in case we have one there */ |
1677 | cache_nick (&rp_msg->private_key, NULL); | 1702 | cache_nick (&rp_msg->private_key, NULL); |
1678 | } | 1703 | } |
1679 | res = GSN_database->store_records (GSN_database->cls, | 1704 | res = nc->GSN_database->store_records (nc->GSN_database->cls, |
1680 | &rp_msg->private_key, | 1705 | &rp_msg->private_key, |
1681 | conv_name, | 1706 | conv_name, |
1682 | rd_nf_count, | 1707 | rd_nf_count, |
1683 | rd_nf); | 1708 | rd_nf); |
1684 | } | 1709 | } |
1685 | 1710 | ||
1686 | if (GNUNET_OK != res) | 1711 | if (GNUNET_OK != res) |
@@ -1817,11 +1842,11 @@ handle_zone_to_name (void *cls, const struct ZoneToNameMessage *ztn_msg) | |||
1817 | ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id); | 1842 | ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id); |
1818 | ztn_ctx.nc = nc; | 1843 | ztn_ctx.nc = nc; |
1819 | ztn_ctx.success = GNUNET_NO; | 1844 | ztn_ctx.success = GNUNET_NO; |
1820 | if (GNUNET_SYSERR == GSN_database->zone_to_name (GSN_database->cls, | 1845 | if (GNUNET_SYSERR == nc->GSN_database->zone_to_name (nc->GSN_database->cls, |
1821 | &ztn_msg->zone, | 1846 | &ztn_msg->zone, |
1822 | &ztn_msg->value_zone, | 1847 | &ztn_msg->value_zone, |
1823 | &handle_zone_to_name_it, | 1848 | &handle_zone_to_name_it, |
1824 | &ztn_ctx)) | 1849 | &ztn_ctx)) |
1825 | { | 1850 | { |
1826 | /* internal error, hang up instead of signalling something | 1851 | /* internal error, hang up instead of signalling something |
1827 | that might be wrong */ | 1852 | that might be wrong */ |
@@ -1935,6 +1960,7 @@ run_zone_iteration_round (struct ZoneIteration *zi, uint64_t limit) | |||
1935 | struct ZoneIterationProcResult proc; | 1960 | struct ZoneIterationProcResult proc; |
1936 | struct GNUNET_TIME_Absolute start; | 1961 | struct GNUNET_TIME_Absolute start; |
1937 | struct GNUNET_TIME_Relative duration; | 1962 | struct GNUNET_TIME_Relative duration; |
1963 | struct NamestoreClient *nc = zi->nc; | ||
1938 | 1964 | ||
1939 | memset (&proc, 0, sizeof(proc)); | 1965 | memset (&proc, 0, sizeof(proc)); |
1940 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 1966 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
@@ -1945,15 +1971,16 @@ run_zone_iteration_round (struct ZoneIteration *zi, uint64_t limit) | |||
1945 | proc.limit = limit; | 1971 | proc.limit = limit; |
1946 | start = GNUNET_TIME_absolute_get (); | 1972 | start = GNUNET_TIME_absolute_get (); |
1947 | GNUNET_break (GNUNET_SYSERR != | 1973 | GNUNET_break (GNUNET_SYSERR != |
1948 | GSN_database->iterate_records (GSN_database->cls, | 1974 | nc->GSN_database->iterate_records (nc->GSN_database->cls, |
1949 | (GNUNET_YES == GNUNET_is_zero ( | 1975 | (GNUNET_YES == |
1950 | &zi->zone)) | 1976 | GNUNET_is_zero ( |
1977 | &zi->zone)) | ||
1951 | ? NULL | 1978 | ? NULL |
1952 | : &zi->zone, | 1979 | : &zi->zone, |
1953 | zi->seq, | 1980 | zi->seq, |
1954 | limit, | 1981 | limit, |
1955 | &zone_iterate_proc, | 1982 | &zone_iterate_proc, |
1956 | &proc)); | 1983 | &proc)); |
1957 | duration = GNUNET_TIME_absolute_get_duration (start); | 1984 | duration = GNUNET_TIME_absolute_get_duration (start); |
1958 | duration = GNUNET_TIME_relative_divide (duration, limit - proc.limit); | 1985 | duration = GNUNET_TIME_relative_divide (duration, limit - proc.limit); |
1959 | GNUNET_STATISTICS_set (statistics, | 1986 | GNUNET_STATISTICS_set (statistics, |
@@ -2212,6 +2239,7 @@ static void | |||
2212 | monitor_iteration_next (void *cls) | 2239 | monitor_iteration_next (void *cls) |
2213 | { | 2240 | { |
2214 | struct ZoneMonitor *zm = cls; | 2241 | struct ZoneMonitor *zm = cls; |
2242 | struct NamestoreClient *nc = zm->nc; | ||
2215 | int ret; | 2243 | int ret; |
2216 | 2244 | ||
2217 | zm->task = NULL; | 2245 | zm->task = NULL; |
@@ -2220,15 +2248,13 @@ monitor_iteration_next (void *cls) | |||
2220 | zm->iteration_cnt = zm->limit / 2; /* leave half for monitor events */ | 2248 | zm->iteration_cnt = zm->limit / 2; /* leave half for monitor events */ |
2221 | else | 2249 | else |
2222 | zm->iteration_cnt = zm->limit; /* use it all */ | 2250 | zm->iteration_cnt = zm->limit; /* use it all */ |
2223 | ret = GSN_database->iterate_records (GSN_database->cls, | 2251 | ret = nc->GSN_database->iterate_records (nc->GSN_database->cls, |
2224 | (GNUNET_YES == GNUNET_is_zero ( | 2252 | (GNUNET_YES == GNUNET_is_zero ( |
2225 | &zm->zone)) | 2253 | &zm->zone)) ? NULL : &zm->zone, |
2226 | ? NULL | 2254 | zm->seq, |
2227 | : &zm->zone, | 2255 | zm->iteration_cnt, |
2228 | zm->seq, | 2256 | &monitor_iterate_cb, |
2229 | zm->iteration_cnt, | 2257 | zm); |
2230 | &monitor_iterate_cb, | ||
2231 | zm); | ||
2232 | if (GNUNET_SYSERR == ret) | 2258 | if (GNUNET_SYSERR == ret) |
2233 | { | 2259 | { |
2234 | GNUNET_SERVICE_client_drop (zm->nc->client); | 2260 | GNUNET_SERVICE_client_drop (zm->nc->client); |
@@ -2330,26 +2356,8 @@ run (void *cls, | |||
2330 | namecache = GNUNET_NAMECACHE_connect (cfg); | 2356 | namecache = GNUNET_NAMECACHE_connect (cfg); |
2331 | GNUNET_assert (NULL != namecache); | 2357 | GNUNET_assert (NULL != namecache); |
2332 | } | 2358 | } |
2333 | /* Loading database plugin */ | ||
2334 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, | ||
2335 | "namestore", | ||
2336 | "database", | ||
2337 | &database)) | ||
2338 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n"); | ||
2339 | |||
2340 | GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database); | ||
2341 | GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg); | ||
2342 | GNUNET_free (database); | ||
2343 | statistics = GNUNET_STATISTICS_create ("namestore", cfg); | 2359 | statistics = GNUNET_STATISTICS_create ("namestore", cfg); |
2344 | GNUNET_SCHEDULER_add_shutdown (&cleanup_task, NULL); | 2360 | GNUNET_SCHEDULER_add_shutdown (&cleanup_task, NULL); |
2345 | if (NULL == GSN_database) | ||
2346 | { | ||
2347 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
2348 | "Could not load database backend `%s'\n", | ||
2349 | db_lib_name); | ||
2350 | GNUNET_SCHEDULER_shutdown (); | ||
2351 | return; | ||
2352 | } | ||
2353 | } | 2361 | } |
2354 | 2362 | ||
2355 | 2363 | ||
diff --git a/src/namestore/plugin_namestore_flat.c b/src/namestore/plugin_namestore_flat.c index 3576b14e0..3feac60d8 100644 --- a/src/namestore/plugin_namestore_flat.c +++ b/src/namestore/plugin_namestore_flat.c | |||
@@ -767,19 +767,16 @@ namestore_flat_zone_to_name (void *cls, | |||
767 | void * | 767 | void * |
768 | libgnunet_plugin_namestore_flat_init (void *cls) | 768 | libgnunet_plugin_namestore_flat_init (void *cls) |
769 | { | 769 | { |
770 | static struct Plugin plugin; | 770 | struct Plugin *plugin; |
771 | const struct GNUNET_CONFIGURATION_Handle *cfg = cls; | 771 | const struct GNUNET_CONFIGURATION_Handle *cfg = cls; |
772 | struct GNUNET_NAMESTORE_PluginFunctions *api; | 772 | struct GNUNET_NAMESTORE_PluginFunctions *api; |
773 | 773 | ||
774 | if (NULL != plugin.cfg) | 774 | plugin = GNUNET_new (struct Plugin); |
775 | return NULL; /* can only initialize once! */ | 775 | plugin->cfg = cfg; |
776 | memset (&plugin, | 776 | if (GNUNET_OK != database_setup (plugin)) |
777 | 0, | ||
778 | sizeof(struct Plugin)); | ||
779 | plugin.cfg = cfg; | ||
780 | if (GNUNET_OK != database_setup (&plugin)) | ||
781 | { | 777 | { |
782 | database_shutdown (&plugin); | 778 | database_shutdown (plugin); |
779 | GNUNET_free (plugin); | ||
783 | return NULL; | 780 | return NULL; |
784 | } | 781 | } |
785 | api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions); | 782 | api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions); |
@@ -808,6 +805,7 @@ libgnunet_plugin_namestore_flat_done (void *cls) | |||
808 | 805 | ||
809 | database_shutdown (plugin); | 806 | database_shutdown (plugin); |
810 | plugin->cfg = NULL; | 807 | plugin->cfg = NULL; |
808 | GNUNET_free (plugin); | ||
811 | GNUNET_free (api); | 809 | GNUNET_free (api); |
812 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 810 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
813 | "Flat file plugin is finished\n"); | 811 | "Flat file plugin is finished\n"); |
diff --git a/src/namestore/plugin_namestore_postgres.c b/src/namestore/plugin_namestore_postgres.c index bdbaf96b3..54a19794f 100644 --- a/src/namestore/plugin_namestore_postgres.c +++ b/src/namestore/plugin_namestore_postgres.c | |||
@@ -577,21 +577,20 @@ database_shutdown (struct Plugin *plugin) | |||
577 | void * | 577 | void * |
578 | libgnunet_plugin_namestore_postgres_init (void *cls) | 578 | libgnunet_plugin_namestore_postgres_init (void *cls) |
579 | { | 579 | { |
580 | static struct Plugin plugin; | 580 | struct Plugin *plugin; |
581 | const struct GNUNET_CONFIGURATION_Handle *cfg = cls; | 581 | const struct GNUNET_CONFIGURATION_Handle *cfg = cls; |
582 | struct GNUNET_NAMESTORE_PluginFunctions *api; | 582 | struct GNUNET_NAMESTORE_PluginFunctions *api; |
583 | 583 | ||
584 | if (NULL != plugin.cfg) | 584 | plugin = GNUNET_new (struct Plugin); |
585 | return NULL; /* can only initialize once! */ | 585 | plugin->cfg = cfg; |
586 | memset (&plugin, 0, sizeof(struct Plugin)); | 586 | if (GNUNET_OK != database_setup (plugin)) |
587 | plugin.cfg = cfg; | ||
588 | if (GNUNET_OK != database_setup (&plugin)) | ||
589 | { | 587 | { |
590 | database_shutdown (&plugin); | 588 | database_shutdown (plugin); |
589 | GNUNET_free (plugin); | ||
591 | return NULL; | 590 | return NULL; |
592 | } | 591 | } |
593 | api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions); | 592 | api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions); |
594 | api->cls = &plugin; | 593 | api->cls = plugin; |
595 | api->store_records = &namestore_postgres_store_records; | 594 | api->store_records = &namestore_postgres_store_records; |
596 | api->iterate_records = &namestore_postgres_iterate_records; | 595 | api->iterate_records = &namestore_postgres_iterate_records; |
597 | api->zone_to_name = &namestore_postgres_zone_to_name; | 596 | api->zone_to_name = &namestore_postgres_zone_to_name; |
@@ -616,6 +615,7 @@ libgnunet_plugin_namestore_postgres_done (void *cls) | |||
616 | 615 | ||
617 | database_shutdown (plugin); | 616 | database_shutdown (plugin); |
618 | plugin->cfg = NULL; | 617 | plugin->cfg = NULL; |
618 | GNUNET_free (plugin); | ||
619 | GNUNET_free (api); | 619 | GNUNET_free (api); |
620 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 620 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
621 | "Postgres namestore plugin is finished\n"); | 621 | "Postgres namestore plugin is finished\n"); |
diff --git a/src/namestore/plugin_namestore_sqlite.c b/src/namestore/plugin_namestore_sqlite.c index f2017b695..fd81780fb 100644 --- a/src/namestore/plugin_namestore_sqlite.c +++ b/src/namestore/plugin_namestore_sqlite.c | |||
@@ -129,7 +129,7 @@ database_setup (struct Plugin *plugin) | |||
129 | GNUNET_SQ_make_try_execute ("PRAGMA legacy_file_format=OFF"), | 129 | GNUNET_SQ_make_try_execute ("PRAGMA legacy_file_format=OFF"), |
130 | GNUNET_SQ_make_try_execute ("PRAGMA auto_vacuum=INCREMENTAL"), | 130 | GNUNET_SQ_make_try_execute ("PRAGMA auto_vacuum=INCREMENTAL"), |
131 | GNUNET_SQ_make_try_execute ("PRAGMA encoding=\"UTF-8\""), | 131 | GNUNET_SQ_make_try_execute ("PRAGMA encoding=\"UTF-8\""), |
132 | GNUNET_SQ_make_try_execute ("PRAGMA locking_mode=EXCLUSIVE"), | 132 | GNUNET_SQ_make_try_execute ("PRAGMA locking_mode=NORMAL"), |
133 | GNUNET_SQ_make_try_execute ("PRAGMA journal_mode=WAL"), | 133 | GNUNET_SQ_make_try_execute ("PRAGMA journal_mode=WAL"), |
134 | GNUNET_SQ_make_try_execute ("PRAGMA page_size=4092"), | 134 | GNUNET_SQ_make_try_execute ("PRAGMA page_size=4092"), |
135 | GNUNET_SQ_make_execute ("CREATE TABLE IF NOT EXISTS ns098records (" | 135 | GNUNET_SQ_make_execute ("CREATE TABLE IF NOT EXISTS ns098records (" |
@@ -201,17 +201,16 @@ database_setup (struct Plugin *plugin) | |||
201 | return GNUNET_SYSERR; | 201 | return GNUNET_SYSERR; |
202 | } | 202 | } |
203 | } | 203 | } |
204 | /* sqlite_filename should be UTF-8-encoded. If it isn't, it's a bug */ | ||
205 | plugin->fn = sqlite_filename; | ||
206 | 204 | ||
207 | /* Open database and precompile statements */ | 205 | /* Open database and precompile statements */ |
208 | if (SQLITE_OK != | 206 | if (SQLITE_OK != |
209 | sqlite3_open (plugin->fn, | 207 | sqlite3_open (sqlite_filename, |
210 | &plugin->dbh)) | 208 | &plugin->dbh)) |
211 | { | 209 | { |
212 | LOG (GNUNET_ERROR_TYPE_ERROR, | 210 | LOG (GNUNET_ERROR_TYPE_ERROR, |
213 | _ ("Unable to initialize SQLite: %s.\n"), | 211 | _ ("Unable to initialize SQLite: %s.\n"), |
214 | sqlite3_errmsg (plugin->dbh)); | 212 | sqlite3_errmsg (plugin->dbh)); |
213 | GNUNET_free (sqlite_filename); | ||
215 | return GNUNET_SYSERR; | 214 | return GNUNET_SYSERR; |
216 | } | 215 | } |
217 | GNUNET_break (SQLITE_OK == | 216 | GNUNET_break (SQLITE_OK == |
@@ -224,7 +223,8 @@ database_setup (struct Plugin *plugin) | |||
224 | GNUNET_break (0); | 223 | GNUNET_break (0); |
225 | LOG (GNUNET_ERROR_TYPE_ERROR, | 224 | LOG (GNUNET_ERROR_TYPE_ERROR, |
226 | _ ("Failed to setup database at `%s'\n"), | 225 | _ ("Failed to setup database at `%s'\n"), |
227 | plugin->fn); | 226 | sqlite_filename); |
227 | GNUNET_free (sqlite_filename); | ||
228 | return GNUNET_SYSERR; | 228 | return GNUNET_SYSERR; |
229 | } | 229 | } |
230 | 230 | ||
@@ -235,7 +235,8 @@ database_setup (struct Plugin *plugin) | |||
235 | GNUNET_break (0); | 235 | GNUNET_break (0); |
236 | LOG (GNUNET_ERROR_TYPE_ERROR, | 236 | LOG (GNUNET_ERROR_TYPE_ERROR, |
237 | _ ("Failed to setup database at `%s'\n"), | 237 | _ ("Failed to setup database at `%s'\n"), |
238 | plugin->fn); | 238 | sqlite_filename); |
239 | GNUNET_free (sqlite_filename); | ||
239 | return GNUNET_SYSERR; | 240 | return GNUNET_SYSERR; |
240 | } | 241 | } |
241 | return GNUNET_OK; | 242 | return GNUNET_OK; |
@@ -296,7 +297,6 @@ database_shutdown (struct Plugin *plugin) | |||
296 | GNUNET_ERROR_TYPE_ERROR, | 297 | GNUNET_ERROR_TYPE_ERROR, |
297 | "sqlite3_close"); | 298 | "sqlite3_close"); |
298 | 299 | ||
299 | GNUNET_free (plugin->fn); | ||
300 | } | 300 | } |
301 | 301 | ||
302 | 302 | ||
@@ -802,23 +802,20 @@ namestore_sqlite_transaction_commit (void *cls, | |||
802 | void * | 802 | void * |
803 | libgnunet_plugin_namestore_sqlite_init (void *cls) | 803 | libgnunet_plugin_namestore_sqlite_init (void *cls) |
804 | { | 804 | { |
805 | static struct Plugin plugin; | 805 | struct Plugin *plugin; |
806 | const struct GNUNET_CONFIGURATION_Handle *cfg = cls; | 806 | const struct GNUNET_CONFIGURATION_Handle *cfg = cls; |
807 | struct GNUNET_NAMESTORE_PluginFunctions *api; | 807 | struct GNUNET_NAMESTORE_PluginFunctions *api; |
808 | 808 | ||
809 | if (NULL != plugin.cfg) | 809 | plugin = GNUNET_new (struct Plugin); |
810 | return NULL; /* can only initialize once! */ | 810 | plugin->cfg = cfg; |
811 | memset (&plugin, | 811 | if (GNUNET_OK != database_setup (plugin)) |
812 | 0, | ||
813 | sizeof(struct Plugin)); | ||
814 | plugin.cfg = cfg; | ||
815 | if (GNUNET_OK != database_setup (&plugin)) | ||
816 | { | 812 | { |
817 | database_shutdown (&plugin); | 813 | database_shutdown (plugin); |
814 | GNUNET_free (plugin); | ||
818 | return NULL; | 815 | return NULL; |
819 | } | 816 | } |
820 | api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions); | 817 | api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions); |
821 | api->cls = &plugin; | 818 | api->cls = plugin; |
822 | api->store_records = &namestore_sqlite_store_records; | 819 | api->store_records = &namestore_sqlite_store_records; |
823 | api->iterate_records = &namestore_sqlite_iterate_records; | 820 | api->iterate_records = &namestore_sqlite_iterate_records; |
824 | api->zone_to_name = &namestore_sqlite_zone_to_name; | 821 | api->zone_to_name = &namestore_sqlite_zone_to_name; |
@@ -846,6 +843,7 @@ libgnunet_plugin_namestore_sqlite_done (void *cls) | |||
846 | 843 | ||
847 | database_shutdown (plugin); | 844 | database_shutdown (plugin); |
848 | plugin->cfg = NULL; | 845 | plugin->cfg = NULL; |
846 | GNUNET_free (plugin); | ||
849 | GNUNET_free (api); | 847 | GNUNET_free (api); |
850 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 848 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
851 | "sqlite plugin is finished\n"); | 849 | "sqlite plugin is finished\n"); |