2 Copyright (c) 2001-2008 MySQL AB, 2009 Sun Microsystems, Inc.
3 Use is subject to license terms.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <my_global.h>
21 #include <mysql_embed.h>
22 #include <mysqld_error.h>
23 #include <my_pthread.h>
24 #include "embedded_priv.h"
26 #include <mysys_err.h>
34 #include "client_settings.h"
38 #if !defined(MSDOS) && !defined(__WIN__)
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
46 #ifdef HAVE_SYS_SELECT_H
47 #include <sys/select.h>
54 #define INADDR_NONE -1
57 extern ulong net_buffer_length
;
58 extern ulong max_allowed_packet
;
60 #if defined(MSDOS) || defined(__WIN__)
61 #define ERRNO WSAGetLastError()
66 #define SOCKET_ERROR -1
67 #define closesocket(A) close(A)
71 struct passwd
*getpwuid(uid_t
);
76 static my_bool
is_NT(void)
78 char *os
=getenv("OS");
79 return (os
&& !strcmp(os
, "Windows_NT")) ? 1 : 0;
83 /**************************************************************************
84 ** Shut down connection
85 **************************************************************************/
87 static void end_server(MYSQL
*mysql
)
89 DBUG_ENTER("end_server");
90 free_old_query(mysql
);
95 int mysql_init_character_set(MYSQL
*mysql
);
98 mysql_real_connect(MYSQL
*mysql
,const char *host
, const char *user
,
99 const char *passwd
, const char *db
,
100 uint port
, const char *unix_socket
,ulong client_flag
)
102 char name_buff
[USERNAME_LENGTH
];
104 DBUG_ENTER("mysql_real_connect");
105 DBUG_PRINT("enter",("host: %s db: %s user: %s",
106 host
? host
: "(Null)",
108 user
? user
: "(Null)"));
110 if (!host
|| !host
[0])
111 host
= mysql
->options
.host
;
113 if (mysql
->options
.methods_to_use
== MYSQL_OPT_USE_REMOTE_CONNECTION
||
114 (mysql
->options
.methods_to_use
== MYSQL_OPT_GUESS_CONNECTION
&&
115 host
&& *host
&& strcmp(host
,LOCAL_HOST
)))
116 DBUG_RETURN(cli_mysql_real_connect(mysql
, host
, user
,
118 unix_socket
, client_flag
));
120 mysql
->methods
= &embedded_methods
;
122 /* use default options */
123 if (mysql
->options
.my_cnf_file
|| mysql
->options
.my_cnf_group
)
125 mysql_read_default_options(&mysql
->options
,
126 (mysql
->options
.my_cnf_file
?
127 mysql
->options
.my_cnf_file
: "my"),
128 mysql
->options
.my_cnf_group
);
129 my_free(mysql
->options
.my_cnf_file
,MYF(MY_ALLOW_ZERO_PTR
));
130 my_free(mysql
->options
.my_cnf_group
,MYF(MY_ALLOW_ZERO_PTR
));
131 mysql
->options
.my_cnf_file
=mysql
->options
.my_cnf_group
=0;
135 db
=mysql
->options
.db
;
137 if (!user
|| !user
[0])
138 user
=mysql
->options
.user
;
140 #ifndef NO_EMBEDDED_ACCESS_CHECKS
143 passwd
=mysql
->options
.password
;
144 #if !defined(DONT_USE_MYSQL_PWD)
146 passwd
=getenv("MYSQL_PWD"); /* get it from environment */
149 mysql
->passwd
= passwd
? my_strdup(passwd
,MYF(0)) : NULL
;
150 #endif /*!NO_EMBEDDED_ACCESS_CHECKS*/
151 if (!user
|| !user
[0])
153 read_user_name(name_buff
);
161 We need to alloc some space for mysql->info but don't want to
162 put extra 'my_free's in mysql_close.
163 So we alloc it with the 'user' string to be freed at once
165 mysql
->user
= my_strdup(user
, MYF(0));
170 client_flag
|=mysql
->options
.client_flag
;
171 /* Send client information for access check */
172 client_flag
|=CLIENT_CAPABILITIES
;
173 if (client_flag
& CLIENT_MULTI_STATEMENTS
)
174 client_flag
|= CLIENT_MULTI_RESULTS
;
175 client_flag
&= ~CLIENT_COMPRESS
;
177 client_flag
|=CLIENT_CONNECT_WITH_DB
;
179 mysql
->info_buffer
= my_malloc(MYSQL_ERRMSG_SIZE
, MYF(0));
180 mysql
->thd
= create_embedded_thd(client_flag
);
182 init_embedded_mysql(mysql
, client_flag
);
184 if (mysql_init_character_set(mysql
))
187 if (check_embedded_connection(mysql
, db
))
190 mysql
->server_status
= SERVER_STATUS_AUTOCOMMIT
;
192 if (mysql
->options
.init_commands
)
194 DYNAMIC_ARRAY
*init_commands
= mysql
->options
.init_commands
;
195 char **ptr
= (char**)init_commands
->buffer
;
196 char **end
= ptr
+ init_commands
->elements
;
198 for (; ptr
<end
; ptr
++)
201 if (mysql_query(mysql
,*ptr
))
205 if (!(res
= (*mysql
->methods
->use_result
)(mysql
)))
207 mysql_free_result(res
);
212 DBUG_PRINT("exit",("Mysql handler: 0x%lx", (long) mysql
));
216 DBUG_PRINT("error",("message: %u (%s)",
217 mysql
->net
.last_errno
,
218 mysql
->net
.last_error
));
220 /* Free alloced memory */
221 my_bool free_me
=mysql
->free_me
;
225 mysql
->free_me
=free_me
;