GNUnet debian-0.26.1-1-g854fad2e0
 
Loading...
Searching...
No Matches
pq_connect.c File Reference

functions to connect to libpq (PostGres) More...

#include "platform.h"
#include "pq.h"
#include <pthread.h>
Include dependency graph for pq_connect.c:

Go to the source code of this file.

Functions

static void reset_connection (struct GNUNET_PQ_Context *db)
 Close connection to db and mark it as uninitialized.
 
static enum GNUNET_GenericReturnValue prepare_check_patch (struct GNUNET_PQ_Context *db)
 Prepare the "gnunet_pq_check_patch" statement.
 
static enum GNUNET_GenericReturnValue prepare_get_oid_by_name (struct GNUNET_PQ_Context *db)
 Prepare the "gnunet_pq_get_oid_by_name" statement.
 
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.
 
static void pq_notice_receiver_cb (void *arg, const PGresult *res)
 Function called by libpq whenever it wants to log something.
 
static void pq_notice_processor_cb (void *arg, const char *message)
 Function called by libpq whenever it wants to log something.
 
struct GNUNET_PQ_ContextGNUNET_PQ_connect (const char *config_str, const char *load_path, const struct GNUNET_PQ_ExecuteStatement *es, const struct GNUNET_PQ_PreparedStatement *ps)
 Create a connection to the Postgres database using config_str for the configuration.
 
struct GNUNET_PQ_ContextGNUNET_PQ_connect2 (const char *config_str, const char *load_path, const char *auto_suffix, const struct GNUNET_PQ_ExecuteStatement *es, const struct GNUNET_PQ_PreparedStatement *ps, enum GNUNET_PQ_Options flags)
 Create a connection to the Postgres database using config_str for the configuration.
 
enum GNUNET_GenericReturnValue GNUNET_PQ_exec_sql (struct GNUNET_PQ_Context *db, const char *buf)
 Execute SQL statements from buf against db.
 
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_suffix and ends with consecutive numbers from "[0000-9999].sql".
 
void GNUNET_PQ_reconnect_if_down (struct GNUNET_PQ_Context *db)
 Reinitialize the database db if the connection is down.
 
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.
 
static enum GNUNET_GenericReturnValue load_initial_oids (struct GNUNET_PQ_Context *db)
 Load the initial set of OIDs for the supported array-datatypes.
 
void GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db)
 Reinitialize the database db.
 
struct GNUNET_PQ_ContextGNUNET_PQ_connect_with_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *load_path_suffix, const struct GNUNET_PQ_ExecuteStatement *es, const struct GNUNET_PQ_PreparedStatement *ps)
 Connect to a postgres database using the configuration option "CONFIG" in section.
 
struct GNUNET_PQ_ContextGNUNET_PQ_connect_with_cfg2 (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *load_path_suffix, const struct GNUNET_PQ_ExecuteStatement *es, const struct GNUNET_PQ_PreparedStatement *ps, enum GNUNET_PQ_Options flags)
 Connect to a postgres database using the configuration option "CONFIG" in section.
 
void GNUNET_PQ_disconnect (struct GNUNET_PQ_Context *db)
 Disconnect from the database, destroying the prepared statements and releasing other associated resources.
 

Detailed Description

functions to connect to libpq (PostGres)

Author
Christian Grothoff
Özgür Kesim

Definition in file pq_connect.c.

Function Documentation

◆ reset_connection()

static void reset_connection ( struct GNUNET_PQ_Context db)
static

Close connection to db and mark it as uninitialized.

Parameters
[in,out]dbconnection to close

Definition at line 37 of file pq_connect.c.

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}
static struct GNUNET_FS_DirectoryBuilder * db

References db.

Referenced by GNUNET_PQ_reconnect(), prepare_check_patch(), and prepare_get_oid_by_name().

Here is the caller graph for this function:

◆ prepare_check_patch()

static enum GNUNET_GenericReturnValue prepare_check_patch ( struct GNUNET_PQ_Context db)
static

Prepare the "gnunet_pq_check_patch" statement.

Parameters
[in,out]dbdatabase to prepare statement for
Returns
GNUNET_OK on success, GNUNET_SYSERR on failure

Definition at line 56 of file pq_connect.c.

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}
static char * res
Currently read line or NULL on EOF.
#define GNUNET_log(kind,...)
@ GNUNET_OK
@ GNUNET_SYSERR
@ GNUNET_ERROR_TYPE_ERROR
static void reset_connection(struct GNUNET_PQ_Context *db)
Close connection to db and mark it as uninitialized.
Definition pq_connect.c:37

References db, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_OK, GNUNET_SYSERR, res, and reset_connection().

Referenced by check_patch_applied(), and GNUNET_PQ_reconnect().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ prepare_get_oid_by_name()

static enum GNUNET_GenericReturnValue prepare_get_oid_by_name ( struct GNUNET_PQ_Context db)
static

Prepare the "gnunet_pq_get_oid_by_name" statement.

Parameters
[in,out]dbdatabase to prepare statement for
Returns
GNUNET_OK on success, GNUNET_SYSERR on failure

Definition at line 96 of file pq_connect.c.

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}

References db, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_OK, GNUNET_SYSERR, res, and reset_connection().

Referenced by GNUNET_PQ_reconnect().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_patch_applied()

static enum GNUNET_GenericReturnValue check_patch_applied ( struct GNUNET_PQ_Context db,
const char *  load_path,
unsigned int  patch_number 
)
static

Check if the patch with patch_number from the given load_path was already applied on the db.

Parameters
[in]dbdatabase to check
load_pathfile system path to database setup files
patch_numbernumber of the patch to check
Returns
GNUNET_OK if patch is applied GNUNET_NO if patch is not applied GNUNET_SYSERR on internal error (DB failure)

Definition at line 137 of file pq_connect.c.

140{
141 const char *load_path_suffix;
142 size_t slen = strlen (load_path) + 10;
143 char patch_name[slen];
144
145 load_path_suffix = strrchr (load_path,
146 '/');
147 if (NULL == load_path_suffix)
148 load_path_suffix = load_path;
149 else
150 load_path_suffix++; /* skip '/' */
151 GNUNET_snprintf (patch_name,
152 sizeof (patch_name),
153 "%s%04u",
154 load_path_suffix,
155 patch_number);
156 {
157 struct GNUNET_PQ_QueryParam params[] = {
158 GNUNET_PQ_query_param_string (patch_name),
160 };
161 char *applied_by;
162 struct GNUNET_PQ_ResultSpec rs[] = {
163 GNUNET_PQ_result_spec_string ("applied_by",
164 &applied_by),
166 };
167 enum GNUNET_DB_QueryStatus qs;
168
169 if (GNUNET_OK !=
171 {
172 GNUNET_break (0);
173 return GNUNET_SYSERR;
174 }
176 "gnunet_pq_check_patch",
177 params,
178 rs);
179 switch (qs)
180 {
183 "Database version %s already applied by %s\n",
184 patch_name,
185 applied_by);
187 return GNUNET_OK;
189 return GNUNET_NO;
191 GNUNET_break (0);
192 return GNUNET_SYSERR;
194 GNUNET_break (0);
195 return GNUNET_SYSERR;
196 }
197 GNUNET_assert (0);
198 return GNUNET_SYSERR;
199 }
200}
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.
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
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
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.
@ GNUNET_NO
#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.
@ GNUNET_ERROR_TYPE_DEBUG
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.
static enum GNUNET_GenericReturnValue prepare_check_patch(struct GNUNET_PQ_Context *db)
Prepare the "gnunet_pq_check_patch" statement.
Definition pq_connect.c:56
Description of a DB query parameter.
Description of a DB result cell.

References db, GNUNET_assert, GNUNET_break, GNUNET_DB_STATUS_HARD_ERROR, GNUNET_DB_STATUS_SOFT_ERROR, GNUNET_DB_STATUS_SUCCESS_NO_RESULTS, GNUNET_DB_STATUS_SUCCESS_ONE_RESULT, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_NO, GNUNET_OK, GNUNET_PQ_cleanup_result(), GNUNET_PQ_eval_prepared_singleton_select(), GNUNET_PQ_query_param_end, GNUNET_PQ_query_param_string(), GNUNET_PQ_result_spec_end, GNUNET_PQ_result_spec_string(), GNUNET_snprintf(), GNUNET_SYSERR, and prepare_check_patch().

Referenced by GNUNET_PQ_run_sql().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pq_notice_receiver_cb()

static void pq_notice_receiver_cb ( void *  arg,
const PGresult *  res 
)
static

Function called by libpq whenever it wants to log something.

We already log whenever we care, so this function does nothing and merely exists to silence the libpq logging.

Parameters
argthe SQL connection that was used
resinformation about some libpq event

Definition at line 212 of file pq_connect.c.

214{
215 /* do nothing, intentionally */
216 (void) arg;
217 (void) res;
218}

References res.

Referenced by GNUNET_PQ_reconnect().

Here is the caller graph for this function:

◆ pq_notice_processor_cb()

static void pq_notice_processor_cb ( void *  arg,
const char *  message 
)
static

Function called by libpq whenever it wants to log something.

We log those using the GNUnet logger.

Parameters
argthe SQL connection that was used
messageinformation about some libpq event

Definition at line 229 of file pq_connect.c.

231{
232 (void) arg;
234 "pq",
235 "%s",
236 message);
237}
#define GNUNET_log_from(kind, comp,...)
@ GNUNET_ERROR_TYPE_INFO

References GNUNET_ERROR_TYPE_INFO, and GNUNET_log_from.

Referenced by GNUNET_PQ_reconnect().

Here is the caller graph for this function:

◆ GNUNET_PQ_connect()

struct GNUNET_PQ_Context * GNUNET_PQ_connect ( const char *  config_str,
const char *  load_path,
const struct GNUNET_PQ_ExecuteStatement es,
const struct GNUNET_PQ_PreparedStatement ps 
)

Create a connection to the Postgres database using config_str for the configuration.

Initialize logging via GNUnet's log routines and disable Postgres's logger. Also ensures that the statements in load_path and es are executed whenever we (re)connect to the database, and that the prepared statements in ps are "ready". If statements in es fail that were created with GNUNET_PQ_make_execute(), then the entire operation fails.

In load_path, a list of "$XXXX.sql" files is expected where $XXXX must be a sequence of contiguous integer values starting at 0000. These files are then loaded in sequence using "psql $config_str" before running statements from es. The directory is inspected again on reconnect.

Parameters
config_strconfiguration to use
load_pathpath to directory with SQL transactions to run, can be NULL
esGNUNET_PQ_PREPARED_STATEMENT_END-terminated array of statements to execute upon EACH connection, can be NULL
psarray of prepared statements to prepare, can be NULL
Returns
NULL on error

Definition at line 241 of file pq_connect.c.

245{
246 return GNUNET_PQ_connect2 (config_str,
247 load_path,
248 NULL == load_path
249 ? NULL
250 : "",
251 es,
252 ps,
254}
static struct GNUNET_PEERSTORE_Handle * ps
Handle to the PEERSTORE service.
@ GNUNET_PQ_FLAG_NONE
Traditional default.
struct GNUNET_PQ_Context * GNUNET_PQ_connect2(const char *config_str, const char *load_path, const char *auto_suffix, const struct GNUNET_PQ_ExecuteStatement *es, const struct GNUNET_PQ_PreparedStatement *ps, enum GNUNET_PQ_Options flags)
Create a connection to the Postgres database using config_str for the configuration.
Definition pq_connect.c:258

References GNUNET_PQ_Context::config_str, GNUNET_PQ_Context::es, GNUNET_PQ_connect2(), GNUNET_PQ_FLAG_NONE, GNUNET_PQ_Context::load_path, and ps.

Here is the call graph for this function:

◆ GNUNET_PQ_connect2()

struct GNUNET_PQ_Context * GNUNET_PQ_connect2 ( const char *  config_str,
const char *  load_path,
const char *  auto_suffix,
const struct GNUNET_PQ_ExecuteStatement es,
const struct GNUNET_PQ_PreparedStatement ps,
enum GNUNET_PQ_Options  flags 
)

Create a connection to the Postgres database using config_str for the configuration.

Initialize logging via GNUnet's log routines and disable Postgres's logger. Also ensures that the statements in load_path and es are executed whenever we (re)connect to the database, and that the prepared statements in ps are "ready". If statements in es fail that were created with GNUNET_PQ_make_execute(), then the entire operation fails.

In load_path, a list of "$XXXX.sql" files is expected where $XXXX must be a sequence of contiguous integer values starting at 0000. These files are then loaded in sequence using "psql $config_str" before running statements from es. The directory is inspected again on reconnect.

Parameters
config_strconfiguration to use
load_pathpath to directory with SQL transactions to run, can be NULL
auto_suffixinfix of SQL series to run on every reconnect; runs multiple (!) files, of the form auto_suffix-XXXX where XXXX is from 0 to 9999 (consecutive).
esGNUNET_PQ_PREPARED_STATEMENT_END-terminated array of statements to execute upon EACH connection, can be NULL
psarray of prepared statements to prepare, can be NULL
flagsconnection flags
Returns
NULL on error

Definition at line 258 of file pq_connect.c.

264{
265 struct GNUNET_PQ_Context *db;
266 unsigned int elen = 0;
267 unsigned int plen = 0;
268
269 if (NULL != es)
270 while (NULL != es[elen].sql)
271 elen++;
272 if (NULL != ps)
273 while (NULL != ps[plen].name)
274 plen++;
275
277 db->flags = flags;
278 db->config_str = GNUNET_strdup (config_str);
279 if (NULL != load_path)
280 db->load_path = GNUNET_strdup (load_path);
281 if (NULL != auto_suffix)
282 db->auto_suffix = GNUNET_strdup (auto_suffix);
283 if (0 != elen)
284 {
285 db->es = GNUNET_new_array (elen + 1,
287 memcpy (db->es,
288 es,
289 elen * sizeof (struct GNUNET_PQ_ExecuteStatement));
290 }
291 if (0 != plen)
292 {
293 db->ps = GNUNET_new_array (plen + 1,
295 memcpy (db->ps,
296 ps,
297 plen * sizeof (struct GNUNET_PQ_PreparedStatement));
298 }
299 db->channel_map = GNUNET_CONTAINER_multishortmap_create (16,
300 GNUNET_YES);
302 if (NULL == db->conn)
303 {
305 GNUNET_free (db->load_path);
306 GNUNET_free (db->auto_suffix);
307 GNUNET_free (db->config_str);
308 GNUNET_free (db);
309 return NULL;
310 }
311 return db;
312}
static char * name
Name (label) of the records to list.
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.
@ GNUNET_YES
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#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.
void GNUNET_PQ_reconnect(struct GNUNET_PQ_Context *db)
Reinitialize the database db.
Definition pq_connect.c:561
Handle to Postgres database.
Definition pq.h:36
struct GNUNET_PQ_ExecuteStatement * es
Statements to execute upon connection.
Definition pq.h:45
enum GNUNET_PQ_Options flags
Flags controlling the connection.
Definition pq.h:106
char * load_path
Path to load SQL files from.
Definition pq.h:70
char * config_str
Configuration to use to connect to the DB.
Definition pq.h:65
char * auto_suffix
Suffix to append to path to load on startup.
Definition pq.h:75
Information needed to run a list of SQL statements using GNUNET_PQ_exec_statements().
Information needed to prepare a list of SQL statements using GNUNET_PQ_prepare_statements().

References GNUNET_PQ_Context::auto_suffix, GNUNET_PQ_Context::config_str, db, GNUNET_PQ_Context::es, GNUNET_PQ_Context::flags, GNUNET_CONTAINER_multishortmap_create(), GNUNET_CONTAINER_multishortmap_destroy(), GNUNET_free, GNUNET_new, GNUNET_new_array, GNUNET_PQ_reconnect(), GNUNET_strdup, GNUNET_YES, GNUNET_PQ_Context::load_path, name, and ps.

Referenced by GNUNET_PQ_connect(), and GNUNET_PQ_connect_with_cfg2().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PQ_exec_sql()

enum GNUNET_GenericReturnValue GNUNET_PQ_exec_sql ( struct GNUNET_PQ_Context db,
const char *  buf 
)

Execute SQL statements from buf against db.

The given filename infix in buf is prefixed with the "load_path" and ".sql" is appended to construct the full filename.

Parameters
dbdatabase context to use
buffilename infix (!) with the SQL code to run
Returns
GNUNET_OK on success, GNUNET_NO if patch buf does not exist, GNUNET_SYSERR on error

Definition at line 316 of file pq_connect.c.

318{
319 struct GNUNET_OS_Process *psql;
321 unsigned long code;
323 char *fn;
324
325 GNUNET_asprintf (&fn,
326 "%s%s.sql",
327 db->load_path,
328 buf);
329 if (GNUNET_YES !=
331 {
332 if (0 == (GNUNET_PQ_FLAG_CHECK_CURRENT & db->flags))
334 "Attempted to load SQL resource `%s', which does not exist. This could be perfectly normal.\n",
335 fn);
336 GNUNET_free (fn);
337 return GNUNET_NO;
338 }
339 if (0 != (GNUNET_PQ_FLAG_CHECK_CURRENT & db->flags))
340 return GNUNET_SYSERR;
342 "Applying SQL file `%s' on database %s\n",
343 fn,
344 db->config_str);
346 NULL,
347 NULL,
348 NULL,
349 "psql",
350 "psql",
351 db->config_str,
352 "-f",
353 fn,
354 "-q",
355 "--set",
356 "ON_ERROR_STOP=1",
357 NULL);
358 if (NULL == psql)
359 {
361 "exec",
362 "psql");
363 GNUNET_free (fn);
364 return GNUNET_SYSERR;
365 }
367 &type,
368 &code);
369 if (GNUNET_OK != ret)
370 {
372 "psql on file %s did not finish, killed it!\n",
373 fn);
374 /* can happen if we got a signal, like CTRL-C, before
375 psql was complete */
376 (void) GNUNET_OS_process_kill (psql,
377 SIGKILL);
379 GNUNET_free (fn);
380 return GNUNET_SYSERR;
381 }
383 if ( (GNUNET_OS_PROCESS_EXITED != type) ||
384 (0 != code) )
385 {
387 "Could not run PSQL on file %s: psql exit code was %d\n",
388 fn,
389 (int) code);
390 GNUNET_free (fn);
391 return GNUNET_SYSERR;
392 }
393 GNUNET_free (fn);
394 return GNUNET_OK;
395}
static int ret
Final status code.
Definition gnunet-arm.c:93
static uint32_t type
Type string converted to DNS type value.
@ GNUNET_PQ_FLAG_CHECK_CURRENT
Check database version is current.
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:540
GNUNET_GenericReturnValue
Named constants for return values.
#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
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
struct GNUNET_OS_Process * GNUNET_OS_start_process(enum GNUNET_OS_InheritStdioFlags std_inheritance, struct GNUNET_DISK_PipeHandle *pipe_stdin, struct GNUNET_DISK_PipeHandle *pipe_stdout, struct GNUNET_DISK_PipeHandle *pipe_stderr, const char *filename,...)
Start a process.
enum GNUNET_GenericReturnValue GNUNET_OS_process_wait_status(struct GNUNET_OS_Process *proc, enum GNUNET_OS_ProcessStatusType *type, unsigned long *code)
Retrieve the status of a process, waiting on it if dead.
GNUNET_OS_ProcessStatusType
Process status types.
void GNUNET_OS_process_destroy(struct GNUNET_OS_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
int GNUNET_OS_process_kill(struct GNUNET_OS_Process *proc, int sig)
Sends a signal to the process.
@ GNUNET_OS_INHERIT_STD_NONE
No standard streams should be inherited.
@ GNUNET_OS_PROCESS_EXITED
The process exited with a return code.

References db, GNUNET_asprintf(), GNUNET_DISK_file_test_read(), GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log, GNUNET_log_strerror_file, GNUNET_NO, GNUNET_OK, GNUNET_OS_INHERIT_STD_NONE, GNUNET_OS_process_destroy(), GNUNET_OS_PROCESS_EXITED, GNUNET_OS_process_kill(), GNUNET_OS_process_wait_status(), GNUNET_OS_start_process(), GNUNET_PQ_FLAG_CHECK_CURRENT, GNUNET_SYSERR, GNUNET_YES, ret, and type.

Referenced by GNUNET_PQ_reconnect(), GNUNET_PQ_run_sql(), libgnunet_plugin_datacache_postgres_done(), and namestore_postgres_drop_tables().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PQ_run_sql()

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_suffix and ends with consecutive numbers from "[0000-9999].sql".

Parameters
dbdatabase context to use
load_suffixsuffix to append to the load path to find the XXXX.sql files, usually "subsystem-".
Returns
GNUNET_OK on success

Definition at line 399 of file pq_connect.c.

401{
402 size_t slen = strlen (load_suffix) + 10;
403 char patch_name[slen];
404
406 "Loading SQL resources from `%s'\n",
407 load_suffix);
408 for (unsigned int i = 1; i<10000; i++)
409 {
411
413 load_suffix,
414 i);
415 if (GNUNET_SYSERR == ret)
416 {
417 GNUNET_break (0);
418 return GNUNET_SYSERR;
419 }
420 if (GNUNET_OK == ret)
421 continue; /* patch already applied, skip it */
422
423 GNUNET_snprintf (patch_name,
424 sizeof (patch_name),
425 "%s%04u",
426 load_suffix,
427 i);
429 patch_name);
430 if (GNUNET_NO == ret)
431 break;
432 if ( (GNUNET_SYSERR == ret) &&
433 (0 != (GNUNET_PQ_FLAG_CHECK_CURRENT & db->flags)) )
434 {
435 /* We are only checking, found unapplied patch, bad! */
437 "Database outdated, patch %s missing. Aborting!\n",
438 patch_name);
439 }
440 if (GNUNET_SYSERR == ret)
441 return GNUNET_SYSERR;
442 }
443 return GNUNET_OK;
444}
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:137
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:316

References check_patch_applied(), db, GNUNET_break, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_log, GNUNET_NO, GNUNET_OK, GNUNET_PQ_exec_sql(), GNUNET_PQ_FLAG_CHECK_CURRENT, GNUNET_snprintf(), GNUNET_SYSERR, and ret.

Referenced by GNUNET_PQ_reconnect().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PQ_reconnect_if_down()

void GNUNET_PQ_reconnect_if_down ( struct GNUNET_PQ_Context db)

Reinitialize the database db if the connection is down.

Parameters
dbdatabase connection to reinitialize

Definition at line 448 of file pq_connect.c.

449{
450 if (1 ==
451 PQconsumeInput (db->conn))
452 return;
453 if (CONNECTION_BAD != PQstatus (db->conn))
454 return;
456}

References db, and GNUNET_PQ_reconnect().

Here is the call graph for this function:

◆ GNUNET_PQ_get_oid_by_name()

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.

Parameters
dbThe db-connection
nameThe name of the datatype
[out]oidThe OID of the datatype.
Returns
GNUNET_OK when the datatype was found, GNUNET_SYSERR otherwise

Definition at line 460 of file pq_connect.c.

464{
465 /* Check if the entry is in the cache already */
466 for (unsigned int i = 0; i < db->oids.num; i++)
467 {
468 /* Pointer comparison */
469 if (name == db->oids.table[i].name)
470 {
471 *oid = db->oids.table[i].oid;
472 return GNUNET_OK;
473 }
474 }
475
476 /* No entry found in cache, ask database */
477 {
478 enum GNUNET_DB_QueryStatus qs;
479 struct GNUNET_PQ_QueryParam params[] = {
482 };
483 struct GNUNET_PQ_ResultSpec spec[] = {
485 oid),
487 };
488
489 GNUNET_assert (NULL != db);
490
492 "gnunet_pq_get_oid_by_name",
493 params,
494 spec);
496 return GNUNET_SYSERR;
497 }
498
499 /* Add the entry to the cache */
500 if (NULL == db->oids.table)
501 {
502 db->oids.table = GNUNET_new_array (8,
503 typeof(*db->oids.table));
504 db->oids.cap = 8;
505 db->oids.num = 0;
506 }
507
508 if (db->oids.cap <= db->oids.num)
509 GNUNET_array_grow (db->oids.table,
510 db->oids.cap,
511 db->oids.cap + 8);
512
513 db->oids.table[db->oids.num].name = name;
514 db->oids.table[db->oids.num].oid = *oid;
515 db->oids.num++;
516
517 return GNUNET_OK;
518}
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint32(const char *name, uint32_t *u32)
uint32_t expected.
uint32_t oid
#define GNUNET_array_grow(arr, size, tsize)
Grow a well-typed (!) array.

References db, GNUNET_array_grow, GNUNET_assert, GNUNET_DB_STATUS_SUCCESS_ONE_RESULT, GNUNET_new_array, GNUNET_OK, GNUNET_PQ_eval_prepared_singleton_select(), GNUNET_PQ_query_param_end, GNUNET_PQ_query_param_string(), GNUNET_PQ_result_spec_end, GNUNET_PQ_result_spec_uint32(), GNUNET_SYSERR, name, and oid.

Referenced by GNUNET_PQ_query_param_array_abs_time(), GNUNET_PQ_query_param_array_bool(), GNUNET_PQ_query_param_array_bytes(), GNUNET_PQ_query_param_array_bytes_same_size(), GNUNET_PQ_query_param_array_ptrs_abs_time(), GNUNET_PQ_query_param_array_ptrs_bytes(), GNUNET_PQ_query_param_array_ptrs_bytes_same_size(), GNUNET_PQ_query_param_array_ptrs_rel_time(), GNUNET_PQ_query_param_array_ptrs_string(), GNUNET_PQ_query_param_array_ptrs_timestamp(), GNUNET_PQ_query_param_array_rel_time(), GNUNET_PQ_query_param_array_string(), GNUNET_PQ_query_param_array_timestamp(), GNUNET_PQ_query_param_array_uint16(), GNUNET_PQ_query_param_array_uint32(), GNUNET_PQ_query_param_array_uint64(), GNUNET_PQ_result_spec_array_abs_time(), GNUNET_PQ_result_spec_array_bool(), GNUNET_PQ_result_spec_array_fixed_size(), GNUNET_PQ_result_spec_array_rel_time(), GNUNET_PQ_result_spec_array_string(), GNUNET_PQ_result_spec_array_timestamp(), GNUNET_PQ_result_spec_array_uint16(), GNUNET_PQ_result_spec_array_uint32(), GNUNET_PQ_result_spec_array_uint64(), GNUNET_PQ_result_spec_array_variable_size(), and load_initial_oids().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ load_initial_oids()

static enum GNUNET_GenericReturnValue load_initial_oids ( struct GNUNET_PQ_Context db)
static

Load the initial set of OIDs for the supported array-datatypes.

Parameters
dbThe database context
Returns
GNUNET_OK on success, GNUNET_SYSERR if any of the types couldn't be found

Definition at line 530 of file pq_connect.c.

531{
532 static const char *typnames[] = {
533 "bool",
534 "int2",
535 "int4",
536 "int8",
537 "bytea",
538 "varchar"
539 };
540 Oid oid;
541
542 for (size_t i = 0; i< sizeof(typnames) / sizeof(*typnames); i++)
543 {
544 if (GNUNET_OK !=
546 typnames[i],
547 &oid))
548 {
550 "pq",
551 "Couldn't retrieve OID for type %s\n",
552 typnames[i]);
553 return GNUNET_SYSERR;
554 }
555 }
556 return GNUNET_OK;
557}
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:460

References db, GNUNET_ERROR_TYPE_ERROR, GNUNET_log_from, GNUNET_OK, GNUNET_PQ_get_oid_by_name(), GNUNET_SYSERR, and oid.

Referenced by GNUNET_PQ_reconnect().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PQ_reconnect()

void GNUNET_PQ_reconnect ( struct GNUNET_PQ_Context db)

Reinitialize the database db.

Parameters
dbdatabase connection to reinitialize

Definition at line 561 of file pq_connect.c.

562{
564 -1);
566 db->conn = PQconnectdb (db->config_str);
567 if ( (NULL == db->conn) ||
568 (CONNECTION_OK != PQstatus (db->conn)) )
569 {
571 "pq",
572 "Database connection to '%s' failed: %s\n",
573 db->config_str,
574 (NULL != db->conn)
575 ? PQerrorMessage (db->conn)
576 : "PQconnectdb returned NULL");
578 return;
579 }
580 PQsetNoticeReceiver (db->conn,
582 db);
583 PQsetNoticeProcessor (db->conn,
585 db);
586 if ( (NULL != db->load_path) &&
587 (NULL != db->auto_suffix) )
588 {
589 PGresult *res;
590 ExecStatusType est;
591
592 res = PQexec (db->conn,
593 "SELECT"
594 " schema_name"
595 " FROM information_schema.schemata"
596 " WHERE schema_name='_v';");
597 est = PQresultStatus (res);
598 if ( (PGRES_COMMAND_OK != est) &&
599 (PGRES_TUPLES_OK != est) )
600 {
602 "Failed to run statement to check versioning schema. Bad!\n");
603 PQclear (res);
605 return;
606 }
607 if (0 == PQntuples (res))
608 {
610
611 PQclear (res);
612 if (0 != (db->flags & GNUNET_PQ_FLAG_DROP))
613 {
615 "Versioning schema does not exist yet. Not attempting drop!\n");
617 return;
618 }
620 "versioning");
621 if (GNUNET_NO == ret)
622 {
624 "Failed to find SQL file to load database versioning logic\n");
626 return;
627 }
628 if (GNUNET_SYSERR == ret)
629 {
631 "Failed to run SQL logic to setup database versioning logic\n");
633 return;
634 }
635 }
636 else
637 {
638 PQclear (res);
639 }
640 }
641
642 /* Prepare statement for OID lookup by name */
643 if (GNUNET_OK !=
645 return;
646
647 /* Reset the OID-cache and retrieve the OIDs for the supported Array types */
648 db->oids.num = 0;
650 {
652 "Failed to retrieve OID information for array types!\n");
654 return;
655 }
656
657 if (NULL != db->auto_suffix)
658 {
659 GNUNET_assert (NULL != db->load_path);
660 if (GNUNET_OK !=
662 return;
663
664 if (GNUNET_SYSERR ==
666 db->auto_suffix))
667 {
668 if (0 == (GNUNET_PQ_FLAG_CHECK_CURRENT & db->flags))
670 "Failed to load SQL statements from `%s*'\n",
671 db->auto_suffix);
673 return;
674 }
675 }
676
677 if ( (NULL != db->es) &&
678 (GNUNET_OK !=
680 db->es)) )
681 {
683 return;
684 }
685 if ( (NULL != db->ps) &&
686 (GNUNET_OK !=
688 db->ps)) )
689 {
691 return;
692 }
694 PQsocket (db->conn));
695}
enum GNUNET_GenericReturnValue GNUNET_PQ_exec_statements(struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_ExecuteStatement *es)
Request execution of an array of statements es from Postgres.
Definition pq_exec.c:54
enum GNUNET_GenericReturnValue GNUNET_PQ_prepare_statements(struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_PreparedStatement *ps)
Request creation of prepared statements ps from Postgres.
Definition pq_prepare.c:88
@ GNUNET_PQ_FLAG_DROP
Dropping database.
void GNUNET_PQ_event_reconnect_(struct GNUNET_PQ_Context *db, int fd)
Internal API.
Definition pq_event.c:434
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:212
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
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:229
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:530
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:399

References db, GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_log, GNUNET_log_from, GNUNET_NO, GNUNET_OK, GNUNET_PQ_event_reconnect_(), GNUNET_PQ_exec_sql(), GNUNET_PQ_exec_statements(), GNUNET_PQ_FLAG_CHECK_CURRENT, GNUNET_PQ_FLAG_DROP, GNUNET_PQ_prepare_statements(), GNUNET_PQ_run_sql(), GNUNET_SYSERR, load_initial_oids(), pq_notice_processor_cb(), pq_notice_receiver_cb(), prepare_check_patch(), prepare_get_oid_by_name(), res, reset_connection(), and ret.

Referenced by do_scheduler_notify(), GNUNET_PQ_connect2(), GNUNET_PQ_eval_result(), GNUNET_PQ_event_do_poll(), GNUNET_PQ_exec_prepared(), and GNUNET_PQ_reconnect_if_down().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PQ_connect_with_cfg()

struct GNUNET_PQ_Context * GNUNET_PQ_connect_with_cfg ( const struct GNUNET_CONFIGURATION_Handle cfg,
const char *  section,
const char *  load_path_suffix,
const struct GNUNET_PQ_ExecuteStatement es,
const struct GNUNET_PQ_PreparedStatement ps 
)

Connect to a postgres database using the configuration option "CONFIG" in section.

Also ensures that the statements in es are executed whenever we (re)connect to the database, and that the prepared statements in ps are "ready".

The caller does not have to ensure that es and ps remain allocated and initialized in memory until GNUNET_PQ_disconnect() is called, as a copy will be made.

Parameters
cfgconfiguration
sectionconfiguration section to use to get Postgres configuration options
load_path_suffixsuffix to append to the SQL_DIR in the configuration
esGNUNET_PQ_PREPARED_STATEMENT_END-terminated array of statements to execute upon EACH connection, can be NULL
psarray of prepared statements to prepare, can be NULL
Returns
the postgres handle, NULL on error

Definition at line 699 of file pq_connect.c.

704{
706 section,
707 load_path_suffix,
708 es,
709 ps,
711}
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition gnunet-arm.c:108
struct GNUNET_PQ_Context * GNUNET_PQ_connect_with_cfg2(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *load_path_suffix, const struct GNUNET_PQ_ExecuteStatement *es, const struct GNUNET_PQ_PreparedStatement *ps, enum GNUNET_PQ_Options flags)
Connect to a postgres database using the configuration option "CONFIG" in section.
Definition pq_connect.c:715

References cfg, GNUNET_PQ_Context::es, GNUNET_PQ_connect_with_cfg2(), GNUNET_PQ_FLAG_NONE, and ps.

Referenced by database_connect(), database_setup(), init_connection(), init_connection(), namestore_postgres_create_tables(), and namestore_postgres_drop_tables().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PQ_connect_with_cfg2()

struct GNUNET_PQ_Context * GNUNET_PQ_connect_with_cfg2 ( const struct GNUNET_CONFIGURATION_Handle cfg,
const char *  section,
const char *  load_path_suffix,
const struct GNUNET_PQ_ExecuteStatement es,
const struct GNUNET_PQ_PreparedStatement ps,
enum GNUNET_PQ_Options  flags 
)

Connect to a postgres database using the configuration option "CONFIG" in section.

Also ensures that the statements in es are executed whenever we (re)connect to the database, and that the prepared statements in ps are "ready".

The caller does not have to ensure that es and ps remain allocated and initialized in memory until GNUNET_PQ_disconnect() is called, as a copy will be made.

Parameters
cfgconfiguration
sectionconfiguration section to use to get Postgres configuration options
load_path_suffixsuffix to append to the SQL_DIR in the configuration
esGNUNET_PQ_PREPARED_STATEMENT_END-terminated array of statements to execute upon EACH connection, can be NULL
psarray of prepared statements to prepare, can be NULL
flagsconnection flags
Returns
the postgres handle, NULL on error

Definition at line 715 of file pq_connect.c.

721{
722 struct GNUNET_PQ_Context *db;
723 char *conninfo;
724 char *load_path;
725
726 if (GNUNET_OK !=
728 section,
729 "CONFIG",
730 &conninfo))
731 conninfo = NULL;
732 load_path = NULL;
733 if (GNUNET_OK !=
735 section,
736 "SQL_DIR",
737 &load_path))
738 {
740 section,
741 "SQL_DIR");
742 }
743 if ( (NULL != load_path_suffix) &&
744 (NULL == load_path) )
745 {
747 section,
748 "SQL_DIR");
749 return NULL;
750 }
751 db = GNUNET_PQ_connect2 (conninfo == NULL ? "" : conninfo,
752 load_path,
753 load_path_suffix,
754 es,
755 ps,
756 flags);
758 GNUNET_free (conninfo);
759 return db;
760}
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.
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.

References cfg, db, GNUNET_PQ_Context::es, GNUNET_PQ_Context::flags, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CONFIGURATION_get_value_string(), GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_free, GNUNET_log_config_missing(), GNUNET_OK, GNUNET_PQ_connect2(), GNUNET_PQ_Context::load_path, and ps.

Referenced by GNUNET_PQ_connect_with_cfg().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PQ_disconnect()

void GNUNET_PQ_disconnect ( struct GNUNET_PQ_Context db)

Disconnect from the database, destroying the prepared statements and releasing other associated resources.

Parameters
dbdatabase handle to disconnect (will be free'd)

Definition at line 764 of file pq_connect.c.

765{
766 if (NULL == db)
767 return;
768 GNUNET_assert (0 ==
771 if (NULL != db->poller_task)
772 {
773 GNUNET_SCHEDULER_cancel (db->poller_task);
774 db->poller_task = NULL;
775 }
776 GNUNET_free (db->es);
777 GNUNET_free (db->ps);
778 GNUNET_free (db->load_path);
779 GNUNET_free (db->auto_suffix);
780 GNUNET_free (db->config_str);
781 GNUNET_free (db->oids.table);
782 db->oids.table = NULL;
783 db->oids.num = 0;
784 db->oids.cap = 0;
785 PQfinish (db->conn);
786 GNUNET_free (db);
787}
unsigned int GNUNET_CONTAINER_multishortmap_size(const struct GNUNET_CONTAINER_MultiShortmap *map)
Get the number of key-value pairs in the map.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition scheduler.c:980

References db, GNUNET_assert, GNUNET_CONTAINER_multishortmap_destroy(), GNUNET_CONTAINER_multishortmap_size(), GNUNET_free, and GNUNET_SCHEDULER_cancel().

Referenced by database_shutdown(), database_shutdown(), libgnunet_plugin_datacache_postgres_done(), libgnunet_plugin_datastore_postgres_done(), namestore_postgres_create_tables(), and namestore_postgres_drop_tables().

Here is the call graph for this function:
Here is the caller graph for this function: