GNUnet  0.11.x
Data Structures | Macros | Functions
mysql.c File Reference

library to help with access to a MySQL database More...

#include "platform.h"
#include <mysql/mysql.h>
#include "gnunet_mysql_lib.h"
Include dependency graph for mysql.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_MYSQL_Context
 Mysql context. More...
 
struct  GNUNET_MYSQL_StatementHandle
 Handle for a prepared statement. More...
 

Macros

#define MAX_PARAM   16
 Maximum number of supported parameters for a prepared statement. More...
 
#define DIE_MYSQL(cmd, dbh)
 Die with an error message that indicates a failure of the command 'cmd' with the message given by strerror(errno). More...
 
#define LOG_MYSQL(level, cmd, dbh)
 Log an error message at log-level 'level' that indicates a failure of the command 'cmd' on file 'filename' with the message given by strerror(errno). More...
 

Functions

static char * get_my_cnf_path (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
 Obtain the location of ".my.cnf". More...
 
static int iopen (struct GNUNET_MYSQL_Context *mc)
 Open the connection with the database (and initialize our default options). More...
 
struct GNUNET_MYSQL_ContextGNUNET_MYSQL_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
 Create a mysql context. More...
 
void GNUNET_MYSQL_statements_invalidate (struct GNUNET_MYSQL_Context *mc)
 Close database connection and all prepared statements (we got a DB error). More...
 
void GNUNET_MYSQL_context_destroy (struct GNUNET_MYSQL_Context *mc)
 Destroy a mysql context. More...
 
struct GNUNET_MYSQL_StatementHandleGNUNET_MYSQL_statement_prepare (struct GNUNET_MYSQL_Context *mc, const char *query)
 Prepare a statement. More...
 
int GNUNET_MYSQL_statement_run (struct GNUNET_MYSQL_Context *mc, const char *sql)
 Run a SQL statement. More...
 
static int prepare_statement (struct GNUNET_MYSQL_StatementHandle *sh)
 Prepare a statement for running. More...
 
MYSQL_STMT * GNUNET_MYSQL_statement_get_stmt (struct GNUNET_MYSQL_StatementHandle *sh)
 Get internal handle for a prepared statement. More...
 

Detailed Description

library to help with access to a MySQL database

Author
Christian Grothoff

Definition in file mysql.c.

Macro Definition Documentation

◆ MAX_PARAM

#define MAX_PARAM   16

Maximum number of supported parameters for a prepared statement.

Increase if needed.

Definition at line 33 of file mysql.c.

◆ DIE_MYSQL

#define DIE_MYSQL (   cmd,
  dbh 
)
Value:
do \
{ \
GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, \
"mysql", \
_ ("`%s' failed at %s:%d with error: %s\n"), \
cmd, \
__FILE__, \
__LINE__, \
mysql_error ((dbh)->dbf)); \
GNUNET_assert (0); \
} while (0);
#define _(String)
GNU gettext support macro.
Definition: platform.h:181

Die with an error message that indicates a failure of the command 'cmd' with the message given by strerror(errno).

Definition at line 41 of file mysql.c.

◆ LOG_MYSQL

#define LOG_MYSQL (   level,
  cmd,
  dbh 
)
Value:
do \
{ \
GNUNET_log_from (level, \
"mysql", \
_ ("`%s' failed at %s:%d with error: %s\n"), \
cmd, \
__FILE__, \
__LINE__, \
mysql_error ((dbh)->dbf)); \
} while (0);
#define _(String)
GNU gettext support macro.
Definition: platform.h:181

Log an error message at log-level 'level' that indicates a failure of the command 'cmd' on file 'filename' with the message given by strerror(errno).

Definition at line 59 of file mysql.c.

Referenced by GNUNET_MYSQL_statement_run(), iopen(), and prepare_statement().

Function Documentation

◆ get_my_cnf_path()

static char* get_my_cnf_path ( const struct GNUNET_CONFIGURATION_Handle cfg,
const char *  section 
)
static

Obtain the location of ".my.cnf".

Parameters
cfgour configuration
sectionthe section
Returns
NULL on error

Definition at line 154 of file mysql.c.

References _, GNUNET_MYSQL_Context::cnffile, GNUNET_asprintf(), GNUNET_assert, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CONFIGURATION_have_value(), GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_free, GNUNET_log_from, GNUNET_log_from_strerror, GNUNET_NO, GNUNET_OK, GNUNET_strdup, and GNUNET_YES.

Referenced by GNUNET_MYSQL_context_create().

156 {
157  char *cnffile;
158  char *home_dir;
159  struct stat st;
160 
161  struct passwd *pw;
162 
163  int configured;
164 
165  pw = getpwuid (getuid ());
166  if (! pw)
167  {
168  GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_ERROR, "mysql", "getpwuid");
169  return NULL;
170  }
171  if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg, section, "CONFIG"))
172  {
175  section,
176  "CONFIG",
177  &cnffile));
178  configured = GNUNET_YES;
179  }
180  else
181  {
182  home_dir = GNUNET_strdup (pw->pw_dir);
183  GNUNET_asprintf (&cnffile, "%s/.my.cnf", home_dir);
184  GNUNET_free (home_dir);
185  configured = GNUNET_NO;
186  }
187 
189  "mysql",
190  _ ("Trying to use file `%s' for MySQL configuration.\n"),
191  cnffile);
192  if ((0 != stat (cnffile, &st)) || (0 != access (cnffile, R_OK)) ||
193  (! S_ISREG (st.st_mode)))
194  {
195  if (configured == GNUNET_YES)
197  "mysql",
198  _ ("Could not access file `%s': %s\n"),
199  cnffile,
200  strerror (errno));
201  GNUNET_free (cnffile);
202  return NULL;
203  }
204  return cnffile;
205 }
#define GNUNET_log_from_strerror(level, component, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
static char * section
Name of the section.
Definition: gnunet-config.c:33
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
int GNUNET_CONFIGURATION_have_value(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Test if we have a value for a particular option.
int 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.
#define GNUNET_YES
Definition: gnunet_common.h:77
#define GNUNET_log_from(kind, comp,...)
static struct GNUNET_SCHEDULER_Task * st
The shutdown task.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ iopen()

static int iopen ( struct GNUNET_MYSQL_Context mc)
static

Open the connection with the database (and initialize our default options).

Parameters
mcdatabase context to initialze
Returns
GNUNET_OK on success

Definition at line 216 of file mysql.c.

References GNUNET_MYSQL_Context::cfg, GNUNET_MYSQL_Context::cnffile, GNUNET_MYSQL_Context::dbf, GNUNET_assert, GNUNET_CONFIGURATION_get_value_number(), GNUNET_CONFIGURATION_get_value_string(), GNUNET_CONFIGURATION_have_value(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_free_non_null, GNUNET_OK, GNUNET_strdup, GNUNET_SYSERR, GNUNET_YES, LOG_MYSQL, reconnect(), GNUNET_MYSQL_Context::section, and timeout.

Referenced by GNUNET_MYSQL_statement_run(), and prepare_statement().

217 {
218  char *mysql_dbname;
219  char *mysql_server;
220  char *mysql_user;
221  char *mysql_password;
222  unsigned long long mysql_port;
223  my_bool reconnect;
224  unsigned int timeout;
225 
226  mc->dbf = mysql_init (NULL);
227  if (mc->dbf == NULL)
228  return GNUNET_SYSERR;
229  if (mc->cnffile != NULL)
230  mysql_options (mc->dbf, MYSQL_READ_DEFAULT_FILE, mc->cnffile);
231  mysql_options (mc->dbf, MYSQL_READ_DEFAULT_GROUP, "client");
232  reconnect = 0;
233  mysql_options (mc->dbf, MYSQL_OPT_RECONNECT, &reconnect);
234  mysql_options (mc->dbf, MYSQL_OPT_CONNECT_TIMEOUT, (const void *) &timeout);
235  mysql_options (mc->dbf, MYSQL_SET_CHARSET_NAME, "UTF8");
236  timeout = 60; /* in seconds */
237  mysql_options (mc->dbf, MYSQL_OPT_READ_TIMEOUT, (const void *) &timeout);
238  mysql_options (mc->dbf, MYSQL_OPT_WRITE_TIMEOUT, (const void *) &timeout);
239  mysql_dbname = NULL;
240  if (GNUNET_YES ==
241  GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "DATABASE"))
244  mc->section,
245  "DATABASE",
246  &mysql_dbname));
247  else
248  mysql_dbname = GNUNET_strdup ("gnunet");
249  mysql_user = NULL;
250  if (GNUNET_YES ==
251  GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "USER"))
252  {
255  mc->section,
256  "USER",
257  &mysql_user));
258  }
259  mysql_password = NULL;
260  if (GNUNET_YES ==
261  GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "PASSWORD"))
262  {
265  mc->section,
266  "PASSWORD",
267  &mysql_password));
268  }
269  mysql_server = NULL;
270  if (GNUNET_YES ==
271  GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "HOST"))
272  {
275  mc->section,
276  "HOST",
277  &mysql_server));
278  }
279  mysql_port = 0;
280  if (GNUNET_YES ==
281  GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "PORT"))
282  {
285  mc->section,
286  "PORT",
287  &mysql_port));
288  }
289 
290  GNUNET_assert (mysql_dbname != NULL);
291  mysql_real_connect (mc->dbf,
292  mysql_server,
293  mysql_user,
294  mysql_password,
295  mysql_dbname,
296  (unsigned int) mysql_port,
297  NULL,
298  CLIENT_IGNORE_SIGPIPE);
299  GNUNET_free_non_null (mysql_server);
300  GNUNET_free_non_null (mysql_user);
301  GNUNET_free_non_null (mysql_password);
302  GNUNET_free (mysql_dbname);
303  if (mysql_error (mc->dbf)[0])
304  {
305  LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_real_connect", mc);
306  return GNUNET_SYSERR;
307  }
308  return GNUNET_OK;
309 }
int GNUNET_CONFIGURATION_get_value_number(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
Get a configuration value that should be a number.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
char * cnffile
Filename of "my.cnf" (msyql configuration).
Definition: mysql.c:105
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: mysql.c:80
static struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
Definition: gnunet-abd.c:61
int GNUNET_CONFIGURATION_have_value(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Test if we have a value for a particular option.
static void reconnect(struct GNUNET_ABD_Handle *handle)
Reconnect to ABD service.
Definition: abd_api.c:316
int 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.
MYSQL * dbf
Handle to the mysql database.
Definition: mysql.c:90
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
const char * section
Our section.
Definition: mysql.c:85
#define LOG_MYSQL(level, cmd, dbh)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; on file &#39;file...
Definition: mysql.c:59
#define GNUNET_YES
Definition: gnunet_common.h:77
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ prepare_statement()

static int prepare_statement ( struct GNUNET_MYSQL_StatementHandle sh)
static

Prepare a statement for running.

Parameters
mcmysql context
shstatement handle to prepare
Returns
GNUNET_OK on success

Definition at line 438 of file mysql.c.

References GNUNET_MYSQL_Context::dbf, GNUNET_ERROR_TYPE_ERROR, GNUNET_log_from, GNUNET_MYSQL_statements_invalidate(), GNUNET_OK, GNUNET_SYSERR, GNUNET_YES, iopen(), LOG_MYSQL, GNUNET_MYSQL_StatementHandle::mc, mc, GNUNET_MYSQL_StatementHandle::query, GNUNET_MYSQL_StatementHandle::statement, and GNUNET_MYSQL_StatementHandle::valid.

Referenced by GNUNET_MYSQL_statement_get_stmt().

439 {
440  struct GNUNET_MYSQL_Context *mc = sh->mc;
441 
442  if (GNUNET_YES == sh->valid)
443  return GNUNET_OK;
444  if ((NULL == mc->dbf) && (GNUNET_OK != iopen (mc)))
445  return GNUNET_SYSERR;
446  sh->statement = mysql_stmt_init (mc->dbf);
447  if (NULL == sh->statement)
448  {
450  return GNUNET_SYSERR;
451  }
452  if (0 != mysql_stmt_prepare (sh->statement, sh->query, strlen (sh->query)))
453  {
455  "mysql",
456  "prepare_statement: %s\n",
457  sh->query);
458  LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_stmt_prepare", mc);
459  mysql_stmt_close (sh->statement);
460  sh->statement = NULL;
462  return GNUNET_SYSERR;
463  }
464  sh->valid = GNUNET_YES;
465  return GNUNET_OK;
466 }
MYSQL_STMT * statement
Handle to MySQL prepared statement.
Definition: mysql.c:137
char * query
Original query string.
Definition: mysql.c:132
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
void GNUNET_MYSQL_statements_invalidate(struct GNUNET_MYSQL_Context *mc)
Close database connection and all prepared statements (we got a DB error).
Definition: mysql.c:341
int valid
Is the MySQL prepared statement valid, or do we need to re-initialize it?
Definition: mysql.c:142
static struct GNUNET_TESTBED_Controller * mc
Handle to the master controller.
MYSQL * dbf
Handle to the mysql database.
Definition: mysql.c:90
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
Mysql context.
Definition: mysql.c:75
struct GNUNET_MYSQL_Context * mc
Mysql Context the statement handle belongs to.
Definition: mysql.c:127
#define LOG_MYSQL(level, cmd, dbh)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; on file &#39;file...
Definition: mysql.c:59
#define GNUNET_YES
Definition: gnunet_common.h:77
static int iopen(struct GNUNET_MYSQL_Context *mc)
Open the connection with the database (and initialize our default options).
Definition: mysql.c:216
#define GNUNET_log_from(kind, comp,...)
Here is the call graph for this function:
Here is the caller graph for this function: