2001-12-06 14:10:51 +02:00
|
|
|
/* Copyright (C) 2000 MySQL AB
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2006-12-23 20:17:15 +01:00
|
|
|
the Free Software Foundation; version 2 of the License.
|
2001-12-06 14:10:51 +02:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
2001-04-11 13:04:03 +02:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2001-12-06 14:10:51 +02:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
2001-04-25 01:11:29 +03:00
|
|
|
|
2006-02-24 18:34:15 +02:00
|
|
|
#include <my_global.h>
|
|
|
|
#include <mysql.h>
|
|
|
|
#include <mysqld_error.h>
|
|
|
|
#include <my_pthread.h>
|
2001-04-11 13:04:03 +02:00
|
|
|
#include <my_sys.h>
|
|
|
|
#include <mysys_err.h>
|
|
|
|
#include <m_string.h>
|
|
|
|
#include <m_ctype.h>
|
|
|
|
#include "errmsg.h"
|
|
|
|
#include <violite.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <time.h>
|
2009-10-09 14:30:54 +02:00
|
|
|
#include <sql_common.h>
|
2010-08-09 11:32:50 +03:00
|
|
|
#include "embedded_priv.h"
|
2003-09-16 16:06:25 +05:00
|
|
|
#include "client_settings.h"
|
2001-04-11 13:04:03 +02:00
|
|
|
#ifdef HAVE_PWD_H
|
|
|
|
#include <pwd.h>
|
|
|
|
#endif
|
2010-07-15 08:16:06 -03:00
|
|
|
#if !defined(__WIN__)
|
2001-04-11 13:04:03 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#ifdef HAVE_SELECT_H
|
|
|
|
# include <select.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_UN_H
|
|
|
|
# include <sys/un.h>
|
|
|
|
#endif
|
|
|
|
#ifndef INADDR_NONE
|
|
|
|
#define INADDR_NONE -1
|
|
|
|
#endif
|
|
|
|
|
2004-05-26 21:40:27 +05:00
|
|
|
extern ulong net_buffer_length;
|
|
|
|
extern ulong max_allowed_packet;
|
|
|
|
|
2010-07-15 08:16:06 -03:00
|
|
|
#if defined(__WIN__)
|
2001-04-11 13:04:03 +02:00
|
|
|
#define ERRNO WSAGetLastError()
|
|
|
|
#define perror(A)
|
|
|
|
#else
|
|
|
|
#include <errno.h>
|
|
|
|
#define ERRNO errno
|
|
|
|
#define SOCKET_ERROR -1
|
|
|
|
#define closesocket(A) close(A)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_GETPWUID
|
|
|
|
struct passwd *getpwuid(uid_t);
|
|
|
|
char* getlogin(void);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __WIN__
|
|
|
|
static my_bool is_NT(void)
|
|
|
|
{
|
|
|
|
char *os=getenv("OS");
|
|
|
|
return (os && !strcmp(os, "Windows_NT")) ? 1 : 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2006-06-19 22:11:01 +05:00
|
|
|
int mysql_init_character_set(MYSQL *mysql);
|
2001-04-11 13:04:03 +02:00
|
|
|
|
|
|
|
MYSQL * STDCALL
|
|
|
|
mysql_real_connect(MYSQL *mysql,const char *host, const char *user,
|
2003-06-18 15:58:57 +05:00
|
|
|
const char *passwd, const char *db,
|
2003-01-20 18:59:45 +04:00
|
|
|
uint port, const char *unix_socket,ulong client_flag)
|
2001-04-11 13:04:03 +02:00
|
|
|
{
|
2003-09-26 15:33:13 +05:00
|
|
|
char name_buff[USERNAME_LENGTH];
|
2004-01-07 21:30:15 +04:00
|
|
|
|
2001-04-11 13:04:03 +02:00
|
|
|
DBUG_ENTER("mysql_real_connect");
|
2009-09-30 12:28:15 +02:00
|
|
|
DBUG_PRINT("enter",("host: %s db: %s user: %s (libmysqld)",
|
2001-04-11 13:04:03 +02:00
|
|
|
host ? host : "(Null)",
|
|
|
|
db ? db : "(Null)",
|
|
|
|
user ? user : "(Null)"));
|
|
|
|
|
2009-10-09 14:30:54 +02:00
|
|
|
/* Test whether we're already connected */
|
|
|
|
if (mysql->server_version)
|
|
|
|
{
|
|
|
|
set_mysql_error(mysql, CR_ALREADY_CONNECTED, unknown_sqlstate);
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
2005-01-03 19:21:54 +04:00
|
|
|
if (!host || !host[0])
|
|
|
|
host= mysql->options.host;
|
|
|
|
|
2003-09-11 20:24:14 +03:00
|
|
|
if (mysql->options.methods_to_use == MYSQL_OPT_USE_REMOTE_CONNECTION ||
|
|
|
|
(mysql->options.methods_to_use == MYSQL_OPT_GUESS_CONNECTION &&
|
2004-09-25 21:56:52 +05:00
|
|
|
host && *host && strcmp(host,LOCAL_HOST)))
|
2003-12-18 15:51:22 +04:00
|
|
|
DBUG_RETURN(cli_mysql_real_connect(mysql, host, user,
|
|
|
|
passwd, db, port,
|
|
|
|
unix_socket, client_flag));
|
2003-06-18 15:58:57 +05:00
|
|
|
|
2003-06-17 21:32:31 +05:00
|
|
|
mysql->methods= &embedded_methods;
|
|
|
|
|
2001-04-11 13:04:03 +02:00
|
|
|
/* use default options */
|
|
|
|
if (mysql->options.my_cnf_file || mysql->options.my_cnf_group)
|
|
|
|
{
|
|
|
|
mysql_read_default_options(&mysql->options,
|
|
|
|
(mysql->options.my_cnf_file ?
|
|
|
|
mysql->options.my_cnf_file : "my"),
|
|
|
|
mysql->options.my_cnf_group);
|
Bug#34043: Server loops excessively in _checkchunk() when safemalloc is enabled
Essentially, the problem is that safemalloc is excruciatingly
slow as it checks all allocated blocks for overrun at each
memory management primitive, yielding a almost exponential
slowdown for the memory management functions (malloc, realloc,
free). The overrun check basically consists of verifying some
bytes of a block for certain magic keys, which catches some
simple forms of overrun. Another minor problem is violation
of aliasing rules and that its own internal list of blocks
is prone to corruption.
Another issue with safemalloc is rather the maintenance cost
as the tool has a significant impact on the server code.
Given the magnitude of memory debuggers available nowadays,
especially those that are provided with the platform malloc
implementation, maintenance of a in-house and largely obsolete
memory debugger becomes a burden that is not worth the effort
due to its slowness and lack of support for detecting more
common forms of heap corruption.
Since there are third-party tools that can provide the same
functionality at a lower or comparable performance cost, the
solution is to simply remove safemalloc. Third-party tools
can provide the same functionality at a lower or comparable
performance cost.
The removal of safemalloc also allows a simplification of the
malloc wrappers, removing quite a bit of kludge: redefinition
of my_malloc, my_free and the removal of the unused second
argument of my_free. Since free() always check whether the
supplied pointer is null, redudant checks are also removed.
Also, this patch adds unit testing for my_malloc and moves
my_realloc implementation into the same file as the other
memory allocation primitives.
2010-07-08 18:20:08 -03:00
|
|
|
my_free(mysql->options.my_cnf_file);
|
|
|
|
my_free(mysql->options.my_cnf_group);
|
2001-04-11 13:04:03 +02:00
|
|
|
mysql->options.my_cnf_file=mysql->options.my_cnf_group=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!db || !db[0])
|
|
|
|
db=mysql->options.db;
|
2002-06-17 16:24:51 +05:00
|
|
|
|
2003-09-26 15:33:13 +05:00
|
|
|
if (!user || !user[0])
|
|
|
|
user=mysql->options.user;
|
|
|
|
|
2004-01-07 21:30:15 +04:00
|
|
|
#ifndef NO_EMBEDDED_ACCESS_CHECKS
|
2003-09-26 15:33:13 +05:00
|
|
|
if (!passwd)
|
|
|
|
{
|
|
|
|
passwd=mysql->options.password;
|
|
|
|
#if !defined(DONT_USE_MYSQL_PWD)
|
|
|
|
if (!passwd)
|
|
|
|
passwd=getenv("MYSQL_PWD"); /* get it from environment */
|
|
|
|
#endif
|
|
|
|
}
|
2004-01-07 21:30:15 +04:00
|
|
|
mysql->passwd= passwd ? my_strdup(passwd,MYF(0)) : NULL;
|
|
|
|
#endif /*!NO_EMBEDDED_ACCESS_CHECKS*/
|
2003-09-26 15:33:13 +05:00
|
|
|
if (!user || !user[0])
|
|
|
|
{
|
|
|
|
read_user_name(name_buff);
|
2004-01-07 21:30:15 +04:00
|
|
|
if (name_buff[0])
|
2003-09-26 15:33:13 +05:00
|
|
|
user= name_buff;
|
|
|
|
}
|
|
|
|
|
2004-01-07 21:30:15 +04:00
|
|
|
if (!user)
|
|
|
|
user= "";
|
2006-02-24 18:34:15 +02:00
|
|
|
/*
|
|
|
|
We need to alloc some space for mysql->info but don't want to
|
|
|
|
put extra 'my_free's in mysql_close.
|
|
|
|
So we alloc it with the 'user' string to be freed at once
|
|
|
|
*/
|
|
|
|
mysql->user= my_strdup(user, MYF(0));
|
2003-09-26 15:33:13 +05:00
|
|
|
|
2001-04-25 01:11:29 +03:00
|
|
|
port=0;
|
|
|
|
unix_socket=0;
|
2002-06-17 16:24:51 +05:00
|
|
|
|
2009-09-11 15:52:08 +03:00
|
|
|
client_flag|=mysql->options.client_flag;
|
2005-05-09 16:26:06 +05:00
|
|
|
/* Send client information for access check */
|
|
|
|
client_flag|=CLIENT_CAPABILITIES;
|
|
|
|
if (client_flag & CLIENT_MULTI_STATEMENTS)
|
|
|
|
client_flag|= CLIENT_MULTI_RESULTS;
|
2010-08-09 11:32:50 +03:00
|
|
|
/*
|
|
|
|
no compression in embedded as we don't send any data,
|
|
|
|
and no pluggable auth, as we cannot do a client-server dialog
|
|
|
|
*/
|
|
|
|
client_flag&= ~(CLIENT_COMPRESS | CLIENT_PLUGIN_AUTH);
|
2005-05-09 16:26:06 +05:00
|
|
|
if (db)
|
|
|
|
client_flag|=CLIENT_CONNECT_WITH_DB;
|
|
|
|
|
2006-02-24 18:34:15 +02:00
|
|
|
mysql->info_buffer= my_malloc(MYSQL_ERRMSG_SIZE, MYF(0));
|
2006-07-18 16:43:39 +05:00
|
|
|
mysql->thd= create_embedded_thd(client_flag);
|
2001-04-11 13:04:03 +02:00
|
|
|
|
2006-06-01 17:06:42 +05:00
|
|
|
init_embedded_mysql(mysql, client_flag);
|
2001-04-11 13:04:03 +02:00
|
|
|
|
2006-06-19 22:11:01 +05:00
|
|
|
if (mysql_init_character_set(mysql))
|
2003-09-26 15:33:13 +05:00
|
|
|
goto error;
|
|
|
|
|
2006-07-13 22:32:18 +05:00
|
|
|
if (check_embedded_connection(mysql, db))
|
2001-04-11 13:04:03 +02:00
|
|
|
goto error;
|
|
|
|
|
2003-06-24 12:10:35 +03:00
|
|
|
mysql->server_status= SERVER_STATUS_AUTOCOMMIT;
|
2001-04-25 01:11:29 +03:00
|
|
|
|
2002-12-19 12:27:46 +04:00
|
|
|
if (mysql->options.init_commands)
|
2001-04-11 13:04:03 +02:00
|
|
|
{
|
2002-12-19 12:27:46 +04:00
|
|
|
DYNAMIC_ARRAY *init_commands= mysql->options.init_commands;
|
|
|
|
char **ptr= (char**)init_commands->buffer;
|
|
|
|
char **end= ptr + init_commands->elements;
|
|
|
|
|
|
|
|
for (; ptr<end; ptr++)
|
|
|
|
{
|
|
|
|
MYSQL_RES *res;
|
|
|
|
if (mysql_query(mysql,*ptr))
|
|
|
|
goto error;
|
|
|
|
if (mysql->fields)
|
|
|
|
{
|
2003-09-18 18:28:42 +05:00
|
|
|
if (!(res= (*mysql->methods->use_result)(mysql)))
|
2002-12-19 12:27:46 +04:00
|
|
|
goto error;
|
|
|
|
mysql_free_result(res);
|
|
|
|
}
|
|
|
|
}
|
2001-04-11 13:04:03 +02:00
|
|
|
}
|
|
|
|
|
2006-11-20 22:42:06 +02:00
|
|
|
DBUG_PRINT("exit",("Mysql handler: 0x%lx", (long) mysql));
|
2001-04-11 13:04:03 +02:00
|
|
|
DBUG_RETURN(mysql);
|
|
|
|
|
|
|
|
error:
|
2007-12-12 18:21:01 +03:00
|
|
|
DBUG_PRINT("error",("message: %u (%s)",
|
2008-02-28 14:55:46 -03:00
|
|
|
mysql->net.last_errno,
|
|
|
|
mysql->net.last_error));
|
2001-04-11 13:04:03 +02:00
|
|
|
{
|
|
|
|
/* Free alloced memory */
|
|
|
|
my_bool free_me=mysql->free_me;
|
2009-10-09 14:30:54 +02:00
|
|
|
free_old_query(mysql);
|
2001-04-11 13:04:03 +02:00
|
|
|
mysql->free_me=0;
|
|
|
|
mysql_close(mysql);
|
|
|
|
mysql->free_me=free_me;
|
|
|
|
}
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|