forked from Meituan-Dianping/SQLAdvisor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.cc
More file actions
executable file
·1496 lines (1222 loc) · 37 KB
/
log.cc
File metadata and controls
executable file
·1496 lines (1222 loc) · 37 KB
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, 2014, Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2009, 2013, Monty Program Ab
Copyright (C) 2012 Percona Inc.
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 */
/**
@file
@brief
logging of commands
@todo
Abort logging when we get an error in reading or writing log files
*/
#include "my_global.h" /* NO_EMBEDDED_ACCESS_CHECKS */
#include "sql_priv.h"
#include "log.h"
#include "sql_base.h" // open_log_table
#include "sql_delete.h" // mysql_truncate
#include "sql_parse.h" // command_name
#include "sql_time.h" // calc_time_from_sec, my_time_compare
#include "sql_acl.h" // SUPER_ACL
#include "mysql/service_my_plugin_log.h"
#include "sp_head.h"
#include <my_dir.h>
#include <stdarg.h>
#include <m_ctype.h> // For test_if_number
#ifdef _WIN32
#include "message.h"
#endif
using std::min;
using std::max;
#include "sql_show.h"
#include "mysqld.h"
/* max size of the log message */
#define MAX_LOG_BUFFER_SIZE 1024
#define MAX_TIME_SIZE 32
#define MAX_USER_HOST_SIZE 512
static
const TABLE_FIELD_TYPE general_log_table_fields[GLT_FIELD_COUNT] =
{
{
{ C_STRING_WITH_LEN("event_time") },
{ C_STRING_WITH_LEN("timestamp") },
{ NULL, 0 }
},
{
{ C_STRING_WITH_LEN("user_host") },
{ C_STRING_WITH_LEN("mediumtext") },
{ C_STRING_WITH_LEN("utf8") }
},
{
{ C_STRING_WITH_LEN("thread_id") },
{ C_STRING_WITH_LEN("bigint(21) unsigned") },
{ NULL, 0 }
},
{
{ C_STRING_WITH_LEN("server_id") },
{ C_STRING_WITH_LEN("int(10) unsigned") },
{ NULL, 0 }
},
{
{ C_STRING_WITH_LEN("command_type") },
{ C_STRING_WITH_LEN("varchar(64)") },
{ C_STRING_WITH_LEN("utf8") }
},
{
{ C_STRING_WITH_LEN("argument") },
{ C_STRING_WITH_LEN("mediumtext") },
{ C_STRING_WITH_LEN("utf8") }
}
};
LOGGER logger;
static bool test_if_number(const char *str,
ulong *res, bool allow_wildcards);
/**
purge logs, master and slave sides both, related error code
convertor.
Called from @c purge_error_message(), @c MYSQL_BIN_LOG::reset_logs()
@param res an internal to purging routines error code
@return the user level error code ER_*
*/
uint purge_log_get_error_code(int res)
{
uint errcode= 0;
switch (res) {
case 0: break;
case LOG_INFO_EOF: errcode= ER_UNKNOWN_TARGET_BINLOG; break;
case LOG_INFO_IO: errcode= ER_IO_ERR_LOG_INDEX_READ; break;
case LOG_INFO_INVALID:errcode= ER_BINLOG_PURGE_PROHIBITED; break;
case LOG_INFO_SEEK: errcode= ER_FSEEK_FAIL; break;
case LOG_INFO_MEM: errcode= ER_OUT_OF_RESOURCES; break;
case LOG_INFO_FATAL: errcode= ER_BINLOG_PURGE_FATAL_ERR; break;
case LOG_INFO_IN_USE: errcode= ER_LOG_IN_USE; break;
case LOG_INFO_EMFILE: errcode= ER_BINLOG_PURGE_EMFILE; break;
default: errcode= ER_LOG_PURGE_UNKNOWN_ERR; break;
}
return errcode;
}
/**
Silence all errors and warnings reported when performing a write
to a log table.
Errors and warnings are not reported to the client or SQL exception
handlers, so that the presence of logging does not interfere and affect
the logic of an application.
*/
class Silence_log_table_errors : public Internal_error_handler
{
char m_message[MYSQL_ERRMSG_SIZE];
public:
Silence_log_table_errors()
{
m_message[0]= '\0';
}
virtual ~Silence_log_table_errors() {}
virtual bool handle_condition(THD *thd,
uint sql_errno,
const char* sql_state,
Sql_condition::enum_warning_level level,
const char* msg,
Sql_condition ** cond_hdl);
const char *message() const { return m_message; }
};
bool
Silence_log_table_errors::handle_condition(THD *,
uint,
const char*,
Sql_condition::enum_warning_level,
const char* msg,
Sql_condition ** cond_hdl)
{
*cond_hdl= NULL;
strmake(m_message, msg, sizeof(m_message)-1);
return TRUE;
}
sql_print_message_func sql_print_message_handlers[3] =
{
sql_print_information,
sql_print_warning,
sql_print_error
};
/**
Create the name of the log specified.
This method forms a new path + file name for the
log specified in @c name.
@param[IN] buff Location for building new string.
@param[IN] name Name of the log file.
@param[IN] log_ext The extension for the log (e.g. .log).
@returns Pointer to new string containing the name.
*/
char *make_log_name(char *buff, const char *name, const char* log_ext)
{
strmake(buff, name, FN_REFLEN-5);
return fn_format(buff, buff, mysql_real_data_home, log_ext,
MYF(MY_UNPACK_FILENAME|MY_REPLACE_EXT));
}
/* log event handlers */
bool Log_to_file_event_handler::
log_error(enum loglevel level, const char *format,
va_list args)
{
return vprint_msg_to_log(level, format, args);
}
void Log_to_file_event_handler::init_pthread_objects()
{
mysql_log.init_pthread_objects();
}
/**
Wrapper around MYSQL_LOG::write() for general log. We need it since we
want all log event handlers to have the same signature.
*/
bool Log_to_file_event_handler::
log_general(THD *thd, time_t event_time, const char *user_host,
uint user_host_len, my_thread_id thread_id,
const char *command_type, uint command_type_len,
const char *sql_text, uint sql_text_len,
const CHARSET_INFO *client_cs)
{
Silence_log_table_errors error_handler;
thd->push_internal_handler(&error_handler);
bool retval= mysql_log.write(event_time, user_host, user_host_len,
thread_id, command_type, command_type_len,
sql_text, sql_text_len);
thd->pop_internal_handler();
return retval;
}
bool Log_to_file_event_handler::init()
{
if (!is_initialized)
{
if (opt_log)
mysql_log.open_query_log(opt_logname);
is_initialized= TRUE;
}
return FALSE;
}
void Log_to_file_event_handler::cleanup()
{
mysql_log.cleanup();
}
void Log_to_file_event_handler::flush()
{
/* reopen log files */
if (opt_log)
mysql_log.reopen_file();
}
/*
Log error with all enabled log event handlers
SYNOPSIS
error_log_print()
level The level of the error significance: NOTE,
WARNING or ERROR.
format format string for the error message
args list of arguments for the format string
RETURN
FALSE - OK
TRUE - error occured
*/
bool LOGGER::error_log_print(enum loglevel level, const char *format,
va_list args)
{
bool error= FALSE;
Log_event_handler **current_handler;
/* currently we don't need locking here as there is no error_log table */
for (current_handler= error_log_handler_list ; *current_handler ;)
error= (*current_handler++)->log_error(level, format, args) || error;
return error;
}
void LOGGER::cleanup_base()
{
DBUG_ASSERT(inited == 1);
mysql_rwlock_destroy(&LOCK_logger);
if (file_log_handler)
file_log_handler->cleanup();
}
void LOGGER::cleanup_end()
{
DBUG_ASSERT(inited == 1);
if (file_log_handler)
{
delete file_log_handler;
file_log_handler=NULL;
}
inited= 0;
}
/**
Perform basic log initialization: create file-based log handler and
init error log.
*/
void LOGGER::init_base()
{
DBUG_ASSERT(inited == 0);
inited= 1;
/*
Here we create file log handler. We don't do it for the table log handler
here as it cannot be created so early. The reason is THD initialization,
which depends on the system variables (parsed later).
*/
if (!file_log_handler)
file_log_handler= new Log_to_file_event_handler;
/* by default we use traditional error log */
init_error_log(LOG_FILE);
file_log_handler->init_pthread_objects();
mysql_rwlock_init(key_rwlock_LOCK_logger, &LOCK_logger);
}
bool LOGGER::flush_logs(THD *thd)
{
int rc= 0;
/*
Now we lock logger, as nobody should be able to use logging routines while
log tables are closed
*/
logger.lock_exclusive();
/* reopen log files */
file_log_handler->flush();
/* end of log flush */
logger.unlock();
return rc;
}
/**
Close and reopen the general log (with locks).
@returns FALSE.
*/
bool LOGGER::flush_general_log()
{
/*
Now we lock logger, as nobody should be able to use logging routines while
log tables are closed
*/
logger.lock_exclusive();
/* Reopen general log file */
if (opt_log)
file_log_handler->get_mysql_log()->reopen_file();
/* End of log flush */
logger.unlock();
return 0;
}
bool LOGGER::general_log_write(THD *thd, enum enum_server_command command,
const char *query, uint query_length)
{
bool error= FALSE;
Log_event_handler **current_handler= general_log_handler_list;
char user_host_buff[MAX_USER_HOST_SIZE + 1];
uint user_host_len= 0;
time_t current_time;
DBUG_ASSERT(thd);
lock_shared();
if (!opt_log)
{
unlock();
return 0;
}
user_host_len= make_user_name(thd, user_host_buff);
current_time= my_time(0);
while (*current_handler)
error|= (*current_handler++)->
log_general(thd, current_time, user_host_buff,
user_host_len, thd->thread_id,
command_name[(uint) command].str,
command_name[(uint) command].length,
query, query_length,
thd->variables.character_set_client) || error;
unlock();
return error;
}
bool LOGGER::general_log_print(THD *thd, enum enum_server_command command,
const char *format, va_list args)
{
uint message_buff_len= 0;
char message_buff[MAX_LOG_BUFFER_SIZE];
/* prepare message */
if (format)
message_buff_len= my_vsnprintf(message_buff, sizeof(message_buff),
format, args);
else
message_buff[0]= '\0';
/* Print the message to the buffer if we want to log this kind of commands */
if (! logger.log_command(thd, command))
return FALSE;
return general_log_write(thd, command, message_buff, message_buff_len);
}
void LOGGER::init_error_log(uint error_log_printer)
{
if (error_log_printer & LOG_NONE)
{
error_log_handler_list[0]= 0;
return;
}
switch (error_log_printer) {
case LOG_FILE:
error_log_handler_list[0]= file_log_handler;
error_log_handler_list[1]= 0;
break;
/* these two are disabled for now */
case LOG_TABLE:
DBUG_ASSERT(0);
break;
case LOG_TABLE|LOG_FILE:
DBUG_ASSERT(0);
break;
}
}
void LOGGER::init_general_log(uint general_log_printer)
{
if (general_log_printer & LOG_NONE)
{
general_log_handler_list[0]= 0;
return;
}
switch (general_log_printer) {
case LOG_FILE:
general_log_handler_list[0]= file_log_handler;
general_log_handler_list[1]= 0;
break;
case LOG_TABLE:
general_log_handler_list[0]= 0;
break;
case LOG_TABLE|LOG_FILE:
general_log_handler_list[0]= file_log_handler;
general_log_handler_list[1]= 0;
break;
}
}
bool LOGGER::activate_log_handler(THD* thd, uint log_type)
{
MYSQL_QUERY_LOG *file_log;
bool res= FALSE;
lock_exclusive();
switch (log_type) {
case QUERY_LOG_GENERAL:
if (!opt_log)
{
file_log= file_log_handler->get_mysql_log();
file_log->open_query_log(opt_logname);
init_general_log(log_output_options);
opt_log= TRUE;
}
break;
default:
DBUG_ASSERT(0);
}
unlock();
return res;
}
void LOGGER::deactivate_log_handler(THD *thd, uint log_type)
{
my_bool *tmp_opt= 0;
MYSQL_LOG *file_log= NULL;
switch (log_type) {
case QUERY_LOG_GENERAL:
tmp_opt= &opt_log;
file_log= file_log_handler->get_mysql_log();
break;
default:
MY_ASSERT_UNREACHABLE();
}
if (!(*tmp_opt))
return;
lock_exclusive();
file_log->close(0);
*tmp_opt= FALSE;
unlock();
}
/* the parameters are unused for the log tables */
bool Log_to_csv_event_handler::init()
{
return 0;
}
int LOGGER::set_handlers(uint error_log_printer,
uint general_log_printer)
{
/* error log table is not supported yet */
DBUG_ASSERT(error_log_printer < LOG_TABLE);
lock_exclusive();
if ((general_log_printer & LOG_TABLE) &&
!is_log_tables_initialized)
{
general_log_printer= (general_log_printer & ~LOG_TABLE) | LOG_FILE;
sql_print_error("Failed to initialize log tables. "
"Falling back to the old-fashioned logs");
}
init_error_log(error_log_printer);
init_general_log(general_log_printer);
unlock();
return 0;
}
#ifdef _WIN32
static int eventSource = 0;
static void setup_windows_event_source()
{
HKEY hRegKey= NULL;
DWORD dwError= 0;
TCHAR szPath[MAX_PATH];
DWORD dwTypes;
if (eventSource) // Ensure that we are only called once
return;
eventSource= 1;
// Create the event source registry key
dwError= RegCreateKey(HKEY_LOCAL_MACHINE,
"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\MySQL",
&hRegKey);
/* Name of the PE module that contains the message resource */
GetModuleFileName(NULL, szPath, MAX_PATH);
/* Register EventMessageFile */
dwError = RegSetValueEx(hRegKey, "EventMessageFile", 0, REG_EXPAND_SZ,
(PBYTE) szPath, (DWORD) (strlen(szPath) + 1));
/* Register supported event types */
dwTypes= (EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE |
EVENTLOG_INFORMATION_TYPE);
dwError= RegSetValueEx(hRegKey, "TypesSupported", 0, REG_DWORD,
(LPBYTE) &dwTypes, sizeof dwTypes);
RegCloseKey(hRegKey);
}
#endif /* _WIN32 */
/**
Find a unique filename for 'filename.#'.
Set '#' to the number next to the maximum found in the most
recent log file extension.
This function will return nonzero if: (i) the generated name
exceeds FN_REFLEN; (ii) if the number of extensions is exhausted;
or (iii) some other error happened while examining the filesystem.
@return
nonzero if not possible to get unique filename.
*/
static int find_uniq_filename(char *name, ulong *next)
{
uint i;
char buff[FN_REFLEN], ext_buf[FN_REFLEN];
struct st_my_dir *dir_info;
reg1 struct fileinfo *file_info;
ulong max_found= 0, number= 0;
size_t buf_length, length;
char *start, *end;
int error= 0;
DBUG_ENTER("find_uniq_filename");
*next= 0;
length= dirname_part(buff, name, &buf_length);
start= name + length;
end= strend(start);
*end='.';
length= (size_t) (end - start + 1);
if ((DBUG_EVALUATE_IF("error_unique_log_filename", 1,
!(dir_info= my_dir(buff,MYF(MY_DONT_SORT))))))
{ // This shouldn't happen
strmov(end,".1"); // use name+1
DBUG_RETURN(1);
}
file_info= dir_info->dir_entry;
for (i= dir_info->number_off_files ; i-- ; file_info++)
{
if (memcmp(file_info->name, start, length) == 0 &&
test_if_number(file_info->name+length, &number,0))
{
set_if_bigger(max_found,(ulong) number);
}
}
my_dirend(dir_info);
/* check if reached the maximum possible extension number */
if (max_found == MAX_LOG_UNIQUE_FN_EXT)
{
sql_print_error("Log filename extension number exhausted: %06lu. \
Please fix this by archiving old logs and \
updating the index files.", max_found);
error= 1;
goto end;
}
*next= max_found + 1;
if (sprintf(ext_buf, "%06lu", *next) < 0)
{
error= 1;
goto end;
}
*end++='.';
/*
Check if the generated extension size + the file name exceeds the
buffer size used. If one did not check this, then the filename might be
truncated, resulting in error.
*/
if (((strlen(ext_buf) + (end - name)) >= FN_REFLEN))
{
sql_print_error("Log filename too large: %s%s (%zu). \
Please fix this by archiving old logs and updating the \
index files.", name, ext_buf, (strlen(ext_buf) + (end - name)));
error= 1;
goto end;
}
if (sprintf(end, "%06lu", *next)<0)
{
error= 1;
goto end;
}
/* print warning if reaching the end of available extensions. */
if ((*next > (MAX_LOG_UNIQUE_FN_EXT - LOG_WARN_UNIQUE_FN_EXT_LEFT)))
sql_print_warning("Next log extension: %lu. \
Remaining log filename extensions: %lu. \
Please consider archiving some logs.", *next, (MAX_LOG_UNIQUE_FN_EXT - *next));
end:
DBUG_RETURN(error);
}
void MYSQL_LOG::init(enum_log_type log_type_arg,
enum cache_type io_cache_type_arg)
{
DBUG_ENTER("MYSQL_LOG::init");
log_type= log_type_arg;
io_cache_type= io_cache_type_arg;
DBUG_PRINT("info",("log_type: %d", log_type));
DBUG_VOID_RETURN;
}
bool MYSQL_LOG::init_and_set_log_file_name(const char *log_name,
const char *new_name,
enum_log_type log_type_arg,
enum cache_type io_cache_type_arg,
bool unique)
{
init(log_type_arg, io_cache_type_arg);
if (new_name && !strmov(log_file_name, new_name))
return TRUE;
else if (!new_name && generate_new_name(log_file_name, log_name, unique))
return TRUE;
return FALSE;
}
/*
Open a (new) log file.
SYNOPSIS
open()
log_name The name of the log to open
log_type_arg The type of the log. E.g. LOG_NORMAL
new_name The new name for the logfile. This is only needed
when the method is used to open the binlog file.
io_cache_type_arg The type of the IO_CACHE to use for this log file
DESCRIPTION
Open the logfile, init IO_CACHE and write startup messages
(in case of general and slow query logs).
RETURN VALUES
0 ok
1 error
*/
bool MYSQL_LOG::open(
#ifdef HAVE_PSI_INTERFACE
PSI_file_key log_file_key,
#endif
const char *log_name, enum_log_type log_type_arg,
const char *new_name, enum cache_type io_cache_type_arg,
bool unique)
{
char buff[FN_REFLEN];
MY_STAT f_stat;
File file= -1;
my_off_t pos= 0;
int open_flags= O_CREAT | O_BINARY;
DBUG_ENTER("MYSQL_LOG::open");
DBUG_PRINT("enter", ("log_type: %d", (int) log_type_arg));
write_error= 0;
if (!(name= my_strdup(log_name, MYF(MY_WME))))
{
name= (char *)log_name; // for the error message
goto err;
}
if (init_and_set_log_file_name(name, new_name,
log_type_arg, io_cache_type_arg, unique) ||
DBUG_EVALUATE_IF("fault_injection_init_name", log_type == LOG_BIN, 0))
goto err;
/* File is regular writable file */
if (my_stat(log_file_name, &f_stat, MYF(0)) && !MY_S_ISREG(f_stat.st_mode))
goto err;
if (io_cache_type == SEQ_READ_APPEND)
open_flags |= O_RDWR | O_APPEND;
else
open_flags |= O_WRONLY | (log_type == LOG_BIN ? 0 : O_APPEND);
db[0]= 0;
#ifdef HAVE_PSI_INTERFACE
/* Keep the key for reopen */
m_log_file_key= log_file_key;
#endif
if ((file= mysql_file_open(log_file_key,
log_file_name, open_flags,
MYF(MY_WME | ME_WAITTANG))) < 0)
goto err;
if ((pos= mysql_file_tell(file, MYF(MY_WME))) == MY_FILEPOS_ERROR)
{
if (my_errno == ESPIPE)
pos= 0;
else
goto err;
}
if (init_io_cache(&log_file, file, IO_SIZE, io_cache_type, pos, 0,
MYF(MY_WME | MY_NABP |
((log_type == LOG_BIN) ? MY_WAIT_IF_FULL : 0))))
goto err;
if (log_type == LOG_NORMAL)
{
char *end;
int len=my_snprintf(buff, sizeof(buff), "%s, Version: %s (%s). "
#ifdef EMBEDDED_LIBRARY
"embedded library\n",
my_progname, server_version, MYSQL_COMPILATION_COMMENT
#elif _WIN32
"started with:\nTCP Port: %d, Named Pipe: %s\n",
my_progname, server_version, MYSQL_COMPILATION_COMMENT,
mysqld_port, mysqld_unix_port
#else
"started with:\n=\n",
my_progname, server_version, MYSQL_COMPILATION_COMMENT
#endif
);
end= strnmov(buff + len, "Time Id Command Argument\n",
sizeof(buff) - len);
if (my_b_write(&log_file, (uchar*) buff, (uint) (end-buff)) ||
flush_io_cache(&log_file))
goto err;
}
log_state= LOG_OPENED;
DBUG_RETURN(0);
err:
if (log_type == LOG_BIN)
{
THD *thd= current_thd;
/*
On fatal error when code enters here we should forcefully clear the
previous errors so that a new critical error message can be pushed
to the client side.
*/
thd->clear_error();
my_error(ER_BINLOG_LOGGING_IMPOSSIBLE, MYF(0), "Either disk is full or "
"file system is read only while opening the binlog. Aborting the "
"server");
_exit(EXIT_FAILURE);
}
else
sql_print_error("Could not open %s for logging (error %d). "
"Turning logging off for the whole duration "
"of the MySQL server process. To turn it on "
"again: fix the cause, shutdown the MySQL "
"server and restart it.",
name, errno);
if (file >= 0)
mysql_file_close(file, MYF(0));
end_io_cache(&log_file);
my_free(name);
name= NULL;
log_state= LOG_CLOSED;
DBUG_RETURN(1);
}
MYSQL_LOG::MYSQL_LOG()
: name(0), write_error(FALSE), inited(FALSE), log_type(LOG_UNKNOWN),
log_state(LOG_CLOSED),
cur_log_ext(-1)
#ifdef HAVE_PSI_INTERFACE
, m_key_LOCK_log(key_LOG_LOCK_log)
#endif
{
/*
We don't want to initialize LOCK_Log here as such initialization depends on
safe_mutex (when using safe_mutex) which depends on MY_INIT(), which is
called only in main(). Doing initialization here would make it happen
before main().
*/
memset(&log_file, 0, sizeof(log_file));
}
void MYSQL_LOG::init_pthread_objects()
{
DBUG_ASSERT(inited == 0);
inited= 1;
mysql_mutex_init(m_key_LOCK_log, &LOCK_log, MY_MUTEX_INIT_SLOW);
}
/*
Close the log file
SYNOPSIS
close()
exiting Bitmask. For the slow and general logs the only used bit is
LOG_CLOSE_TO_BE_OPENED. This is used if we intend to call
open at once after close.
NOTES
One can do an open on the object at once after doing a close.
The internal structures are not freed until cleanup() is called
*/
void MYSQL_LOG::close(uint exiting)
{ // One can't set log_type here!
DBUG_ENTER("MYSQL_LOG::close");
DBUG_PRINT("enter",("exiting: %d", (int) exiting));
if (log_state == LOG_OPENED)
{
end_io_cache(&log_file);
if (mysql_file_sync(log_file.file, MYF(MY_WME)) && ! write_error)
{
char errbuf[MYSYS_STRERROR_SIZE];
write_error= 1;
sql_print_error(ER_DEFAULT(ER_ERROR_ON_WRITE), name, errno,
my_strerror(errbuf, sizeof(errbuf), errno));
}
if (mysql_file_close(log_file.file, MYF(MY_WME)) && ! write_error)
{
char errbuf[MYSYS_STRERROR_SIZE];
write_error= 1;
sql_print_error(ER_DEFAULT(ER_ERROR_ON_WRITE), name, errno,
my_strerror(errbuf, sizeof(errbuf), errno));
}
}
log_state= (exiting & LOG_CLOSE_TO_BE_OPENED) ? LOG_TO_BE_OPENED : LOG_CLOSED;
my_free(name);
name= NULL;
DBUG_VOID_RETURN;
}
/** This is called only once. */
void MYSQL_LOG::cleanup()
{
DBUG_ENTER("cleanup");
if (inited)
{
inited= 0;
mysql_mutex_destroy(&LOCK_log);
close(0);
}
DBUG_VOID_RETURN;
}
int MYSQL_LOG::generate_new_name(char *new_name, const char *log_name,
bool unique)
{
fn_format(new_name, log_name, mysql_data_home, "", 4);
if (unique)
{
if (!fn_ext(log_name)[0])
{
if (find_uniq_filename(new_name, &cur_log_ext))
{
my_printf_error(ER_NO_UNIQUE_LOGFILE, ER(ER_NO_UNIQUE_LOGFILE),
MYF(ME_FATALERROR), log_name);
sql_print_error(ER(ER_NO_UNIQUE_LOGFILE), log_name);
return 1;
}
}
}
return 0;
}
int MYSQL_LOG::purge_up_to(ulong to_ext, const char *log_name)
{
char buff[FN_REFLEN];
int error= 0;
DBUG_ENTER("MYSQL_LOG::purge_up_to");
do {
snprintf(buff, sizeof(buff), "%s.%06lu", name, to_ext);
if ((error= unlink(buff)))
{
if (my_errno == ENOENT)
error= 0;
break;
}
--to_ext;
} while (to_ext > 0);
DBUG_RETURN(error);
}
/*
Reopen the log file
SYNOPSIS
reopen_file()
DESCRIPTION
Reopen the log file. The method is used during FLUSH LOGS
and locks LOCK_log mutex
*/