GNUnet 0.28.0-dev.3-7-g31e20e2e6
 
Loading...
Searching...
No Matches
pq_connect.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet
3 Copyright (C) 2017, 2019, 2020, 2021, 2023 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
26#include "platform.h"
27#include "pq.h"
28#include <pthread.h>
29
30
36static void
38{
39 if (NULL == db->conn)
40 return;
41 PQfinish (db->conn);
42 db->conn = NULL;
43 db->prepared_check_patch = false;
44 db->prepared_get_oid_by_name = false;
45}
46
47
57{
58 PGresult *res;
59
60 if (db->prepared_check_patch)
61 return GNUNET_OK;
62 res = PQprepare (db->conn,
63 "gnunet_pq_check_patch",
64 "SELECT"
65 " applied_by"
66 " FROM _v.patches"
67 " WHERE patch_name = $1"
68 " LIMIT 1",
69 1,
70 NULL);
71 if (PGRES_COMMAND_OK !=
72 PQresultStatus (res))
73 {
75 "Failed to run SQL logic to setup database versioning logic: %s/%s\n",
76 PQresultErrorMessage (res),
77 PQerrorMessage (db->conn));
78 PQclear (res);
80 return GNUNET_SYSERR;
81 }
82 PQclear (res);
83 db->prepared_check_patch = true;
84 return GNUNET_OK;
85}
86
87
97{
98 PGresult *res;
99
100 if (db->prepared_get_oid_by_name)
101 return GNUNET_OK;
102 res = PQprepare (db->conn,
103 "gnunet_pq_get_oid_by_name",
104 "SELECT typname, oid"
105 " FROM pg_type"
106 " WHERE oid = to_regtype($1)",
107 1,
108 NULL);
109 if (PGRES_COMMAND_OK != PQresultStatus (res))
110 {
112 "Failed to run SQL statement prepare OID lookups: %s/%s\n",
113 PQresultErrorMessage (res),
114 PQerrorMessage (db->conn));
115 PQclear (res);
117 return GNUNET_SYSERR;
118 }
119 PQclear (res);
120 db->prepared_get_oid_by_name = true;
121 return GNUNET_OK;
122}
123
124
136{
137 PGresult *res;
138 ExecStatusType est;
139
140 if (GNUNET_OK == db->versioning_ok)
141 return GNUNET_OK;
142 if (GNUNET_NO == db->versioning_ok)
143 return GNUNET_NO;
144 res = PQexec (db->conn,
145 "SELECT"
146 " schema_name"
147 " FROM information_schema.schemata"
148 " WHERE schema_name='_v';");
149 est = PQresultStatus (res);
150 if ( (PGRES_COMMAND_OK != est) &&
151 (PGRES_TUPLES_OK != est) )
152 {
154 "Failed to run statement to check versioning schema. Bad!\n");
155 PQclear (res);
156 return GNUNET_SYSERR;
157 }
158 if (0 == PQntuples (res))
159 {
160 PQclear (res);
161 db->versioning_ok = GNUNET_NO;
163 "_v schema not found\n");
164 return GNUNET_NO;
165 }
166 PQclear (res);
167 db->versioning_ok = GNUNET_OK;
168 return GNUNET_OK;
169}
170
171
185 const char *load_path,
186 unsigned int patch_number)
187{
188 const char *load_path_suffix;
189 size_t slen = strlen (load_path) + 10;
190 char patch_name[slen];
191
192 if (GNUNET_SYSERR ==
194 {
195 GNUNET_break (0);
196 return GNUNET_SYSERR; /* no versioning, cannot check */
197 }
198 load_path_suffix = strrchr (load_path,
199 '/');
200 if (NULL == load_path_suffix)
201 load_path_suffix = load_path;
202 else
203 load_path_suffix++; /* skip '/' */
204 GNUNET_snprintf (patch_name,
205 sizeof (patch_name),
206 "%s%04u",
207 load_path_suffix,
208 patch_number);
209 {
210 struct GNUNET_PQ_QueryParam params[] = {
211 GNUNET_PQ_query_param_string (patch_name),
213 };
214 char *applied_by;
215 struct GNUNET_PQ_ResultSpec rs[] = {
216 GNUNET_PQ_result_spec_string ("applied_by",
217 &applied_by),
219 };
220 enum GNUNET_DB_QueryStatus qs;
221
222 if (GNUNET_OK !=
224 {
225 GNUNET_break (0);
226 return GNUNET_SYSERR;
227 }
229 "gnunet_pq_check_patch",
230 params,
231 rs);
232 switch (qs)
233 {
236 "Database version %s already applied by %s\n",
237 patch_name,
238 applied_by);
240 return GNUNET_OK;
242 return GNUNET_NO;
244 GNUNET_break (0);
245 return GNUNET_SYSERR;
247 GNUNET_break (0);
248 return GNUNET_SYSERR;
249 }
250 GNUNET_assert (0);
251 return GNUNET_SYSERR;
252 }
253}
254
255
264static void
266 const PGresult *res)
267{
268 /* do nothing, intentionally */
269 (void) arg;
270 (void) res;
271}
272
273
281static void
283 const char *message)
284{
285 (void) arg;
287 "pq",
288 "%s",
289 message);
290}
291
292
295 const char *buf)
296{
297 struct GNUNET_Process *psql;
299 unsigned long code;
300 char *fn;
301
302 GNUNET_asprintf (&fn,
303 "%s%s.sql",
304 db->load_path,
305 buf);
306 if (GNUNET_YES !=
308 {
309 GNUNET_free (fn);
310 return GNUNET_NO;
311 }
313 "Applying SQL file `%s' on database %s\n",
314 fn,
315 db->config_str);
317 if (GNUNET_OK !=
319 "psql",
320 "psql",
321 db->config_str,
322 "-f",
323 fn,
324 "-q",
325 "--set",
326 "ON_ERROR_STOP=1",
327 NULL))
328 {
330 "exec",
331 "psql");
333 GNUNET_free (fn);
334 return GNUNET_SYSERR;
335 }
338 true,
339 &type,
340 &code));
342 if ( (GNUNET_OS_PROCESS_EXITED != type) ||
343 (0 != code) )
344 {
346 "Could not run PSQL on file %s: psql exit code was %d\n",
347 fn,
348 (int) code);
349 GNUNET_free (fn);
350 return GNUNET_SYSERR;
351 }
352 GNUNET_free (fn);
353 return GNUNET_OK;
354}
355
356
359 const char *load_suffix)
360{
361 char *fn;
362
364 "Loading SQL resources from `%s'\n",
365 load_suffix);
366 for (unsigned int i = 1; i<10000; i++)
367 {
369
371 load_suffix,
372 i);
373 if (GNUNET_SYSERR == ret)
374 {
375 GNUNET_break (0);
376 return GNUNET_SYSERR;
377 }
378 if (GNUNET_OK == ret)
379 continue; /* patch already applied, skip it */
380 /* patch not applied, check if it exists... */
381 GNUNET_asprintf (&fn,
382 "%s%s%04u",
383 db->load_path,
384 load_suffix,
385 i);
387 GNUNET_free (fn);
388 if (GNUNET_YES == ret)
389 return GNUNET_NO;
390 return GNUNET_OK;
391 }
392 GNUNET_break (0); /* 10k patches applied!? */
393 return GNUNET_OK;
394}
395
396
399 const char *load_suffix)
400{
401 size_t slen = strlen (load_suffix) + 10;
402 char patch_name[slen];
403
405 "Loading SQL resources from `%s'\n",
406 load_suffix);
407 for (unsigned int i = 1; i<10000; i++)
408 {
410
412 load_suffix,
413 i);
414 if (GNUNET_SYSERR == ret)
415 {
416 GNUNET_break (0);
417 return GNUNET_SYSERR;
418 }
419 if (GNUNET_OK == ret)
420 continue; /* patch already applied, skip it */
421
422 GNUNET_snprintf (patch_name,
423 sizeof (patch_name),
424 "%s%04u",
425 load_suffix,
426 i);
428 patch_name);
429 if (GNUNET_NO == ret)
430 break; /* no such file, we are done */
431 if (GNUNET_SYSERR == ret)
432 return GNUNET_SYSERR;
433 }
434 return GNUNET_OK;
435}
436
437
438void
440{
441 if (1 ==
442 PQconsumeInput (db->conn))
443 return;
444 if (CONNECTION_BAD != PQstatus (db->conn))
445 return;
447}
448
449
452 struct GNUNET_PQ_Context *db,
453 const char *name,
454 Oid *oid)
455{
456 /* Check if the entry is in the cache already */
457 for (unsigned int i = 0; i < db->oids.num; i++)
458 {
459 /* Pointer comparison */
460 if (name == db->oids.table[i].name)
461 {
462 *oid = db->oids.table[i].oid;
463 return GNUNET_OK;
464 }
465 }
466
467 /* No entry found in cache, ask database */
468 {
469 enum GNUNET_DB_QueryStatus qs;
470 struct GNUNET_PQ_QueryParam params[] = {
473 };
474 struct GNUNET_PQ_ResultSpec spec[] = {
476 oid),
478 };
479
480 GNUNET_assert (NULL != db);
481
483 "gnunet_pq_get_oid_by_name",
484 params,
485 spec);
487 return GNUNET_SYSERR;
488 }
489
490 /* Add the entry to the cache */
491 if (NULL == db->oids.table)
492 {
493 db->oids.table = GNUNET_new_array (8,
494 typeof(*db->oids.table));
495 db->oids.cap = 8;
496 db->oids.num = 0;
497 }
498
499 if (db->oids.cap <= db->oids.num)
500 GNUNET_array_grow (db->oids.table,
501 db->oids.cap,
502 db->oids.cap + 8);
503
504 db->oids.table[db->oids.num].name = name;
505 db->oids.table[db->oids.num].oid = *oid;
506 db->oids.num++;
507
508 return GNUNET_OK;
509}
510
511
522{
523 static const char *typnames[] = {
524 "bool",
525 "int2",
526 "int4",
527 "int8",
528 "bytea",
529 "varchar"
530 };
531 Oid oid;
532
533 for (size_t i = 0; i< sizeof(typnames) / sizeof(*typnames); i++)
534 {
535 if (GNUNET_OK !=
537 typnames[i],
538 &oid))
539 {
541 "pq",
542 "Couldn't retrieve OID for type %s\n",
543 typnames[i]);
544 return GNUNET_SYSERR;
545 }
546 }
547 return GNUNET_OK;
548}
549
550
551void
553{
555 -1);
557 db->versioning_ok = GNUNET_SYSERR; /* new connection, new game */
558 db->conn = PQconnectdb (db->config_str);
559 if ( (NULL == db->conn) ||
560 (CONNECTION_OK != PQstatus (db->conn)) )
561 {
563 "pq",
564 "Database connection to '%s' failed: %s\n",
565 db->config_str,
566 (NULL != db->conn)
567 ? PQerrorMessage (db->conn)
568 : "PQconnectdb returned NULL");
570 return;
571 }
572 PQsetNoticeReceiver (db->conn,
574 db);
575 PQsetNoticeProcessor (db->conn,
577 db);
578 if (NULL != db->rc)
579 db->rc (db->rc_cls,
580 db);
581
582 /* Prepare statement for OID lookup by name */
583 if (GNUNET_OK !=
585 return;
586
587 /* Reset the OID-cache and retrieve the OIDs for the supported Array types */
588 db->oids.num = 0;
590 {
592 "Failed to retrieve OID information for array types!\n");
594 return;
595 }
597 PQsocket (db->conn));
598}
599
600
603{
605
607 if (GNUNET_SYSERR == ret)
608 return GNUNET_SYSERR;
609 if (GNUNET_YES == ret)
610 return GNUNET_NO; /* already setup */
612 "versioning");
613 if (GNUNET_NO == ret)
614 {
616 "Failed to find SQL file to load database versioning logic\n");
617 return GNUNET_SYSERR;
618 }
619 if (GNUNET_SYSERR == ret)
620 {
622 "Failed to run SQL logic to setup database versioning logic\n");
623 return GNUNET_SYSERR;
624 }
625 db->versioning_ok = GNUNET_YES;
626 return GNUNET_OK;
627}
628
629
630struct GNUNET_PQ_Context *
632 const char *section,
635{
636 struct GNUNET_PQ_Context *db;
637 char *conninfo;
638 char *load_path;
639
640 if (GNUNET_OK !=
642 section,
643 "CONFIG",
644 &conninfo))
645 conninfo = GNUNET_strdup ("");
646 load_path = NULL;
647 if (GNUNET_OK !=
649 section,
650 "SQL_DIR",
651 &load_path))
652 {
654 section,
655 "SQL_DIR");
656 }
658 db->versioning_ok = GNUNET_SYSERR;
659 db->config_str = conninfo;
660 db->load_path = load_path;
661 db->rc = rc;
662 db->rc_cls = rc_cls;
663 db->channel_map = GNUNET_CONTAINER_multishortmap_create (16,
664 true);
666 if (NULL == db->conn)
667 {
669 GNUNET_free (db->config_str);
670 GNUNET_free (db);
671 return NULL;
672 }
673 return db;
674}
675
676
677void
679{
680 if (NULL == db)
681 return;
682 GNUNET_assert (0 ==
685 if (NULL != db->poller_task)
686 {
687 GNUNET_SCHEDULER_cancel (db->poller_task);
688 db->poller_task = NULL;
689 }
690 GNUNET_free (db->load_path);
691 GNUNET_free (db->config_str);
692 GNUNET_free (db->oids.table);
693 db->oids.num = 0;
694 db->oids.cap = 0;
695 PQfinish (db->conn);
696 GNUNET_free (db);
697}
698
699
700/* end of pq/pq_connect.c */
static int ret
Final status code.
Definition gnunet-arm.c:93
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition gnunet-arm.c:108
static char * name
Name (label) of the records to list.
static char * res
Currently read line or NULL on EOF.
static uint32_t type
Type string converted to DNS type value.
static struct GNUNET_FS_DirectoryBuilder * db
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
@ GNUNET_DB_STATUS_SUCCESS_ONE_RESULT
The transaction succeeded, and yielded one result.
@ GNUNET_DB_STATUS_HARD_ERROR
A hard error occurred, retrying will not help.
@ GNUNET_DB_STATUS_SUCCESS_NO_RESULTS
The transaction succeeded, but yielded zero results.
@ GNUNET_DB_STATUS_SOFT_ERROR
A soft error occurred, retrying the transaction may succeed.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint32(const char *name, uint32_t *u32)
uint32_t expected.
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_singleton_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params, struct GNUNET_PQ_ResultSpec *rs)
Execute a named prepared statement that is a SELECT statement which must return a single result in co...
Definition pq_eval.c:199
void(* GNUNET_PQ_ReconnectCallback)(void *cls, struct GNUNET_PQ_Context *pq)
Function called each time we connect or reconnect to the database.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_string(const char *name, char **dst)
0-terminated string expected.
#define GNUNET_PQ_query_param_end
End of query parameter specification.
void GNUNET_PQ_cleanup_result(struct GNUNET_PQ_ResultSpec *rs)
Free all memory that was allocated in rs during GNUNET_PQ_extract_result().
Definition pq.c:142
uint32_t oid
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_string(const char *ptr)
Generate query parameter for a string.
#define GNUNET_PQ_result_spec_end
End of result parameter specification.
#define GNUNET_PQ_RECONNECT_CALLBACK_CLOSURE
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
enum GNUNET_GenericReturnValue GNUNET_DISK_file_test_read(const char *fil)
Check that fil corresponds to a filename and the file has read permissions.
Definition disk.c:565
struct GNUNET_CONTAINER_MultiShortmap * GNUNET_CONTAINER_multishortmap_create(unsigned int len, int do_not_copy_keys)
Create a multi peer map (hash map for public keys of peers).
void GNUNET_CONTAINER_multishortmap_destroy(struct GNUNET_CONTAINER_MultiShortmap *map)
Destroy a hash map.
unsigned int GNUNET_CONTAINER_multishortmap_size(const struct GNUNET_CONTAINER_MultiShortmap *map)
Get the number of key-value pairs in the map.
#define GNUNET_log(kind,...)
#define GNUNET_log_from(kind, comp,...)
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
@ GNUNET_ERROR_TYPE_INFO
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_array_grow(arr, size, tsize)
Grow a well-typed (!) array.
int GNUNET_snprintf(char *buf, size_t size, const char *format,...) __attribute__((format(printf
Like snprintf, just aborts if the buffer is of insufficient size.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
enum GNUNET_GenericReturnValue GNUNET_process_run_command_va(struct GNUNET_Process *p, const char *filename,...)
Set the command and start a process.
Definition os_process.c:903
enum GNUNET_GenericReturnValue GNUNET_process_wait(struct GNUNET_Process *proc, bool blocking, enum GNUNET_OS_ProcessStatusType *type, unsigned long *code)
Wait for a process to terminate.
void GNUNET_process_destroy(struct GNUNET_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition os_process.c:363
GNUNET_OS_ProcessStatusType
Process status types.
struct GNUNET_Process * GNUNET_process_create(enum GNUNET_OS_InheritStdioFlags std_inheritance)
Create a process handle.
Definition os_process.c:462
@ GNUNET_OS_INHERIT_STD_NONE
No standard streams should be inherited.
@ GNUNET_OS_PROCESS_EXITED
The process exited with a return code.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition scheduler.c:986
shared internal data structures of libgnunetpq
void GNUNET_PQ_event_reconnect_(struct GNUNET_PQ_Context *db, int fd)
Internal API.
Definition pq_event.c:434
static enum GNUNET_GenericReturnValue check_patch_applied(struct GNUNET_PQ_Context *db, const char *load_path, unsigned int patch_number)
Check if the patch with patch_number from the given load_path was already applied on the db.
Definition pq_connect.c:184
static enum GNUNET_GenericReturnValue prepare_check_patch(struct GNUNET_PQ_Context *db)
Prepare the "gnunet_pq_check_patch" statement.
Definition pq_connect.c:56
enum GNUNET_GenericReturnValue GNUNET_PQ_check_current(struct GNUNET_PQ_Context *db, const char *load_suffix)
Check if the database is current with respect to database migrations using prefix.
Definition pq_connect.c:358
static void pq_notice_receiver_cb(void *arg, const PGresult *res)
Function called by libpq whenever it wants to log something.
Definition pq_connect.c:265
static void reset_connection(struct GNUNET_PQ_Context *db)
Close connection to db and mark it as uninitialized.
Definition pq_connect.c:37
static enum GNUNET_GenericReturnValue prepare_get_oid_by_name(struct GNUNET_PQ_Context *db)
Prepare the "gnunet_pq_get_oid_by_name" statement.
Definition pq_connect.c:96
void GNUNET_PQ_reconnect_if_down(struct GNUNET_PQ_Context *db)
Reinitialize the database db if the connection is down.
Definition pq_connect.c:439
void GNUNET_PQ_disconnect(struct GNUNET_PQ_Context *db)
Disconnect from the database, destroying the prepared statements and releasing other associated resou...
Definition pq_connect.c:678
struct GNUNET_PQ_Context * GNUNET_PQ_init(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, GNUNET_PQ_ReconnectCallback rc, GNUNET_PQ_RECONNECT_CALLBACK_CLOSURE *rc_cls)
Definition pq_connect.c:631
static enum GNUNET_GenericReturnValue check_versioning_ok(struct GNUNET_PQ_Context *db)
Check if the "_v" versioning schema exists (and cache the result in db).
Definition pq_connect.c:135
void GNUNET_PQ_reconnect_(struct GNUNET_PQ_Context *db)
Reinitialize the database db.
Definition pq_connect.c:552
enum GNUNET_GenericReturnValue GNUNET_PQ_load_versioning(struct GNUNET_PQ_Context *db)
Setup database versioning.
Definition pq_connect.c:602
static void pq_notice_processor_cb(void *arg, const char *message)
Function called by libpq whenever it wants to log something.
Definition pq_connect.c:282
enum GNUNET_GenericReturnValue GNUNET_PQ_get_oid_by_name(struct GNUNET_PQ_Context *db, const char *name, Oid *oid)
Returns the oid for a given datatype by name.
Definition pq_connect.c:451
enum GNUNET_GenericReturnValue GNUNET_PQ_exec_sql(struct GNUNET_PQ_Context *db, const char *buf)
Execute SQL statements from buf against db.
Definition pq_connect.c:294
static enum GNUNET_GenericReturnValue load_initial_oids(struct GNUNET_PQ_Context *db)
Load the initial set of OIDs for the supported array-datatypes.
Definition pq_connect.c:521
enum GNUNET_GenericReturnValue GNUNET_PQ_run_sql(struct GNUNET_PQ_Context *db, const char *load_suffix)
Within the db context, run all the SQL files in the load path where the name starts with the load_suf...
Definition pq_connect.c:398
Handle to Postgres database.
Definition pq.h:36
GNUNET_PQ_ReconnectCallback rc
Function to call whenever we needed to reconnect conn.
Definition pq.h:45
char * load_path
Path to load SQL files from.
Definition pq.h:60
GNUNET_PQ_RECONNECT_CALLBACK_CLOSURE * rc_cls
Closure for rc.
Definition pq.h:50
Description of a DB query parameter.
Description of a DB result cell.