From b870a543b9b4ba4f101f48c1b1eb28add3a337fa Mon Sep 17 00:00:00 2001 From: Mikurula <675430579@qq.com> Date: Fri, 17 Aug 2018 16:17:24 +0800 Subject: [PATCH] config set variables can change remote settings --- plugins/admin/admin-commands.c | 5 +++++ src/chassis-config.c | 21 +++++++++++++++++++++ src/chassis-config.h | 2 ++ 3 files changed, 28 insertions(+) diff --git a/plugins/admin/admin-commands.c b/plugins/admin/admin-commands.c index 0300a72..b893ddf 100644 --- a/plugins/admin/admin-commands.c +++ b/plugins/admin/admin-commands.c @@ -1246,6 +1246,11 @@ void admin_set_config(network_mysqld_con* con, char* key, char* value) g_list_free(options); + if(0 == ret && !chassis_config_set_remote_options(con->srv->config_manager, key, value)) { + network_mysqld_con_send_error(con->client,C("Variable is set locally but cannot replace remote settings")); + return; + } + if(0 == ret) { network_mysqld_con_send_ok_full(con->client, 1, 0, SERVER_STATUS_AUTOCOMMIT, 0); } else if(ASSIGN_NOT_SUPPORT == ret){ diff --git a/src/chassis-config.c b/src/chassis-config.c index 44e3e98..ce71ae2 100644 --- a/src/chassis-config.c +++ b/src/chassis-config.c @@ -321,6 +321,27 @@ chassis_config_load_options_mysql(chassis_config_t *conf) return FALSE; } +gboolean +chassis_config_set_remote_options(chassis_config_t *conf, char* key, char* value) +{ + if(conf->type == CHASSIS_CONF_MYSQL){ + MYSQL *conn = chassis_config_get_mysql_connection(conf); + if (!conn) { + g_warning("Cannot connect to mysql server."); + return FALSE; + } + char sql[1024] = { 0 }, real_value[1024] = { 0 }; + mysql_real_escape_string(conn, real_value, value, strlen(value)); + snprintf(sql, sizeof(sql), + "REPLACE INTO %s.`settings`(option_key,option_value) VALUES ('%s', '%s')", conf->schema, key, real_value); + if (mysql_query(conn, sql)) { + g_warning("sql failed: %s | error: %s", sql, mysql_error(conn)); + return FALSE; + } + } + return TRUE; +} + gint chassis_config_reload_options(chassis_config_t *conf) { switch (conf->type) { diff --git a/src/chassis-config.h b/src/chassis-config.h index eb468a0..58a26e4 100644 --- a/src/chassis-config.h +++ b/src/chassis-config.h @@ -38,6 +38,8 @@ void chassis_config_free(chassis_config_t *); gint chassis_config_reload_options(chassis_config_t *conf); +gboolean chassis_config_set_remote_options(chassis_config_t *conf, char* key, char* value); + GHashTable *chassis_config_get_options(chassis_config_t *); gboolean chassis_config_parse_options(chassis_config_t *, GList *entries);