1
/* Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16
#include <my_global.h>
18
#include <mysqld_error.h>
19
#include <my_pthread.h>
21
#include <mysys_err.h>
29
#include <sql_common.h>
30
#include "embedded_priv.h"
31
#include "client_settings.h"
36
#include <sys/socket.h>
37
#include <netinet/in.h>
38
#include <arpa/inet.h>
43
#ifdef HAVE_SYS_SELECT_H
44
#include <sys/select.h>
51
#define INADDR_NONE -1
54
extern ulong net_buffer_length;
55
extern ulong max_allowed_packet;
58
#define ERRNO WSAGetLastError()
63
#define SOCKET_ERROR -1
64
#define closesocket(A) close(A)
68
struct passwd *getpwuid(uid_t);
73
static my_bool is_NT(void)
75
char *os=getenv("OS");
76
return (os && !strcmp(os, "Windows_NT")) ? 1 : 0;
81
int mysql_init_character_set(MYSQL *mysql);
84
mysql_real_connect(MYSQL *mysql,const char *host, const char *user,
85
const char *passwd, const char *db,
86
uint port, const char *unix_socket,ulong client_flag)
88
char name_buff[USERNAME_LENGTH];
90
DBUG_ENTER("mysql_real_connect");
91
DBUG_PRINT("enter",("host: %s db: %s user: %s (libmysqld)",
92
host ? host : "(Null)",
94
user ? user : "(Null)"));
96
/* Test whether we're already connected */
97
if (mysql->server_version)
99
set_mysql_error(mysql, CR_ALREADY_CONNECTED, unknown_sqlstate);
103
if (!host || !host[0])
104
host= mysql->options.host;
106
if (mysql->options.methods_to_use == MYSQL_OPT_USE_REMOTE_CONNECTION ||
107
(mysql->options.methods_to_use == MYSQL_OPT_GUESS_CONNECTION &&
108
host && *host && strcmp(host,LOCAL_HOST)))
109
DBUG_RETURN(cli_mysql_real_connect(mysql, host, user,
111
unix_socket, client_flag));
113
mysql->methods= &embedded_methods;
115
/* use default options */
116
if (mysql->options.my_cnf_file || mysql->options.my_cnf_group)
118
mysql_read_default_options(&mysql->options,
119
(mysql->options.my_cnf_file ?
120
mysql->options.my_cnf_file : "my"),
121
mysql->options.my_cnf_group);
122
my_free(mysql->options.my_cnf_file);
123
my_free(mysql->options.my_cnf_group);
124
mysql->options.my_cnf_file=mysql->options.my_cnf_group=0;
128
db=mysql->options.db;
130
if (!user || !user[0])
131
user=mysql->options.user;
133
#ifndef NO_EMBEDDED_ACCESS_CHECKS
136
passwd=mysql->options.password;
137
#if !defined(DONT_USE_MYSQL_PWD)
139
passwd=getenv("MYSQL_PWD"); /* get it from environment */
142
mysql->passwd= passwd ? my_strdup(passwd,MYF(0)) : NULL;
143
#endif /*!NO_EMBEDDED_ACCESS_CHECKS*/
144
if (!user || !user[0])
146
read_user_name(name_buff);
154
We need to alloc some space for mysql->info but don't want to
155
put extra 'my_free's in mysql_close.
156
So we alloc it with the 'user' string to be freed at once
158
mysql->user= my_strdup(user, MYF(0));
163
client_flag|=mysql->options.client_flag;
164
/* Send client information for access check */
165
client_flag|=CLIENT_CAPABILITIES;
166
if (client_flag & CLIENT_MULTI_STATEMENTS)
167
client_flag|= CLIENT_MULTI_RESULTS;
169
no compression in embedded as we don't send any data,
170
and no pluggable auth, as we cannot do a client-server dialog
172
client_flag&= ~(CLIENT_COMPRESS | CLIENT_PLUGIN_AUTH);
174
client_flag|=CLIENT_CONNECT_WITH_DB;
176
mysql->info_buffer= my_malloc(MYSQL_ERRMSG_SIZE, MYF(0));
177
mysql->thd= create_embedded_thd(client_flag);
179
init_embedded_mysql(mysql, client_flag);
181
if (mysql_init_character_set(mysql))
184
if (check_embedded_connection(mysql, db))
187
mysql->server_status= SERVER_STATUS_AUTOCOMMIT;
189
if (mysql->options.init_commands)
191
DYNAMIC_ARRAY *init_commands= mysql->options.init_commands;
192
char **ptr= (char**)init_commands->buffer;
193
char **end= ptr + init_commands->elements;
195
for (; ptr<end; ptr++)
198
if (mysql_query(mysql,*ptr))
202
if (!(res= (*mysql->methods->use_result)(mysql)))
204
mysql_free_result(res);
209
DBUG_PRINT("exit",("Mysql handler: 0x%lx", (long) mysql));
213
DBUG_PRINT("error",("message: %u (%s)",
214
mysql->net.last_errno,
215
mysql->net.last_error));
217
/* Free alloced memory */
218
my_bool free_me=mysql->free_me;
219
free_old_query(mysql);
222
mysql->free_me=free_me;