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:208

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:208

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 157 of file mysql.c.

References _, ACCESS, 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_malloc, GNUNET_NO, GNUNET_OK, GNUNET_strdup, GNUNET_YES, plibc_conv_to_win_path(), STAT, and STRERROR.

Referenced by GNUNET_MYSQL_context_create().

159 {
160  char *cnffile;
161  char *home_dir;
162  struct stat st;
163 
164 #ifndef WINDOWS
165  struct passwd *pw;
166 #endif
167  int configured;
168 
169 #ifndef WINDOWS
170  pw = getpwuid (getuid ());
171  if (! pw)
172  {
173  GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_ERROR, "mysql", "getpwuid");
174  return NULL;
175  }
176  if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg, section, "CONFIG"))
177  {
180  section,
181  "CONFIG",
182  &cnffile));
183  configured = GNUNET_YES;
184  }
185  else
186  {
187  home_dir = GNUNET_strdup (pw->pw_dir);
188  GNUNET_asprintf (&cnffile, "%s/.my.cnf", home_dir);
189  GNUNET_free (home_dir);
190  configured = GNUNET_NO;
191  }
192 #else
193  home_dir = (char *) GNUNET_malloc (_MAX_PATH + 1);
194  plibc_conv_to_win_path ("~/", home_dir);
195  GNUNET_asprintf (&cnffile, "%s/.my.cnf", home_dir);
196  GNUNET_free (home_dir);
197  configured = GNUNET_NO;
198 #endif
200  "mysql",
201  _ ("Trying to use file `%s' for MySQL configuration.\n"),
202  cnffile);
203  if ((0 != STAT (cnffile, &st)) || (0 != ACCESS (cnffile, R_OK)) ||
204  (! S_ISREG (st.st_mode)))
205  {
206  if (configured == GNUNET_YES)
208  "mysql",
209  _ ("Could not access file `%s': %s\n"),
210  cnffile,
211  STRERROR (errno));
212  GNUNET_free (cnffile);
213  return NULL;
214  }
215  return cnffile;
216 }
#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 STRERROR(i)
Definition: plibc.h:676
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define STAT(p, b)
Definition: plibc.h:663
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
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 plibc_conv_to_win_path(const char *pszUnix, char *pszWindows)
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:80
#define GNUNET_log_from(kind, comp,...)
static struct GNUNET_SCHEDULER_Task * st
The shutdown task.
#define ACCESS(p, m)
Definition: plibc.h:656
#define GNUNET_malloc(size)
Wrapper around malloc.
#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 227 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().

228 {
229  char *mysql_dbname;
230  char *mysql_server;
231  char *mysql_user;
232  char *mysql_password;
233  unsigned long long mysql_port;
234  my_bool reconnect;
235  unsigned int timeout;
236 
237  mc->dbf = mysql_init (NULL);
238  if (mc->dbf == NULL)
239  return GNUNET_SYSERR;
240  if (mc->cnffile != NULL)
241  mysql_options (mc->dbf, MYSQL_READ_DEFAULT_FILE, mc->cnffile);
242  mysql_options (mc->dbf, MYSQL_READ_DEFAULT_GROUP, "client");
243  reconnect = 0;
244  mysql_options (mc->dbf, MYSQL_OPT_RECONNECT, &reconnect);
245  mysql_options (mc->dbf, MYSQL_OPT_CONNECT_TIMEOUT, (const void *) &timeout);
246  mysql_options (mc->dbf, MYSQL_SET_CHARSET_NAME, "UTF8");
247  timeout = 60; /* in seconds */
248  mysql_options (mc->dbf, MYSQL_OPT_READ_TIMEOUT, (const void *) &timeout);
249  mysql_options (mc->dbf, MYSQL_OPT_WRITE_TIMEOUT, (const void *) &timeout);
250  mysql_dbname = NULL;
251  if (GNUNET_YES ==
252  GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "DATABASE"))
255  mc->section,
256  "DATABASE",
257  &mysql_dbname));
258  else
259  mysql_dbname = GNUNET_strdup ("gnunet");
260  mysql_user = NULL;
261  if (GNUNET_YES ==
262  GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "USER"))
263  {
266  mc->section,
267  "USER",
268  &mysql_user));
269  }
270  mysql_password = NULL;
271  if (GNUNET_YES ==
272  GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "PASSWORD"))
273  {
276  mc->section,
277  "PASSWORD",
278  &mysql_password));
279  }
280  mysql_server = NULL;
281  if (GNUNET_YES ==
282  GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "HOST"))
283  {
286  mc->section,
287  "HOST",
288  &mysql_server));
289  }
290  mysql_port = 0;
291  if (GNUNET_YES ==
292  GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "PORT"))
293  {
296  mc->section,
297  "PORT",
298  &mysql_port));
299  }
300 
301  GNUNET_assert (mysql_dbname != NULL);
302  mysql_real_connect (mc->dbf,
303  mysql_server,
304  mysql_user,
305  mysql_password,
306  mysql_dbname,
307  (unsigned int) mysql_port,
308  NULL,
309  CLIENT_IGNORE_SIGPIPE);
310  GNUNET_free_non_null (mysql_server);
311  GNUNET_free_non_null (mysql_user);
312  GNUNET_free_non_null (mysql_password);
313  GNUNET_free (mysql_dbname);
314  if (mysql_error (mc->dbf)[0])
315  {
316  LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_real_connect", mc);
317  return GNUNET_SYSERR;
318  }
319  return GNUNET_OK;
320 }
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:107
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#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:82
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:92
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
const char * section
Our section.
Definition: mysql.c:87
#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:80
#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 449 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().

450 {
451  struct GNUNET_MYSQL_Context *mc = sh->mc;
452 
453  if (GNUNET_YES == sh->valid)
454  return GNUNET_OK;
455  if ((NULL == mc->dbf) && (GNUNET_OK != iopen (mc)))
456  return GNUNET_SYSERR;
457  sh->statement = mysql_stmt_init (mc->dbf);
458  if (NULL == sh->statement)
459  {
461  return GNUNET_SYSERR;
462  }
463  if (0 != mysql_stmt_prepare (sh->statement, sh->query, strlen (sh->query)))
464  {
466  "mysql",
467  "prepare_statement: %s\n",
468  sh->query);
469  LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_stmt_prepare", mc);
470  mysql_stmt_close (sh->statement);
471  sh->statement = NULL;
473  return GNUNET_SYSERR;
474  }
475  sh->valid = GNUNET_YES;
476  return GNUNET_OK;
477 }
MYSQL_STMT * statement
Handle to MySQL prepared statement.
Definition: mysql.c:140
char * query
Original query string.
Definition: mysql.c:135
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
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:352
int valid
Is the MySQL prepared statement valid, or do we need to re-initialize it?
Definition: mysql.c:145
static struct GNUNET_TESTBED_Controller * mc
Handle to the master controller.
MYSQL * dbf
Handle to the mysql database.
Definition: mysql.c:92
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
Mysql context.
Definition: mysql.c:76
struct GNUNET_MYSQL_Context * mc
Mysql Context the statement handle belongs to.
Definition: mysql.c:130
#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:80
static int iopen(struct GNUNET_MYSQL_Context *mc)
Open the connection with the database (and initialize our default options).
Definition: mysql.c:227
#define GNUNET_log_from(kind, comp,...)
Here is the call graph for this function:
Here is the caller graph for this function: