diff options
Diffstat (limited to 'client')
-rw-r--r-- | client/Makefile.am | 2 | ||||
-rw-r--r-- | client/errmsg.c | 22 | ||||
-rw-r--r-- | client/insert_test.c | 8 | ||||
-rw-r--r-- | client/mysql.cc | 127 | ||||
-rw-r--r-- | client/mysqldump.c | 25 | ||||
-rw-r--r-- | client/mysqltest.c | 42 | ||||
-rw-r--r-- | client/thread_test.c | 2 | ||||
-rw-r--r-- | client/violite.c | 394 |
8 files changed, 179 insertions, 443 deletions
diff --git a/client/Makefile.am b/client/Makefile.am index a91c7d05f77..559cb27f01e 100644 --- a/client/Makefile.am +++ b/client/Makefile.am @@ -16,7 +16,7 @@ # This file is public domain and comes with NO WARRANTY of any kind -INCLUDES = -I$(srcdir)/../include \ +INCLUDES = -I$(srcdir)/../include $(openssl_includes) \ -I../include -I$(srcdir)/.. -I$(top_srcdir) \ -I.. LIBS = @CLIENT_LIBS@ diff --git a/client/errmsg.c b/client/errmsg.c index 67cfe874f77..9f67a15dcb2 100644 --- a/client/errmsg.c +++ b/client/errmsg.c @@ -45,7 +45,13 @@ const char *client_errors[]= "Kann Named Pipe nicht oeffnen. Host: %-.64s pipe: %-.32s (%lu)", "Kann den Status der Named Pipe nicht setzen. Host: %-.64s pipe: %-.32s (%lu)", "Can't initialize character set %-.64s (path: %-.64s)", - "Got packet bigger than 'max_allowed_packet'" + "Got packet bigger than 'max_allowed_packet'", + "Embedded server", + "Error on SHOW SLAVE STATUS:", + "Error on SHOW SLAVE HOSTS:", + "Error connecting to slave:", + "Error connecting to master:" + }; /* Start of code added by Roberto M. Serqueira - martinsc@uol.com.br - 05.24.2001 */ @@ -73,7 +79,12 @@ const char *client_errors[]= "Não pode abrir 'named pipe' para o 'host' %-.64s - 'pipe' %-.32s (%lu)", "Não pode estabelecer o estado do 'named pipe' para o 'host' %-.64s - 'pipe' %-.32s (%lu)", "Não pode inicializar conjunto de caracteres %-.64s (caminho %-.64s)", - "Obteve pacote maior do que 'max_allowed_packet'" + "Obteve pacote maior do que 'max_allowed_packet'", + "Embedded server", + "Error on SHOW SLAVE STATUS:", + "Error on SHOW SLAVE HOSTS:", + "Error connecting to slave:", + "Error connecting to master:" }; #else /* ENGLISH */ @@ -99,7 +110,12 @@ const char *client_errors[]= "Can't open named pipe to host: %-.64s pipe: %-.32s (%lu)", "Can't set state of named pipe to host: %-.64s pipe: %-.32s (%lu)", "Can't initialize character set %-.64s (path: %-.64s)", - "Got packet bigger than 'max_allowed_packet'" + "Got packet bigger than 'max_allowed_packet'", + "Embedded server", + "Error on SHOW SLAVE STATUS:", + "Error on SHOW SLAVE HOSTS:", + "Error connecting to slave:", + "Error connecting to master:" }; #endif diff --git a/client/insert_test.c b/client/insert_test.c index 640935d63b2..82609f68e4e 100644 --- a/client/insert_test.c +++ b/client/insert_test.c @@ -34,19 +34,13 @@ int main(int argc, char **argv) exit(1); } - if (!(sock = mysql_connect(&mysql,NULL,0,0))) + if (!(sock = mysql_real_connect(&mysql,NULL,NULL,NULL,argv[1],0,NULL,0))) { fprintf(stderr,"Couldn't connect to engine!\n%s\n",mysql_error(&mysql)); perror(""); exit(1); } - if (mysql_select_db(sock,argv[1])) - { - fprintf(stderr,"Couldn't select database %s!\n%s\n",argv[1], - mysql_error(sock)); - } - num = atoi(argv[2]); count = 0; while (count < num) diff --git a/client/mysql.cc b/client/mysql.cc index e4db7e93256..ec8b6689dcd 100644 --- a/client/mysql.cc +++ b/client/mysql.cc @@ -19,9 +19,11 @@ * * Written by: * Michael 'Monty' Widenius - * Andi Gutmans <andi@zend.com> + * Andi Gutmans <andi@zend.com> * Zeev Suraski <zeev@zend.com> * Jani Tolonen <jani@mysql.com> + * Matt Wagner <mwagner@mysql.com> + * Jeremy Cole <jcole@mysql.com> * **/ @@ -38,6 +40,7 @@ #include <getopt.h> #include "my_readline.h" #include <signal.h> +#include <violite.h> const char *VER="11.15"; @@ -120,7 +123,7 @@ static bool info_flag=0,ignore_errors=0,wait_flag=0,quick=0, no_rehash=0,skip_updates=0,safe_updates=0,one_database=0, opt_compress=0, vertical=0,skip_line_numbers=0,skip_column_names=0,opt_html=0, - opt_nopager=1, opt_outfile=0, no_named_cmds=1; + opt_xml=0,opt_nopager=1, opt_outfile=0, no_named_cmds=1; static uint verbose=0,opt_silent=0,opt_mysql_port=0; static my_string opt_mysql_unix_port=0; static int connect_flag=CLIENT_INTERACTIVE; @@ -131,6 +134,11 @@ static String glob_buffer,old_buffer; static int wait_time = 5; static STATUS status; static ulong select_limit,max_join_size,opt_connect_timeout=0; +static const char *xmlmeta[] = { + "&", "&", + "<", "<", + 0, 0 +}; static char default_pager[FN_REFLEN]; char pager[FN_REFLEN], outfile[FN_REFLEN]; FILE *PAGER, *OUTFILE; @@ -166,6 +174,9 @@ static int sql_connect(char *host,char *database,char *user,char *password, uint silent); static int put_info(const char *str,INFO_TYPE info,uint error=0); static void safe_put_field(const char *pos,ulong length); +static const char *array_value(const char **array, char *key); +static void xmlencode(char *dest, char *src); +static void my_chomp(char *end); static void init_pager(); static void end_pager(); static void init_tee(); @@ -250,6 +261,7 @@ static bool add_line(String &buffer,char *line,char *in_string); static void remove_cntrl(String &buffer); static void print_table_data(MYSQL_RES *result); static void print_table_data_html(MYSQL_RES *result); +static void print_table_data_xml(MYSQL_RES *result); static void print_tab_data(MYSQL_RES *result); static void print_table_data_vertically(MYSQL_RES *result); static ulong start_timer(void); @@ -316,9 +328,19 @@ int main(int argc,char *argv[]) mysql_thread_id(&mysql),mysql_get_server_info(&mysql)); put_info((char*) glob_buffer.ptr(),INFO_INFO); +#ifdef HAVE_OPENSSL + if(mysql.net.vio->ssl_ && SSL_get_cipher(mysql.net.vio->ssl_)) { + sprintf((char*) glob_buffer.ptr(), + "SSL cipher in use is %s\n", SSL_get_cipher(mysql.net.vio->ssl_)); + put_info((char*) glob_buffer.ptr(),INFO_INFO); + } else + put_info("SSL is not in use\n",INFO_INFO); +#endif /* HAVE_OPENSSL */ + + #ifdef HAVE_READLINE initialize_readline(my_progname); - if (!status.batch && !quick && !opt_html) + if (!status.batch && !quick && !opt_html && !opt_xml) { /*read-history from file, default ~/.mysql_history*/ if (getenv("MYSQL_HISTFILE")) @@ -355,8 +377,11 @@ sig_handler mysql_end(int sig) { if (connected) mysql_close(&mysql); + else + mysql_ssl_clear(&mysql); /* SSL data structres should be freed + even if connection was not made */ #ifdef HAVE_READLINE - if (!status.batch && !quick && ! opt_html) + if (!status.batch && !quick && !opt_html && !opt_xml) { /* write-history */ if (verbose) @@ -401,6 +426,7 @@ static struct option long_options[] = {"force", no_argument, 0, 'f'}, {"help", no_argument, 0, '?'}, {"html", no_argument, 0, 'H'}, + {"xml", no_argument, 0, 'X'}, {"host", required_argument, 0, 'h'}, {"ignore-spaces", no_argument, 0, 'i'}, {"no-auto-rehash",no_argument, 0, 'A'}, @@ -495,6 +521,7 @@ static void usage(int version) -i, --ignore-space Ignore space after function names.\n\ -h, --host=... Connect to host.\n\ -H, --html Produce HTML output.\n\ + -X, --xml Produce XML output.\n\ -L, --skip-line-numbers\n\ Don't write line number for errors.\n"); #ifndef __WIN__ @@ -569,7 +596,7 @@ static int get_options(int argc, char **argv) set_all_changeable_vars(changeable_vars); while ((c=getopt_long(argc,argv, - (char*) "?ABCD:LfgGHinNoqrstTU::vVw::WEe:h:O:P:S:u:#::p::", + (char*) "?ABCD:LfgGHXinNoqrstTU::vVw::WEe:h:O:P:S:u:#::p::", long_options, &option_index)) != EOF) { switch(c) { @@ -690,6 +717,7 @@ static int get_options(int argc, char **argv) case 'G': no_named_cmds=0; break; case 'g': no_named_cmds=1; break; case 'H': opt_html=1; break; + case 'X': opt_xml=1; break; case 'i': connect_flag|= CLIENT_IGNORE_SPACE; break; case 'B': if (!status.batch) @@ -1457,6 +1485,8 @@ com_go(String *buffer,char *line __attribute__((unused))) init_pager(); if (opt_html) print_table_data_html(result); + else if (opt_xml) + print_table_data_xml(result); else if (vertical) print_table_data_vertically(result); else if (opt_silent && verbose <= 2 && !output_tables) @@ -1659,6 +1689,49 @@ print_table_data_html(MYSQL_RES *result) } +static void +print_table_data_xml(MYSQL_RES *result) +{ + MYSQL_ROW cur; + MYSQL_FIELD *fields; + + mysql_field_seek(result,0); + + char *statement; + statement=(char*) my_malloc(strlen(glob_buffer.ptr())*5+1, MYF(MY_WME)); + xmlencode(statement, (char*) glob_buffer.ptr()); + + (void) my_chomp(strend(statement)); + + tee_fprintf(PAGER,"<?xml version=\"1.0\"?>\n\n<resultset statement=\"%s\">", statement); + + my_free(statement,MYF(MY_ALLOW_ZERO_PTR)); + + fields = mysql_fetch_fields(result); + + while ((cur = mysql_fetch_row(result))) + { + (void) tee_fputs("\n <row>\n", PAGER); + for (uint i=0; i < mysql_num_fields(result); i++) + { + char *data; + ulong *lengths=mysql_fetch_lengths(result); + data=(char*) my_malloc(lengths[i]*5+1, MYF(MY_WME)); + tee_fprintf(PAGER, "\t<%s>", (fields[i].name ? + (fields[i].name[0] ? fields[i].name : + " ") : "NULL")); + xmlencode(data, cur[i]); + safe_put_field(data, strlen(data)); + tee_fprintf(PAGER, "</%s>\n", (fields[i].name ? + (fields[i].name[0] ? fields[i].name : + " ") : "NULL")); + my_free(data,MYF(MY_ALLOW_ZERO_PTR)); + } + (void) tee_fputs(" </row>\n", PAGER); + } + (void) tee_fputs("</resultset>\n", PAGER); +} + static void print_table_data_vertically(MYSQL_RES *result) @@ -1690,6 +1763,45 @@ print_table_data_vertically(MYSQL_RES *result) } } +static const char +*array_value(const char **array, char *key) { + int x; + for(x=0; array[x]; x+=2) + if(!strcmp(array[x], key)) + return array[x+1]; + return 0; +} + +static void +xmlencode(char *dest, char *src) +{ + char *p = src; + const char *t; + char s[2] = { 0, 0 }; + *dest = 0; + + do + { + s[0] = *p; + if (!(t=array_value(xmlmeta, s))) + t = s; + dest=strmov(dest, t); + } while(*p++); +} + +static void +my_chomp(char *end) { + char *mend; + mend = end; + + do { + if (isspace(*mend)) { + *mend = '\0'; + } else + mend--; + } while (mend && *mend); +} + static void safe_put_field(const char *pos,ulong length) @@ -1720,7 +1832,7 @@ safe_put_field(const char *pos,ulong length) tee_fputs("\\n", PAGER); // This too else if (*pos == '\\') tee_fputs("\\\\", PAGER); - else + else tee_putc(*pos, PAGER); } } @@ -2105,6 +2217,9 @@ sql_real_connect(char *host,char *database,char *user,char *password, mysql_close(&mysql); connected= 0; } + else + mysql_ssl_clear(&mysql); /* SSL data structres should be freed + even if connection was not made */ mysql_init(&mysql); if (opt_connect_timeout) { diff --git a/client/mysqldump.c b/client/mysqldump.c index 9038c88e085..b296d24290d 100644 --- a/client/mysqldump.c +++ b/client/mysqldump.c @@ -274,12 +274,12 @@ puts("\ static void write_heder(FILE *sql_file, char *db_name) { - fprintf(sql_file, "# MySQL dump %s\n#\n", DUMP_VERSION); - fprintf(sql_file, "# Host: %s Database: %s\n", + fprintf(sql_file, "-- MySQL dump %s\n#\n", DUMP_VERSION); + fprintf(sql_file, "-- Host: %s Database: %s\n", current_host ? current_host : "localhost", db_name ? db_name : ""); - fputs("#--------------------------------------------------------\n", + fputs("---------------------------------------------------------\n", sql_file); - fprintf(sql_file, "# Server version\t%s\n", + fprintf(sql_file, "-- Server version\t%s\n", mysql_get_server_info(&mysql_connection)); return; } /* write_heder */ @@ -515,7 +515,7 @@ static int dbConnect(char *host, char *user,char *passwd) DBUG_ENTER("dbConnect"); if (verbose) { - fprintf(stderr, "# Connecting to %s...\n", host ? host : "localhost"); + fprintf(stderr, "-- Connecting to %s...\n", host ? host : "localhost"); } mysql_init(&mysql_connection); if (opt_compress) @@ -542,7 +542,7 @@ static int dbConnect(char *host, char *user,char *passwd) static void dbDisconnect(char *host) { if (verbose) - fprintf(stderr, "# Disconnecting from %s...\n", host ? host : "localhost"); + fprintf(stderr, "-- Disconnecting from %s...\n", host ? host : "localhost"); mysql_close(sock); } /* dbDisconnect */ @@ -608,7 +608,7 @@ static uint getTableStructure(char *table, char* db) delayed= opt_delayed ? " DELAYED " : ""; if (verbose) - fprintf(stderr, "# Retrieving table structure for table %s...\n", table); + fprintf(stderr, "-- Retrieving table structure for table %s...\n", table); sprintf(insert_pat,"SET OPTION SQL_QUOTE_SHOW_CREATE=%d", opt_quoted); table_name=quote_name(table,table_buff); @@ -837,7 +837,7 @@ static uint getTableStructure(char *table, char* db) { /* If old MySQL version */ if (verbose) fprintf(stderr, - "# Warning: Couldn't get status information for table '%s' (%s)\n", + "-- Warning: Couldn't get status information for table '%s' (%s)\n", table,mysql_error(sock)); } } @@ -934,7 +934,7 @@ static void dumpTable(uint numFields, char *table) ulong rownr, row_break, total_length, init_length; if (verbose) - fprintf(stderr, "# Sending SELECT query...\n"); + fprintf(stderr, "-- Sending SELECT query...\n"); if (path) { char filename[FN_REFLEN], tmp_path[FN_REFLEN]; @@ -977,11 +977,10 @@ static void dumpTable(uint numFields, char *table) sprintf(query, "SELECT * FROM %s", quote_name(table,table_buff)); if (where) { - fprintf(md_result_file,"# WHERE: %s\n",where); + fprintf(md_result_file,"-- WHERE: %s\n",where); strxmov(strend(query), " WHERE ",where,NullS); } - fputs("#\n\n", md_result_file); - + fputs("\n\n", md_result_file); if (mysql_query(sock, query)) { DBerror(sock, "when retrieving data from server"); @@ -997,7 +996,7 @@ static void dumpTable(uint numFields, char *table) return; } if (verbose) - fprintf(stderr, "# Retrieving rows...\n"); + fprintf(stderr, "-- Retrieving rows...\n"); if (mysql_num_fields(res) != numFields) { fprintf(stderr,"%s: Error in field count for table: '%s' ! Aborting.\n", diff --git a/client/mysqltest.c b/client/mysqltest.c index 7d149fe6f66..f269dd373c6 100644 --- a/client/mysqltest.c +++ b/client/mysqltest.c @@ -257,7 +257,6 @@ static int mysql_rpl_parse_enabled(MYSQL* mysql __attribute__((unused))) { retur static int mysql_rpl_probe(MYSQL *mysql __attribute__((unused))) { return 1; } #endif - static void do_eval(DYNAMIC_STRING* query_eval, const char* query) { const char* p; @@ -1112,6 +1111,25 @@ char* safe_get_param(char* str, char** arg, const char* msg) DBUG_RETURN(str); } +int safe_connect(MYSQL* con, const char* host, const char* user, + const char* pass, + const char* db, int port, const char* sock) +{ + int con_error = 1; + int i; + for (i = 0; i < MAX_CON_TRIES; ++i) + { + if(mysql_real_connect(con, host,user, pass, + db, port, sock, 0)) + { + con_error = 0; + break; + } + sleep(CON_RETRY_SLEEP); + } + return con_error; +} + int do_connect(struct st_query* q) { @@ -1120,7 +1138,7 @@ int do_connect(struct st_query* q) char* p=q->first_argument; char buff[FN_REFLEN]; int con_port; - int i, con_error; + int con_error; DBUG_ENTER("do_connect"); DBUG_PRINT("enter",("connect: %s",p)); @@ -1153,20 +1171,9 @@ int do_connect(struct st_query* q) con_sock=fn_format(buff, con_sock, TMPDIR, "",0); if (!con_db[0]) con_db=db; - con_error = 1; - for (i = 0; i < MAX_CON_TRIES; ++i) - { - if(mysql_real_connect(&next_con->mysql, con_host, + if((con_error = safe_connect(&next_con->mysql, con_host, con_user, con_pass, - con_db, con_port, con_sock, 0)) - { - con_error = 0; - break; - } - sleep(CON_RETRY_SLEEP); - } - - if(con_error) + con_db, con_port, con_sock))) die("Could not open connection '%s': %s", con_name, mysql_error(&next_con->mysql)); @@ -1966,9 +1973,8 @@ int main(int argc, char** argv) if (!cur_con->name) die("Out of memory"); - if (!mysql_real_connect(&cur_con->mysql, host, - user, pass, db, port, unix_sock, - 0)) + if (safe_connect(&cur_con->mysql, host, + user, pass, db, port, unix_sock)) die("Failed in mysql_real_connect(): %s", mysql_error(&cur_con->mysql)); while (!read_query(&q)) diff --git a/client/thread_test.c b/client/thread_test.c index dbe2acee8db..41adb23c408 100644 --- a/client/thread_test.c +++ b/client/thread_test.c @@ -19,7 +19,7 @@ #ifndef THREAD -int main(void) +int main(int argc __attribute__((unused)), char **argv __attribute__((unused))) { printf("This test must be compiled with multithread support to work\n"); exit(1); diff --git a/client/violite.c b/client/violite.c deleted file mode 100644 index 224ba051d82..00000000000 --- a/client/violite.c +++ /dev/null @@ -1,394 +0,0 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with this library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, - MA 02111-1307, USA */ - -#include <global.h> - -#ifndef HAVE_VIO /* is Vio suppored by the Vio lib ? */ - -#include <errno.h> -#include <assert.h> -#include <violite.h> -#include <my_sys.h> -#include <my_net.h> -#include <m_string.h> - -#if defined(__EMX__) -#include <sys/ioctl.h> -#define ioctlsocket(A,B,C) ioctl((A),(B),(void *)(C),sizeof(*(C))) -#undef HAVE_FCNTL -#endif /* defined(__EMX__) */ - -#if defined(MSDOS) || defined(__WIN__) -#ifdef __WIN__ -#undef errno -#undef EINTR -#undef EAGAIN -#define errno WSAGetLastError() -#define EINTR WSAEINTR -#define EAGAIN WSAEINPROGRESS -#endif /* __WIN__ */ -#define O_NONBLOCK 1 /* For emulation of fcntl() */
-#endif -#ifndef EWOULDBLOCK -#define EWOULDBLOCK EAGAIN -#endif - -#ifndef __WIN__ -#define HANDLE void * -#endif - -struct st_vio -{ - my_socket sd; /* my_socket - real or imaginary */ - HANDLE hPipe; - my_bool localhost; /* Are we from localhost? */ - int fcntl_mode; /* Buffered fcntl(sd,F_GETFL) */ - my_bool fcntl_set; /* Have we done any fcntl yet?*/ - struct sockaddr_in local; /* Local internet address */ - struct sockaddr_in remote; /* Remote internet address */ - enum enum_vio_type type; /* Type of connection */ - char desc[30]; /* String description */ -}; - -typedef void *vio_ptr; -typedef char *vio_cstring; - -/* - * Helper to fill most of the Vio* with defaults. - */ - -static void vio_reset(Vio* vio, enum enum_vio_type type, - my_socket sd, HANDLE hPipe, - my_bool localhost) -{ - bzero((char*) vio, sizeof(*vio)); - vio->type = type; - vio->sd = sd; - vio->hPipe = hPipe; - vio->localhost= localhost; -} - -Vio *vio_new(my_socket sd, enum enum_vio_type type, my_bool localhost) -{ - Vio *vio; - DBUG_ENTER("vio_new"); - DBUG_PRINT("enter", ("sd=%d", sd)); - if ((vio = (Vio*) my_malloc(sizeof(*vio),MYF(MY_WME)))) - { - vio_reset(vio, type, sd, 0, localhost); - sprintf(vio->desc, "socket (%d)", vio->sd); - } - DBUG_RETURN(vio); -} - - -#ifdef __WIN__ - -Vio *vio_new_win32pipe(HANDLE hPipe) -{ - Vio *vio; - DBUG_ENTER("vio_new_handle"); - if ((vio = (Vio*) my_malloc(sizeof(Vio),MYF(MY_WME)))) - { - vio_reset(vio, VIO_TYPE_NAMEDPIPE, 0, hPipe, TRUE); - strmov(vio->desc, "named pipe"); - } - DBUG_RETURN(vio); -} - -#endif - -void vio_delete(Vio * vio) -{ - /* It must be safe to delete null pointers. */ - /* This matches the semantics of C++'s delete operator. */ - if (vio) - { - vio_close(vio); - my_free((gptr) vio,MYF(0)); - } -} - -int vio_errno(Vio *vio) -{ - return errno; /* On Win32 this mapped to WSAGetLastError() */ -} - - -int vio_read(Vio * vio, gptr buf, int size) -{ - int r; - DBUG_ENTER("vio_read"); - DBUG_PRINT("enter", ("sd=%d, buf=%p, size=%d", vio->sd, buf, size)); - assert(vio->sd >= 0); -#ifdef __WIN__ - if (vio->type == VIO_TYPE_NAMEDPIPE) - { - DWORD length;
- if (!ReadFile(vio->hPipe, buf, size, &length, NULL)) - DBUG_RETURN(-1); - DBUG_RETURN(length); - } - r = recv(vio->sd, buf, size,0); -#else - errno=0; /* For linux */ - r = read(vio->sd, buf, size); -#endif /* __WIN__ */ -#ifndef DBUG_OFF - if (r < 0) - { - DBUG_PRINT("error", ("Got error %d during read",errno)); - } -#endif /* DBUG_OFF */ - DBUG_PRINT("exit", ("%d", r)); - DBUG_RETURN(r); -} - - -int vio_write(Vio * vio, const gptr buf, int size) -{ - int r; - DBUG_ENTER("vio_write"); - DBUG_PRINT("enter", ("sd=%d, buf=%p, size=%d", vio->sd, buf, size)); - assert(vio->sd >= 0); -#ifdef __WIN__ - if ( vio->type == VIO_TYPE_NAMEDPIPE) - { - DWORD length;
- if (!WriteFile(vio->hPipe, (char*) buf, size, &length, NULL)) - DBUG_RETURN(-1); - DBUG_RETURN(length); - } - r = send(vio->sd, buf, size,0); -#else - r = write(vio->sd, buf, size); -#endif /* __WIN__ */ -#ifndef DBUG_OFF - if (r < 0) - { - DBUG_PRINT("error", ("Got error on write: %d",errno)); - } -#endif /* DBUG_OFF */ - DBUG_PRINT("exit", ("%d", r)); - DBUG_RETURN(r); -} - - -int vio_blocking(Vio * vio, my_bool set_blocking_mode) -{ - int r=0; - DBUG_ENTER("vio_blocking"); - DBUG_PRINT("enter", ("set_blocking_mode: %d", (int) set_blocking_mode)); - -#if !defined(___WIN__) && !defined(__EMX__) -#if !defined(NO_FCNTL_NONBLOCK) - - if (vio->sd >= 0) - { - int old_fcntl=vio->fcntl_mode; - if (!vio->fcntl_set) - { - vio->fcntl_set = TRUE; - old_fcntl=vio->fcntl_mode = fcntl(vio->sd, F_GETFL); - } - if (set_blocking_mode) - vio->fcntl_mode &= ~O_NONBLOCK; /*clear bit */ - else - vio->fcntl_mode |= O_NONBLOCK; /*set bit */ - if (old_fcntl != vio->fcntl_mode) - r = fcntl(vio->sd, F_SETFL, vio->fcntl_mode); - } -#endif /* !defined(NO_FCNTL_NONBLOCK) */ -#else /* !defined(__WIN__) && !defined(__EMX__) */ -#ifndef __EMX__ - if (vio->type != VIO_TYPE_NAMEDPIPE) -#endif - { - ulong arg; - int old_fcntl=vio->fcntl_mode; - if (!vio->fcntl_set) - { - vio->fcntl_set = TRUE; - old_fnctl=vio->fcntl_mode=0; - } - if (set_blocking_mode) - { - arg = 0; - vio->fcntl_mode &= ~O_NONBLOCK; /*clear bit */ - } - else - { - arg = 1; - vio->fcntl_mode |= O_NONBLOCK; /*set bit */ - } - if (old_fcntl != vio->fcntl_mode) - r = ioctlsocket(vio->sd,FIONBIO,(void*) &arg, sizeof(arg)); - } -#endif /* !defined(__WIN__) && !defined(__EMX__) */ - DBUG_RETURN(r); -} - -my_bool -vio_is_blocking(Vio * vio) -{ - my_bool r; - DBUG_ENTER("vio_is_blocking"); - r = !(vio->fcntl_mode & O_NONBLOCK); - DBUG_PRINT("exit", ("%d", (int) r)); - DBUG_RETURN(r); -} - - -int vio_fastsend(Vio * vio, my_bool onoff) -{ - int r=0; - DBUG_ENTER("vio_fastsend"); - DBUG_PRINT("enter", ("onoff:%d", (int) onoff)); - assert(vio->sd >= 0); - -#ifdef IPTOS_THROUGHPUT - { -#ifndef __EMX__ - int tos = IPTOS_THROUGHPUT; - if (!setsockopt(vio->sd, IPPROTO_IP, IP_TOS, (void *) &tos, sizeof(tos))) -#endif /* !__EMX__ */ - { - int nodelay = 1; - if (setsockopt(vio->sd, IPPROTO_TCP, TCP_NODELAY, (void *) &nodelay, - sizeof(nodelay))) { - DBUG_PRINT("warning", - ("Couldn't set socket option for fast send")); - r= -1; - } - } - } -#endif /* IPTOS_THROUGHPUT */ - DBUG_PRINT("exit", ("%d", r)); - DBUG_RETURN(r); -} - -int vio_keepalive(Vio* vio, my_bool set_keep_alive) -{ - int r=0; - uint opt = 0; - DBUG_ENTER("vio_keepalive"); - DBUG_PRINT("enter", ("sd=%d, set_keep_alive=%d", vio->sd, (int) - set_keep_alive)); - if (vio->type != VIO_TYPE_NAMEDPIPE) - { - assert(vio->sd >= 0); - if (set_keep_alive) - opt = 1; - r = setsockopt(vio->sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, - sizeof(opt)); - } - DBUG_RETURN(r); -} - - -my_bool -vio_should_retry(Vio * vio) -{ - int en = errno; - return en == EAGAIN || en == EINTR || en == EWOULDBLOCK; -} - - -int vio_close(Vio * vio) -{ - int r; - DBUG_ENTER("vio_close"); - assert(vio->sd >= 0); /* Vill also work on PIPE:s */ -#ifdef __WIN__ - if (vio->type == VIO_TYPE_NAMEDPIPE) - { -#if defined(__NT__) && defined(MYSQL_SERVER) - CancelIO(vio->hPipe); - DisconnectNamedPipe(vio->hPipe); -#endif - r=CloseHandle(vio->hPipe); - } - else -#endif /* __WIN__ */ - { - r=0; - if (shutdown(vio->sd,2)) - r= -1; - if (closesocket(vio->sd)) - r= -1; - } - if (r) - { - DBUG_PRINT("error", ("close() failed, error: %d",errno)); - /* FIXME: error handling (not critical for MySQL) */ - } - vio_reset(vio,VIO_CLOSED,-1,0,TRUE); /* For debugging */ - DBUG_RETURN(r); -} - - -const char *vio_description(Vio * vio) -{ - return vio->desc; -} - -enum enum_vio_type vio_type(Vio* vio) -{ - return vio->type; -} - -my_socket vio_fd(Vio* vio) -{ - return vio->sd; -} - - -my_bool vio_peer_addr(Vio * vio, char *buf) -{ - DBUG_ENTER("vio_peer_addr"); - DBUG_PRINT("enter", ("sd=%d", vio->sd)); - if (vio->localhost) - { - strmov(buf,"127.0.0.1"); - } - else - { - size_socket addrLen = sizeof(struct sockaddr); - if (getpeername(vio->sd, (struct sockaddr *) (& (vio->remote)), - &addrLen) != 0) - { - DBUG_PRINT("exit", ("getpeername, error: %d", errno)); - DBUG_RETURN(1); - } - my_inet_ntoa(vio->remote.sin_addr,buf); - } - DBUG_PRINT("exit", ("addr=%s", buf)); - DBUG_RETURN(0); -} - - -void vio_in_addr(Vio *vio, struct in_addr *in) -{ - DBUG_ENTER("vio_in_addr"); - if (vio->localhost) - bzero((char*) in, sizeof(*in)); /* This should never be executed */ - else - *in=vio->remote.sin_addr; - DBUG_VOID_RETURN; -} - -#endif /* HAVE_VIO */ |