Rename afs.[ch] to afs_common.[ch].
[paraslash.git] / mysql_selector.c
index a6c667725bee0ddce273621dbc9d9877345580b3..46fc65b88b432bdba89fa6b892b3c180ba720a0e 100644 (file)
@@ -1,19 +1,7 @@
 /*
  * Copyright (C) 1999-2007 Andre Noll <maan@systemlinux.org>
  *
- *     This program is free software; you can redistribute it and/or modify
- *     it under the terms of the GNU General Public License as published by
- *     the Free Software Foundation; either version 2 of the License, or
- *     (at your option) any later version.
- *
- *     This program is distributed in the hope that it will be useful,
- *     but WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *     GNU General Public License for more details.
- *
- *     You should have received a copy of the GNU General Public License
- *     along with this program; if not, write to the Free Software
- *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
 /** \file mysql_selector.c para_server's mysql-based audio file selector */
@@ -25,7 +13,7 @@
 #include "server.cmdline.h"
 #include "server.h"
 #include "vss.h"
-#include "afs.h"
+#include "afs_common.h"
 #include <mysql/mysql.h>
 #include <mysql/mysql_version.h>
 #include <regex.h>
 #include "string.h"
 #include "user_list.h"
 #include "mysql_selector_command_list.h"
+#include "ipc.h"
 
 /** pointer to the shared memory area */
 extern struct misc_meta_data *mmd;
 
 static void *mysql_ptr = NULL;
+static int mysql_lock;
 
 /**
  * contains name/replacement pairs used by s_a_r_list()
@@ -77,11 +67,11 @@ static const struct para_macro mysql_macro_list[] = {
 };
 
 /**
- * simple search and replace routine
+ * Simple search and replace routine.
  *
- * \param src source string
- * \param macro_name the name of the macro
- * \param replacement the replacement format string
+ * \param src Source String.
+ * \param macro_name The name of the macro.
+ * \param replacement The replacement format string.
  *
  * In \p src, replace each occurence of \p macro_name(arg) by the string
  * determined by the \p replacement format string. \p replacement may (but
@@ -89,7 +79,7 @@ static const struct para_macro mysql_macro_list[] = {
  * replaced by \p arg.
  *
  * \return A string in which all matches in \p src are replaced, or \p NULL if
- * an syntax error was encountered. Caller must free the result.
+ * an error was encountered. Caller must free the result.
  *
  * \sa regcomp(3)
  */
@@ -105,7 +95,8 @@ __must_check __malloc static char *s_a_r(const char *src, const char* macro_name
 
        if (!macro_name || !replacement || !src)
                return para_strdup(src);
-       regcomp(&preg, macro_name, 0);
+       if (regcomp(&preg, macro_name, 0) != 0)
+               return NULL;
        while (regexec(&preg,  bufptr, nmatch, pmatch, eflags)
                        != REG_NOMATCH) {
                char *tmp, *arg, *o_bracket, *c_bracket;
@@ -164,7 +155,7 @@ __must_check __malloc static char *s_a_r_list(const struct para_macro *macro_lis
        return ret;
 }
 
-static int real_query(const char *query)
+static int lockless_real_query(const char *query)
 {
        if (!mysql_ptr)
                return -E_NOTCONN;
@@ -177,19 +168,32 @@ static int real_query(const char *query)
        return 1;
 }
 
+static int real_query(const char *query)
+{
+       int ret;
+
+       mutex_lock(mysql_lock);
+       ret = lockless_real_query(query);
+       mutex_unlock(mysql_lock);
+       return ret;
+}
+
 /*
  * Use open connection given by mysql_ptr to query server. Returns a
  * result pointer on succes and NULL on errors
  */
 static struct MYSQL_RES *get_result(const char *query)
 {
-       void *result;
+       void *result = NULL;
 
-       if (real_query(query) < 0)
-               return NULL;
+       mutex_lock(mysql_lock);
+       if (lockless_real_query(query) < 0)
+               goto out;
        result = mysql_store_result(mysql_ptr);
        if (!result)
                PARA_ERROR_LOG("%s", "store_result error\n");
+out:
+       mutex_unlock(mysql_lock);
        return result;
 }
 /*
@@ -1959,7 +1963,7 @@ static int com_vrfy_clean(int fd, int argc, __a_unused char *argv[])
                goto out;
        }
        if (vrfy_mode) {
-               send_va_buffer(fd, "found %i invalid entr%s\n", num_rows,
+               send_va_buffer(fd, "found %lli invalid entr%s\n", num_rows,
                        num_rows == 1? "y" : "ies");
                ret = print_results(fd, result, top, left, num_rows - 1, right);
                goto out;
@@ -2256,11 +2260,19 @@ out:
 
 static void shutdown_connection(void)
 {
+       int ret;
+
        if (mysql_ptr) {
                PARA_NOTICE_LOG("%s", "shutting down mysql connection\n");
                mysql_close(mysql_ptr);
                mysql_ptr = NULL;
        }
+       if (mysql_lock) {
+               ret = mutex_destroy(mysql_lock);
+               if (ret < 0)
+                       PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
+               mysql_lock = 0;
+       }
 }
 
 /**
@@ -2292,6 +2304,10 @@ int mysql_selector_init(struct audio_file_selector *db)
        db->get_audio_file_list = server_get_audio_file_list;
        db->update_audio_file = update_audio_file_server_handler;
        db->shutdown = shutdown_connection;
+       ret = mutex_new();
+       if (ret < 0)
+               return ret;
+       mysql_lock = ret;
        ret = init_mysql_server();
        if (ret < 0)
                PARA_WARNING_LOG("%s\n", PARA_STRERROR(-ret));