Update of /usr/cvsroot/asterisk-addons
In directory mongoose.digium.com:/tmp/cvs-serv21894

Modified Files:
        res_config_mysql.c 
Log Message:
handle connection locking more properly (issue #4973)


Index: res_config_mysql.c
===================================================================
RCS file: /usr/cvsroot/asterisk-addons/res_config_mysql.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- res_config_mysql.c  26 Aug 2005 20:34:41 -0000      1.9
+++ res_config_mysql.c  13 Oct 2005 20:43:54 -0000      1.10
@@ -8,6 +8,8 @@
  *
  * res_config_mysql.c <mysql plugin for RealTime configuration engine>
  *
+ * v2.0   - (10-07-05) - mutex_lock fixes (bug #4973, comment #0034602)
+ *
  * v1.9   - (08-19-05) - Added support to correctly honor the family database 
specified
  *                       in extconfig.conf (bug #4973)
  *
@@ -123,11 +125,12 @@
        ast_log(LOG_DEBUG, "MySQL RealTime: Retrieve SQL: %s\n", sql);
 
        /* We now have our complete statement; Lets connect to the server and 
execute it. */
+       ast_mutex_lock(&mysql_lock);
        if(!mysql_reconnect(database)) {
+               ast_mutex_unlock(&mysql_lock);
                return NULL;
        }
 
-       ast_mutex_lock(&mysql_lock);
        if(mysql_real_query(&mysql, sql, strlen(sql))) {
                ast_log(LOG_WARNING, "MySQL RealTime: Failed to query database. 
Check debug for more info.\n");
                ast_log(LOG_DEBUG, "MySQL RealTime: Query: %s\n", sql);
@@ -234,11 +237,12 @@
        ast_log(LOG_DEBUG, "MySQL RealTime: Retrieve SQL: %s\n", sql);
 
        /* We now have our complete statement; Lets connect to the server and 
execute it. */
+       ast_mutex_lock(&mysql_lock);
        if(!mysql_reconnect(database)) {
+               ast_mutex_unlock(&mysql_lock);
                return NULL;
        }
 
-       ast_mutex_lock(&mysql_lock);
        if(mysql_real_query(&mysql, sql, strlen(sql))) {
                ast_log(LOG_WARNING, "MySQL RealTime: Failed to query database. 
Check debug for more info.\n");
                ast_log(LOG_DEBUG, "MySQL RealTime: Query: %s\n", sql);
@@ -317,17 +321,18 @@
        ast_log(LOG_DEBUG,"MySQL RealTime: Update SQL: %s\n", sql);
 
        /* We now have our complete statement; Lets connect to the server and 
execute it. */
+       ast_mutex_lock(&mysql_lock);
        if(!mysql_reconnect(database)) {
-               return -1;
+               ast_mutex_unlock(&mysql_lock);
+               return -1;
        }
 
-       ast_mutex_lock(&mysql_lock);
        if(mysql_real_query(&mysql, sql, strlen(sql))) {
                ast_log(LOG_WARNING, "MySQL RealTime: Failed to query database. 
Check debug for more info.\n");
                ast_log(LOG_DEBUG, "MySQL RealTime: Query: %s\n", sql);
                ast_log(LOG_DEBUG, "MySQL RealTime: Query Failed because: 
%s\n", mysql_error(&mysql));
                ast_mutex_unlock(&mysql_lock);
-               return -1;
+               return -1;
        }
 
        numrows = mysql_affected_rows(&mysql);
@@ -335,14 +340,14 @@
 
        ast_log(LOG_DEBUG,"MySQL RealTime: Updated %llu rows on table: %s\n", 
numrows, table);
 
-       /* From http://dev.mysql.com/doc/mysql/en/mysql-affected-rows.html
-        * An integer greater than zero indicates the number of rows affected
-        * Zero indicates that no records were updated
-        * -1 indicates that the query returned an error (although, if the 
query failed, it should have been caught above.)
-       */
+       /* From http://dev.mysql.com/doc/mysql/en/mysql-affected-rows.html
+        * An integer greater than zero indicates the number of rows affected
+        * Zero indicates that no records were updated
+        * -1 indicates that the query returned an error (although, if the 
query failed, it should have been caught above.)
+       */
 
-       if(numrows >= 0)
-               return (int)numrows;
+       if(numrows >= 0)
+               return (int)numrows;
 
        return -1;
 }
@@ -373,7 +378,9 @@
        ast_log(LOG_DEBUG, "MySQL RealTime: Static SQL: %s\n", sql);
 
        /* We now have our complete statement; Lets connect to the server and 
execute it. */
+       ast_mutex_lock(&mysql_lock);
        if(!mysql_reconnect(database)) {
+               ast_mutex_unlock(&mysql_lock);
                return NULL;
        }
 
@@ -437,6 +444,8 @@
 {
        parse_config();
 
+       ast_mutex_lock(&mysql_lock);
+
        if(!mysql_reconnect(NULL)) {
                ast_log(LOG_WARNING, "MySQL RealTime: Couldn't establish 
connection. Check debug.\n");
                ast_log(LOG_DEBUG, "MySQL RealTime: Cannot Connect: %s\n", 
mysql_error(&mysql));
@@ -448,6 +457,8 @@
        }
        ast_cli_register(&cli_realtime_mysql_status);
 
+       ast_mutex_unlock(&mysql_lock);
+
        return 0;
 }
 
@@ -480,9 +491,6 @@
        connected = 0;
        parse_config();
 
-       /* Need to unlock so that mysql_reconnect can regain the lock. */
-       ast_mutex_unlock(&mysql_lock);
-
        if(!mysql_reconnect(NULL)) {
                ast_log(LOG_WARNING, "MySQL RealTime: Couldn't establish 
connection. Check debug.\n");
                ast_log(LOG_DEBUG, "MySQL RealTime: Cannot Connect: %s\n", 
mysql_error(&mysql));
@@ -490,6 +498,9 @@
 
        ast_verbose(VERBOSE_PREFIX_2 "MySQL RealTime reloaded.\n");
 
+       /* Done reloading. Release lock so others can now use driver. */
+       ast_mutex_unlock(&mysql_lock);
+
        return 0;
 }
 
@@ -587,25 +598,23 @@
        else
                ast_copy_string(my_database, database, sizeof(my_database));
 
-       ast_mutex_lock(&mysql_lock);
+       /* mutex lock should have been locked before calling this function. */
+
        if((!connected) && (dbhost || dbsock) && dbuser && dbpass && 
my_database) {
                if(!mysql_init(&mysql)) {
                        ast_log(LOG_WARNING, "MySQL RealTime: Insufficient 
memory to allocate MySQL resource.\n");
                        connected = 0;
-                       ast_mutex_unlock(&mysql_lock);
                        return 0;
                }
                if(mysql_real_connect(&mysql, dbhost, dbuser, dbpass, 
my_database, dbport, dbsock, 0)) {
                        ast_log(LOG_DEBUG, "MySQL RealTime: Successfully 
connected to database.\n");
                        connected = 1;
                        connect_time = time(NULL);
-                       ast_mutex_unlock(&mysql_lock);
                        return 1;
                } else {
                        ast_log(LOG_ERROR, "MySQL RealTime: Failed to connect 
database server %s on %s. Check debug for more info.\n", dbname, dbhost);
                        ast_log(LOG_DEBUG, "MySQL RealTime: Cannot Connect: 
%s\n", mysql_error(&mysql));
                        connected = 0;
-                       ast_mutex_unlock(&mysql_lock);
                        return 0;
                }
        } else {
@@ -613,7 +622,6 @@
                        connected = 0;
                        ast_log(LOG_ERROR, "MySQL RealTime: Failed to 
reconnect. Check debug for more info.\n");
                        ast_log(LOG_DEBUG, "MySQL RealTime: Server Error: 
%s\n", mysql_error(&mysql));
-                       ast_mutex_unlock(&mysql_lock);
                        return 0;
                }
 
@@ -622,12 +630,10 @@
                if(mysql_select_db(&mysql, my_database) != 0) {
                        ast_log(LOG_WARNING, "MySQL RealTime: Unable to select 
database: %s. Still Connected.\n", my_database);
                        ast_log(LOG_DEBUG, "MySQL RealTime: Database Select 
Failed: %s\n", mysql_error(&mysql));
-                       ast_mutex_unlock(&mysql_lock);
                        return 0;
                }
 
                ast_log(LOG_DEBUG, "MySQL RealTime: Everything is fine.\n");
-               ast_mutex_unlock(&mysql_lock);
                return 1;
        }
 }

_______________________________________________
Asterisk-Cvs mailing list
[email protected]
http://lists.digium.com/mailman/listinfo/asterisk-cvs

Reply via email to