forked from hhyo/inception
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
4781 lines (4213 loc) · 142 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, 2012, Oracle and/or its affiliates. All rights reserved.
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 */
/*
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
In all other cases, the code should be idential for the client and
server.
*/
#include <my_global.h>
#include "mysql.h"
#include "hash.h"
#include "mysql/client_authentication.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 "my_default.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(__WIN__)
#include <my_pthread.h> /* because of signal() */
#endif /* !defined(__WIN__) */
#include <sys/stat.h>
#include <signal.h>
#include <time.h>
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#if !defined(__WIN__)
#ifdef HAVE_SELECT_H
# include <select.h>
#endif
#ifdef HAVE_SYS_SELECT_H
# include <sys/select.h>
#endif
#endif /* !defined(__WIN__) */
#ifdef HAVE_SYS_UN_H
# include <sys/un.h>
#endif
#ifndef _WIN32
#include <errno.h>
#define SOCKET_ERROR -1
#endif
#include "client_settings.h"
#include <sql_common.h>
#include <mysql/client_plugin.h>
#define native_password_plugin_name "mysql_native_password"
#define old_password_plugin_name "mysql_old_password"
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";
#ifdef HAVE_SMEM
// char *shared_memory_base_name= 0;
const char *def_shared_memory_base_name= default_shared_memory_base_name;
#endif
static void mysql_close_free_options(MYSQL *mysql);
static void mysql_close_free(MYSQL *mysql);
static void mysql_prune_stmt_list(MYSQL *mysql);
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;
}
#ifdef _WIN32
/**
Convert the connect timeout option to a timeout value for WIN32
synchronization functions.
@remark Specific for WIN32 connection methods shared memory and
named pipe.
@param mysql Connection handle (client side).
@return The timeout value in milliseconds, or INFINITE if no timeout.
*/
static DWORD get_win32_connect_timeout(MYSQL *mysql)
{
DWORD timeout_ms;
uint timeout_sec;
/*
A timeout of 0 means no timeout. Also, the connect_timeout
option value is in seconds, while WIN32 timeouts are 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= INFINITE;
else
timeout_ms= (DWORD) (timeout_sec * 1000);
return timeout_ms;
}
#endif
/**
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
static HANDLE create_named_pipe(MYSQL *mysql, DWORD connect_timeout,
const char **arg_host,
const char **arg_unix_socket)
{
HANDLE hPipe=INVALID_HANDLE_VALUE;
char pipe_name[1024];
DWORD dwMode;
int i;
my_bool testing_named_pipes=0;
const 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))
{
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
/*
Create new shared memory connection, return handler of connection
@param mysql Pointer of mysql structure
@param net Pointer of net structure
@param connect_timeout Timeout of connection (in milliseconds)
@return HANDLE to the shared memory area.
*/
#ifdef HAVE_SMEM
static HANDLE create_shared_memory(MYSQL *mysql, NET *net,
DWORD connect_timeout)
{
ulong smem_buffer_length = shared_memory_buffer_length + 4;
/*
event_connect_request is event object for start connection actions
event_connect_answer is event object for confirm, that server put data
handle_connect_file_map is file-mapping object, use for create shared
memory
handle_connect_map is pointer on shared memory
handle_map is pointer on shared memory for client
event_server_wrote,
event_server_read,
event_client_wrote,
event_client_read are events for transfer data between server and client
handle_file_map is file-mapping object, use for create shared memory
*/
HANDLE event_connect_request = NULL;
HANDLE event_connect_answer = NULL;
HANDLE handle_connect_file_map = NULL;
char *handle_connect_map = NULL;
char *handle_map = NULL;
HANDLE event_server_wrote = NULL;
HANDLE event_server_read = NULL;
HANDLE event_client_wrote = NULL;
HANDLE event_client_read = NULL;
HANDLE event_conn_closed = NULL;
HANDLE handle_file_map = NULL;
ulong connect_number;
char connect_number_char[22], *p;
char *tmp= NULL;
char *suffix_pos;
DWORD error_allow = 0;
DWORD error_code = 0;
DWORD event_access_rights= SYNCHRONIZE | EVENT_MODIFY_STATE;
char *shared_memory_base_name = mysql->options.shared_memory_base_name;
static const char *name_prefixes[] = {"","Global\\"};
const char *prefix;
int i;
/*
If this is NULL, somebody freed the MYSQL* options. mysql_close()
is a good candidate. We don't just silently (re)set it to
def_shared_memory_base_name as that would create really confusing/buggy
behavior if the user passed in a different name on the command-line or
in a my.cnf.
*/
DBUG_ASSERT(shared_memory_base_name != NULL);
/*
get enough space base-name + '_' + longest suffix we might ever send
*/
if (!(tmp= (char *)my_malloc(strlen(shared_memory_base_name) + 32L, MYF(MY_FAE))))
goto err;
/*
The name of event and file-mapping events create agree next rule:
shared_memory_base_name+unique_part
Where:
shared_memory_base_name is unique value for each server
unique_part is uniquel value for each object (events and file-mapping)
*/
for (i = 0; i< array_elements(name_prefixes); i++)
{
prefix= name_prefixes[i];
suffix_pos = strxmov(tmp, prefix , shared_memory_base_name, "_", NullS);
strmov(suffix_pos, "CONNECT_REQUEST");
event_connect_request= OpenEvent(event_access_rights, FALSE, tmp);
if (event_connect_request)
{
break;
}
}
if (!event_connect_request)
{
error_allow = CR_SHARED_MEMORY_CONNECT_REQUEST_ERROR;
goto err;
}
strmov(suffix_pos, "CONNECT_ANSWER");
if (!(event_connect_answer= OpenEvent(event_access_rights,FALSE,tmp)))
{
error_allow = CR_SHARED_MEMORY_CONNECT_ANSWER_ERROR;
goto err;
}
strmov(suffix_pos, "CONNECT_DATA");
if (!(handle_connect_file_map= OpenFileMapping(FILE_MAP_WRITE,FALSE,tmp)))
{
error_allow = CR_SHARED_MEMORY_CONNECT_FILE_MAP_ERROR;
goto err;
}
if (!(handle_connect_map= MapViewOfFile(handle_connect_file_map,
FILE_MAP_WRITE,0,0,sizeof(DWORD))))
{
error_allow = CR_SHARED_MEMORY_CONNECT_MAP_ERROR;
goto err;
}
/* Send to server request of connection */
if (!SetEvent(event_connect_request))
{
error_allow = CR_SHARED_MEMORY_CONNECT_SET_ERROR;
goto err;
}
/* Wait of answer from server */
if (WaitForSingleObject(event_connect_answer, connect_timeout) !=
WAIT_OBJECT_0)
{
error_allow = CR_SHARED_MEMORY_CONNECT_ABANDONED_ERROR;
goto err;
}
/* Get number of connection */
connect_number = uint4korr(handle_connect_map);/*WAX2*/
p= int10_to_str(connect_number, connect_number_char, 10);
/*
The name of event and file-mapping events create agree next rule:
shared_memory_base_name+unique_part+number_of_connection
Where:
shared_memory_base_name is uniquel value for each server
unique_part is uniquel value for each object (events and file-mapping)
number_of_connection is number of connection between server and client
*/
suffix_pos = strxmov(tmp, prefix , shared_memory_base_name, "_", connect_number_char,
"_", NullS);
strmov(suffix_pos, "DATA");
if ((handle_file_map = OpenFileMapping(FILE_MAP_WRITE,FALSE,tmp)) == NULL)
{
error_allow = CR_SHARED_MEMORY_FILE_MAP_ERROR;
goto err2;
}
if ((handle_map = MapViewOfFile(handle_file_map,FILE_MAP_WRITE,0,0,
smem_buffer_length)) == NULL)
{
error_allow = CR_SHARED_MEMORY_MAP_ERROR;
goto err2;
}
strmov(suffix_pos, "SERVER_WROTE");
if ((event_server_wrote = OpenEvent(event_access_rights,FALSE,tmp)) == NULL)
{
error_allow = CR_SHARED_MEMORY_EVENT_ERROR;
goto err2;
}
strmov(suffix_pos, "SERVER_READ");
if ((event_server_read = OpenEvent(event_access_rights,FALSE,tmp)) == NULL)
{
error_allow = CR_SHARED_MEMORY_EVENT_ERROR;
goto err2;
}
strmov(suffix_pos, "CLIENT_WROTE");
if ((event_client_wrote = OpenEvent(event_access_rights,FALSE,tmp)) == NULL)
{
error_allow = CR_SHARED_MEMORY_EVENT_ERROR;
goto err2;
}
strmov(suffix_pos, "CLIENT_READ");
if ((event_client_read = OpenEvent(event_access_rights,FALSE,tmp)) == NULL)
{
error_allow = CR_SHARED_MEMORY_EVENT_ERROR;
goto err2;
}
strmov(suffix_pos, "CONNECTION_CLOSED");
if ((event_conn_closed = OpenEvent(event_access_rights,FALSE,tmp)) == NULL)
{
error_allow = CR_SHARED_MEMORY_EVENT_ERROR;
goto err2;
}
/*
Set event that server should send data
*/
SetEvent(event_server_read);
err2:
if (error_allow == 0)
{
net->vio= vio_new_win32shared_memory(handle_file_map,handle_map,
event_server_wrote,
event_server_read,event_client_wrote,
event_client_read,event_conn_closed);
}
else
{
error_code = GetLastError();
if (event_server_read)
CloseHandle(event_server_read);
if (event_server_wrote)
CloseHandle(event_server_wrote);
if (event_client_read)
CloseHandle(event_client_read);
if (event_client_wrote)
CloseHandle(event_client_wrote);
if (event_conn_closed)
CloseHandle(event_conn_closed);
if (handle_map)
UnmapViewOfFile(handle_map);
if (handle_file_map)
CloseHandle(handle_file_map);
}
err:
my_free(tmp);
if (error_allow)
error_code = GetLastError();
if (event_connect_request)
CloseHandle(event_connect_request);
if (event_connect_answer)
CloseHandle(event_connect_answer);
if (handle_connect_map)
UnmapViewOfFile(handle_connect_map);
if (handle_connect_file_map)
CloseHandle(handle_connect_file_map);
if (error_allow)
{
if (error_allow == CR_SHARED_MEMORY_EVENT_ERROR)
set_mysql_extended_error(mysql, error_allow, unknown_sqlstate,
ER(error_allow), suffix_pos, error_code);
else
set_mysql_extended_error(mysql, error_allow, unknown_sqlstate,
ER(error_allow), error_code);
return(INVALID_HANDLE_VALUE);
}
return(handle_map);
}
#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)
{
NET *net= &mysql->net;
ulong len=0;
if (net->vio != 0)
len=my_net_read(net);
if (len == packet_error || len == 0)
{
DBUG_PRINT("error",("Wrong connection or packet. fd: %s len: %lu",
vio_description(net->vio),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;
net->last_errno=uint2korr(pos);
pos+=2;
len-=2;
if (protocol_41(mysql) && pos[0] == '#')
{
strmake(net->sqlstate, pos+1, SQLSTATE_LENGTH);
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;
/*
Do not check the socket/protocol buffer on COM_QUIT as the
result of a previous command might not have been read. This
can happen if a client sends a query but does not reap the
result before attempting to close the connection.
*/
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;
}
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));
init_alloc_root(&mysql->field_alloc,8192,0); /* Assume rowlength < 8192 */
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;
}
#ifdef __WIN__
static my_bool is_NT(void)
{
char *os=getenv("OS");
return (os && !strcmp(os, "Windows_NT")) ? 1 : 0;
}
#endif
#ifdef CHECK_LICENSE
/**
Check server side variable 'license'.
If the variable does not exist or does not contain 'Commercial',
we're talking to non-commercial server from commercial client.
@retval 0 success
@retval !0 network error or the server is not commercial.
Error code is saved in mysql->net.last_errno.
*/
static int check_license(MYSQL *mysql)
{
MYSQL_ROW row;
MYSQL_RES *res;
NET *net= &mysql->net;
static const char query[]= "SELECT @@license";
static const char required_license[]= STRINGIFY_ARG(LICENSE);
if (mysql_real_query(mysql, query, sizeof(query)-1))
{
if (net->last_errno == ER_UNKNOWN_SYSTEM_VARIABLE)
{
set_mysql_extended_error(mysql, CR_WRONG_LICENSE, unknown_sqlstate,
ER(CR_WRONG_LICENSE), required_license);
}
return 1;
}
if (!(res= mysql_use_result(mysql)))
return 1;
row= mysql_fetch_row(res);
/*
If no rows in result set, or column value is NULL (none of these
two is ever true for server variables now), or column value
mismatch, set wrong license error.
*/
if (!net->last_errno &&
(!row || !row[0] ||
strncmp(row[0], required_license, sizeof(required_license))))
{
set_mysql_extended_error(mysql, CR_WRONG_LICENSE, unknown_sqlstate,
ER(CR_WRONG_LICENSE), required_license);
}
mysql_free_result(res);
return net->last_errno;
}
#endif /* CHECK_LICENSE */
/**************************************************************************
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: 0x%lx", (long) 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;
}