From: Florian Forster Date: Mon, 25 Mar 2013 06:28:36 +0000 (+0100) Subject: mysql plugin: Call mysql_init() only once. X-Git-Tag: collectd-4.10.9~6 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=dee89f856ffba3fcfccf530cd98f4d515bb46728;p=collectd.git mysql plugin: Call mysql_init() only once. 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. --- diff --git a/src/mysql.c b/src/mysql.c index 6f9efe35..8b3cd210 100644 --- a/src/mysql.c +++ b/src/mysql.c @@ -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 - : "", - 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 + : "", + 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 : "", (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 : "", - 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 : "", + 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)