GNUnet  0.10.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 34 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 42 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 60 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 153 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().

155 {
156  char *cnffile;
157  char *home_dir;
158  struct stat st;
159 
160  struct passwd *pw;
161 
162  int configured;
163 
164  pw = getpwuid(getuid());
165  if (!pw)
166  {
168  return NULL;
169  }
170  if (GNUNET_YES == GNUNET_CONFIGURATION_have_value(cfg, section, "CONFIG"))
171  {
174  section,
175  "CONFIG",
176  &cnffile));
177  configured = GNUNET_YES;
178  }
179  else
180  {
181  home_dir = GNUNET_strdup(pw->pw_dir);
182  GNUNET_asprintf(&cnffile, "%s/.my.cnf", home_dir);
183  GNUNET_free(home_dir);
184  configured = GNUNET_NO;
185  }
186 
188  "mysql",
189  _("Trying to use file `%s' for MySQL configuration.\n"),
190  cnffile);
191  if ((0 != stat(cnffile, &st)) || (0 != access(cnffile, R_OK)) ||
192  (!S_ISREG(st.st_mode)))
193  {
194  if (configured == GNUNET_YES)
196  "mysql",
197  _("Could not access file `%s': %s\n"),
198  cnffile,
199  strerror(errno));
200  GNUNET_free(cnffile);
201  return NULL;
202  }
203  return cnffile;
204 }
#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 215 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().

216 {
217  char *mysql_dbname;
218  char *mysql_server;
219  char *mysql_user;
220  char *mysql_password;
221  unsigned long long mysql_port;
222  my_bool reconnect;
223  unsigned int timeout;
224 
225  mc->dbf = mysql_init(NULL);
226  if (mc->dbf == NULL)
227  return GNUNET_SYSERR;
228  if (mc->cnffile != NULL)
229  mysql_options(mc->dbf, MYSQL_READ_DEFAULT_FILE, mc->cnffile);
230  mysql_options(mc->dbf, MYSQL_READ_DEFAULT_GROUP, "client");
231  reconnect = 0;
232  mysql_options(mc->dbf, MYSQL_OPT_RECONNECT, &reconnect);
233  mysql_options(mc->dbf, MYSQL_OPT_CONNECT_TIMEOUT, (const void *)&timeout);
234  mysql_options(mc->dbf, MYSQL_SET_CHARSET_NAME, "UTF8");
235  timeout = 60; /* in seconds */
236  mysql_options(mc->dbf, MYSQL_OPT_READ_TIMEOUT, (const void *)&timeout);
237  mysql_options(mc->dbf, MYSQL_OPT_WRITE_TIMEOUT, (const void *)&timeout);
238  mysql_dbname = NULL;
239  if (GNUNET_YES ==
240  GNUNET_CONFIGURATION_have_value(mc->cfg, mc->section, "DATABASE"))
243  mc->section,
244  "DATABASE",
245  &mysql_dbname));
246  else
247  mysql_dbname = GNUNET_strdup("gnunet");
248  mysql_user = NULL;
249  if (GNUNET_YES ==
250  GNUNET_CONFIGURATION_have_value(mc->cfg, mc->section, "USER"))
251  {
254  mc->section,
255  "USER",
256  &mysql_user));
257  }
258  mysql_password = NULL;
259  if (GNUNET_YES ==
260  GNUNET_CONFIGURATION_have_value(mc->cfg, mc->section, "PASSWORD"))
261  {
264  mc->section,
265  "PASSWORD",
266  &mysql_password));
267  }
268  mysql_server = NULL;
269  if (GNUNET_YES ==
270  GNUNET_CONFIGURATION_have_value(mc->cfg, mc->section, "HOST"))
271  {
274  mc->section,
275  "HOST",
276  &mysql_server));
277  }
278  mysql_port = 0;
279  if (GNUNET_YES ==
280  GNUNET_CONFIGURATION_have_value(mc->cfg, mc->section, "PORT"))
281  {
284  mc->section,
285  "PORT",
286  &mysql_port));
287  }
288 
289  GNUNET_assert(mysql_dbname != NULL);
290  mysql_real_connect(mc->dbf,
291  mysql_server,
292  mysql_user,
293  mysql_password,
294  mysql_dbname,
295  (unsigned int)mysql_port,
296  NULL,
297  CLIENT_IGNORE_SIGPIPE);
298  GNUNET_free_non_null(mysql_server);
299  GNUNET_free_non_null(mysql_user);
300  GNUNET_free_non_null(mysql_password);
301  GNUNET_free(mysql_dbname);
302  if (mysql_error(mc->dbf)[0])
303  {
304  LOG_MYSQL(GNUNET_ERROR_TYPE_ERROR, "mysql_real_connect", mc);
305  return GNUNET_SYSERR;
306  }
307  return GNUNET_OK;
308 }
static void reconnect(struct GNUNET_ATS_ApplicationHandle *ch)
Re-establish the connection to the ATS service.
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
User defined timestamp for completing operations.
Definition: gnunet-arm.c:114
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_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:60
#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 437 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().

438 {
439  struct GNUNET_MYSQL_Context *mc = sh->mc;
440 
441  if (GNUNET_YES == sh->valid)
442  return GNUNET_OK;
443  if ((NULL == mc->dbf) && (GNUNET_OK != iopen(mc)))
444  return GNUNET_SYSERR;
445  sh->statement = mysql_stmt_init(mc->dbf);
446  if (NULL == sh->statement)
447  {
449  return GNUNET_SYSERR;
450  }
451  if (0 != mysql_stmt_prepare(sh->statement, sh->query, strlen(sh->query)))
452  {
454  "mysql",
455  "prepare_statement: %s\n",
456  sh->query);
457  LOG_MYSQL(GNUNET_ERROR_TYPE_ERROR, "mysql_stmt_prepare", mc);
458  mysql_stmt_close(sh->statement);
459  sh->statement = NULL;
461  return GNUNET_SYSERR;
462  }
463  sh->valid = GNUNET_YES;
464  return GNUNET_OK;
465 }
MYSQL_STMT * statement
Handle to MySQL prepared statement.
Definition: mysql.c:136
char * query
Original query string.
Definition: mysql.c:131
#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:340
int valid
Is the MySQL prepared statement valid, or do we need to re-initialize it?
Definition: mysql.c:141
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:76
struct GNUNET_MYSQL_Context * mc
Mysql Context the statement handle belongs to.
Definition: mysql.c:126
#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:60
#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:215
#define GNUNET_log_from(kind, comp,...)
Here is the call graph for this function:
Here is the caller graph for this function: