2 This file is part of GNUnet
3 Copyright (C) 2012 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @brief library to help with access to a MySQL database
23 * @author Christian Grothoff
26 #include <mysql/mysql.h>
27 #include "gnunet_mysql_lib.h"
30 * Maximum number of supported parameters for a prepared
31 * statement. Increase if needed.
37 * Die with an error message that indicates
38 * a failure of the command 'cmd' with the message given
41 #define DIE_MYSQL(cmd, dbh) 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);
44 * Log an error message at log-level 'level' that indicates
45 * a failure of the command 'cmd' on file 'filename'
46 * with the message given by strerror(errno).
48 #define LOG_MYSQL(level, cmd, dbh) do { GNUNET_log_from (level, "mysql", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, mysql_error((dbh)->dbf)); } while(0);
54 struct GNUNET_MYSQL_Context
60 const struct GNUNET_CONFIGURATION_Handle *cfg;
68 * Handle to the mysql database.
73 * Head of list of our prepared statements.
75 struct GNUNET_MYSQL_StatementHandle *shead;
78 * Tail of list of our prepared statements.
80 struct GNUNET_MYSQL_StatementHandle *stail;
83 * Filename of "my.cnf" (msyql configuration).
91 * Handle for a prepared statement.
93 struct GNUNET_MYSQL_StatementHandle
99 struct GNUNET_MYSQL_StatementHandle *next;
104 struct GNUNET_MYSQL_StatementHandle *prev;
107 * Mysql Context the statement handle belongs to.
109 struct GNUNET_MYSQL_Context *mc;
112 * Original query string.
117 * Handle to MySQL prepared statement.
119 MYSQL_STMT *statement;
122 * Is the MySQL prepared statement valid, or do we need to re-initialize it?
130 * Obtain the location of ".my.cnf".
132 * @param cfg our configuration
133 * @param section the section
134 * @return NULL on error
137 get_my_cnf_path (const struct GNUNET_CONFIGURATION_Handle *cfg,
150 pw = getpwuid (getuid ());
153 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_ERROR, "mysql", "getpwuid");
156 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg, section, "CONFIG"))
158 GNUNET_assert (GNUNET_OK ==
159 GNUNET_CONFIGURATION_get_value_filename (cfg, section,
162 configured = GNUNET_YES;
166 home_dir = GNUNET_strdup (pw->pw_dir);
167 GNUNET_asprintf (&cnffile, "%s/.my.cnf", home_dir);
168 GNUNET_free (home_dir);
169 configured = GNUNET_NO;
172 home_dir = (char *) GNUNET_malloc (_MAX_PATH + 1);
173 plibc_conv_to_win_path ("~/", home_dir);
174 GNUNET_asprintf (&cnffile, "%s/.my.cnf", home_dir);
175 GNUNET_free (home_dir);
176 configured = GNUNET_NO;
178 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "mysql",
179 _("Trying to use file `%s' for MySQL configuration.\n"),
181 if ((0 != STAT (cnffile, &st)) || (0 != ACCESS (cnffile, R_OK)) ||
182 (!S_ISREG (st.st_mode)))
184 if (configured == GNUNET_YES)
185 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
186 _("Could not access file `%s': %s\n"), cnffile,
188 GNUNET_free (cnffile);
196 * Open the connection with the database (and initialize
197 * our default options).
199 * @param mc database context to initialze
200 * @return GNUNET_OK on success
203 iopen (struct GNUNET_MYSQL_Context *mc)
208 char *mysql_password;
209 unsigned long long mysql_port;
211 unsigned int timeout;
213 mc->dbf = mysql_init (NULL);
215 return GNUNET_SYSERR;
216 if (mc->cnffile != NULL)
217 mysql_options (mc->dbf, MYSQL_READ_DEFAULT_FILE, mc->cnffile);
218 mysql_options (mc->dbf, MYSQL_READ_DEFAULT_GROUP, "client");
220 mysql_options (mc->dbf, MYSQL_OPT_RECONNECT, &reconnect);
221 mysql_options (mc->dbf, MYSQL_OPT_CONNECT_TIMEOUT, (const void *) &timeout);
222 mysql_options (mc->dbf, MYSQL_SET_CHARSET_NAME, "UTF8");
223 timeout = 60; /* in seconds */
224 mysql_options (mc->dbf, MYSQL_OPT_READ_TIMEOUT, (const void *) &timeout);
225 mysql_options (mc->dbf, MYSQL_OPT_WRITE_TIMEOUT, (const void *) &timeout);
228 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "DATABASE"))
229 GNUNET_assert (GNUNET_OK ==
230 GNUNET_CONFIGURATION_get_value_string (mc->cfg, mc->section,
234 mysql_dbname = GNUNET_strdup ("gnunet");
237 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "USER"))
239 GNUNET_assert (GNUNET_OK ==
240 GNUNET_CONFIGURATION_get_value_string (mc->cfg, mc->section,
241 "USER", &mysql_user));
243 mysql_password = NULL;
245 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "PASSWORD"))
247 GNUNET_assert (GNUNET_OK ==
248 GNUNET_CONFIGURATION_get_value_string (mc->cfg, mc->section,
254 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "HOST"))
256 GNUNET_assert (GNUNET_OK ==
257 GNUNET_CONFIGURATION_get_value_string (mc->cfg, mc->section,
263 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "PORT"))
265 GNUNET_assert (GNUNET_OK ==
266 GNUNET_CONFIGURATION_get_value_number (mc->cfg, mc->section,
267 "PORT", &mysql_port));
270 GNUNET_assert (mysql_dbname != NULL);
271 mysql_real_connect (mc->dbf, mysql_server, mysql_user, mysql_password,
272 mysql_dbname, (unsigned int) mysql_port, NULL,
273 CLIENT_IGNORE_SIGPIPE);
274 GNUNET_free_non_null (mysql_server);
275 GNUNET_free_non_null (mysql_user);
276 GNUNET_free_non_null (mysql_password);
277 GNUNET_free (mysql_dbname);
278 if (mysql_error (mc->dbf)[0])
280 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_real_connect", mc);
281 return GNUNET_SYSERR;
288 * Create a mysql context.
290 * @param cfg configuration
291 * @param section configuration section to use to get MySQL configuration options
292 * @return the mysql context
294 struct GNUNET_MYSQL_Context *
295 GNUNET_MYSQL_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
298 struct GNUNET_MYSQL_Context *mc;
300 mc = GNUNET_new (struct GNUNET_MYSQL_Context);
302 mc->section = section;
303 mc->cnffile = get_my_cnf_path (cfg, section);
310 * Close database connection and all prepared statements (we got a DB
313 * @param mc mysql context
316 GNUNET_MYSQL_statements_invalidate (struct GNUNET_MYSQL_Context *mc)
318 struct GNUNET_MYSQL_StatementHandle *sh;
320 for (sh = mc->shead; NULL != sh; sh = sh->next)
322 if (GNUNET_YES == sh->valid)
324 mysql_stmt_close (sh->statement);
325 sh->valid = GNUNET_NO;
327 sh->statement = NULL;
331 mysql_close (mc->dbf);
338 * Destroy a mysql context. Also frees all associated prepared statements.
340 * @param mc context to destroy
343 GNUNET_MYSQL_context_destroy (struct GNUNET_MYSQL_Context *mc)
345 struct GNUNET_MYSQL_StatementHandle *sh;
347 GNUNET_MYSQL_statements_invalidate (mc);
348 while (NULL != (sh = mc->shead))
350 GNUNET_CONTAINER_DLL_remove (mc->shead, mc->stail, sh);
351 GNUNET_free (sh->query);
355 mysql_library_end ();
360 * Prepare a statement. Prepared statements are automatically discarded
361 * when the MySQL context is destroyed.
363 * @param mc mysql context
364 * @param query query text
365 * @return prepared statement, NULL on error
367 struct GNUNET_MYSQL_StatementHandle *
368 GNUNET_MYSQL_statement_prepare (struct GNUNET_MYSQL_Context *mc,
371 struct GNUNET_MYSQL_StatementHandle *sh;
373 sh = GNUNET_new (struct GNUNET_MYSQL_StatementHandle);
375 sh->query = GNUNET_strdup (query);
376 GNUNET_CONTAINER_DLL_insert (mc->shead, mc->stail, sh);
382 * Run a SQL statement.
384 * @param mc mysql context
385 * @param sql SQL statement to run
386 * @return GNUNET_OK on success
387 * GNUNET_SYSERR if there was a problem
390 GNUNET_MYSQL_statement_run (struct GNUNET_MYSQL_Context *mc, const char *sql)
392 if ((NULL == mc->dbf) && (GNUNET_OK != iopen (mc)))
393 return GNUNET_SYSERR;
394 mysql_query (mc->dbf, sql);
395 if (mysql_error (mc->dbf)[0])
397 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_query", mc);
398 GNUNET_MYSQL_statements_invalidate (mc);
399 return GNUNET_SYSERR;
406 * Prepare a statement for running.
408 * @param mc mysql context
409 * @param sh statement handle to prepare
410 * @return GNUNET_OK on success
413 prepare_statement (struct GNUNET_MYSQL_StatementHandle *sh)
415 struct GNUNET_MYSQL_Context *mc = sh->mc;
417 if (GNUNET_YES == sh->valid)
419 if ((NULL == mc->dbf) && (GNUNET_OK != iopen (mc)))
420 return GNUNET_SYSERR;
421 sh->statement = mysql_stmt_init (mc->dbf);
422 if (NULL == sh->statement)
424 GNUNET_MYSQL_statements_invalidate (mc);
425 return GNUNET_SYSERR;
427 if (0 != mysql_stmt_prepare (sh->statement, sh->query, strlen (sh->query)))
429 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_stmt_prepare", mc);
430 mysql_stmt_close (sh->statement);
431 sh->statement = NULL;
432 GNUNET_MYSQL_statements_invalidate (mc);
433 return GNUNET_SYSERR;
435 sh->valid = GNUNET_YES;
441 * Get internal handle for a prepared statement. This function should rarely
442 * be used, and if, with caution! On failures during the interaction with
443 * the handle, you must call 'GNUNET_MYSQL_statements_invalidate'!
445 * @param sh prepared statement to introspect
446 * @return MySQL statement handle, NULL on error
449 GNUNET_MYSQL_statement_get_stmt (struct GNUNET_MYSQL_StatementHandle *sh)
451 (void) prepare_statement (sh);
452 return sh->statement;