forked from MariaDB/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysqltest.cc
11312 lines (9600 loc) · 295 KB
/
mysqltest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2000, 2013, Oracle and/or its affiliates.
Copyright (c) 2009, 2017, MariaDB
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; version 2 of the License.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/*
mysqltest
Tool used for executing a .test file
See the "MySQL Test framework manual" for more information
https://mariadb.com/kb/en/library/mysqltest/
Please keep the test framework tools identical in all versions!
Written by:
Sasha Pachev <[email protected]>
Matt Wagner <[email protected]>
Monty
Jani
Holyfoot
And many others
*/
#define MTEST_VERSION "3.5"
#include "client_priv.h"
#include <mysql_version.h>
#include <mysqld_error.h>
#include <sql_common.h>
#include <m_ctype.h>
#include <my_dir.h>
#include <hash.h>
#include <stdarg.h>
#include <violite.h>
#define PCRE_STATIC 1 /* Important on Windows */
#include "pcreposix.h" /* pcreposix regex library */
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#ifdef _WIN32
#include <direct.h>
#endif
#include <signal.h>
#include <my_stacktrace.h>
#include <welcome_copyright_notice.h> // ORACLE_WELCOME_COPYRIGHT_NOTICE
#ifdef _WIN32
#include <crtdbg.h>
#define SIGNAL_FMT "exception 0x%x"
#else
#define SIGNAL_FMT "signal %d"
#endif
#include <my_context.h>
static my_bool non_blocking_api_enabled= 0;
#if !defined(EMBEDDED_LIBRARY) && !defined(MY_CONTEXT_DISABLE)
#define WRAP_NONBLOCK_ENABLED non_blocking_api_enabled
#include "../tests/nonblock-wrappers.h"
#endif
/* Use cygwin for --exec and --system before 5.0 */
#if MYSQL_VERSION_ID < 50000
#define USE_CYGWIN
#endif
#define MAX_VAR_NAME_LENGTH 256
#define MAX_COLUMNS 256
#define MAX_EMBEDDED_SERVER_ARGS 64
#define MAX_DELIMITER_LENGTH 16
#define DEFAULT_MAX_CONN 64
#define DIE_BUFF_SIZE 256*1024
/* Flags controlling send and reap */
#define QUERY_SEND_FLAG 1
#define QUERY_REAP_FLAG 2
#define QUERY_PRINT_ORIGINAL_FLAG 4
#ifndef HAVE_SETENV
static int setenv(const char *name, const char *value, int overwrite);
#endif
C_MODE_START
static sig_handler signal_handler(int sig);
static my_bool get_one_option(int optid, const struct my_option *,
char *argument);
C_MODE_END
enum {
OPT_LOG_DIR=OPT_MAX_CLIENT_OPTION, OPT_RESULT_FORMAT_VERSION
};
static int record= 0, opt_sleep= -1;
static char *opt_db= 0, *opt_pass= 0;
const char *opt_user= 0, *opt_host= 0, *unix_sock= 0, *opt_basedir= "./";
const char *opt_logdir= "";
const char *opt_prologue= 0, *opt_charsets_dir;
static int opt_port= 0;
static int opt_max_connect_retries;
static int opt_result_format_version;
static int opt_max_connections= DEFAULT_MAX_CONN;
static int error_count= 0;
static my_bool opt_compress= 0, silent= 0, verbose= 0;
static my_bool debug_info_flag= 0, debug_check_flag= 0;
static my_bool tty_password= 0;
static my_bool opt_mark_progress= 0;
static my_bool ps_protocol= 0, ps_protocol_enabled= 0;
static my_bool sp_protocol= 0, sp_protocol_enabled= 0;
static my_bool view_protocol= 0, view_protocol_enabled= 0;
static my_bool cursor_protocol= 0, cursor_protocol_enabled= 0;
static my_bool parsing_disabled= 0;
static my_bool display_result_vertically= FALSE, display_result_lower= FALSE,
display_metadata= FALSE, display_result_sorted= FALSE,
display_session_track_info= FALSE;
static my_bool disable_query_log= 0, disable_result_log= 0;
static my_bool disable_connect_log= 0;
static my_bool disable_warnings= 0, disable_column_names= 0;
static my_bool prepare_warnings_enabled= 0;
static my_bool disable_info= 1;
static my_bool abort_on_error= 1, opt_continue_on_error= 0;
static my_bool server_initialized= 0;
static my_bool is_windows= 0;
static char **default_argv;
static const char *load_default_groups[]=
{ "mysqltest", "client", "client-server", "client-mariadb", 0 };
static char line_buffer[MAX_DELIMITER_LENGTH], *line_buffer_pos= line_buffer;
/* Info on properties that can be set with --enable_X and --disable_X */
struct property {
my_bool *var; /* Actual variable */
my_bool set; /* Has been set for ONE command */
my_bool old; /* If set, thus is the old value */
my_bool reverse; /* Varible is true if disabled */
const char *env_name; /* Env. variable name */
};
static struct property prop_list[] = {
{ &abort_on_error, 0, 1, 0, "$ENABLED_ABORT_ON_ERROR" },
{ &disable_connect_log, 0, 1, 1, "$ENABLED_CONNECT_LOG" },
{ &disable_info, 0, 1, 1, "$ENABLED_INFO" },
{ &display_session_track_info, 0, 1, 1, "$ENABLED_STATE_CHANGE_INFO" },
{ &display_metadata, 0, 0, 0, "$ENABLED_METADATA" },
{ &ps_protocol_enabled, 0, 0, 0, "$ENABLED_PS_PROTOCOL" },
{ &disable_query_log, 0, 0, 1, "$ENABLED_QUERY_LOG" },
{ &disable_result_log, 0, 0, 1, "$ENABLED_RESULT_LOG" },
{ &disable_warnings, 0, 0, 1, "$ENABLED_WARNINGS" }
};
static my_bool once_property= FALSE;
enum enum_prop {
P_ABORT= 0,
P_CONNECT,
P_INFO,
P_SESSION_TRACK,
P_META,
P_PS,
P_QUERY,
P_RESULT,
P_WARN,
P_MAX
};
static uint start_lineno= 0; /* Start line of current command */
static uint my_end_arg= 0;
/* Number of lines of the result to include in failure report */
static uint opt_tail_lines= 0;
static uint opt_connect_timeout= 0;
static uint opt_wait_for_pos_timeout= 0;
static char delimiter[MAX_DELIMITER_LENGTH]= ";";
static size_t delimiter_length= 1;
static char TMPDIR[FN_REFLEN];
static char global_subst_from[200];
static char global_subst_to[200];
static char *global_subst= NULL;
static char *read_command_buf= NULL;
static MEM_ROOT require_file_root;
static const my_bool my_true= 1;
static const my_bool my_false= 0;
/* Block stack */
enum block_cmd {
cmd_none,
cmd_if,
cmd_while
};
struct st_block
{
int line; /* Start line of block */
my_bool ok; /* Should block be executed */
enum block_cmd cmd; /* Command owning the block */
char delim[MAX_DELIMITER_LENGTH]; /* Delimiter before block */
};
static struct st_block block_stack[32];
static struct st_block *cur_block, *block_stack_end;
/* Open file stack */
struct st_test_file
{
FILE* file;
char *file_name;
uint lineno; /* Current line in file */
};
static struct st_test_file file_stack[16];
static struct st_test_file* cur_file;
static struct st_test_file* file_stack_end;
static CHARSET_INFO *charset_info= &my_charset_latin1; /* Default charset */
static const char *embedded_server_groups[]=
{
"server",
"embedded",
"mysqltest_SERVER",
NullS
};
static int embedded_server_arg_count=0;
static char *embedded_server_args[MAX_EMBEDDED_SERVER_ARGS];
/*
Timer related variables
See the timer_output() definition for details
*/
static char *timer_file = NULL;
static ulonglong timer_start;
static void timer_output(void);
static ulonglong timer_now(void);
static ulong connection_retry_sleep= 100000; /* Microseconds */
static const char *opt_plugin_dir;
static const char *opt_suite_dir, *opt_overlay_dir;
static size_t suite_dir_len, overlay_dir_len;
/* Precompiled re's */
static regex_t ps_re; /* the query can be run using PS protocol */
static regex_t sp_re; /* the query can be run as a SP */
static regex_t view_re; /* the query can be run as a view*/
static void init_re(void);
static int match_re(regex_t *, char *);
static void free_re(void);
static char *get_string(char **to_ptr, char **from_ptr,
struct st_command *command);
static int replace(DYNAMIC_STRING *ds_str,
const char *search_str, size_t search_len,
const char *replace_str, size_t replace_len);
static uint opt_protocol=0;
DYNAMIC_ARRAY q_lines;
#include "sslopt-vars.h"
struct Parser
{
int read_lines,current_line;
} parser;
struct MasterPos
{
char file[FN_REFLEN];
ulong pos;
} master_pos;
/* if set, all results are concated and compared against this file */
const char *result_file_name= 0;
typedef struct
{
char *name;
size_t name_len;
char *str_val;
size_t str_val_len;
int int_val;
size_t alloced_len;
bool int_dirty; /* do not update string if int is updated until first read */
bool is_int;
bool alloced;
} VAR;
/*Perl/shell-like variable registers */
VAR var_reg[10];
HASH var_hash;
struct st_connection
{
MYSQL *mysql;
/* Used when creating views and sp, to avoid implicit commit */
MYSQL* util_mysql;
char *name;
size_t name_len;
MYSQL_STMT* stmt;
/* Set after send to disallow other queries before reap */
my_bool pending;
#ifdef EMBEDDED_LIBRARY
pthread_t tid;
const char *cur_query;
int cur_query_len;
int command, result;
pthread_mutex_t query_mutex;
pthread_cond_t query_cond;
pthread_mutex_t result_mutex;
pthread_cond_t result_cond;
int query_done;
my_bool has_thread;
#endif /*EMBEDDED_LIBRARY*/
};
struct st_connection *connections= NULL;
struct st_connection* cur_con= NULL, *next_con, *connections_end;
/*
List of commands in mysqltest
Must match the "command_names" array
Add new commands before Q_UNKNOWN!
*/
enum enum_commands {
Q_CONNECTION=1, Q_QUERY,
Q_CONNECT, Q_SLEEP, Q_REAL_SLEEP,
Q_INC, Q_DEC,
Q_SOURCE, Q_DISCONNECT,
Q_LET, Q_ECHO,
Q_WHILE, Q_END_BLOCK,
Q_SYSTEM, Q_RESULT,
Q_REQUIRE, Q_SAVE_MASTER_POS,
Q_SYNC_WITH_MASTER,
Q_SYNC_SLAVE_WITH_MASTER,
Q_ERROR,
Q_SEND, Q_REAP,
Q_DIRTY_CLOSE, Q_REPLACE, Q_REPLACE_COLUMN,
Q_PING, Q_EVAL,
Q_EVALP,
Q_EVAL_RESULT,
Q_ENABLE_QUERY_LOG, Q_DISABLE_QUERY_LOG,
Q_ENABLE_RESULT_LOG, Q_DISABLE_RESULT_LOG,
Q_ENABLE_CONNECT_LOG, Q_DISABLE_CONNECT_LOG,
Q_WAIT_FOR_SLAVE_TO_STOP,
Q_ENABLE_WARNINGS, Q_DISABLE_WARNINGS,
Q_ENABLE_INFO, Q_DISABLE_INFO,
Q_ENABLE_SESSION_TRACK_INFO, Q_DISABLE_SESSION_TRACK_INFO,
Q_ENABLE_METADATA, Q_DISABLE_METADATA,
Q_ENABLE_COLUMN_NAMES, Q_DISABLE_COLUMN_NAMES,
Q_EXEC, Q_DELIMITER,
Q_DISABLE_ABORT_ON_ERROR, Q_ENABLE_ABORT_ON_ERROR,
Q_DISPLAY_VERTICAL_RESULTS, Q_DISPLAY_HORIZONTAL_RESULTS,
Q_QUERY_VERTICAL, Q_QUERY_HORIZONTAL, Q_SORTED_RESULT,
Q_LOWERCASE,
Q_START_TIMER, Q_END_TIMER,
Q_CHARACTER_SET, Q_DISABLE_PS_PROTOCOL, Q_ENABLE_PS_PROTOCOL,
Q_ENABLE_NON_BLOCKING_API, Q_DISABLE_NON_BLOCKING_API,
Q_DISABLE_RECONNECT, Q_ENABLE_RECONNECT,
Q_IF,
Q_DISABLE_PARSING, Q_ENABLE_PARSING,
Q_REPLACE_REGEX, Q_REMOVE_FILE, Q_FILE_EXIST,
Q_WRITE_FILE, Q_COPY_FILE, Q_PERL, Q_DIE, Q_EXIT, Q_SKIP,
Q_CHMOD_FILE, Q_APPEND_FILE, Q_CAT_FILE, Q_DIFF_FILES,
Q_SEND_QUIT, Q_CHANGE_USER, Q_MKDIR, Q_RMDIR,
Q_LIST_FILES, Q_LIST_FILES_WRITE_FILE, Q_LIST_FILES_APPEND_FILE,
Q_SEND_SHUTDOWN, Q_SHUTDOWN_SERVER,
Q_RESULT_FORMAT_VERSION,
Q_MOVE_FILE, Q_REMOVE_FILES_WILDCARD, Q_SEND_EVAL,
Q_ENABLE_PREPARE_WARNINGS, Q_DISABLE_PREPARE_WARNINGS,
Q_RESET_CONNECTION,
Q_UNKNOWN, /* Unknown command. */
Q_COMMENT, /* Comments, ignored. */
Q_COMMENT_WITH_COMMAND,
Q_EMPTY_LINE
};
const char *command_names[]=
{
"connection",
"query",
"connect",
"sleep",
"real_sleep",
"inc",
"dec",
"source",
"disconnect",
"let",
"echo",
"while",
"end",
"system",
"result",
"require",
"save_master_pos",
"sync_with_master",
"sync_slave_with_master",
"error",
"send",
"reap",
"dirty_close",
"replace_result",
"replace_column",
"ping",
"eval",
"evalp",
"eval_result",
/* Enable/disable that the _query_ is logged to result file */
"enable_query_log",
"disable_query_log",
/* Enable/disable that the _result_ from a query is logged to result file */
"enable_result_log",
"disable_result_log",
"enable_connect_log",
"disable_connect_log",
"wait_for_slave_to_stop",
"enable_warnings",
"disable_warnings",
"enable_info",
"disable_info",
"enable_session_track_info",
"disable_session_track_info",
"enable_metadata",
"disable_metadata",
"enable_column_names",
"disable_column_names",
"exec",
"delimiter",
"disable_abort_on_error",
"enable_abort_on_error",
"vertical_results",
"horizontal_results",
"query_vertical",
"query_horizontal",
"sorted_result",
"lowercase_result",
"start_timer",
"end_timer",
"character_set",
"disable_ps_protocol",
"enable_ps_protocol",
"enable_non_blocking_api",
"disable_non_blocking_api",
"disable_reconnect",
"enable_reconnect",
"if",
"disable_parsing",
"enable_parsing",
"replace_regex",
"remove_file",
"file_exists",
"write_file",
"copy_file",
"perl",
"die",
/* Don't execute any more commands, compare result */
"exit",
"skip",
"chmod",
"append_file",
"cat_file",
"diff_files",
"send_quit",
"change_user",
"mkdir",
"rmdir",
"list_files",
"list_files_write_file",
"list_files_append_file",
"send_shutdown",
"shutdown_server",
"result_format",
"move_file",
"remove_files_wildcard",
"send_eval",
"enable_prepare_warnings",
"disable_prepare_warnings",
"reset_connection",
0
};
/*
The list of error codes to --error are stored in an internal array of
structs. This struct can hold numeric SQL error codes, error names or
SQLSTATE codes as strings. The element next to the last active element
in the list is set to type ERR_EMPTY. When an SQL statement returns an
error, we use this list to check if this is an expected error.
*/
enum match_err_type
{
ERR_EMPTY= 0,
ERR_ERRNO,
ERR_SQLSTATE
};
struct st_match_err
{
enum match_err_type type;
union
{
uint errnum;
char sqlstate[SQLSTATE_LENGTH+1]; /* \0 terminated string */
} code;
};
struct st_expected_errors
{
struct st_match_err err[12];
uint count;
};
static struct st_expected_errors saved_expected_errors;
struct st_command
{
char *query, *query_buf,*first_argument,*last_argument,*end;
DYNAMIC_STRING content;
DYNAMIC_STRING eval_query;
int first_word_len, query_len;
my_bool abort_on_error, used_replace;
struct st_expected_errors expected_errors;
char *require_file;
enum enum_commands type;
};
TYPELIB command_typelib= {array_elements(command_names),"",
command_names, 0};
DYNAMIC_STRING ds_res;
/* Points to ds_warning in run_query, so it can be freed */
DYNAMIC_STRING *ds_warn= 0;
struct st_command *curr_command= 0;
char builtin_echo[FN_REFLEN];
struct st_replace_regex
{
DYNAMIC_ARRAY regex_arr; /* stores a list of st_regex subsitutions */
/*
Temporary storage areas for substitutions. To reduce unnessary copying
and memory freeing/allocation, we pre-allocate two buffers, and alternate
their use, one for input/one for output, the roles changing on the next
st_regex substition. At the end of substitutions buf points to the
one containing the final result.
*/
char* buf;
char* even_buf;
char* odd_buf;
int even_buf_len;
int odd_buf_len;
};
struct st_replace_regex *glob_replace_regex= 0;
struct st_replace;
struct st_replace *glob_replace= 0;
void replace_strings_append(struct st_replace *rep, DYNAMIC_STRING* ds,
const char *from);
ATTRIBUTE_NORETURN
static void cleanup_and_exit(int exit_code);
ATTRIBUTE_NORETURN
void really_die(const char *msg);
void report_or_die(const char *fmt, ...) ATTRIBUTE_FORMAT(printf, 1, 2);
ATTRIBUTE_NORETURN ATTRIBUTE_FORMAT(printf, 1, 2)
void die(const char *fmt, ...);
static void make_error_message(char *buf, size_t len, const char *fmt, va_list args);
ATTRIBUTE_NORETURN ATTRIBUTE_FORMAT(printf, 1, 2)
void abort_not_supported_test(const char *fmt, ...);
void verbose_msg(const char *fmt, ...) ATTRIBUTE_FORMAT(printf, 1, 2);
void log_msg(const char *fmt, ...) ATTRIBUTE_FORMAT(printf, 1, 2);
VAR* var_from_env(const char *, const char *);
VAR* var_init(VAR* v, const char *name, size_t name_len, const char *val, size_t val_len);
VAR* var_get(const char *var_name, const char** var_name_end,
my_bool raw, my_bool ignore_not_existing);
void eval_expr(VAR* v, const char *p, const char** p_end,
bool open_end=false, bool do_eval=true);
my_bool match_delimiter(int c, const char *delim, size_t length);
void dump_result_to_reject_file(char *buf, int size);
void dump_warning_messages();
void do_eval(DYNAMIC_STRING *query_eval, const char *query,
const char *query_end, my_bool pass_through_escape_chars);
void str_to_file(const char *fname, char *str, size_t size);
void str_to_file2(const char *fname, char *str, size_t size, my_bool append);
void fix_win_paths(char *val, size_t len);
const char *get_errname_from_code (uint error_code);
int multi_reg_replace(struct st_replace_regex* r,char* val);
#ifdef _WIN32
void free_tmp_sh_file();
void free_win_path_patterns();
#endif
/* For replace_column */
static char *replace_column[MAX_COLUMNS];
static uint max_replace_column= 0;
void do_get_replace_column(struct st_command*);
void free_replace_column();
/* For replace */
void do_get_replace(struct st_command *command);
void free_replace();
/* For replace_regex */
void do_get_replace_regex(struct st_command *command);
void free_replace_regex();
/* Used by sleep */
void check_eol_junk_line(const char *eol);
void free_all_replace(){
free_replace();
free_replace_regex();
free_replace_column();
}
void var_set_int(const char* name, int value);
class LogFile {
FILE* m_file;
char m_file_name[FN_REFLEN];
size_t m_bytes_written;
public:
LogFile() : m_file(NULL), m_bytes_written(0) {
bzero(m_file_name, sizeof(m_file_name));
}
~LogFile() {
close();
}
const char* file_name() const { return m_file_name; }
size_t bytes_written() const { return m_bytes_written; }
void open(const char* dir, const char* name, const char* ext)
{
DBUG_ENTER("LogFile::open");
DBUG_PRINT("enter", ("dir: '%s', name: '%s'", dir, name));
if (!name)
{
m_file= stdout;
DBUG_VOID_RETURN;
}
fn_format(m_file_name, name, dir, ext,
*dir ? MY_REPLACE_DIR | MY_REPLACE_EXT :
MY_REPLACE_EXT);
DBUG_PRINT("info", ("file_name: %s", m_file_name));
if ((m_file= fopen(m_file_name, "wb+")) == NULL)
die("Failed to open log file %s, errno: %d", m_file_name, errno);
DBUG_VOID_RETURN;
}
void close()
{
if (m_file) {
if (m_file != stdout)
fclose(m_file);
else
fflush(m_file);
}
m_file= NULL;
}
void flush()
{
if (m_file && m_file != stdout)
{
if (fflush(m_file))
die("Failed to flush '%s', errno: %d", m_file_name, errno);
}
}
void write(DYNAMIC_STRING* ds)
{
DBUG_ENTER("LogFile::write");
DBUG_PRINT("enter", ("length: %u", (uint) ds->length));
DBUG_ASSERT(m_file);
if (ds->length == 0)
DBUG_VOID_RETURN;
DBUG_ASSERT(ds->str);
#ifdef EXTRA_DEBUG
DBUG_PRINT("extra", ("str: %*s", (int) ds->length, ds->str));
#endif
if (fwrite(ds->str, 1, ds->length, m_file) != ds->length)
die("Failed to write %lu bytes to '%s', errno: %d",
(unsigned long)ds->length, m_file_name, errno);
m_bytes_written+= ds->length;
DBUG_VOID_RETURN;
}
void show_tail(uint lines) {
DBUG_ENTER("LogFile::show_tail");
if (!m_file || m_file == stdout)
DBUG_VOID_RETURN;
if (lines == 0)
DBUG_VOID_RETURN;
lines++;
int show_offset= 0;
char buf[256+1]; /* + zero termination for DBUG_PRINT */
size_t bytes;
bool found_bof= false;
/* Search backward in file until "lines" newline has been found */
while (lines && !found_bof)
{
show_offset-= sizeof(buf)-1;
while(fseek(m_file, show_offset, SEEK_END) != 0 && show_offset < 0)
{
found_bof= true;
// Seeking before start of file
show_offset++;
}
if ((bytes= fread(buf, 1, sizeof(buf)-1, m_file)) <= 0)
{
// ferror=0 will happen here if no queries executed yet
if (ferror(m_file))
fprintf(stderr,
"Failed to read from '%s', errno: %d, feof:%d, ferror:%d\n",
m_file_name, errno, feof(m_file), ferror(m_file));
DBUG_VOID_RETURN;
}
DBUG_PRINT("info", ("Read %zu bytes from file, buf: %.*s",
bytes, (int)bytes, buf));
char* show_from= buf + bytes;
while(show_from > buf && lines > 0 )
{
show_from--;
if (*show_from == '\n')
lines--;
}
if (show_from != buf)
{
// The last new line was found in this buf, adjust offset
show_offset+= (int)(show_from - buf) + 1;
DBUG_PRINT("info", ("adjusted offset to %d", show_offset));
}
DBUG_PRINT("info", ("show_offset: %d", show_offset));
}
fprintf(stderr, "\nThe result from queries just before the failure was:\n");
DBUG_PRINT("info", ("show_offset: %d", show_offset));
if (!lines)
{
fprintf(stderr, "< snip >\n");
if (fseek(m_file, show_offset, SEEK_END) != 0)
{
fprintf(stderr, "Failed to seek to position %d in '%s', errno: %d",
show_offset, m_file_name, errno);
DBUG_VOID_RETURN;
}
}
else {
DBUG_PRINT("info", ("Showing the whole file"));
if (fseek(m_file, 0L, SEEK_SET) != 0)
{
fprintf(stderr, "Failed to seek to pos 0 in '%s', errno: %d",
m_file_name, errno);
DBUG_VOID_RETURN;
}
}
while ((bytes= fread(buf, 1, sizeof(buf)-1, m_file)) > 0)
if (bytes != fwrite(buf, 1, bytes, stderr))
die("Failed to write to '%s', errno: %d",
m_file_name, errno);
if (!lines)
{
fprintf(stderr,
"\nMore results from queries before failure can be found in %s\n",
m_file_name);
}
fflush(stderr);
DBUG_VOID_RETURN;
}
};
LogFile log_file;
LogFile progress_file;
void replace_dynstr_append_mem(DYNAMIC_STRING *ds, const char *val, size_t len);
void replace_dynstr_append(DYNAMIC_STRING *ds, const char *val);
void replace_dynstr_append_uint(DYNAMIC_STRING *ds, uint val);
void dynstr_append_sorted(DYNAMIC_STRING* ds, DYNAMIC_STRING* ds_input,
bool keep_header);
static int match_expected_error(struct st_command *command,
unsigned int err_errno,
const char *err_sqlstate);
void handle_error(struct st_command*,
unsigned int err_errno, const char *err_error,
const char *err_sqlstate, DYNAMIC_STRING *ds);
void handle_no_error(struct st_command*);
void revert_properties();
static void handle_no_active_connection(struct st_command* command,
struct st_connection *cn, DYNAMIC_STRING *ds);
/* Wrapper for fgets.Strips \r off newlines on Windows.
Should be used with together with my_popen().
*/
static char *my_fgets(char * s, int n, FILE * stream, int *len)
{
char *buf = fgets(s, n, stream);
if (!buf)
{
*len= 0;
return buf;
}
*len = (int)strlen(buf);
#ifdef _WIN32
/* Strip '\r' off newlines. */
if (*len > 1 && buf[*len - 2] == '\r' && buf[*len - 1] == '\n')
{
buf[*len - 2]= '\n';
buf[*len - 1]= 0;
(*len)--;
}
#endif
return buf;
}
/*
Wrapper for popen().
On Windows, uses binary mode to workaround
C runtime bug mentioned in MDEV-9409
*/
static FILE* my_popen(const char *cmd, const char *mode)
{
FILE *f= popen(cmd, mode);
#ifdef _WIN32
if (f)
_setmode(fileno(f), O_BINARY);
#endif
return f;
}
#ifdef EMBEDDED_LIBRARY
#define EMB_SEND_QUERY 1
#define EMB_READ_QUERY_RESULT 2
#define EMB_END_CONNECTION 3
#define EMB_PREPARE_STMT 4
#define EMB_EXECUTE_STMT 5
#define EMB_CLOSE_STMT 6
/* workaround for MySQL BUG#57491 */
#undef MY_WME
#define MY_WME 0
/* attributes of the query thread */
pthread_attr_t cn_thd_attrib;
/*
This procedure represents the connection and actually
runs queries when in the EMBEDDED-SERVER mode.
The run_query_normal() just sends request for running
mysql_send_query and mysql_read_query_result() here.
*/
pthread_handler_t connection_thread(void *arg)
{
struct st_connection *cn= (struct st_connection*)arg;
mysql_thread_init();
while (cn->command != EMB_END_CONNECTION)
{
if (!cn->command)
{
pthread_mutex_lock(&cn->query_mutex);
while (!cn->command)
pthread_cond_wait(&cn->query_cond, &cn->query_mutex);
pthread_mutex_unlock(&cn->query_mutex);
}
switch (cn->command)
{
case EMB_END_CONNECTION:
goto end_thread;
case EMB_SEND_QUERY:
cn->result= mysql_send_query(cn->mysql,
cn->cur_query, cn->cur_query_len);
break;
case EMB_READ_QUERY_RESULT:
cn->result= mysql_read_query_result(cn->mysql);
break;
case EMB_PREPARE_STMT:
cn->result= mysql_stmt_prepare(cn->stmt,
cn->cur_query, cn->cur_query_len);
break;
case EMB_EXECUTE_STMT:
cn->result= mysql_stmt_execute(cn->stmt);
break;
case EMB_CLOSE_STMT:
cn->result= mysql_stmt_close(cn->stmt);
break;
default:
DBUG_ASSERT(0);
}
cn->command= 0;
pthread_mutex_lock(&cn->result_mutex);
cn->query_done= 1;
pthread_cond_signal(&cn->result_cond);
pthread_mutex_unlock(&cn->result_mutex);
}
end_thread:
cn->query_done= 1;
mysql_close(cn->mysql);
cn->mysql= 0;
mysql_thread_end();
pthread_exit(0);
return 0;
}
static void wait_query_thread_done(struct st_connection *con)
{
DBUG_ASSERT(con->has_thread);
if (!con->query_done)
{
pthread_mutex_lock(&con->result_mutex);
while (!con->query_done)
pthread_cond_wait(&con->result_cond, &con->result_mutex);
pthread_mutex_unlock(&con->result_mutex);
}
}
static void signal_connection_thd(struct st_connection *cn, int command)
{
DBUG_ASSERT(cn->has_thread);
cn->query_done= 0;
cn->command= command;
pthread_mutex_lock(&cn->query_mutex);
pthread_cond_signal(&cn->query_cond);
pthread_mutex_unlock(&cn->query_mutex);
}
/*
Sometimes we try to execute queries when the connection is closed.
It's done to make sure it was closed completely.
So that if our connection is closed (cn->has_thread == 0), we just return
the mysql_send_query() result which is an error in this case.
*/
static int do_send_query(struct st_connection *cn, const char *q, int q_len)
{
if (!cn->has_thread)