summaryrefslogtreecommitdiff
path: root/src/libstrongswan/plugins/mysql/mysql_database.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstrongswan/plugins/mysql/mysql_database.c')
-rw-r--r--src/libstrongswan/plugins/mysql/mysql_database.c135
1 files changed, 66 insertions, 69 deletions
diff --git a/src/libstrongswan/plugins/mysql/mysql_database.c b/src/libstrongswan/plugins/mysql/mysql_database.c
index 341217dd4..2338428f2 100644
--- a/src/libstrongswan/plugins/mysql/mysql_database.c
+++ b/src/libstrongswan/plugins/mysql/mysql_database.c
@@ -15,13 +15,13 @@
#define _GNU_SOURCE
#include <string.h>
-#include <pthread.h>
-#include <mysql/mysql.h>
+#include <mysql.h>
#include "mysql_database.h"
#include <debug.h>
-#include <utils/mutex.h>
+#include <threading/thread_value.h>
+#include <threading/mutex.h>
#include <utils/linked_list.h>
/* Older mysql.h headers do not define it, but we need it. It is not returned
@@ -42,37 +42,37 @@ struct private_mysql_database_t {
* public functions
*/
mysql_database_t public;
-
+
/**
* connection pool, contains conn_t
*/
linked_list_t *pool;
-
+
/**
* mutex to lock pool
*/
mutex_t *mutex;
-
+
/**
- * hostname to connect to
- */
+ * hostname to connect to
+ */
char *host;
-
+
/**
* username to use
*/
char *username;
-
+
/**
* password
*/
char *password;
-
+
/**
* database name
*/
char *database;
-
+
/**
* tcp port
*/
@@ -85,12 +85,12 @@ typedef struct conn_t conn_t;
* connection pool entry
*/
struct conn_t {
-
+
/**
* MySQL database connection
*/
MYSQL *mysql;
-
+
/**
* connection in use?
*/
@@ -104,19 +104,20 @@ static void conn_release(conn_t *conn)
{
conn->in_use = FALSE;
}
+
/**
* thread specific initialization flag
*/
-pthread_key_t initialized;
+thread_value_t *initialized;
/**
* Initialize a thread for mysql usage
*/
static void thread_initialize()
{
- if (pthread_getspecific(initialized) == NULL)
+ if (initialized->get(initialized) == NULL)
{
- pthread_setspecific(initialized, (void*)TRUE);
+ initialized->set(initialized, (void*)TRUE);
mysql_thread_init();
}
}
@@ -130,11 +131,7 @@ bool mysql_database_init()
{
return FALSE;
}
- if (pthread_key_create(&initialized, (void*)mysql_thread_end))
- {
- mysql_library_end();
- return FALSE;
- }
+ initialized = thread_value_create((thread_cleanup_t)mysql_thread_end);
return TRUE;
}
@@ -143,7 +140,7 @@ bool mysql_database_init()
*/
void mysql_database_deinit()
{
- pthread_key_delete(initialized);
+ initialized->destroy(initialized);
mysql_thread_end();
/* mysql_library_end(); would be the clean way, however, it hangs... */
}
@@ -164,9 +161,9 @@ static conn_t *conn_get(private_mysql_database_t *this)
{
conn_t *current, *found = NULL;
enumerator_t *enumerator;
-
+
thread_initialize();
-
+
while (TRUE)
{
this->mutex->lock(this->mutex);
@@ -231,28 +228,28 @@ static MYSQL_STMT* run(MYSQL *mysql, char *sql, va_list *args)
{
MYSQL_STMT *stmt;
int params;
-
+
stmt = mysql_stmt_init(mysql);
if (stmt == NULL)
{
- DBG1("creating MySQL statement failed: %s", mysql_error(mysql));
+ DBG1("creating MySQL statement failed: %s", mysql_error(mysql));
return NULL;
}
if (mysql_stmt_prepare(stmt, sql, strlen(sql)))
{
- DBG1("preparing MySQL statement failed: %s", mysql_stmt_error(stmt));
- mysql_stmt_close(stmt);
- return NULL;
+ DBG1("preparing MySQL statement failed: %s", mysql_stmt_error(stmt));
+ mysql_stmt_close(stmt);
+ return NULL;
}
params = mysql_stmt_param_count(stmt);
if (params > 0)
{
int i;
MYSQL_BIND *bind;
-
+
bind = alloca(sizeof(MYSQL_BIND) * params);
memset(bind, 0, sizeof(MYSQL_BIND) * params);
-
+
for (i = 0; i < params; i++)
{
switch (va_arg(*args, db_type_t))
@@ -285,7 +282,7 @@ static MYSQL_STMT* run(MYSQL *mysql, char *sql, va_list *args)
break;
}
case DB_BLOB:
- {
+ {
chunk_t chunk = va_arg(*args, chunk_t);
bind[i].buffer_type = MYSQL_TYPE_BLOB;
bind[i].buffer = chunk.ptr;
@@ -300,28 +297,28 @@ static MYSQL_STMT* run(MYSQL *mysql, char *sql, va_list *args)
bind[i].buffer_length = sizeof(double);
break;
}
- case DB_NULL:
+ case DB_NULL:
{
bind[i].buffer_type = MYSQL_TYPE_NULL;
break;
}
default:
- DBG1("invalid data type supplied");
- mysql_stmt_close(stmt);
- return NULL;
+ DBG1("invalid data type supplied");
+ mysql_stmt_close(stmt);
+ return NULL;
}
}
if (mysql_stmt_bind_param(stmt, bind))
{
- DBG1("binding MySQL param failed: %s", mysql_stmt_error(stmt));
- mysql_stmt_close(stmt);
+ DBG1("binding MySQL param failed: %s", mysql_stmt_error(stmt));
+ mysql_stmt_close(stmt);
return NULL;
}
}
if (mysql_stmt_execute(stmt))
{
- DBG1("executing MySQL statement failed: %s", mysql_stmt_error(stmt));
- mysql_stmt_close(stmt);
+ DBG1("executing MySQL statement failed: %s", mysql_stmt_error(stmt));
+ mysql_stmt_close(stmt);
return NULL;
}
return stmt;
@@ -353,9 +350,9 @@ typedef struct {
static void mysql_enumerator_destroy(mysql_enumerator_t *this)
{
int columns, i;
-
+
columns = mysql_stmt_field_count(this->stmt);
-
+
for (i = 0; i < columns; i++)
{
switch (this->bind[i].buffer_type)
@@ -385,9 +382,9 @@ static bool mysql_enumerator_enumerate(mysql_enumerator_t *this, ...)
{
int i, columns;
va_list args;
-
+
columns = mysql_stmt_field_count(this->stmt);
-
+
/* free/reset data set of previous call */
for (i = 0; i < columns; i++)
{
@@ -419,7 +416,7 @@ static bool mysql_enumerator_enumerate(mysql_enumerator_t *this, ...)
DBG1("fetching MySQL row failed: %s", mysql_stmt_error(this->stmt));
return FALSE;
}
-
+
va_start(args, this);
for (i = 0; i < columns; i++)
{
@@ -445,9 +442,9 @@ static bool mysql_enumerator_enumerate(mysql_enumerator_t *this, ...)
this->bind[i].buffer = malloc(this->length[i]+1);
this->bind[i].buffer_length = this->length[i];
*value = this->bind[i].buffer;
- mysql_stmt_fetch_column(this->stmt, &this->bind[i], i, 0);
- ((char*)this->bind[i].buffer)[this->length[i]] = '\0';
- break;
+ mysql_stmt_fetch_column(this->stmt, &this->bind[i], i, 0);
+ ((char*)this->bind[i].buffer)[this->length[i]] = '\0';
+ break;
}
case MYSQL_TYPE_BLOB:
{
@@ -456,8 +453,8 @@ static bool mysql_enumerator_enumerate(mysql_enumerator_t *this, ...)
this->bind[i].buffer_length = this->length[i];
value->ptr = this->bind[i].buffer;
value->len = this->length[i];
- mysql_stmt_fetch_column(this->stmt, &this->bind[i], i, 0);
- break;
+ mysql_stmt_fetch_column(this->stmt, &this->bind[i], i, 0);
+ break;
}
case MYSQL_TYPE_DOUBLE:
{
@@ -481,7 +478,7 @@ static enumerator_t* query(private_mysql_database_t *this, char *sql, ...)
va_list args;
mysql_enumerator_t *enumerator = NULL;
conn_t *conn;
-
+
conn = conn_get(this);
if (!conn)
{
@@ -493,7 +490,7 @@ static enumerator_t* query(private_mysql_database_t *this, char *sql, ...)
if (stmt)
{
int columns, i;
-
+
enumerator = malloc_thing(mysql_enumerator_t);
enumerator->public.enumerate = (void*)mysql_enumerator_enumerate;
enumerator->public.destroy = (void*)mysql_enumerator_destroy;
@@ -527,7 +524,7 @@ static enumerator_t* query(private_mysql_database_t *this, char *sql, ...)
break;
}
case DB_BLOB:
- {
+ {
enumerator->bind[i].buffer_type = MYSQL_TYPE_BLOB;
enumerator->bind[i].length = &enumerator->length[i];
break;
@@ -539,17 +536,17 @@ static enumerator_t* query(private_mysql_database_t *this, char *sql, ...)
break;
}
default:
- DBG1("invalid result data type supplied");
- mysql_enumerator_destroy(enumerator);
- va_end(args);
- return NULL;
+ DBG1("invalid result data type supplied");
+ mysql_enumerator_destroy(enumerator);
+ va_end(args);
+ return NULL;
}
}
if (mysql_stmt_bind_result(stmt, enumerator->bind))
{
DBG1("binding MySQL result failed: %s", mysql_stmt_error(stmt));
- mysql_enumerator_destroy(enumerator);
- enumerator = NULL;
+ mysql_enumerator_destroy(enumerator);
+ enumerator = NULL;
}
}
else
@@ -569,7 +566,7 @@ static int execute(private_mysql_database_t *this, int *rowid, char *sql, ...)
va_list args;
conn_t *conn;
int affected = -1;
-
+
conn = conn_get(this);
if (!conn)
{
@@ -590,7 +587,7 @@ static int execute(private_mysql_database_t *this, int *rowid, char *sql, ...)
conn_release(conn);
return affected;
}
-
+
/**
* Implementation of database_t.get_driver
*/
@@ -646,7 +643,7 @@ static bool parse_uri(private_mysql_database_t *this, char *uri)
{
*pos = '\0';
database = pos + 1;
-
+
this->host = strdup(host);
this->username = strdup(username);
this->password = strdup(password);
@@ -668,19 +665,19 @@ mysql_database_t *mysql_database_create(char *uri)
{
conn_t *conn;
private_mysql_database_t *this;
-
+
if (!strneq(uri, "mysql://", 8))
{
return NULL;
}
this = malloc_thing(private_mysql_database_t);
-
+
this->public.db.query = (enumerator_t* (*)(database_t *this, char *sql, ...))query;
this->public.db.execute = (int (*)(database_t *this, int *rowid, char *sql, ...))execute;
this->public.db.get_driver = (db_driver_t(*)(database_t*))get_driver;
this->public.db.destroy = (void(*)(database_t*))destroy;
-
+
if (!parse_uri(this, uri))
{
free(this);
@@ -688,13 +685,13 @@ mysql_database_t *mysql_database_create(char *uri)
}
this->mutex = mutex_create(MUTEX_TYPE_DEFAULT);
this->pool = linked_list_create();
-
+
/* check connectivity */
conn = conn_get(this);
if (!conn)
{
- destroy(this);
- return NULL;
+ destroy(this);
+ return NULL;
}
conn_release(conn);
return &this->public;