From 475dfb7288c8bb8eea3c3614868fcdf72e8c43a8 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Fri, 29 Jan 2016 12:28:54 +0200 Subject: [PATCH] Use common function for setting up MySQL connection. Earlier the same (or almost the same) code was duplicated in several places. A conflicting declaration was also removed. There was no implementation for that declaration. --- server/core/dbusers.c | 109 ++++++++---------- .../include/mysql_client_server_protocol.h | 1 - 2 files changed, 46 insertions(+), 64 deletions(-) diff --git a/server/core/dbusers.c b/server/core/dbusers.c index 43078c85e..c807e9ed4 100644 --- a/server/core/dbusers.c +++ b/server/core/dbusers.c @@ -143,6 +143,7 @@ static int get_databases(SERVICE *, MYSQL *); static const char* get_mysql_users_db_count_query(char* server_version); static const char* get_mysql_users_query(char* server_version, bool include_root); static int get_users(SERVICE *service, USERS *users); +static MYSQL *gw_mysql_init(void); static int gw_mysql_set_timeouts(MYSQL* handle); static bool host_has_singlechar_wildcard(const char *host); static bool host_matches_singlechar_wildcard(const char* user, const char* wild); @@ -778,32 +779,13 @@ get_all_users(SERVICE *service, USERS *users) while (server != NULL) { + con = gw_mysql_init(); - con = mysql_init(NULL); - - if (con == NULL) + if (!con) { - MXS_ERROR("mysql_init: %s", mysql_error(con)); goto cleanup; } - /** Set read, write and connect timeout values */ - if (gw_mysql_set_timeouts(con)) - { - MXS_ERROR("Failed to set timeout values for backend connection."); - mysql_close(con); - goto cleanup; - } - - if (mysql_options(con, MYSQL_OPT_USE_REMOTE_CONNECTION, NULL)) - { - MXS_ERROR("Failed to set external connection. " - "It is needed for backend server connections."); - mysql_close(con); - goto cleanup; - } - - while (!service->svc_do_shutdown && server != NULL && (mysql_real_connect(con, @@ -837,28 +819,10 @@ get_all_users(SERVICE *service, USERS *users) while (server != NULL) { + con = gw_mysql_init(); - con = mysql_init(NULL); - - if (con == NULL) + if (!con) { - MXS_ERROR("mysql_init: %s", mysql_error(con)); - goto cleanup; - } - - /** Set read, write and connect timeout values */ - if (gw_mysql_set_timeouts(con)) - { - MXS_ERROR("Failed to set timeout values for backend connection."); - mysql_close(con); - goto cleanup; - } - - if (mysql_options(con, MYSQL_OPT_USE_REMOTE_CONNECTION, NULL)) - { - MXS_ERROR("Failed to set external connection. " - "It is needed for backend server connections."); - mysql_close(con); goto cleanup; } @@ -1288,28 +1252,13 @@ get_users(SERVICE *service, USERS *users) return get_all_users(service, users); } - con = mysql_init(NULL); + con = gw_mysql_init(); - if (con == NULL) + if (!con) { - MXS_ERROR("mysql_init: %s", mysql_error(con)); - return -1; - } - /** Set read, write and connect timeout values */ - if (gw_mysql_set_timeouts(con)) - { - MXS_ERROR("Failed to set timeout values for backend connection."); - mysql_close(con); return -1; } - if (mysql_options(con, MYSQL_OPT_USE_REMOTE_CONNECTION, NULL)) - { - MXS_ERROR("Failed to set external connection. " - "It is needed for backend server connections."); - mysql_close(con); - return -1; - } /** * Attempt to connect to one of the databases database or until we run * out of databases @@ -2224,6 +2173,42 @@ static int normalize_hostname(const char *input_host, char *output_host) return netmask; } +/** + * Returns a MYSQL object suitably configured. + * + * @return An object or NULL if something fails. + */ +MYSQL *gw_mysql_init() +{ + MYSQL* con = mysql_init(NULL); + + if (con) + { + if (gw_mysql_set_timeouts(con) == 0) + { + if (mysql_options(con, MYSQL_OPT_USE_REMOTE_CONNECTION, NULL) != 0) + { + MXS_ERROR("Failed to set external connection. " + "It is needed for backend server connections."); + mysql_close(con); + con = NULL; + } + } + else + { + MXS_ERROR("Failed to set timeout values for backend connection."); + mysql_close(con); + con = NULL; + } + } + else + { + MXS_ERROR("mysql_init: %s", mysql_error(NULL)); + } + + return con; +} + /** * Set read, write and connect timeout values for MySQL database connection. * @@ -2589,7 +2574,6 @@ bool check_service_permissions(SERVICE* service) MYSQL_RES* res; char *user, *password, *dpasswd; SERVER_REF* server; - int conn_timeout = 1; bool rval = true; if (is_internal_service(service->routerModule)) @@ -2614,15 +2598,14 @@ bool check_service_permissions(SERVICE* service) dpasswd = decryptPassword(password); - if ((mysql = mysql_init(NULL)) == NULL) + mysql = gw_mysql_init(); + + if (!mysql) { - MXS_ERROR("[%s] MySQL connection initialization failed.", __FUNCTION__); free(dpasswd); return false; } - mysql_options(mysql, MYSQL_OPT_USE_REMOTE_CONNECTION, NULL); - mysql_options(mysql, MYSQL_OPT_CONNECT_TIMEOUT, &conn_timeout); /** Connect to the first server. This assumes all servers have identical * user permissions. */ diff --git a/server/modules/include/mysql_client_server_protocol.h b/server/modules/include/mysql_client_server_protocol.h index ffb04cfe0..83acb80e9 100644 --- a/server/modules/include/mysql_client_server_protocol.h +++ b/server/modules/include/mysql_client_server_protocol.h @@ -324,7 +324,6 @@ typedef struct { MySQLProtocol* mysql_protocol_init(DCB* dcb, int fd); void mysql_protocol_done (DCB* dcb); -MySQLProtocol *gw_mysql_init(MySQLProtocol *data); int gw_receive_backend_auth(MySQLProtocol *protocol); int gw_decode_mysql_server_handshake(MySQLProtocol *protocol, uint8_t *payload); int gw_read_backend_handshake(MySQLProtocol *protocol);