diff options
Diffstat (limited to 'client')
-rw-r--r-- | client/Makefile.am | 12 | ||||
-rw-r--r-- | client/mysqladmin.c | 78 | ||||
-rw-r--r-- | client/mysqlmanager-pwgen.c | 153 | ||||
-rw-r--r-- | client/mysqlmanagerc.c | 192 | ||||
-rw-r--r-- | client/mysqltest.c | 158 |
5 files changed, 544 insertions, 49 deletions
diff --git a/client/Makefile.am b/client/Makefile.am index 3239744542f..8763486d3e2 100644 --- a/client/Makefile.am +++ b/client/Makefile.am @@ -22,7 +22,7 @@ INCLUDES = -I$(srcdir)/../include $(openssl_includes) \ LIBS = @CLIENT_LIBS@ LDADD = @CLIENT_EXTRA_LDFLAGS@ ../libmysql/libmysqlclient.la bin_PROGRAMS = mysql mysqladmin mysqlcheck mysqlshow \ - mysqldump mysqlimport mysqltest mysqlbinlog + mysqldump mysqlimport mysqltest mysqlbinlog mysqlmanagerc mysqlmanager-pwgen noinst_PROGRAMS = insert_test select_test thread_test noinst_HEADERS = sql_string.h completion_hash.h my_readline.h \ client_priv.h @@ -36,14 +36,16 @@ mysqldump_DEPENDENCIES= $(LIBRARIES) $(pkglib_LTLIBRARIES) mysqlimport_DEPENDENCIES= $(LIBRARIES) $(pkglib_LTLIBRARIES) insert_test_DEPENDENCIES= $(LIBRARIES) $(pkglib_LTLIBRARIES) select_test_DEPENDENCIES= $(LIBRARIES) $(pkglib_LTLIBRARIES) -mysqltest_SOURCES= mysqltest.c +mysqltest_SOURCES= mysqltest.c mysqltest_DEPENDENCIES= $(LIBRARIES) $(pkglib_LTLIBRARIES) -mysqlbinlog_SOURCES = mysqlbinlog.cc +mysqlbinlog_SOURCES = mysqlbinlog.cc mysqlbinlog_DEPENDENCIES= $(LIBRARIES) $(pkglib_LTLIBRARIES) -sql_src= log_event.h log_event.cc +mysqlmanagerc_SOURCES = mysqlmanagerc.c +mysqlmanagerc_DEPENDENCIES= $(LIBRARIES) $(pkglib_LTLIBRARIES) +sql_src=log_event.h log_event.cc # Fix for mit-threads -DEFS= -DUNDEF_THREADS_HACK +DEFS = -DUNDEF_THREADS_HACK link_sources: for f in $(sql_src) ; do \ diff --git a/client/mysqladmin.c b/client/mysqladmin.c index 87a5be2aad2..6cc55faf626 100644 --- a/client/mysqladmin.c +++ b/client/mysqladmin.c @@ -23,7 +23,7 @@ #include <my_pthread.h> /* because of signal() */ #endif -#define ADMIN_VERSION "8.21" +#define ADMIN_VERSION "8.22" #define MAX_MYSQL_VAR 64 #define SHUTDOWN_DEF_TIMEOUT 3600 /* Wait for shutdown */ #define MAX_TRUNC_LENGTH 3 @@ -51,7 +51,7 @@ static void print_version(void); static void usage(void); static my_bool sql_connect(MYSQL *mysql,const char *host, const char *user, const char *password,uint wait); -static my_bool execute_commands(MYSQL *mysql,int argc, char **argv); +static int execute_commands(MYSQL *mysql,int argc, char **argv); static int drop_db(MYSQL *mysql,const char *db); static sig_handler endprog(int signal_number); static void nice_time(ulong sec,char *buff); @@ -275,16 +275,24 @@ int main(int argc,char *argv[]) while (!interrupted) { new_line = 0; - if (execute_commands(&mysql,argc,commands) && !option_force) + if ((error=execute_commands(&mysql,argc,commands))) { - if (option_wait && !interrupted) + if (error > 0) + break; /* Wrong command error */ + if (!option_force) { - mysql_close(&mysql); - if (!sql_connect(&mysql,host,user,opt_password,option_wait)) - continue; /* Retry */ + if (option_wait && !interrupted) + { + mysql_close(&mysql); + if (!sql_connect(&mysql,host,user,opt_password,option_wait)) + { + sleep(1); /* Don't retry too rapidly */ + continue; /* Retry */ + } + } + error=1; + break; } - error=1; - break; } if (interval) { @@ -301,7 +309,7 @@ int main(int argc,char *argv[]) my_free(user,MYF(MY_ALLOW_ZERO_PTR)); free_defaults(argv); my_end(0); - exit(error); + exit(error ? 1 : 0); return 0; } @@ -383,8 +391,14 @@ static my_bool sql_connect(MYSQL *mysql,const char *host, const char *user, } } +/* + Execute a command. + Return 0 on ok + -1 on retryable error + 1 on fatal error +*/ -static my_bool execute_commands(MYSQL *mysql,int argc, char **argv) +static int execute_commands(MYSQL *mysql,int argc, char **argv) { char *status; @@ -404,7 +418,7 @@ static my_bool execute_commands(MYSQL *mysql,int argc, char **argv) { my_printf_error(0,"CREATE DATABASE failed; error: '%-.200s'", MYF(ME_BELL), mysql_error(mysql)); - return 1; + return -1; } argc--; argv++; break; @@ -417,7 +431,7 @@ static my_bool execute_commands(MYSQL *mysql,int argc, char **argv) return 1; } if (drop_db(mysql,argv[1])) - return 1; + return -1; argc--; argv++; break; } @@ -433,7 +447,7 @@ static my_bool execute_commands(MYSQL *mysql,int argc, char **argv) { my_printf_error(0,"shutdown failed; error: '%s'",MYF(ME_BELL), mysql_error(mysql)); - return 1; + return -1; } mysql_close(mysql); /* Close connection to avoid error messages */ if (got_pidfile) @@ -450,7 +464,7 @@ static my_bool execute_commands(MYSQL *mysql,int argc, char **argv) { my_printf_error(0,"reload failed; error: '%s'",MYF(ME_BELL), mysql_error(mysql)); - return 1; + return -1; } break; case ADMIN_REFRESH: @@ -461,7 +475,7 @@ static my_bool execute_commands(MYSQL *mysql,int argc, char **argv) { my_printf_error(0,"refresh failed; error: '%s'",MYF(ME_BELL), mysql_error(mysql)); - return 1; + return -1; } break; case ADMIN_FLUSH_THREADS: @@ -469,7 +483,7 @@ static my_bool execute_commands(MYSQL *mysql,int argc, char **argv) { my_printf_error(0,"refresh failed; error: '%s'",MYF(ME_BELL), mysql_error(mysql)); - return 1; + return -1; } break; case ADMIN_VER: @@ -513,7 +527,7 @@ static my_bool execute_commands(MYSQL *mysql,int argc, char **argv) { my_printf_error(0,"process list failed; error: '%s'",MYF(ME_BELL), mysql_error(mysql)); - return 1; + return -1; } print_header(result); while ((row=mysql_fetch_row(result))) @@ -552,7 +566,7 @@ static my_bool execute_commands(MYSQL *mysql,int argc, char **argv) } argc--; argv++; if (error) - return error; + return -1; break; } case ADMIN_DEBUG: @@ -560,7 +574,7 @@ static my_bool execute_commands(MYSQL *mysql,int argc, char **argv) { my_printf_error(0,"debug failed; error: '%s'",MYF(ME_BELL), mysql_error(mysql)); - return 1; + return -1; } break; case ADMIN_VARIABLES: @@ -574,7 +588,7 @@ static my_bool execute_commands(MYSQL *mysql,int argc, char **argv) { my_printf_error(0,"unable to show variables; error: '%s'",MYF(ME_BELL), mysql_error(mysql)); - return 1; + return -1; } print_header(res); while ((row=mysql_fetch_row(res))) @@ -596,7 +610,7 @@ static my_bool execute_commands(MYSQL *mysql,int argc, char **argv) { my_printf_error(0, "unable to show status; error: '%s'", MYF(ME_BELL), mysql_error(mysql)); - return 1; + return -1; } if (!opt_vertical) print_header(res); @@ -646,7 +660,7 @@ static my_bool execute_commands(MYSQL *mysql,int argc, char **argv) { my_printf_error(0,"refresh failed; error: '%s'",MYF(ME_BELL), mysql_error(mysql)); - return 1; + return -1; } break; } @@ -656,7 +670,7 @@ static my_bool execute_commands(MYSQL *mysql,int argc, char **argv) { my_printf_error(0,"refresh failed; error: '%s'",MYF(ME_BELL), mysql_error(mysql)); - return 1; + return -1; } break; } @@ -666,7 +680,7 @@ static my_bool execute_commands(MYSQL *mysql,int argc, char **argv) { my_printf_error(0,"refresh failed; error: '%s'",MYF(ME_BELL), mysql_error(mysql)); - return 1; + return -1; } break; } @@ -676,7 +690,7 @@ static my_bool execute_commands(MYSQL *mysql,int argc, char **argv) { my_printf_error(0,"refresh failed; error: '%s'",MYF(ME_BELL), mysql_error(mysql)); - return 1; + return -1; } break; } @@ -684,7 +698,7 @@ static my_bool execute_commands(MYSQL *mysql,int argc, char **argv) { char buff[128],crypted_pw[33]; - if(argc < 2) + if (argc < 2) { my_printf_error(0,"Too few arguments to change password",MYF(ME_BELL)); return 1; @@ -699,13 +713,13 @@ static my_bool execute_commands(MYSQL *mysql,int argc, char **argv) { my_printf_error(0, "Can't turn off logging; error: '%s'", MYF(ME_BELL),mysql_error(mysql)); - return 1; + return -1; } if (mysql_query(mysql,buff)) { my_printf_error(0,"unable to change password; error: '%s'", MYF(ME_BELL),mysql_error(mysql)); - return 1; + return -1; } argc--; argv++; break; @@ -716,7 +730,7 @@ static my_bool execute_commands(MYSQL *mysql,int argc, char **argv) { my_printf_error(0, "Error starting slave: %s", MYF(ME_BELL), mysql_error(mysql)); - return 1; + return -1; } else puts("Slave started"); @@ -726,7 +740,7 @@ static my_bool execute_commands(MYSQL *mysql,int argc, char **argv) { my_printf_error(0, "Error stopping slave: %s", MYF(ME_BELL), mysql_error(mysql)); - return 1; + return -1; } else puts("Slave stopped"); @@ -751,7 +765,7 @@ static my_bool execute_commands(MYSQL *mysql,int argc, char **argv) { my_printf_error(0,"mysqld doesn't answer to ping, error: '%s'", MYF(ME_BELL),mysql_error(mysql)); - return 1; + return -1; } } mysql->reconnect=1; /* Automatic reconnect is default */ diff --git a/client/mysqlmanager-pwgen.c b/client/mysqlmanager-pwgen.c new file mode 100644 index 00000000000..eca9bfa23ea --- /dev/null +++ b/client/mysqlmanager-pwgen.c @@ -0,0 +1,153 @@ +/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult 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 + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 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 */ + +#define MANAGER_PWGEN_VERSION "1.0" + +#include <my_global.h> +#include <m_ctype.h> +#include <my_sys.h> +#include <m_string.h> +#include <mysql_version.h> +#include <errno.h> +#include <getopt.h> +#include <md5.h> + +const char* outfile=0,*user="root"; + +struct option long_options[] = +{ + {"output-file",required_argument,0,'o'}, + {"user",required_argument,0,'u'}, + {"help",no_argument,0,'?'}, + {"version",no_argument,0,'V'}, + {0,0,0,0} +}; + +static void die(const char* fmt, ...) +{ + va_list args; + DBUG_ENTER("die"); + va_start(args, fmt); + if (fmt) + { + fprintf(stderr, "%s: ", my_progname); + vfprintf(stderr, fmt, args); + fprintf(stderr, "\n"); + fflush(stderr); + } + va_end(args); + exit(1); +} + +static void print_version(void) +{ + printf("%s Ver %s Distrib %s, for %s (%s)\n",my_progname, + MANAGER_PWGEN_VERSION, + MYSQL_SERVER_VERSION,SYSTEM_TYPE,MACHINE_TYPE); +} + +void usage() +{ + print_version(); + printf("MySQL AB, by Sasha\n"); + printf("This software comes with ABSOLUTELY NO WARRANTY\n\n"); + printf("Generates a password file to be used by mysqltest.\n\n"); + printf("Usage: %s [OPTIONS]\n", my_progname); + printf("-?,--help Display this message and exit\n\ +-V,--version Display version info\n\ +-u,--user= Put given user in the password file\n\ +-o,--output-file= Write the output to the file with the given name\n"); +} + +int parse_args(int argc, char** argv) +{ + int c,option_index=0; + while ((c=getopt_long(argc,argv,"?Vu:o:",long_options,&option_index)) + != EOF) + { + switch (c) + { + case 'o': + outfile=optarg; + break; + case 'u': + user=optarg; + break; + case '?': + usage(); + exit(0); + case 'V': + print_version(); + exit(0); + default: + usage(); + exit(1); + } + } + return 0; +} + +void get_pass(char* pw, int len) +{ + FILE* fp; + char* pw_end=pw+len; +/* /dev/random is more secure than rand() because the seed is easy to + predict, so we resort to rand() only if /dev/random is not available */ + if ((fp=fopen("/dev/random","r"))) + { + fread(pw,len,1,fp); + fclose(fp); + while (pw<pw_end) + { + *pw++='a'+((uint)*pw % 26); + } + } + else + { + srand(time(NULL)); + while (pw<pw_end) + *pw++='a'+((uint)rand() % 26); + } + *pw_end=0; +} + +int main(int argc, char** argv) +{ + FILE* fp; + my_MD5_CTX context; + uchar digest[16]; + char pw[17]; + uint i; + + MY_INIT(argv[0]); + parse_args(argc,argv); + if (!outfile) + die("Missing --output-file"); + + if (!(fp=fopen(outfile,"w"))) + die("Could not open '%s'(errno=%d)",outfile,errno); + get_pass(pw,sizeof(pw)-1); + my_MD5Init(&context); + my_MD5Update(&context,pw,sizeof(pw)-1); + my_MD5Final(digest,&context); + fprintf(fp,"%s:",user); + for (i=0;i<sizeof(digest);i++) + fprintf(fp,"%02x",digest[i]); + fprintf(fp,"\n"); + fclose(fp); + printf("%s\n",pw); + return 0; +} diff --git a/client/mysqlmanagerc.c b/client/mysqlmanagerc.c new file mode 100644 index 00000000000..66b77e237e2 --- /dev/null +++ b/client/mysqlmanagerc.c @@ -0,0 +1,192 @@ +/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult 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 + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 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 */ + +#define MANAGER_CLIENT_VERSION "1.0" + +#include <my_global.h> +#include <my_sys.h> +#include <m_string.h> +#include <mysql.h> +#include <mysql_version.h> +#include <m_ctype.h> +#ifdef OS2 +#include <config-os2.h> +#else +#include <my_config.h> +#endif +#include <my_dir.h> +#include <hash.h> +#include <mysqld_error.h> +#include <stdio.h> +#include <stdlib.h> +#include <getopt.h> +#include <stdarg.h> +#include <sys/stat.h> +#include <unistd.h> +#include <errno.h> +#include <violite.h> + +#ifndef MYSQL_MANAGER_PORT +#define MYSQL_MANAGER_PORT 23546 +#endif + +static void die(const char* fmt, ...); + +const char* user="root",*host="localhost"; +char* pass=0; +int quiet=0; +uint port=MYSQL_MANAGER_PORT; +static const char *load_default_groups[]= { "mysqlmanagerc",0 }; +char** default_argv; +MYSQL_MANAGER *manager; +FILE* fp, *fp_out; + +struct option long_options[] = +{ + {"host",required_argument,0,'h'}, + {"user",required_argument,0,'u'}, + {"password",optional_argument,0,'p',}, + {"port",required_argument,0,'P'}, + {"help",no_argument,0,'?'}, + {"version",no_argument,0,'V'}, + {"quiet",no_argument,0,'q'}, + {0,0,0,0} +}; + +static void die(const char* fmt, ...) +{ + va_list args; + DBUG_ENTER("die"); + va_start(args, fmt); + if (fmt) + { + fprintf(stderr, "%s: ", my_progname); + vfprintf(stderr, fmt, args); + fprintf(stderr, "\n"); + fflush(stderr); + } + va_end(args); + exit(1); +} + +static void print_version(void) +{ + printf("%s Ver %s Distrib %s, for %s (%s)\n",my_progname, + MANAGER_CLIENT_VERSION, + MYSQL_SERVER_VERSION,SYSTEM_TYPE,MACHINE_TYPE); +} + +void usage() +{ + print_version(); + printf("MySQL AB, by Sasha\n"); + printf("This software comes with ABSOLUTELY NO WARRANTY\n\n"); + printf("Command-line client for MySQL manager daemon.\n\n"); + printf("Usage: %s [OPTIONS] < command_file\n", my_progname); + printf("\n\ + -?, --help Display this help and exit.\n"); + printf("\ + -h, --host=... Connect to host.\n\ + -u, --user=... User for login.\n\ + -p[password], --password[=...]\n\ + Password to use when connecting to server.\n\ + -P, --port=... Port number to use for connection.\n\ + -q, --quiet, --silent Suppress all normal output.\n\ + -V, --version Output version information and exit.\n\ + --no-defaults Don't read default options from any options file.\n\n"); +} +int parse_args(int argc, char **argv) +{ + int c, option_index = 0; + my_bool tty_password=0; + + load_defaults("my",load_default_groups,&argc,&argv); + default_argv= argv; + + while ((c = getopt_long(argc, argv, "h:p::u:P:?Vq", + long_options, &option_index)) != EOF) + { + switch (c) + { + case 'h': + host=optarg; + break; + case 'u': + user=optarg; + break; + case 'p': + if (optarg) + { + my_free(pass,MYF(MY_ALLOW_ZERO_PTR)); + pass=my_strdup(optarg,MYF(MY_FAE)); + while (*optarg) *optarg++= 'x'; /* Destroy argument */ + } + else + tty_password=1; + break; + case 'P': + port=atoi(optarg); + break; + case 'q': + quiet=1; + break; + case 'V': + print_version(); + exit(0); + case '?': + usage(); + exit(0); + default: + usage(); + exit(1); + } + } + return 0; +} +int main(int argc, char** argv) +{ + MY_INIT(argv[0]); + fp=stdin; + fp_out=stdout; + parse_args(argc,argv); + if (!(manager=mysql_manager_init(0))) + die("Failed in mysql_manager_init()"); + if (!mysql_manager_connect(manager,host,user,pass,port)) + die("Could not connect to MySQL manager: %s(%d)",manager->last_error, + manager->last_errno); + for (;!feof(fp);) + { + char buf[4096]; + if (!fgets(buf,sizeof(buf),fp)) + break; + if (!quiet) + fprintf(fp_out,"<<%s",buf); + if (mysql_manager_command(manager,buf,strlen(buf))) + die("Error in command: %s(%d)",manager->last_error,manager->last_errno); + while (!manager->eof) + { + if (mysql_manager_fetch_line(manager,buf,sizeof(buf))) + die("Error fetching result line: %s(%d)", manager->last_error, + manager->last_errno); + if (!quiet) + fprintf(fp_out,">>%s\n",buf); + } + } + mysql_manager_close(manager); + return 0; +} + + diff --git a/client/mysqltest.c b/client/mysqltest.c index f7b655e7de7..2ed981d3133 100644 --- a/client/mysqltest.c +++ b/client/mysqltest.c @@ -80,12 +80,23 @@ * the server - to solve the problem, we try again * after some sleep if connection fails the first * time */ +#ifndef MYSQL_MANAGER_PORT +#define MYSQL_MANAGER_PORT 23546 +#endif + +enum {OPT_MANAGER_USER=256,OPT_MANAGER_HOST,OPT_MANAGER_PASSWD, + OPT_MANAGER_PORT,OPT_MANAGER_WAIT_TIMEOUT}; static int record = 0, verbose = 0, silent = 0, opt_sleep=0; static char *db = 0, *pass=0; const char* user = 0, *host = 0, *unix_sock = 0, *opt_basedir="./"; static int port = 0, opt_big_test=0, opt_compress=0; static uint start_lineno, *lineno; +const char* manager_user="root",*manager_host="localhost"; +char *manager_pass=0; +int manager_port=MYSQL_MANAGER_PORT; +int manager_wait_timeout=3; +MYSQL_MANAGER* manager=0; static char **default_argv; static const char *load_default_groups[]= { "mysqltest","client",0 }; @@ -141,11 +152,13 @@ typedef struct int int_val; int alloced_len; int int_dirty; /* do not update string if int is updated until first read */ + int alloced; } VAR; VAR var_reg[10]; /*Perl/shell-like variable registers */ HASH var_hash; +int disable_query_log=0; struct connection cons[MAX_CONS]; struct connection* cur_con, *next_con, *cons_end; @@ -167,6 +180,8 @@ Q_DIRTY_CLOSE, Q_REPLACE, Q_PING, Q_EVAL, Q_RPL_PROBE, Q_ENABLE_RPL_PARSE, Q_DISABLE_RPL_PARSE, Q_EVAL_RESULT, +Q_ENABLE_QUERY_LOG, Q_DISABLE_QUERY_LOG, +Q_SERVER_START, Q_SERVER_STOP, Q_UNKNOWN, /* Unknown command. */ Q_COMMENT, /* Comments, ignored. */ Q_COMMENT_WITH_COMMAND @@ -198,6 +213,8 @@ const char *command_names[] = { "ping", "eval", "rpl_probe", "enable_rpl_parse", "disable_rpl_parse", "eval_result", + "enable_query_log", "disable_query_log", + "server_start", "server_stop", 0 }; @@ -242,6 +259,7 @@ static int initialize_replace_buffer(void); static void free_replace_buffer(void); static void do_eval(DYNAMIC_STRING* query_eval, const char* query); void str_to_file(const char* fname, char* str, int size); +int do_server_op(struct st_query* q,const char* op); struct st_replace *glob_replace; static char *out_buff; @@ -336,6 +354,11 @@ static void close_files() static void free_used_memory() { uint i; + DBUG_ENTER("free_used_memory"); +#ifndef EMBEDDED_LIBRARY + if (manager) + mysql_manager_close(manager); +#endif close_cons(); close_files(); hash_free(&var_hash); @@ -359,6 +382,7 @@ static void free_used_memory() free_defaults(default_argv); mysql_server_end(); my_end(MY_CHECK_ERROR); + DBUG_VOID_RETURN; } static void die(const char* fmt, ...) @@ -616,6 +640,46 @@ int open_file(const char* name) return 0; } +#ifndef EMBEDDED_LIBRARY +int do_server_start(struct st_query* q) +{ + return do_server_op(q,"start"); +} + +int do_server_stop(struct st_query* q) +{ + return do_server_op(q,"stop"); +} + +int do_server_op(struct st_query* q,const char* op) +{ + char* p=q->first_argument; + char com_buf[256],*com_p; + com_p=strmov(com_buf,op); + com_p=strmov(com_p,"_exec "); + if (!*p) + die("Missing server name in server_%s\n",op); + while (*p && !isspace(*p)) + { + *com_p++=*p++; + } + *com_p++=' '; + com_p=int10_to_str(manager_wait_timeout,com_p,10); + *com_p++ = '\n'; + *com_p=0; + if (mysql_manager_command(manager,com_buf,(int)(com_p-com_buf))) + die("Error in command: %s(%d)",manager->last_error,manager->last_errno); + while (!manager->eof) + { + if (mysql_manager_fetch_line(manager,com_buf,sizeof(com_buf))) + die("Error fetching result line: %s(%d)", manager->last_error, + manager->last_errno); + } + + return 0; +} +#endif + int do_source(struct st_query* q) { char* p=q->first_argument, *name; @@ -659,6 +723,18 @@ int var_query_set(VAR* v, const char* p, const char** p_end) return 0; } +void var_copy(VAR* dest, VAR* src) +{ + dest->int_val=src->int_val; + dest->int_dirty=src->int_dirty; + if (dest->alloced_len < src->alloced_len && + !(dest->str_val=my_realloc(dest->str_val,src->alloced_len, + MYF(MY_WME)))) + die("Out of memory"); + dest->str_val_len=src->str_val_len; + memcpy(dest->str_val,src->str_val,src->str_val_len); +} + int eval_expr(VAR* v, const char* p, const char** p_end) { VAR* vp; @@ -666,7 +742,7 @@ int eval_expr(VAR* v, const char* p, const char** p_end) { if ((vp = var_get(p,p_end,0))) { - memcpy(v, vp, sizeof(*v)); + var_copy(v, vp); return 0; } } @@ -739,6 +815,7 @@ int do_system(struct st_query* q) if (system(expr_buf) && q->abort_on_error) die("system command '%s' failed", expr_buf); } + var_free(&v); return 0; } @@ -754,6 +831,7 @@ int do_echo(struct st_query* q) write(1, v.str_val, v.str_val_len); } write(1, "\n", 1); + var_free(&v); return 0; } @@ -1146,6 +1224,19 @@ char* safe_get_param(char* str, char** arg, const char* msg) DBUG_RETURN(str); } +#ifndef EMBEDDED_LIBRARY +void init_manager() +{ + if (!(manager=mysql_manager_init(0))) + die("Failed in mysql_manager_init()"); + if (!mysql_manager_connect(manager,manager_host,manager_user, + manager_pass,manager_port)) + die("Could not connect to MySQL manager: %s(%d)",manager->last_error, + manager->last_errno); + +} +#endif + int safe_connect(MYSQL* con, const char* host, const char* user, const char* pass, const char* db, int port, const char* sock) @@ -1231,10 +1322,10 @@ int do_done(struct st_query* q) parser.current_line = *--cur_block; } else - { - ++parser.current_line; - --cur_block; - } + { + ++parser.current_line; + --cur_block; + } return 0; } @@ -1243,7 +1334,6 @@ int do_while(struct st_query* q) char* p=q->first_argument; const char* expr_start, *expr_end; VAR v; - var_init(&v,0,0,0,0); if (cur_block == block_stack_end) die("Nesting too deeply"); if (!*block_ok) @@ -1260,6 +1350,7 @@ int do_while(struct st_query* q) expr_end = strrchr(expr_start, ')'); if (!expr_end) die("missing ')' in while"); + var_init(&v,0,0,0,0); eval_expr(&v, ++expr_start, &expr_end); *cur_block++ = parser.current_line++; if (!v.int_val) @@ -1269,6 +1360,7 @@ int do_while(struct st_query* q) } else *++block_ok = 1; + var_free(&v); return 0; } @@ -1537,7 +1629,6 @@ int read_query(struct st_query** q_ptr) return 0; } - struct option long_options[] = { {"debug", optional_argument, 0, '#'}, @@ -1547,6 +1638,11 @@ struct option long_options[] = {"compress", no_argument, 0, 'C'}, {"help", no_argument, 0, '?'}, {"host", required_argument, 0, 'h'}, + {"manager-user",required_argument, 0, OPT_MANAGER_USER}, + {"manager-host",required_argument, 0, OPT_MANAGER_HOST}, + {"manager-password",required_argument,0,OPT_MANAGER_PASSWD}, + {"manager-port",required_argument,0,OPT_MANAGER_PORT}, + {"manager-wait-timeout",required_argument,0,OPT_MANAGER_WAIT_TIMEOUT}, {"password", optional_argument, 0, 'p'}, {"port", required_argument, 0, 'P'}, {"quiet", no_argument, 0, 's'}, @@ -1630,6 +1726,23 @@ int parse_args(int argc, char **argv) case 'r': record = 1; break; + case (int)OPT_MANAGER_WAIT_TIMEOUT: + manager_wait_timeout=atoi(optarg); + break; + case (int)OPT_MANAGER_PORT: + manager_port=atoi(optarg); + break; + case (int)OPT_MANAGER_HOST: + manager_host=optarg; + break; + case (int)OPT_MANAGER_USER: + manager_user=optarg; + break; + case (int)OPT_MANAGER_PASSWD: + my_free(manager_pass,MYF(MY_ALLOW_ZERO_PTR)); + manager_pass=my_strdup(optarg,MYF(MY_FAE)); + while (*optarg) *optarg++= 'x'; /* Destroy argument */ + break; case 'u': user = optarg; break; @@ -1814,9 +1927,13 @@ int run_query(MYSQL* mysql, struct st_query* q, int flags) if ((flags & QUERY_SEND) && mysql_send_query(mysql, query, query_len)) die("At line %u: unable to send query '%s'", start_lineno, query); + if ((flags & QUERY_SEND) && !disable_query_log) + { + dynstr_append_mem(ds,query,query_len); + dynstr_append_mem(ds,";\n",2); + } if (!(flags & QUERY_REAP)) DBUG_RETURN(0); - if (mysql_read_query_result(mysql) || (!(last_result = res = mysql_store_result(mysql)) && @@ -1834,7 +1951,11 @@ int run_query(MYSQL* mysql, struct st_query* q, int flags) for (i=0 ; q->expected_errno[i] ; i++) { if ((q->expected_errno[i] == mysql_errno(mysql))) + { + dynstr_append(ds,mysql_error(mysql)); + dynstr_append_mem(ds,"\n",1); goto end; /* Ok */ + } } if (i) { @@ -1979,6 +2100,7 @@ static VAR* var_init(VAR* v, const char* name, int name_len, const char* val, die("Out of memory"); tmp_var->name = (name) ? (char*)tmp_var + sizeof(*tmp_var) : 0; + tmp_var->alloced = (v == 0); if(!(tmp_var->str_val = my_malloc(val_alloc_len, MYF(MY_WME)))) die("Out of memory"); @@ -1997,7 +2119,8 @@ static VAR* var_init(VAR* v, const char* name, int name_len, const char* val, static void var_free(void* v) { my_free(((VAR*) v)->str_val, MYF(MY_WME)); - my_free((char*) v, MYF(MY_WME)); + if (((VAR*)v)->alloced) + my_free((char*) v, MYF(MY_WME)); } @@ -2030,6 +2153,8 @@ int main(int argc, char** argv) struct st_query* q; my_bool require_file=0, q_send_flag=0; char save_file[FN_REFLEN]; + mysql_server_init(sizeof(embedded_server_args) / sizeof(char *) - 1, + embedded_server_args, embedded_server_groups); MY_INIT(argv[0]); { DBUG_ENTER("main"); @@ -2066,7 +2191,9 @@ int main(int argc, char** argv) if (cur_file == file_stack) *++cur_file = stdin; *lineno=1; - +#ifndef EMBEDDED_LIBRARY + init_manager(); +#endif if (!( mysql_init(&cur_con->mysql))) die("Failed in mysql_init()"); if (opt_compress) @@ -2074,7 +2201,7 @@ int main(int argc, char** argv) cur_con->name = my_strdup("default", MYF(MY_WME)); if (!cur_con->name) die("Out of memory"); - + 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)); @@ -2096,8 +2223,14 @@ int main(int argc, char** argv) case Q_RPL_PROBE: do_rpl_probe(q); break; case Q_ENABLE_RPL_PARSE: do_enable_rpl_parse(q); break; case Q_DISABLE_RPL_PARSE: do_disable_rpl_parse(q); break; + case Q_ENABLE_QUERY_LOG: disable_query_log=0; break; + case Q_DISABLE_QUERY_LOG: disable_query_log=1; break; case Q_SOURCE: do_source(q); break; case Q_SLEEP: do_sleep(q); break; +#ifndef EMBEDDED_LIBRARY + case Q_SERVER_START: do_server_start(q); break; + case Q_SERVER_STOP: do_server_stop(q); break; +#endif case Q_INC: do_inc(q); break; case Q_DEC: do_dec(q); break; case Q_ECHO: do_echo(q); break; @@ -2202,9 +2335,10 @@ int main(int argc, char** argv) printf("ok\n"); } + mysql_server_end(); free_used_memory(); exit(error ? 1 : 0); - DBUG_RETURN(error ? 1 : 0); /* Keep compiler happy */ + return error ? 1 : 0; /* Keep compiler happy */ } } |