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.
This commit is contained in:
Johan Wikman
2016-01-29 12:28:54 +02:00
parent 0902297fe5
commit 475dfb7288
2 changed files with 46 additions and 64 deletions

View File

@ -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_db_count_query(char* server_version);
static const char* get_mysql_users_query(char* server_version, bool include_root); static const char* get_mysql_users_query(char* server_version, bool include_root);
static int get_users(SERVICE *service, USERS *users); static int get_users(SERVICE *service, USERS *users);
static MYSQL *gw_mysql_init(void);
static int gw_mysql_set_timeouts(MYSQL* handle); static int gw_mysql_set_timeouts(MYSQL* handle);
static bool host_has_singlechar_wildcard(const char *host); static bool host_has_singlechar_wildcard(const char *host);
static bool host_matches_singlechar_wildcard(const char* user, const char* wild); 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) while (server != NULL)
{ {
con = gw_mysql_init();
con = mysql_init(NULL); if (!con)
if (con == NULL)
{ {
MXS_ERROR("mysql_init: %s", mysql_error(con));
goto cleanup; 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 && while (!service->svc_do_shutdown &&
server != NULL && server != NULL &&
(mysql_real_connect(con, (mysql_real_connect(con,
@ -837,28 +819,10 @@ get_all_users(SERVICE *service, USERS *users)
while (server != NULL) while (server != NULL)
{ {
con = gw_mysql_init();
con = mysql_init(NULL); if (!con)
if (con == NULL)
{ {
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; goto cleanup;
} }
@ -1288,28 +1252,13 @@ get_users(SERVICE *service, USERS *users)
return get_all_users(service, 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; 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 * Attempt to connect to one of the databases database or until we run
* out of databases * out of databases
@ -2224,6 +2173,42 @@ static int normalize_hostname(const char *input_host, char *output_host)
return netmask; 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. * Set read, write and connect timeout values for MySQL database connection.
* *
@ -2589,7 +2574,6 @@ bool check_service_permissions(SERVICE* service)
MYSQL_RES* res; MYSQL_RES* res;
char *user, *password, *dpasswd; char *user, *password, *dpasswd;
SERVER_REF* server; SERVER_REF* server;
int conn_timeout = 1;
bool rval = true; bool rval = true;
if (is_internal_service(service->routerModule)) if (is_internal_service(service->routerModule))
@ -2614,15 +2598,14 @@ bool check_service_permissions(SERVICE* service)
dpasswd = decryptPassword(password); 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); free(dpasswd);
return false; 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 /** Connect to the first server. This assumes all servers have identical
* user permissions. */ * user permissions. */

View File

@ -324,7 +324,6 @@ typedef struct {
MySQLProtocol* mysql_protocol_init(DCB* dcb, int fd); MySQLProtocol* mysql_protocol_init(DCB* dcb, int fd);
void mysql_protocol_done (DCB* dcb); void mysql_protocol_done (DCB* dcb);
MySQLProtocol *gw_mysql_init(MySQLProtocol *data);
int gw_receive_backend_auth(MySQLProtocol *protocol); int gw_receive_backend_auth(MySQLProtocol *protocol);
int gw_decode_mysql_server_handshake(MySQLProtocol *protocol, uint8_t *payload); int gw_decode_mysql_server_handshake(MySQLProtocol *protocol, uint8_t *payload);
int gw_read_backend_handshake(MySQLProtocol *protocol); int gw_read_backend_handshake(MySQLProtocol *protocol);