mysql plugin: Call mysql_init() only once.
authorFlorian Forster <octo@collectd.org>
Mon, 25 Mar 2013 06:28:36 +0000 (07:28 +0100)
committerFlorian Forster <octo@collectd.org>
Mon, 25 Mar 2013 06:33:22 +0000 (07:33 +0100)
Apparently, if you call it with a structure allocated by mysql_init()
itself, it leaks memory. Thanks to Yves Mettier for pointing this out!

Fixes Github issue #274.

src/mysql.c

index 6f9efe3..8b3cd21 100644 (file)
@@ -59,7 +59,7 @@ struct mysql_database_s /* {{{ */
        int   slave_sql_running;
 
        MYSQL *con;
-       int    state;
+       _Bool  is_connected;
 };
 typedef struct mysql_database_s mysql_database_t; /* }}} */
 
@@ -359,30 +359,31 @@ static int mysql_config (oconfig_item_t *ci) /* {{{ */
 
 static MYSQL *getconnection (mysql_database_t *db)
 {
-       if (db->state != 0)
+       if (db->is_connected)
        {
-               int err;
-               if ((err = mysql_ping (db->con)) != 0)
-               {
-                       WARNING ("mysql_ping failed for %s: %s",
-                                       (db->instance != NULL)
-                                       ? db->instance
-                                       : "<legacy>",
-                                       mysql_error (db->con));
-                       db->state = 0;
-               }
-               else
-               {
-                       db->state = 1;
+               int status;
+
+               status = mysql_ping (db->con);
+               if (status == 0)
                        return (db->con);
-               }
+
+               WARNING ("mysql plugin: Lost connection to instance \"%s\": %s",
+                               (db->instance != NULL)
+                               ? db->instance
+                               : "<legacy>",
+                               mysql_error (db->con));
        }
+       db->is_connected = 0;
 
-       if ((db->con = mysql_init (db->con)) == NULL)
+       if (db->con == NULL)
        {
-               ERROR ("mysql_init failed: %s", mysql_error (db->con));
-               db->state = 0;
-               return (NULL);
+               db->con = mysql_init (NULL);
+               if (db->con == NULL)
+               {
+                       ERROR ("mysql plugin: mysql_init failed: %s",
+                                       mysql_error (db->con));
+                       return (NULL);
+               }
        }
 
        if (mysql_real_connect (db->con, db->host, db->user, db->pass,
@@ -393,20 +394,18 @@ static MYSQL *getconnection (mysql_database_t *db)
                                (db->database != NULL) ? db->database : "<none>",
                                (db->host != NULL) ? db->host : "localhost",
                                mysql_error (db->con));
-               db->state = 0;
                return (NULL);
        }
-       else
-       {
-               INFO ("mysql plugin: Successfully connected to database %s "
-                               "at server %s (server version: %s, protocol version: %d)",
-                               (db->database != NULL) ? db->database : "<none>",
-                               mysql_get_host_info (db->con),
-                               mysql_get_server_info (db->con),
-                               mysql_get_proto_info (db->con));
-               db->state = 1;
-               return (db->con);
-       }
+
+       INFO ("mysql plugin: Successfully connected to database %s "
+                       "at server %s (server version: %s, protocol version: %d)",
+                       (db->database != NULL) ? db->database : "<none>",
+                       mysql_get_host_info (db->con),
+                       mysql_get_server_info (db->con),
+                       mysql_get_proto_info (db->con));
+
+       db->is_connected = 1;
+       return (db->con);
 } /* static MYSQL *getconnection (mysql_database_t *db) */
 
 static void set_host (mysql_database_t *db, char *buf, size_t buflen)