2004-08-05 11:59:17 +02:00
|
|
|
/* Copyright (C) 2000-2004 MySQL AB
|
2001-12-06 13:10:51 +01:00
|
|
|
|
|
|
|
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
|
2004-08-05 11:59:17 +02:00
|
|
|
the Free Software Foundation.
|
|
|
|
|
|
|
|
There are special exceptions to the terms and conditions of the GPL as it
|
|
|
|
is applied to this software. View the full text of the exception in file
|
2004-08-05 17:05:11 +02:00
|
|
|
EXCEPTIONS-CLIENT in the directory of this software distribution.
|
2001-12-06 13:10:51 +01:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
2001-10-03 23:41:38 +02:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2001-12-06 13:10:51 +01: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-10-03 23:41:38 +02:00
|
|
|
|
|
|
|
#include <my_global.h>
|
2001-10-11 21:02:16 +02:00
|
|
|
#if defined(THREAD)
|
|
|
|
#include <my_pthread.h> /* because of signal() */
|
2001-10-03 23:41:38 +02:00
|
|
|
#endif
|
2001-10-11 21:02:16 +02:00
|
|
|
#include "mysql.h"
|
|
|
|
#include "mysql_version.h"
|
|
|
|
#include "mysqld_error.h"
|
2001-10-03 23:41:38 +02:00
|
|
|
#include <my_sys.h>
|
|
|
|
#include <mysys_err.h>
|
|
|
|
#include <m_string.h>
|
|
|
|
#include <m_ctype.h>
|
2001-10-11 21:02:16 +02:00
|
|
|
#include <my_net.h>
|
|
|
|
#include <errmsg.h>
|
2001-10-03 23:41:38 +02:00
|
|
|
#include <violite.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2006-04-16 03:17:32 +02:00
|
|
|
#if defined(__NETWARE__)
|
2003-01-28 07:38:28 +01:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/utsname.h>
|
2001-10-11 21:02:16 +02:00
|
|
|
#elif !defined( __WIN__)
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#ifdef HAVE_SYS_UN_H
|
|
|
|
# include <sys/un.h>
|
2001-10-03 23:41:38 +02:00
|
|
|
#endif
|
|
|
|
#include <netdb.h>
|
|
|
|
#ifdef HAVE_SELECT_H
|
|
|
|
# include <select.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
2001-10-11 21:02:16 +02:00
|
|
|
#include <sys/utsname.h>
|
|
|
|
#endif /* __WIN__ */
|
|
|
|
|
2001-10-03 23:41:38 +02:00
|
|
|
#ifndef INADDR_NONE
|
|
|
|
#define INADDR_NONE -1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define RES_BUF_SHIFT 5
|
|
|
|
#define NET_BUF_SIZE 2048
|
|
|
|
|
|
|
|
MYSQL_MANAGER* STDCALL mysql_manager_init(MYSQL_MANAGER* con)
|
|
|
|
{
|
|
|
|
int net_buf_size=NET_BUF_SIZE;
|
|
|
|
if (!con)
|
|
|
|
{
|
|
|
|
if (!(con=(MYSQL_MANAGER*)my_malloc(sizeof(*con)+net_buf_size,
|
|
|
|
MYF(MY_WME|MY_ZEROFILL))))
|
|
|
|
return 0;
|
|
|
|
con->free_me=1;
|
|
|
|
con->net_buf=(char*)con+sizeof(*con);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bzero((char*)con,sizeof(*con));
|
|
|
|
if (!(con->net_buf=my_malloc(net_buf_size,MYF(0))))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
con->net_buf_pos=con->net_data_end=con->net_buf;
|
|
|
|
con->net_buf_size=net_buf_size;
|
|
|
|
return con;
|
|
|
|
}
|
|
|
|
|
|
|
|
MYSQL_MANAGER* STDCALL mysql_manager_connect(MYSQL_MANAGER* con,
|
|
|
|
const char* host,
|
|
|
|
const char* user,
|
|
|
|
const char* passwd,
|
|
|
|
unsigned int port)
|
|
|
|
{
|
|
|
|
my_socket sock;
|
|
|
|
struct sockaddr_in sock_addr;
|
4 small items in this:
- when we don't have in_addr_t, use uint32.
- a forgotten initialization of slave_proxy_id in sql/log_event.cc (was not really "forgot", was
"we needn't init it there", but there was one case where we needed...).
- made slave_proxy_id always meaningful in THD and Log_event, so we can
rely more on it (no need to test if it's meaningful). THD::slave_proxy_id
is equal to THD::thread_id except for the slave SQL thread.
- clean up the slave's temporary table (i.e. free their memory) when slave
server shuts down.
extra/resolveip.c:
removed #define as it is simpler to put it in my_net.h
(because we need the #define elsewhere)
include/my_net.h:
When in_addr_t is not defined, use uint32.
libmysql/libmysql.c:
using in_addr_t is more generic.
libmysql/manager.c:
using in_addr_t is more generic.
mysql-test/t/rpl_chain_temp_table.test:
comments
sql/log_event.cc:
* Had forgot to initialize slave_proxy_id in the event constructor (char* buf...).
Initializing is in fact only needed for Create_file_log_event, because
it uses slave_proxy_id even if it does not write an event to the binlog
(it uses slave_proxy_id to write it to SQL-LOAD.info).
* When we write events we now always write slave_proxy_id, which is now always
meaningful (as thd->slave_proxy_id is now always meaningful, see change in
sql_class.cc).
sql/mini_client.cc:
in_addr_t is more generic.
sql/slave.cc:
A RELAY_LOG_INFO method to free the slave's temporary tables from memory
at slave's server shutdown.
It is called by end_slave(), which is called by close_connections(),
which is called when the server terminates
(close_connections() is just before clean_up(); putting the call in
clean_up() was buggy, as active_mi is already deleted by close_connections().
sql/slave.h:
new method
sql/sql_class.cc:
By default we set THD::slave_proxy_id to THD::thread_id,
so THD::slave_proxy_id is always meaningful (not 0).
It's always the same as the thread id except for the slave
SQL thread.
2003-10-31 23:20:23 +01:00
|
|
|
in_addr_t ip_addr;
|
2001-10-03 23:41:38 +02:00
|
|
|
char msg_buf[MAX_MYSQL_MANAGER_MSG];
|
|
|
|
int msg_len;
|
2001-10-12 17:37:25 +02:00
|
|
|
Vio* vio;
|
2002-08-08 02:12:02 +02:00
|
|
|
my_bool not_used;
|
2001-10-03 23:41:38 +02:00
|
|
|
|
|
|
|
if (!host)
|
|
|
|
host="localhost";
|
|
|
|
if (!user)
|
|
|
|
user="root";
|
|
|
|
if (!passwd)
|
|
|
|
passwd="";
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2002-06-11 10:20:31 +02:00
|
|
|
if ((sock=(my_socket)socket(AF_INET,SOCK_STREAM,0)) == INVALID_SOCKET)
|
2001-10-03 23:41:38 +02:00
|
|
|
{
|
|
|
|
con->last_errno=errno;
|
|
|
|
strmov(con->last_error,"Cannot create socket");
|
|
|
|
goto err;
|
|
|
|
}
|
2001-10-12 17:37:25 +02:00
|
|
|
if (!(vio=vio_new(sock,VIO_TYPE_TCPIP,FALSE)))
|
2001-10-03 23:41:38 +02:00
|
|
|
{
|
|
|
|
con->last_errno=ENOMEM;
|
|
|
|
strmov(con->last_error,"Cannot create network I/O object");
|
|
|
|
goto err;
|
|
|
|
}
|
2002-08-08 02:12:02 +02:00
|
|
|
vio_blocking(vio, TRUE, ¬_used);
|
2001-10-12 17:37:25 +02:00
|
|
|
my_net_init(&con->net,vio);
|
2001-10-03 23:41:38 +02:00
|
|
|
bzero((char*) &sock_addr,sizeof(sock_addr));
|
|
|
|
sock_addr.sin_family = AF_INET;
|
|
|
|
if ((int) (ip_addr = inet_addr(host)) != (int) INADDR_NONE)
|
|
|
|
{
|
|
|
|
memcpy_fixed(&sock_addr.sin_addr,&ip_addr,sizeof(ip_addr));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int tmp_errno;
|
|
|
|
struct hostent tmp_hostent,*hp;
|
|
|
|
char buff2[GETHOSTBYNAME_BUFF_SIZE];
|
|
|
|
hp = my_gethostbyname_r(host,&tmp_hostent,buff2,sizeof(buff2),
|
|
|
|
&tmp_errno);
|
|
|
|
if (!hp)
|
|
|
|
{
|
|
|
|
con->last_errno=tmp_errno;
|
2004-02-19 09:11:46 +01:00
|
|
|
sprintf(con->last_error,"Could not resolve host '%-.64s'",host);
|
2002-05-16 15:06:57 +02:00
|
|
|
my_gethostbyname_r_free();
|
2001-10-03 23:41:38 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
memcpy(&sock_addr.sin_addr,hp->h_addr, (size_t) hp->h_length);
|
2002-05-16 15:06:57 +02:00
|
|
|
my_gethostbyname_r_free();
|
2001-10-03 23:41:38 +02:00
|
|
|
}
|
|
|
|
sock_addr.sin_port = (ushort) htons((ushort) port);
|
|
|
|
if (my_connect(sock,(struct sockaddr *) &sock_addr, sizeof(sock_addr),
|
2002-10-02 12:33:08 +02:00
|
|
|
0))
|
2001-10-03 23:41:38 +02:00
|
|
|
{
|
|
|
|
con->last_errno=errno;
|
|
|
|
sprintf(con->last_error ,"Could not connect to %-.64s", host);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
/* read the greating */
|
2001-10-12 17:37:25 +02:00
|
|
|
if (my_net_read(&con->net) == packet_error)
|
2001-10-03 23:41:38 +02:00
|
|
|
{
|
|
|
|
con->last_errno=errno;
|
|
|
|
strmov(con->last_error,"Read error on socket");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
sprintf(msg_buf,"%-.16s %-.16s\n",user,passwd);
|
|
|
|
msg_len=strlen(msg_buf);
|
2001-10-12 17:37:25 +02:00
|
|
|
if (my_net_write(&con->net,msg_buf,msg_len) || net_flush(&con->net))
|
2001-10-03 23:41:38 +02:00
|
|
|
{
|
2001-10-12 17:37:25 +02:00
|
|
|
con->last_errno=con->net.last_errno;
|
2001-10-03 23:41:38 +02:00
|
|
|
strmov(con->last_error,"Write error on socket");
|
|
|
|
goto err;
|
|
|
|
}
|
2001-10-12 17:37:25 +02:00
|
|
|
if (my_net_read(&con->net) == packet_error)
|
2001-10-03 23:41:38 +02:00
|
|
|
{
|
|
|
|
con->last_errno=errno;
|
|
|
|
strmov(con->last_error,"Read error on socket");
|
|
|
|
goto err;
|
|
|
|
}
|
2001-11-06 23:13:29 +01:00
|
|
|
if ((con->cmd_status=atoi((char*) con->net.read_pos)) != MANAGER_OK)
|
2001-10-03 23:41:38 +02:00
|
|
|
{
|
|
|
|
strmov(con->last_error,"Access denied");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!my_multi_malloc(MYF(0), &con->host, (uint)strlen(host)+1,
|
|
|
|
&con->user, (uint)strlen(user)+1,
|
|
|
|
&con->passwd, (uint)strlen(passwd)+1,
|
|
|
|
NullS))
|
|
|
|
{
|
|
|
|
con->last_errno=ENOMEM;
|
|
|
|
strmov(con->last_error,"Out of memory");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
strmov(con->host,host);
|
|
|
|
strmov(con->user,user);
|
|
|
|
strmov(con->passwd,passwd);
|
|
|
|
return con;
|
2002-06-11 10:20:31 +02:00
|
|
|
|
2001-10-03 23:41:38 +02:00
|
|
|
err:
|
|
|
|
{
|
|
|
|
my_bool free_me=con->free_me;
|
|
|
|
con->free_me=0;
|
|
|
|
mysql_manager_close(con);
|
|
|
|
con->free_me=free_me;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-06-11 10:20:31 +02:00
|
|
|
void STDCALL mysql_manager_close(MYSQL_MANAGER* con)
|
2001-10-03 23:41:38 +02:00
|
|
|
{
|
2002-06-11 10:20:31 +02:00
|
|
|
/*
|
|
|
|
No need to free con->user and con->passwd, because they were
|
|
|
|
allocated in my_multimalloc() along with con->host, freeing
|
|
|
|
con->hosts frees the whole block
|
2001-10-03 23:41:38 +02:00
|
|
|
*/
|
2002-06-11 10:20:31 +02:00
|
|
|
my_free((gptr)con->host,MYF(MY_ALLOW_ZERO_PTR));
|
2001-10-12 17:37:25 +02:00
|
|
|
net_end(&con->net);
|
2001-10-03 23:41:38 +02:00
|
|
|
if (con->free_me)
|
|
|
|
my_free((gptr)con,MYF(0));
|
|
|
|
}
|
|
|
|
|
2002-06-11 10:20:31 +02:00
|
|
|
|
2001-10-03 23:41:38 +02:00
|
|
|
int STDCALL mysql_manager_command(MYSQL_MANAGER* con,const char* cmd,
|
|
|
|
int cmd_len)
|
|
|
|
{
|
|
|
|
if (!cmd_len)
|
|
|
|
cmd_len=strlen(cmd);
|
2001-10-12 17:37:25 +02:00
|
|
|
if (my_net_write(&con->net,(char*)cmd,cmd_len) || net_flush(&con->net))
|
2001-10-03 23:41:38 +02:00
|
|
|
{
|
|
|
|
con->last_errno=errno;
|
|
|
|
strmov(con->last_error,"Write error on socket");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
con->eof=0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-06-11 10:20:31 +02:00
|
|
|
|
|
|
|
int STDCALL mysql_manager_fetch_line(MYSQL_MANAGER* con, char* res_buf,
|
|
|
|
int res_buf_size)
|
2001-10-03 23:41:38 +02:00
|
|
|
{
|
|
|
|
char* res_buf_end=res_buf+res_buf_size;
|
2001-11-06 23:13:29 +01:00
|
|
|
char* net_buf=(char*) con->net.read_pos, *net_buf_end;
|
2001-10-03 23:41:38 +02:00
|
|
|
int res_buf_shift=RES_BUF_SHIFT;
|
2004-10-18 09:43:52 +02:00
|
|
|
ulong num_bytes;
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2001-10-03 23:41:38 +02:00
|
|
|
if (res_buf_size<RES_BUF_SHIFT)
|
|
|
|
{
|
|
|
|
con->last_errno=ENOMEM;
|
|
|
|
strmov(con->last_error,"Result buffer too small");
|
|
|
|
return 1;
|
|
|
|
}
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2001-10-12 17:37:25 +02:00
|
|
|
if ((num_bytes=my_net_read(&con->net)) == packet_error)
|
2001-10-03 23:41:38 +02:00
|
|
|
{
|
2001-10-12 17:37:25 +02:00
|
|
|
con->last_errno=errno;
|
|
|
|
strmov(con->last_error,"socket read failed");
|
|
|
|
return 1;
|
2001-10-03 23:41:38 +02:00
|
|
|
}
|
2001-10-12 17:37:25 +02:00
|
|
|
|
|
|
|
net_buf_end=net_buf+num_bytes;
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2001-10-12 17:37:25 +02:00
|
|
|
if ((con->eof=(net_buf[3]==' ')))
|
2001-10-03 23:41:38 +02:00
|
|
|
res_buf_shift--;
|
2001-10-12 17:37:25 +02:00
|
|
|
net_buf+=res_buf_shift;
|
|
|
|
res_buf_end[-1]=0;
|
|
|
|
for (;net_buf<net_buf_end && res_buf < res_buf_end;res_buf++,net_buf++)
|
2001-10-03 23:41:38 +02:00
|
|
|
{
|
2002-06-11 10:20:31 +02:00
|
|
|
if ((*res_buf=*net_buf) == '\r')
|
2001-10-12 17:37:25 +02:00
|
|
|
{
|
|
|
|
*res_buf=0;
|
2001-10-03 23:41:38 +02:00
|
|
|
break;
|
2001-10-12 17:37:25 +02:00
|
|
|
}
|
2001-10-03 23:41:38 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|