forked from MariaDB/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
4232 lines (3711 loc) · 125 KB
/
client.c
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) 2003, 2016, Oracle and/or its affiliates.
Copyright (c) 2009, 2020, 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 Street, Fifth Floor, Boston, MA 02110-1335 USA */
/*
This file is included by both libmysql.c (the MySQL client C API)
and the mysqld server to connect to another MYSQL server.
The differences for the two cases are:
- Things that only works for the client:
- Trying to automaticly determinate user name if not supplied to
mysql_real_connect()
- Support for reading local file with LOAD DATA LOCAL
- SHARED memory handling
- Prepared statements
- Things that only works for the server
- Alarm handling on connect
In all other cases, the code should be idential for the client and
server.
*/
#include <my_global.h>
#include <my_default.h>
#include "mysql.h"
#include "hash.h"
/* Remove client convenience wrappers */
#undef max_allowed_packet
#undef net_buffer_length
#ifdef EMBEDDED_LIBRARY
#undef MYSQL_SERVER
#ifndef MYSQL_CLIENT
#define MYSQL_CLIENT
#endif
#define CLI_MYSQL_REAL_CONNECT STDCALL cli_mysql_real_connect
#undef net_flush
my_bool net_flush(NET *net);
#else /*EMBEDDED_LIBRARY*/
#define CLI_MYSQL_REAL_CONNECT STDCALL mysql_real_connect
#endif /*EMBEDDED_LIBRARY*/
#include <my_sys.h>
#include <mysys_err.h>
#include <m_string.h>
#include <m_ctype.h>
#include "mysql_version.h"
#include "mysqld_error.h"
#include "errmsg.h"
#include <violite.h>
#if !defined(_WIN32)
#include <my_pthread.h> /* because of signal() */
#endif /* !defined(_WIN32) */
#include <sys/stat.h>
#include <signal.h>
#include <time.h>
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#if !defined(_WIN32)
#ifdef HAVE_SELECT_H
# include <select.h>
#endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#endif /* !defined(_WIN32) */
#ifdef HAVE_SYS_UN_H
# include <sys/un.h>
#endif
#define CONNECT_TIMEOUT 0
#include "client_settings.h"
#include <ssl_compat.h>
#include <sql_common.h>
#include <mysql/client_plugin.h>
typedef enum {
ALWAYS_ACCEPT, /* heuristics is disabled, use CLIENT_LOCAL_FILES */
WAIT_FOR_QUERY, /* heuristics is enabled, not sending files */
ACCEPT_FILE_REQUEST /* heuristics is enabled, ready to send a file */
} auto_local_infile_state;
#define native_password_plugin_name "mysql_native_password"
#define old_password_plugin_name "mysql_old_password"
PSI_memory_key key_memory_mysql_options;
PSI_memory_key key_memory_MYSQL_DATA;
PSI_memory_key key_memory_MYSQL;
PSI_memory_key key_memory_MYSQL_RES;
PSI_memory_key key_memory_MYSQL_ROW;
PSI_memory_key key_memory_MYSQL_state_change_info;
PSI_memory_key key_memory_MYSQL_HANDSHAKE;
#if defined (_WIN32) && !defined (EMBEDDED_LIBRARY)
PSI_memory_key key_memory_create_shared_memory;
#endif /* _WIN32 && ! EMBEDDED_LIBRARY */
#ifdef HAVE_PSI_INTERFACE
/*
This code is common to the client and server,
and also used in the server when server A connects to server B,
for example with replication.
Therefore, the code is also instrumented.
*/
static PSI_memory_info all_client_memory[]=
{
#if defined (_WIN32) && !defined (EMBEDDED_LIBRARY)
{ &key_memory_create_shared_memory, "create_shared_memory", 0},
#endif /* _WIN32 && ! EMBEDDED_LIBRARY */
{ &key_memory_mysql_options, "mysql_options", 0},
{ &key_memory_MYSQL_DATA, "MYSQL_DATA", 0},
{ &key_memory_MYSQL, "MYSQL", 0},
{ &key_memory_MYSQL_RES, "MYSQL_RES", 0},
{ &key_memory_MYSQL_ROW, "MYSQL_ROW", 0},
{ &key_memory_MYSQL_state_change_info, "MYSQL_STATE_CHANGE_INFO", 0},
{ &key_memory_MYSQL_HANDSHAKE, "MYSQL_HANDSHAKE", 0}
};
void init_client_psi_keys(void)
{
const char *category= "client";
int count;
count= array_elements(all_client_memory);
mysql_memory_register(category, all_client_memory, count);
}
#endif /* HAVE_PSI_INTERFACE */
uint mariadb_deinitialize_ssl= 1;
uint mysql_port=0;
char *mysql_unix_port= 0;
const char *unknown_sqlstate= "HY000";
const char *not_error_sqlstate= "00000";
const char *cant_connect_sqlstate= "08001";
static void mysql_close_free_options(MYSQL *mysql);
static void mysql_close_free(MYSQL *mysql);
static void mysql_prune_stmt_list(MYSQL *mysql);
static int cli_report_progress(MYSQL *mysql, char *packet, uint length);
CHARSET_INFO *default_client_charset_info = &my_charset_latin1;
/* Server error code and message */
unsigned int mysql_server_last_errno;
char mysql_server_last_error[MYSQL_ERRMSG_SIZE];
/**
Convert the connect timeout option to a timeout value for VIO
functions (vio_socket_connect() and vio_io_wait()).
@param mysql Connection handle (client side).
@return The timeout value in milliseconds, or -1 if no timeout.
*/
static int get_vio_connect_timeout(MYSQL *mysql)
{
int timeout_ms;
uint timeout_sec;
/*
A timeout of 0 means no timeout. Also, the connect_timeout
option value is in seconds, while VIO timeouts are measured
in milliseconds. Hence, check for a possible overflow. In
case of overflow, set to no timeout.
*/
timeout_sec= mysql->options.connect_timeout;
if (!timeout_sec || (timeout_sec > INT_MAX/1000))
timeout_ms= -1;
else
timeout_ms= (int) (timeout_sec * 1000);
return timeout_ms;
}
/**
Set the internal error message to mysql handler
@param mysql connection handle (client side)
@param errcode CR_ error code, passed to ER macro to get
error text
@parma sqlstate SQL standard sqlstate
*/
void set_mysql_error(MYSQL *mysql, int errcode, const char *sqlstate)
{
NET *net;
DBUG_ENTER("set_mysql_error");
DBUG_PRINT("enter", ("error :%d '%s'", errcode, ER(errcode)));
DBUG_ASSERT(mysql != 0);
if (mysql)
{
net= &mysql->net;
net->last_errno= errcode;
strmov(net->last_error, ER(errcode));
strmov(net->sqlstate, sqlstate);
}
else
{
mysql_server_last_errno= errcode;
strmov(mysql_server_last_error, ER(errcode));
}
DBUG_VOID_RETURN;
}
/**
Clear possible error state of struct NET
@param net clear the state of the argument
*/
void net_clear_error(NET *net)
{
net->last_errno= 0;
net->last_error[0]= '\0';
strmov(net->sqlstate, not_error_sqlstate);
}
/**
Set an error message on the client.
@param mysql connection handle
@param errcode CR_* errcode, for client errors
@param sqlstate SQL standard sql state, unknown_sqlstate for the
majority of client errors.
@param format error message template, in sprintf format
@param ... variable number of arguments
*/
void set_mysql_extended_error(MYSQL *mysql, int errcode,
const char *sqlstate,
const char *format, ...)
{
NET *net;
va_list args;
DBUG_ENTER("set_mysql_extended_error");
DBUG_PRINT("enter", ("error :%d '%s'", errcode, format));
DBUG_ASSERT(mysql != 0);
net= &mysql->net;
net->last_errno= errcode;
va_start(args, format);
my_vsnprintf(net->last_error, sizeof(net->last_error)-1,
format, args);
va_end(args);
strmov(net->sqlstate, sqlstate);
DBUG_VOID_RETURN;
}
/*
Create a named pipe connection
*/
#ifdef _WIN32
HANDLE create_named_pipe(MYSQL *mysql, uint connect_timeout, char **arg_host,
char **arg_unix_socket)
{
HANDLE hPipe=INVALID_HANDLE_VALUE;
char pipe_name[1024];
DWORD dwMode;
int i;
char *host= *arg_host, *unix_socket= *arg_unix_socket;
if ( ! unix_socket || (unix_socket)[0] == 0x00)
unix_socket = mysql_unix_port;
if (!host || !strcmp(host,LOCAL_HOST))
host=LOCAL_HOST_NAMEDPIPE;
pipe_name[sizeof(pipe_name)-1]= 0; /* Safety if too long string */
strxnmov(pipe_name, sizeof(pipe_name)-1, "\\\\", host, "\\pipe\\",
unix_socket, NullS);
DBUG_PRINT("info",("Server name: '%s'. Named Pipe: %s", host, unix_socket));
for (i=0 ; i < 100 ; i++) /* Don't retry forever */
{
if ((hPipe = CreateFile(pipe_name,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL )) != INVALID_HANDLE_VALUE)
break;
if (GetLastError() != ERROR_PIPE_BUSY)
{
set_mysql_extended_error(mysql, CR_NAMEDPIPEOPEN_ERROR,
unknown_sqlstate, ER(CR_NAMEDPIPEOPEN_ERROR),
host, unix_socket, (ulong) GetLastError());
return INVALID_HANDLE_VALUE;
}
/* wait for for an other instance */
if (! WaitNamedPipe(pipe_name, connect_timeout*1000) )
{
set_mysql_extended_error(mysql, CR_NAMEDPIPEWAIT_ERROR, unknown_sqlstate,
ER(CR_NAMEDPIPEWAIT_ERROR),
host, unix_socket, (ulong) GetLastError());
return INVALID_HANDLE_VALUE;
}
}
if (hPipe == INVALID_HANDLE_VALUE)
{
set_mysql_extended_error(mysql, CR_NAMEDPIPEOPEN_ERROR, unknown_sqlstate,
ER(CR_NAMEDPIPEOPEN_ERROR), host, unix_socket,
(ulong) GetLastError());
return INVALID_HANDLE_VALUE;
}
dwMode = PIPE_READMODE_BYTE | PIPE_WAIT;
if ( !SetNamedPipeHandleState(hPipe, &dwMode, NULL, NULL) )
{
CloseHandle( hPipe );
set_mysql_extended_error(mysql, CR_NAMEDPIPESETSTATE_ERROR,
unknown_sqlstate, ER(CR_NAMEDPIPESETSTATE_ERROR),
host, unix_socket, (ulong) GetLastError());
return INVALID_HANDLE_VALUE;
}
*arg_host=host ; *arg_unix_socket=unix_socket; /* connect arg */
return (hPipe);
}
#endif
/**
Read a packet from server. Give error message if socket was down
or packet is an error message
@retval packet_error An error occurred during reading.
Error message is set.
@retval
*/
ulong
cli_safe_read(MYSQL *mysql)
{
ulong reallen = 0;
return cli_safe_read_reallen(mysql, &reallen);
}
ulong
cli_safe_read_reallen(MYSQL *mysql, ulong* reallen)
{
NET *net= &mysql->net;
ulong len=0;
restart:
if (net->vio != 0)
len= my_net_read_packet_reallen(net, 0, reallen);
if (len == packet_error || len == 0)
{
DBUG_PRINT("error",("Wrong connection or packet. fd: %s len: %lu",
net->vio ? vio_description(net->vio) : NULL, len));
#ifdef MYSQL_SERVER
if (net->vio && (net->last_errno == ER_NET_READ_INTERRUPTED))
return (packet_error);
#endif /*MYSQL_SERVER*/
end_server(mysql);
set_mysql_error(mysql, net->last_errno == ER_NET_PACKET_TOO_LARGE ?
CR_NET_PACKET_TOO_LARGE: CR_SERVER_LOST, unknown_sqlstate);
return (packet_error);
}
if (net->read_pos[0] == 255)
{
if (len > 3)
{
char *pos= (char*) net->read_pos+1;
uint last_errno=uint2korr(pos);
if (last_errno == 65535 &&
(mysql->server_capabilities & CLIENT_PROGRESS_OBSOLETE))
{
if (cli_report_progress(mysql, pos+2, (uint) (len-3)))
{
/* Wrong packet */
set_mysql_error(mysql, CR_MALFORMED_PACKET, unknown_sqlstate);
return (packet_error);
}
goto restart;
}
net->last_errno= last_errno;
pos+=2;
len-=2;
if (protocol_41(mysql) && pos[0] == '#')
{
strmake_buf(net->sqlstate, pos+1);
pos+= SQLSTATE_LENGTH+1;
}
else
{
/*
The SQL state hasn't been received -- it should be reset to HY000
(unknown error sql state).
*/
strmov(net->sqlstate, unknown_sqlstate);
}
(void) strmake(net->last_error,(char*) pos,
MY_MIN((uint) len,(uint) sizeof(net->last_error)-1));
}
else
set_mysql_error(mysql, CR_UNKNOWN_ERROR, unknown_sqlstate);
/*
Cover a protocol design error: error packet does not
contain the server status. Therefore, the client has no way
to find out whether there are more result sets of
a multiple-result-set statement pending. Luckily, in 5.0 an
error always aborts execution of a statement, wherever it is
a multi-statement or a stored procedure, so it should be
safe to unconditionally turn off the flag here.
*/
mysql->server_status&= ~SERVER_MORE_RESULTS_EXISTS;
DBUG_PRINT("error",("Got error: %d/%s (%s)",
net->last_errno,
net->sqlstate,
net->last_error));
return(packet_error);
}
return len;
}
void free_rows(MYSQL_DATA *cur)
{
if (cur)
{
free_root(&cur->alloc,MYF(0));
my_free(cur);
}
}
my_bool
cli_advanced_command(MYSQL *mysql, enum enum_server_command command,
const uchar *header, ulong header_length,
const uchar *arg, ulong arg_length, my_bool skip_check,
MYSQL_STMT *stmt)
{
NET *net= &mysql->net;
my_bool result= 1;
my_bool stmt_skip= stmt ? stmt->state != MYSQL_STMT_INIT_DONE : FALSE;
DBUG_ENTER("cli_advanced_command");
if (mysql->net.vio == 0)
{ /* Do reconnect if possible */
if (mysql_reconnect(mysql) || stmt_skip)
DBUG_RETURN(1);
}
if (mysql->status != MYSQL_STATUS_READY ||
mysql->server_status & SERVER_MORE_RESULTS_EXISTS)
{
DBUG_PRINT("error",("state: %d", mysql->status));
set_mysql_error(mysql, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate);
DBUG_RETURN(1);
}
net_clear_error(net);
mysql->info=0;
mysql->affected_rows= ~(my_ulonglong) 0;
/*
We don't want to clear the protocol buffer on COM_QUIT, because if
the previous command was a shutdown command, we may have the
response for the COM_QUIT already in the communication buffer
*/
net_clear(&mysql->net, (command != COM_QUIT));
if (net_write_command(net,(uchar) command, header, header_length,
arg, arg_length))
{
DBUG_PRINT("error",("Can't send command to server. Error: %d",
socket_errno));
if (net->last_errno == ER_NET_PACKET_TOO_LARGE)
{
set_mysql_error(mysql, CR_NET_PACKET_TOO_LARGE, unknown_sqlstate);
goto end;
}
if (net->last_errno == ER_NET_ERROR_ON_WRITE && command == COM_BINLOG_DUMP)
goto end;
end_server(mysql);
if (mysql_reconnect(mysql) || stmt_skip)
goto end;
if (net_write_command(net,(uchar) command, header, header_length,
arg, arg_length))
{
set_mysql_error(mysql, CR_SERVER_GONE_ERROR, unknown_sqlstate);
goto end;
}
}
result=0;
if (!skip_check)
result= ((mysql->packet_length=cli_safe_read(mysql)) == packet_error ?
1 : 0);
end:
DBUG_PRINT("exit",("result: %d", result));
DBUG_RETURN(result);
}
void free_old_query(MYSQL *mysql)
{
DBUG_ENTER("free_old_query");
if (mysql->fields)
free_root(&mysql->field_alloc,MYF(0));
/* Assume rowlength < 8192 */
init_alloc_root(PSI_INSTRUMENT_ME, &mysql->field_alloc, 8192, 0,
MYF(mysql->options.use_thread_specific_memory ?
MY_THREAD_SPECIFIC : 0));
mysql->fields= 0;
mysql->field_count= 0; /* For API */
mysql->warning_count= 0;
mysql->info= 0;
DBUG_VOID_RETURN;
}
/**
Finish reading of a partial result set from the server.
Get the EOF packet, and update mysql->status
and mysql->warning_count.
@return TRUE if a communication or protocol error, an error
is set in this case, FALSE otherwise.
*/
my_bool flush_one_result(MYSQL *mysql)
{
ulong packet_length;
DBUG_ASSERT(mysql->status != MYSQL_STATUS_READY);
do
{
packet_length= cli_safe_read(mysql);
/*
There is an error reading from the connection,
or (sic!) there were no error and no
data in the stream, i.e. no more data from the server.
Since we know our position in the stream (somewhere in
the middle of a result set), this latter case is an error too
-- each result set must end with a EOF packet.
cli_safe_read() has set an error for us, just return.
*/
if (packet_length == packet_error)
return TRUE;
}
while (packet_length > 8 || mysql->net.read_pos[0] != 254);
/* Analyze EOF packet of the result set. */
if (protocol_41(mysql))
{
char *pos= (char*) mysql->net.read_pos + 1;
mysql->warning_count=uint2korr(pos);
pos+=2;
mysql->server_status=uint2korr(pos);
pos+=2;
}
return FALSE;
}
/**
Read a packet from network. If it's an OK packet, flush it.
@return TRUE if error, FALSE otherwise. In case of
success, is_ok_packet is set to TRUE or FALSE,
based on what we got from network.
*/
my_bool opt_flush_ok_packet(MYSQL *mysql, my_bool *is_ok_packet)
{
ulong packet_length= cli_safe_read(mysql);
if (packet_length == packet_error)
return TRUE;
/* cli_safe_read always reads a non-empty packet. */
DBUG_ASSERT(packet_length);
*is_ok_packet= mysql->net.read_pos[0] == 0;
if (*is_ok_packet)
{
uchar *pos= mysql->net.read_pos + 1;
net_field_length_ll(&pos); /* affected rows */
net_field_length_ll(&pos); /* insert id */
mysql->server_status=uint2korr(pos);
pos+=2;
if (protocol_41(mysql))
{
mysql->warning_count=uint2korr(pos);
pos+=2;
}
}
return FALSE;
}
/*
Flush result set sent from server
*/
static void cli_flush_use_result(MYSQL *mysql, my_bool flush_all_results)
{
/* Clear the current execution status */
DBUG_ENTER("cli_flush_use_result");
DBUG_PRINT("warning",("Not all packets read, clearing them"));
if (flush_one_result(mysql))
DBUG_VOID_RETURN; /* An error occurred */
if (! flush_all_results)
DBUG_VOID_RETURN;
while (mysql->server_status & SERVER_MORE_RESULTS_EXISTS)
{
my_bool is_ok_packet;
if (opt_flush_ok_packet(mysql, &is_ok_packet))
DBUG_VOID_RETURN; /* An error occurred. */
if (is_ok_packet)
{
/*
Indeed what we got from network was an OK packet, and we
know that OK is the last one in a multi-result-set, so
just return.
*/
DBUG_VOID_RETURN;
}
/*
It's a result set, not an OK packet. A result set contains
of two result set subsequences: field metadata, terminated
with EOF packet, and result set data, again terminated with
EOF packet. Read and flush them.
*/
if (flush_one_result(mysql) || flush_one_result(mysql))
DBUG_VOID_RETURN; /* An error occurred. */
}
DBUG_VOID_RETURN;
}
/*
Report progress to the client
RETURN VALUES
0 ok
1 error
*/
static int cli_report_progress(MYSQL *mysql, char *pkt, uint length)
{
uint stage, max_stage, proc_length;
double progress;
uchar *packet= (uchar*)pkt;
uchar *start= packet;
if (length < 5)
return 1; /* Wrong packet */
if (!(mysql->options.extension && mysql->options.extension->report_progress))
return 0; /* No callback, ignore packet */
packet++; /* Ignore number of strings */
stage= (uint) *packet++;
max_stage= (uint) *packet++;
progress= uint3korr(packet)/1000.0;
packet+= 3;
proc_length= net_field_length(&packet);
if (packet + proc_length > start + length)
return 1; /* Wrong packet */
(*mysql->options.extension->report_progress)(mysql, stage, max_stage,
progress, (char*) packet,
proc_length);
return 0;
}
/**************************************************************************
Shut down connection
**************************************************************************/
void end_server(MYSQL *mysql)
{
int save_errno= errno;
DBUG_ENTER("end_server");
if (mysql->net.vio != 0)
{
DBUG_PRINT("info",("Net: %s", vio_description(mysql->net.vio)));
#ifdef MYSQL_SERVER
slave_io_thread_detach_vio();
#endif
vio_delete(mysql->net.vio);
mysql->net.vio= 0; /* Marker */
mysql_prune_stmt_list(mysql);
}
net_end(&mysql->net);
free_old_query(mysql);
errno= save_errno;
DBUG_VOID_RETURN;
}
void STDCALL
mysql_free_result(MYSQL_RES *result)
{
DBUG_ENTER("mysql_free_result");
DBUG_PRINT("enter",("mysql_res: %p", result));
if (result)
{
MYSQL *mysql= result->handle;
if (mysql)
{
if (mysql->unbuffered_fetch_owner == &result->unbuffered_fetch_cancelled)
mysql->unbuffered_fetch_owner= 0;
if (mysql->status == MYSQL_STATUS_USE_RESULT)
{
(*mysql->methods->flush_use_result)(mysql, FALSE);
mysql->status=MYSQL_STATUS_READY;
if (mysql->unbuffered_fetch_owner)
*mysql->unbuffered_fetch_owner= TRUE;
}
}
free_rows(result->data);
if (result->fields)
free_root(&result->field_alloc,MYF(0));
my_free(result->row);
my_free(result);
}
DBUG_VOID_RETURN;
}
/****************************************************************************
Get options from my.cnf
****************************************************************************/
static const char *default_options[]=
{
"port","socket","compress","password","pipe", "timeout", "user",
"init-command", "host", "database", "debug", "return-found-rows",
"ssl-key" ,"ssl-cert" ,"ssl-ca" ,"ssl-capath",
"character-sets-dir", "default-character-set", "interactive-timeout",
"connect-timeout", "local-infile", "disable-local-infile",
"ssl-cipher", "max-allowed-packet", "protocol",
"multi-results", "multi-statements", "multi-queries", "secure-auth",
"report-data-truncation", "plugin-dir", "default-auth",
"bind-address", "ssl-crl", "ssl-crlpath",
"enable-cleartext-plugin",
NullS
};
enum option_id {
OPT_port=1, OPT_socket, OPT_compress, OPT_password, OPT_pipe, OPT_timeout, OPT_user,
OPT_init_command, OPT_host, OPT_database, OPT_debug, OPT_return_found_rows,
OPT_ssl_key, OPT_ssl_cert, OPT_ssl_ca, OPT_ssl_capath,
OPT_character_sets_dir, OPT_default_character_set, OPT_interactive_timeout,
OPT_connect_timeout, OPT_local_infile, OPT_disable_local_infile,
OPT_ssl_cipher, OPT_max_allowed_packet, OPT_protocol,
OPT_multi_results, OPT_multi_statements, OPT_multi_queries, OPT_secure_auth,
OPT_report_data_truncation, OPT_plugin_dir, OPT_default_auth,
OPT_bind_address, OPT_ssl_crl, OPT_ssl_crlpath,
OPT_enable_cleartext_plugin,
OPT_keep_this_one_last
};
static char *opt_strdup(const char *from, myf my_flags)
{
return my_strdup(key_memory_mysql_options, from, my_flags);
}
static TYPELIB option_types={array_elements(default_options)-1,
"options",default_options, NULL};
static int add_init_command(struct st_mysql_options *options, const char *cmd)
{
char *tmp;
if (!options->init_commands)
{
options->init_commands= (DYNAMIC_ARRAY*)my_malloc(key_memory_mysql_options,
sizeof(DYNAMIC_ARRAY),
MYF(MY_WME));
my_init_dynamic_array(key_memory_mysql_options, options->init_commands,
sizeof(char*),5, 5, MYF(0));
}
if (!(tmp= opt_strdup(cmd,MYF(MY_WME))) ||
insert_dynamic(options->init_commands, &tmp))
{
my_free(tmp);
return 1;
}
return 0;
}
#define ALLOCATE_EXTENSIONS(OPTS) \
(OPTS)->extension= (struct st_mysql_options_extention *) \
my_malloc(key_memory_mysql_options, \
sizeof(struct st_mysql_options_extention), \
MYF(MY_WME | MY_ZEROFILL)) \
#define ENSURE_EXTENSIONS_PRESENT(OPTS) \
do { \
if (!(OPTS)->extension) \
ALLOCATE_EXTENSIONS(OPTS); \
} while (0)
#define EXTENSION_SET_STRING_X(OPTS, X, STR, dup) \
do { \
if ((OPTS)->extension) \
my_free((OPTS)->extension->X); \
else \
ALLOCATE_EXTENSIONS(OPTS); \
(OPTS)->extension->X= ((STR) != NULL) ? \
dup((STR), MYF(MY_WME)) : NULL; \
} while (0)
#define EXTENSION_SET_STRING(OPTS, X, STR) \
EXTENSION_SET_STRING_X(OPTS, X, STR, opt_strdup)
#if defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY)
#define SET_SSL_OPTION_X(OPTS, opt_var, arg, dup) \
my_free((OPTS)->opt_var); \
(OPTS)->opt_var= arg ? dup(arg, MYF(MY_WME)) : NULL;
#define EXTENSION_SET_SSL_STRING_X(OPTS, X, STR, dup) \
EXTENSION_SET_STRING_X((OPTS), X, (STR), dup);
static char *set_ssl_option_unpack_path(const char *arg, myf flags)
{
char buff[FN_REFLEN + 1];
unpack_filename(buff, (char *)arg);
return opt_strdup(buff, flags);
}
#else
#define SET_SSL_OPTION_X(OPTS, opt_var,arg, dup) do { } while(0)
#define EXTENSION_SET_SSL_STRING_X(OPTS, X, STR, dup) do { } while(0)
#endif /* defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY) */
#define SET_SSL_OPTION(OPTS, opt_var,arg) SET_SSL_OPTION_X(OPTS, opt_var, arg, opt_strdup)
#define EXTENSION_SET_SSL_STRING(OPTS, X, STR) EXTENSION_SET_SSL_STRING_X(OPTS, X, STR, opt_strdup)
#define SET_SSL_PATH_OPTION(OPTS, opt_var,arg) SET_SSL_OPTION_X(OPTS, opt_var, arg, set_ssl_option_unpack_path)
#define EXTENSION_SET_SSL_PATH_STRING(OPTS, X, STR) EXTENSION_SET_SSL_STRING_X(OPTS, X, STR, set_ssl_option_unpack_path)
void mysql_read_default_options(struct st_mysql_options *options,
const char *filename,const char *group)
{
int argc;
char *argv_buff[1],**argv;
const char *groups[5];
DBUG_ENTER("mysql_read_default_options");
DBUG_PRINT("enter",("file: %s group: %s",filename,group ? group :"NULL"));
compile_time_assert(OPT_keep_this_one_last ==
array_elements(default_options));
argc=1; argv=argv_buff; argv_buff[0]= (char*) "client";
groups[0]= (char*) "client";
groups[1]= (char*) "client-server";
groups[2]= (char*) "client-mariadb";
groups[3]= (char*) group;
groups[4]=0;
my_load_defaults(filename, groups, &argc, &argv, NULL);
if (argc != 1) /* If some default option */
{
char **option=argv;
while (*++option)
{
/* DBUG_PRINT("info",("option: %s",option[0])); */
if (option[0][0] == '-' && option[0][1] == '-')
{
char *end=strcend(*option,'=');
char *opt_arg=0;
if (*end)
{
opt_arg=end+1;
*end=0; /* Remove '=' */
}
/* Change all '_' in variable name to '-' */
for (end= *option ; *(end= strcend(end,'_')) ; )
*end= '-';
switch (find_type(*option + 2, &option_types, FIND_TYPE_BASIC)) {
case OPT_port:
if (opt_arg)
options->port=atoi(opt_arg);
break;
case OPT_socket:
if (opt_arg)
{
my_free(options->unix_socket);
options->unix_socket=opt_strdup(opt_arg,MYF(MY_WME));
}
break;
case OPT_compress:
options->compress=1;
options->client_flag|= CLIENT_COMPRESS;
break;
case OPT_password:
if (opt_arg)
{
my_free(options->password);
options->password=opt_strdup(opt_arg,MYF(MY_WME));
}
break;
case OPT_pipe:
options->protocol = MYSQL_PROTOCOL_PIPE;
break;
case OPT_connect_timeout:
case OPT_timeout:
if (opt_arg)
options->connect_timeout=atoi(opt_arg);
break;
case OPT_user:
if (opt_arg)
{
my_free(options->user);
options->user=opt_strdup(opt_arg,MYF(MY_WME));
}
break;
case OPT_init_command:
add_init_command(options,opt_arg);
break;
case OPT_host:
if (opt_arg)
{
my_free(options->host);
options->host=opt_strdup(opt_arg,MYF(MY_WME));
}
break;
case OPT_database:
if (opt_arg)
{
my_free(options->db);
options->db=opt_strdup(opt_arg,MYF(MY_WME));
}
break;
case OPT_debug:
#ifdef MYSQL_CLIENT
mysql_debug(opt_arg ? opt_arg : "d:t:o,/tmp/client.trace");
break;
#endif
case OPT_return_found_rows:
options->client_flag|=CLIENT_FOUND_ROWS;
break;
case OPT_ssl_key:
SET_SSL_OPTION(options, ssl_key, opt_arg);
break;
case OPT_ssl_cert:
SET_SSL_OPTION(options, ssl_cert, opt_arg);
break;
case OPT_ssl_ca:
SET_SSL_OPTION(options, ssl_ca, opt_arg);
break;
case OPT_ssl_capath:
SET_SSL_OPTION(options, ssl_capath, opt_arg);
break;
case OPT_ssl_cipher:
SET_SSL_OPTION(options, ssl_cipher, opt_arg);