1
/* Copyright (C) 2000 MySQL AB
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#include <my_global.h>
18
#include <mysql_embed.h>
19
#include <mysqld_error.h>
20
#include <my_pthread.h>
21
#include "embedded_priv.h"
23
#include <mysys_err.h>
31
#include "client_settings.h"
35
#if !defined(MSDOS) && !defined(__WIN__)
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;
57
#if defined(MSDOS) || defined(__WIN__)
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;
80
/**************************************************************************
81
** Shut down connection
82
**************************************************************************/
84
static void end_server(MYSQL *mysql)
86
DBUG_ENTER("end_server");
87
free_old_query(mysql);
92
int mysql_init_character_set(MYSQL *mysql);
95
mysql_real_connect(MYSQL *mysql,const char *host, const char *user,
96
const char *passwd, const char *db,
97
uint port, const char *unix_socket,ulong client_flag)
99
char name_buff[USERNAME_LENGTH];
101
DBUG_ENTER("mysql_real_connect");
102
DBUG_PRINT("enter",("host: %s db: %s user: %s",
103
host ? host : "(Null)",
105
user ? user : "(Null)"));
107
if (!host || !host[0])
108
host= mysql->options.host;
110
if (mysql->options.methods_to_use == MYSQL_OPT_USE_REMOTE_CONNECTION ||
111
(mysql->options.methods_to_use == MYSQL_OPT_GUESS_CONNECTION &&
112
host && *host && strcmp(host,LOCAL_HOST)))
113
DBUG_RETURN(cli_mysql_real_connect(mysql, host, user,
115
unix_socket, client_flag));
117
mysql->methods= &embedded_methods;
119
/* use default options */
120
if (mysql->options.my_cnf_file || mysql->options.my_cnf_group)
122
mysql_read_default_options(&mysql->options,
123
(mysql->options.my_cnf_file ?
124
mysql->options.my_cnf_file : "my"),
125
mysql->options.my_cnf_group);
126
my_free(mysql->options.my_cnf_file,MYF(MY_ALLOW_ZERO_PTR));
127
my_free(mysql->options.my_cnf_group,MYF(MY_ALLOW_ZERO_PTR));
128
mysql->options.my_cnf_file=mysql->options.my_cnf_group=0;
132
db=mysql->options.db;
134
if (!user || !user[0])
135
user=mysql->options.user;
137
#ifndef NO_EMBEDDED_ACCESS_CHECKS
140
passwd=mysql->options.password;
141
#if !defined(DONT_USE_MYSQL_PWD)
143
passwd=getenv("MYSQL_PWD"); /* get it from environment */
146
mysql->passwd= passwd ? my_strdup(passwd,MYF(0)) : NULL;
147
#endif /*!NO_EMBEDDED_ACCESS_CHECKS*/
148
if (!user || !user[0])
150
read_user_name(name_buff);
158
We need to alloc some space for mysql->info but don't want to
159
put extra 'my_free's in mysql_close.
160
So we alloc it with the 'user' string to be freed at once
162
mysql->user= my_strdup(user, MYF(0));
167
client_flag|=mysql->options.client_flag;
168
/* Send client information for access check */
169
client_flag|=CLIENT_CAPABILITIES;
170
if (client_flag & CLIENT_MULTI_STATEMENTS)
171
client_flag|= CLIENT_MULTI_RESULTS;
172
client_flag&= ~CLIENT_COMPRESS;
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;
222
mysql->free_me=free_me;