3 This file is part of GNUnet
4 Copyright (C) 2012 GNUnet e.V.
6 GNUnet is free software: you can redistribute it and/or modify it
7 under the terms of the GNU Affero General Public License as published
8 by the Free Software Foundation, either version 3 of the License,
9 or (at your option) any later version.
11 GNUnet is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Affero General Public License for more details.
16 You should have received a copy of the GNU Affero General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 SPDX-License-Identifier: AGPL3.0-or-later
23 * @brief library to help with access to a MySQL database
24 * @author Christian Grothoff
27 #include <mysql/mysql.h>
28 #include "gnunet_mysql_lib.h"
31 * Maximum number of supported parameters for a prepared
32 * statement. Increase if needed.
38 * Die with an error message that indicates
39 * a failure of the command 'cmd' with the message given
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)); \
56 * Log an error message at log-level 'level' that indicates
57 * a failure of the command 'cmd' on file 'filename'
58 * with the message given by strerror(errno).
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)); \
76 struct GNUNET_MYSQL_Context
82 const struct GNUNET_CONFIGURATION_Handle *cfg;
90 * Handle to the mysql database.
95 * Head of list of our prepared statements.
97 struct GNUNET_MYSQL_StatementHandle *shead;
100 * Tail of list of our prepared statements.
102 struct GNUNET_MYSQL_StatementHandle *stail;
105 * Filename of "my.cnf" (msyql configuration).
112 * Handle for a prepared statement.
114 struct GNUNET_MYSQL_StatementHandle
120 struct GNUNET_MYSQL_StatementHandle *next;
125 struct GNUNET_MYSQL_StatementHandle *prev;
128 * Mysql Context the statement handle belongs to.
130 struct GNUNET_MYSQL_Context *mc;
133 * Original query string.
138 * Handle to MySQL prepared statement.
140 MYSQL_STMT *statement;
143 * Is the MySQL prepared statement valid, or do we need to re-initialize it?
150 * Obtain the location of ".my.cnf".
152 * @param cfg our configuration
153 * @param section the section
154 * @return NULL on error
157 get_my_cnf_path (const struct GNUNET_CONFIGURATION_Handle *cfg,
170 pw = getpwuid (getuid ());
173 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_ERROR, "mysql", "getpwuid");
176 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg, section, "CONFIG"))
178 GNUNET_assert (GNUNET_OK ==
179 GNUNET_CONFIGURATION_get_value_filename (cfg,
183 configured = GNUNET_YES;
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;
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;
199 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
201 _ ("Trying to use file `%s' for MySQL configuration.\n"),
203 if ((0 != STAT (cnffile, &st)) || (0 != ACCESS (cnffile, R_OK)) ||
204 (! S_ISREG (st.st_mode)))
206 if (configured == GNUNET_YES)
207 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
209 _ ("Could not access file `%s': %s\n"),
212 GNUNET_free (cnffile);
220 * Open the connection with the database (and initialize
221 * our default options).
223 * @param mc database context to initialze
224 * @return #GNUNET_OK on success
227 iopen (struct GNUNET_MYSQL_Context *mc)
232 char *mysql_password;
233 unsigned long long mysql_port;
235 unsigned int timeout;
237 mc->dbf = mysql_init (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");
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);
252 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "DATABASE"))
253 GNUNET_assert (GNUNET_OK ==
254 GNUNET_CONFIGURATION_get_value_string (mc->cfg,
259 mysql_dbname = GNUNET_strdup ("gnunet");
262 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "USER"))
264 GNUNET_assert (GNUNET_OK ==
265 GNUNET_CONFIGURATION_get_value_string (mc->cfg,
270 mysql_password = NULL;
272 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "PASSWORD"))
274 GNUNET_assert (GNUNET_OK ==
275 GNUNET_CONFIGURATION_get_value_string (mc->cfg,
282 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "HOST"))
284 GNUNET_assert (GNUNET_OK ==
285 GNUNET_CONFIGURATION_get_value_string (mc->cfg,
292 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "PORT"))
294 GNUNET_assert (GNUNET_OK ==
295 GNUNET_CONFIGURATION_get_value_number (mc->cfg,
301 GNUNET_assert (mysql_dbname != NULL);
302 mysql_real_connect (mc->dbf,
307 (unsigned int) mysql_port,
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])
316 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_real_connect", mc);
317 return GNUNET_SYSERR;
324 * Create a mysql context.
326 * @param cfg configuration
327 * @param section configuration section to use to get MySQL configuration options
328 * @return the mysql context
330 struct GNUNET_MYSQL_Context *
331 GNUNET_MYSQL_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
334 struct GNUNET_MYSQL_Context *mc;
336 mc = GNUNET_new (struct GNUNET_MYSQL_Context);
338 mc->section = section;
339 mc->cnffile = get_my_cnf_path (cfg, section);
346 * Close database connection and all prepared statements (we got a DB
349 * @param mc mysql context
352 GNUNET_MYSQL_statements_invalidate (struct GNUNET_MYSQL_Context *mc)
354 struct GNUNET_MYSQL_StatementHandle *sh;
356 for (sh = mc->shead; NULL != sh; sh = sh->next)
358 if (GNUNET_YES == sh->valid)
360 mysql_stmt_close (sh->statement);
361 sh->valid = GNUNET_NO;
363 sh->statement = NULL;
367 mysql_close (mc->dbf);
374 * Destroy a mysql context. Also frees all associated prepared statements.
376 * @param mc context to destroy
379 GNUNET_MYSQL_context_destroy (struct GNUNET_MYSQL_Context *mc)
381 struct GNUNET_MYSQL_StatementHandle *sh;
383 GNUNET_MYSQL_statements_invalidate (mc);
384 while (NULL != (sh = mc->shead))
386 GNUNET_CONTAINER_DLL_remove (mc->shead, mc->stail, sh);
387 GNUNET_free (sh->query);
391 mysql_library_end ();
396 * Prepare a statement. Prepared statements are automatically discarded
397 * when the MySQL context is destroyed.
399 * @param mc mysql context
400 * @param query query text
401 * @return prepared statement, NULL on error
403 struct GNUNET_MYSQL_StatementHandle *
404 GNUNET_MYSQL_statement_prepare (struct GNUNET_MYSQL_Context *mc,
407 struct GNUNET_MYSQL_StatementHandle *sh;
409 sh = GNUNET_new (struct GNUNET_MYSQL_StatementHandle);
411 sh->query = GNUNET_strdup (query);
412 GNUNET_CONTAINER_DLL_insert (mc->shead, mc->stail, sh);
418 * Run a SQL statement.
420 * @param mc mysql context
421 * @param sql SQL statement to run
422 * @return #GNUNET_OK on success
423 * #GNUNET_SYSERR if there was a problem
426 GNUNET_MYSQL_statement_run (struct GNUNET_MYSQL_Context *mc, const char *sql)
428 if ((NULL == mc->dbf) && (GNUNET_OK != iopen (mc)))
429 return GNUNET_SYSERR;
430 mysql_query (mc->dbf, sql);
431 if (mysql_error (mc->dbf)[0])
433 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_query", mc);
434 GNUNET_MYSQL_statements_invalidate (mc);
435 return GNUNET_SYSERR;
442 * Prepare a statement for running.
444 * @param mc mysql context
445 * @param sh statement handle to prepare
446 * @return #GNUNET_OK on success
449 prepare_statement (struct GNUNET_MYSQL_StatementHandle *sh)
451 struct GNUNET_MYSQL_Context *mc = sh->mc;
453 if (GNUNET_YES == sh->valid)
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)
460 GNUNET_MYSQL_statements_invalidate (mc);
461 return GNUNET_SYSERR;
463 if (0 != mysql_stmt_prepare (sh->statement, sh->query, strlen (sh->query)))
465 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
467 "prepare_statement: %s\n",
469 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_stmt_prepare", mc);
470 mysql_stmt_close (sh->statement);
471 sh->statement = NULL;
472 GNUNET_MYSQL_statements_invalidate (mc);
473 return GNUNET_SYSERR;
475 sh->valid = GNUNET_YES;
481 * Get internal handle for a prepared statement. This function should rarely
482 * be used, and if, with caution! On failures during the interaction with
483 * the handle, you must call 'GNUNET_MYSQL_statements_invalidate'!
485 * @param sh prepared statement to introspect
486 * @return MySQL statement handle, NULL on error
489 GNUNET_MYSQL_statement_get_stmt (struct GNUNET_MYSQL_StatementHandle *sh)
491 (void) prepare_statement (sh);
492 return sh->statement;