26 #include <mysql/mysql.h>
42 #define DIE_MYSQL(cmd, dbh) \
45 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, \
47 _ ("`%s' failed at %s:%d with error: %s\n"), \
51 mysql_error ((dbh)->dbf)); \
60 #define LOG_MYSQL(level, cmd, dbh) \
63 GNUNET_log_from (level, \
65 _ ("`%s' failed at %s:%d with error: %s\n"), \
69 mysql_error ((dbh)->dbf)); \
166 pw = getpwuid (getuid ());
191 _ (
"Trying to use file `%s' for MySQL configuration.\n"),
193 if ((0 != stat (cnffile, &
st)) || (0 != access (cnffile, R_OK)) ||
194 (! S_ISREG (
st.st_mode)))
199 _ (
"Could not access file `%s': %s\n"),
222 char *mysql_password;
223 unsigned long long mysql_port;
227 mc->dbf = mysql_init (NULL);
230 if (
mc->cnffile != NULL)
231 mysql_options (
mc->dbf, MYSQL_READ_DEFAULT_FILE,
mc->cnffile);
232 mysql_options (
mc->dbf, MYSQL_READ_DEFAULT_GROUP,
"client");
234 mysql_options (
mc->dbf, MYSQL_OPT_RECONNECT, &
reconnect);
235 mysql_options (
mc->dbf, MYSQL_OPT_CONNECT_TIMEOUT, (
const void *) &
timeout);
236 mysql_options (
mc->dbf, MYSQL_SET_CHARSET_NAME,
"UTF8");
238 mysql_options (
mc->dbf, MYSQL_OPT_READ_TIMEOUT, (
const void *) &
timeout);
239 mysql_options (
mc->dbf, MYSQL_OPT_WRITE_TIMEOUT, (
const void *) &
timeout);
260 mysql_password = NULL;
292 mysql_real_connect (
mc->dbf,
297 (
unsigned int) mysql_port,
299 CLIENT_IGNORE_SIGPIPE);
304 if (mysql_error (
mc->dbf)[0])
346 for (
sh =
mc->shead; NULL !=
sh;
sh =
sh->next)
350 mysql_stmt_close (
sh->statement);
353 sh->statement = NULL;
357 mysql_close (
mc->dbf);
374 while (NULL != (
sh =
mc->shead))
381 mysql_library_end ();
420 mysql_query (
mc->dbf, sql);
421 if (mysql_error (
mc->dbf)[0])
446 sh->statement = mysql_stmt_init (
mc->dbf);
447 if (NULL ==
sh->statement)
452 if (0 != mysql_stmt_prepare (
sh->statement,
sh->query, strlen (
sh->query)))
456 "prepare_statement: %s\n",
459 mysql_stmt_close (
sh->statement);
460 sh->statement = NULL;
481 return sh->statement;
static void reconnect(struct GNUNET_ABD_Handle *handle)
Reconnect to ABD service.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
static struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
static struct SolverHandle * sh
static struct GNUNET_SCHEDULER_Task * st
The shutdown task.
static struct GNUNET_TESTBED_Controller * mc
Handle to the master controller.
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_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.
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_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.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
#define GNUNET_log_from(kind, comp,...)
#define GNUNET_log_from_strerror(level, component, cmd)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_ERROR
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_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_MYSQL_StatementHandle * GNUNET_MYSQL_statement_prepare(struct GNUNET_MYSQL_Context *mc, const char *query)
Prepare a statement.
MYSQL_STMT * GNUNET_MYSQL_statement_get_stmt(struct GNUNET_MYSQL_StatementHandle *sh)
Get internal handle for a prepared statement.
struct GNUNET_MYSQL_Context * GNUNET_MYSQL_context_create(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
Create a mysql context.
void GNUNET_MYSQL_statements_invalidate(struct GNUNET_MYSQL_Context *mc)
Close database connection and all prepared statements (we got a DB error).
void GNUNET_MYSQL_context_destroy(struct GNUNET_MYSQL_Context *mc)
Destroy a mysql context.
int GNUNET_MYSQL_statement_run(struct GNUNET_MYSQL_Context *mc, const char *sql)
Run a SQL statement.
static int iopen(struct GNUNET_MYSQL_Context *mc)
Open the connection with the database (and initialize our default options).
#define LOG_MYSQL(level, cmd, dbh)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' on file 'file...
static char * get_my_cnf_path(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
Obtain the location of ".my.cnf".
static int prepare_statement(struct GNUNET_MYSQL_StatementHandle *sh)
Prepare a statement for running.
MYSQL * dbf
Handle to the mysql database.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
const char * section
Our section.
struct GNUNET_MYSQL_StatementHandle * stail
Tail of list of our prepared statements.
struct GNUNET_MYSQL_StatementHandle * shead
Head of list of our prepared statements.
char * cnffile
Filename of "my.cnf" (msyql configuration).
Handle for a prepared statement.
struct GNUNET_MYSQL_StatementHandle * prev
Kept in a DLL.
struct GNUNET_MYSQL_StatementHandle * next
Kept in a DLL.
char * query
Original query string.
MYSQL_STMT * statement
Handle to MySQL prepared statement.
struct GNUNET_MYSQL_Context * mc
Mysql Context the statement handle belongs to.
int valid
Is the MySQL prepared statement valid, or do we need to re-initialize it?
struct GNUNET_CONFIGURATION_Handle * cfg
The configuration to use while connecting to controller.