+static void mysql_database_free (mysql_database_t *db) /* {{{ */
+{
+ if (db == NULL)
+ return;
+
+ sfree (db->host);
+ sfree (db->user);
+ sfree (db->pass);
+ sfree (db->socket);
+ sfree (db);
+} /* }}} void mysql_database_free */
+
+/* Configuration handling functions {{{
+ *
+ * <Plugin mysql>
+ * <Database "plugin_instance1">
+ * Host "localhost"
+ * Port 22000
+ * ...
+ * </Database>
+ * </Plugin>
+ */
+
+static int mysql_config_set_string (char **ret_string, /* {{{ */
+ oconfig_item_t *ci)
+{
+ char *string;
+
+ if ((ci->values_num != 1)
+ || (ci->values[0].type != OCONFIG_TYPE_STRING))
+ {
+ WARNING ("mysql plugin: The `%s' config option "
+ "needs exactly one string argument.", ci->key);
+ return (-1);
+ }
+
+ string = strdup (ci->values[0].value.string);
+ if (string == NULL)
+ {
+ ERROR ("mysql plugin: strdup failed.");
+ return (-1);
+ }
+
+ if (*ret_string != NULL)
+ free (*ret_string);
+ *ret_string = string;
+
+ return (0);
+} /* }}} int mysql_config_set_string */
+
+static int mysql_config_set_int (int *ret_int, /* {{{ */
+ oconfig_item_t *ci)
+{
+ if ((ci->values_num != 1)
+ || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
+ {
+ WARNING ("mysql plugin: The `%s' config option "
+ "needs exactly one string argument.", ci->key);
+ return (-1);
+ }
+
+ *ret_int = ci->values[0].value.number;
+
+ return (0);
+} /* }}} int mysql_config_set_int */
+
+static int mysql_config_add_database (oconfig_item_t *ci) /* {{{ */
+{
+ mysql_database_t *db;
+ int status;
+ int i;
+
+ if ((ci->values_num != 1)
+ || (ci->values[0].type != OCONFIG_TYPE_STRING))
+ {
+ WARNING ("mysql plugin: The `Database' block "
+ "needs exactly one string argument.");
+ return (-1);
+ }
+
+ db = (mysql_database_t *) malloc (sizeof (*db));
+ if (db == NULL)
+ {
+ ERROR ("mysql plugin: malloc failed.");
+ return (-1);
+ }
+ memset (db, 0, sizeof (*db));
+
+ status = mysql_config_set_string (&db->name, ci);
+ if (status != 0)
+ {
+ sfree (db);
+ return (status);
+ }
+
+ /* Fill the `mysql_database_t' structure.. */
+ for (i = 0; i < ci->children_num; i++)
+ {
+ oconfig_item_t *child = ci->children + i;
+
+ if (strcasecmp ("Host", child->key) == 0)
+ status = mysql_config_set_string (&db->host, child);
+ else if (strcasecmp ("User", child->key) == 0)
+ status = mysql_config_set_string (&db->user, child);
+ else if (strcasecmp ("Password", child->key) == 0)
+ status = mysql_config_set_string (&db->pass, child);
+ else if (strcasecmp ("Port", child->key) == 0)
+ status = mysql_config_set_int (&db->port, child);
+ else if (strcasecmp ("Socket", child->key) == 0)
+ status = mysql_config_set_string (&db->socket, child);
+ else
+ {
+ WARNING ("mysql plugin: Option `%s' not allowed here.", child->key);
+ status = -1;
+ }
+
+ if (status != 0)
+ break;
+ }
+
+ /* Check that all necessary options have been given. */
+ while (status == 0)
+ {
+ if ((db->port < 0) || (db->port >= 65535))
+ {
+ ERROR ("mysql plugin: Port number out of range: %i",
+ db->port);
+ status = -1;
+ }
+ break;
+ } /* while (status == 0) */
+
+ /* If all went well, add this database to the global list of databases. */
+ if (status == 0)
+ {
+ mysql_database_t **temp;
+
+ temp = (mysql_database_t **) realloc (databases,
+ sizeof (*databases) * (databases_num + 1));
+ if (temp == NULL)
+ {
+ ERROR ("mysql plugin: realloc failed");
+ status = -1;
+ }
+ else
+ {
+ databases = temp;
+ databases[databases_num] = db;
+ databases_num++;
+ }
+ }
+
+ if (status != 0)
+ {
+ mysql_database_free (db);
+ return (-1);
+ }
+
+ return (0);
+} /* }}} int mysql_config_add_database */
+
+static int mysql_config (oconfig_item_t *ci) /* {{{ */
+{
+ int status = 0;
+ int i;
+ oconfig_item_t *lci = NULL; /* legacy config */
+
+ for (i = 0; i < ci->children_num; i++)
+ {
+ oconfig_item_t *child = ci->children + i;
+
+ if (strcasecmp ("Database", child->key) == 0 && child->children_num > 0)
+ mysql_config_add_database (child);
+ else
+ {
+ /* legacy mode - convert to <Database ...> config */
+ if (lci == NULL)
+ {
+ lci = malloc (sizeof(*lci));
+ if (lci == NULL)
+ {
+ ERROR ("mysql plugin: malloc failed.");
+ return (-1);
+ }
+ memset (lci, '\0', sizeof (*lci));
+ }
+ if (strcasecmp ("Database", child->key) == 0)
+ {
+ lci->key = child->key;
+ lci->values = child->values;
+ lci->values_num = child->values_num;
+ lci->parent = child->parent;
+ }
+ else
+ {
+ lci->children_num++;
+ lci->children =
+ realloc (lci->children,
+ lci->children_num * sizeof (*child));
+ if (lci->children == NULL)
+ {
+ ERROR ("mysql plugin: realloc failed.");
+ return (-1);
+ }
+ memcpy (&lci->children[lci->children_num-1], child, sizeof (*child));
+ }
+ }
+ } /* for (ci->children) */
+
+ if (lci)
+ {
+ if (lci->key == NULL)
+ {
+ ERROR ("mysql plugin: no Database configured.");
+ status = -1;
+ }
+ else
+ mysql_config_add_database (lci);
+ sfree (lci->children);
+ sfree (lci);
+ }
+ return (status);
+} /* }}} int mysql_config */
+
+static MYSQL *getconnection (mysql_database_t *db)