~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/log.cc

Removed dead variable, sorted authors file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
    Abort logging when we get an error in reading or writing log files
25
25
*/
26
26
 
27
 
#include <drizzled/server_includes.h>
 
27
#include "mysql_priv.h"
28
28
#include "sql_repl.h"
29
29
#include "rpl_filter.h"
30
30
#include "rpl_rli.h"
31
31
 
32
 
#include <mysys/my_dir.h>
 
32
#include <my_dir.h>
33
33
#include <stdarg.h>
 
34
#include <m_ctype.h>                            // For test_if_number
34
35
 
35
 
#include <drizzled/plugin.h>
36
 
#include <drizzled/drizzled_error_messages.h>
37
 
#include <libdrizzle/gettext.h>
 
36
#include <mysql/plugin.h>
38
37
 
39
38
/* max size of the log message */
40
39
#define MAX_LOG_BUFFER_SIZE 1024
46
45
 
47
46
LOGGER logger;
48
47
 
49
 
DRIZZLE_BIN_LOG mysql_bin_log;
 
48
MYSQL_BIN_LOG mysql_bin_log;
50
49
ulong sync_binlog_counter= 0;
51
50
 
52
51
static bool test_if_number(const char *str,
204
203
 
205
204
 
206
205
/* Check if a given table is opened log table */
207
 
int check_if_log_table(uint32_t db_len __attribute__((unused)),
208
 
                       const char *db __attribute__((unused)),
209
 
                       uint32_t table_name_len __attribute__((unused)),
210
 
                       const char *table_name __attribute__((unused)),
211
 
                       uint32_t check_if_opened __attribute__((unused)))
 
206
int check_if_log_table(uint db_len __attribute__((__unused__)),
 
207
                       const char *db __attribute__((__unused__)),
 
208
                       uint table_name_len __attribute__((__unused__)),
 
209
                       const char *table_name __attribute__((__unused__)),
 
210
                       uint check_if_opened __attribute__((__unused__)))
212
211
{
213
212
  return 0;
214
213
}
215
214
 
 
215
/* log event handlers */
 
216
 
 
217
bool Log_to_file_event_handler::
 
218
  log_error(enum loglevel level, const char *format,
 
219
            va_list args)
 
220
{
 
221
  return vprint_msg_to_log(level, format, args);
 
222
}
 
223
 
 
224
void Log_to_file_event_handler::init_pthread_objects()
 
225
{
 
226
  mysql_log.init_pthread_objects();
 
227
  mysql_slow_log.init_pthread_objects();
 
228
}
 
229
 
 
230
 
 
231
/** Wrapper around MYSQL_LOG::write() for slow log. */
 
232
 
 
233
bool Log_to_file_event_handler::
 
234
  log_slow(THD *thd, time_t current_time, time_t query_start_arg,
 
235
           const char *user_host, uint user_host_len,
 
236
           uint64_t query_utime, uint64_t lock_utime, bool is_command,
 
237
           const char *sql_text, uint sql_text_len)
 
238
{
 
239
  return mysql_slow_log.write(thd, current_time, query_start_arg,
 
240
                              user_host, user_host_len,
 
241
                              query_utime, lock_utime, is_command,
 
242
                              sql_text, sql_text_len);
 
243
}
 
244
 
 
245
 
 
246
/**
 
247
   Wrapper around MYSQL_LOG::write() for general log. We need it since we
 
248
   want all log event handlers to have the same signature.
 
249
*/
 
250
 
 
251
bool Log_to_file_event_handler::
 
252
  log_general(THD *thd __attribute__((__unused__)),
 
253
              time_t event_time, const char *user_host,
 
254
              uint user_host_len, int thread_id,
 
255
              const char *command_type, uint command_type_len,
 
256
              const char *sql_text, uint sql_text_len,
 
257
              CHARSET_INFO *client_cs __attribute__((__unused__)))
 
258
{
 
259
  return mysql_log.write(event_time, user_host, user_host_len,
 
260
                         thread_id, command_type, command_type_len,
 
261
                         sql_text, sql_text_len);
 
262
}
 
263
 
 
264
 
 
265
bool Log_to_file_event_handler::init()
 
266
{
 
267
  if (!is_initialized)
 
268
  {
 
269
    if (opt_slow_log)
 
270
      mysql_slow_log.open_slow_log(sys_var_slow_log_path.value);
 
271
 
 
272
    if (opt_log)
 
273
      mysql_log.open_query_log(sys_var_general_log_path.value);
 
274
 
 
275
    is_initialized= true;
 
276
  }
 
277
 
 
278
  return false;
 
279
}
 
280
 
 
281
 
 
282
void Log_to_file_event_handler::cleanup()
 
283
{
 
284
  mysql_log.cleanup();
 
285
  mysql_slow_log.cleanup();
 
286
}
 
287
 
 
288
void Log_to_file_event_handler::flush()
 
289
{
 
290
  /* reopen log files */
 
291
  if (opt_log)
 
292
    mysql_log.reopen_file();
 
293
  if (opt_slow_log)
 
294
    mysql_slow_log.reopen_file();
 
295
}
 
296
 
216
297
/*
217
298
  Log error with all enabled log event handlers
218
299
 
247
328
{
248
329
  assert(inited == 1);
249
330
  rwlock_destroy(&LOCK_logger);
 
331
  if (file_log_handler)
 
332
    file_log_handler->cleanup();
250
333
}
251
334
 
252
335
 
253
336
void LOGGER::cleanup_end()
254
337
{
255
338
  assert(inited == 1);
 
339
  if (file_log_handler)
 
340
    delete file_log_handler;
256
341
}
257
342
 
258
343
 
265
350
  assert(inited == 0);
266
351
  inited= 1;
267
352
 
 
353
  /*
 
354
    Here we create file log handler. We don't do it for the table log handler
 
355
    here as it cannot be created so early. The reason is THD initialization,
 
356
    which depends on the system variables (parsed later).
 
357
  */
 
358
  if (!file_log_handler)
 
359
    file_log_handler= new Log_to_file_event_handler;
 
360
 
268
361
  /* by default we use traditional error log */
269
362
  init_error_log(LOG_FILE);
270
363
 
 
364
  file_log_handler->init_pthread_objects();
271
365
  my_rwlock_init(&LOCK_logger, NULL);
272
366
}
273
367
 
274
368
 
275
 
bool LOGGER::flush_logs(THD *thd __attribute__((unused)))
 
369
bool LOGGER::flush_logs(THD *thd __attribute__((__unused__)))
276
370
{
277
371
  int rc= 0;
278
372
 
282
376
  */
283
377
  logger.lock_exclusive();
284
378
 
 
379
  /* reopen log files */
 
380
  file_log_handler->flush();
 
381
 
285
382
  /* end of log flush */
286
383
  logger.unlock();
287
384
  return rc;
288
385
}
289
386
 
290
 
void LOGGER::init_error_log(uint32_t error_log_printer)
 
387
 
 
388
/*
 
389
  Log slow query with all enabled log event handlers
 
390
 
 
391
  SYNOPSIS
 
392
    slow_log_print()
 
393
 
 
394
    thd                 THD of the query being logged
 
395
    query               The query being logged
 
396
    query_length        The length of the query string
 
397
    current_utime       Current time in microseconds (from undefined start)
 
398
 
 
399
  RETURN
 
400
    FALSE   OK
 
401
    TRUE    error occured
 
402
*/
 
403
 
 
404
bool LOGGER::slow_log_print(THD *thd, const char *query, uint query_length,
 
405
                            uint64_t current_utime)
 
406
 
 
407
{
 
408
  bool error= false;
 
409
  Log_event_handler **current_handler;
 
410
  bool is_command= false;
 
411
  char user_host_buff[MAX_USER_HOST_SIZE];
 
412
  Security_context *sctx= thd->security_ctx;
 
413
  uint user_host_len= 0;
 
414
  uint64_t query_utime, lock_utime;
 
415
 
 
416
  /*
 
417
    Print the message to the buffer if we have slow log enabled
 
418
  */
 
419
 
 
420
  if (*slow_log_handler_list)
 
421
  {
 
422
    time_t current_time;
 
423
 
 
424
    /* do not log slow queries from replication threads */
 
425
    if (thd->slave_thread && !opt_log_slow_slave_statements)
 
426
      return 0;
 
427
 
 
428
    lock_shared();
 
429
    if (!opt_slow_log)
 
430
    {
 
431
      unlock();
 
432
      return 0;
 
433
    }
 
434
 
 
435
    /* fill in user_host value: the format is "%s[%s] @ %s [%s]" */
 
436
    user_host_len= (strxnmov(user_host_buff, MAX_USER_HOST_SIZE,
 
437
                             sctx->priv_user ? sctx->priv_user : "", "[",
 
438
                             sctx->user ? sctx->user : "", "] @ ",
 
439
                             sctx->host ? sctx->host : "", " [",
 
440
                             sctx->ip ? sctx->ip : "", "]", NullS) -
 
441
                    user_host_buff);
 
442
 
 
443
    current_time= my_time_possible_from_micro(current_utime);
 
444
    if (thd->start_utime)
 
445
    {
 
446
      query_utime= (current_utime - thd->start_utime);
 
447
      lock_utime=  (thd->utime_after_lock - thd->start_utime);
 
448
    }
 
449
    else
 
450
    {
 
451
      query_utime= lock_utime= 0;
 
452
    }
 
453
 
 
454
    if (!query)
 
455
    {
 
456
      is_command= true;
 
457
      query= command_name[thd->command].str;
 
458
      query_length= command_name[thd->command].length;
 
459
    }
 
460
 
 
461
    for (current_handler= slow_log_handler_list; *current_handler ;)
 
462
      error= (*current_handler++)->log_slow(thd, current_time, thd->start_time,
 
463
                                            user_host_buff, user_host_len,
 
464
                                            query_utime, lock_utime, is_command,
 
465
                                            query, query_length) || error;
 
466
 
 
467
    unlock();
 
468
  }
 
469
  return error;
 
470
}
 
471
 
 
472
bool LOGGER::general_log_write(THD *thd, enum enum_server_command command,
 
473
                               const char *query, uint query_length)
 
474
{
 
475
  bool error= false;
 
476
  Log_event_handler **current_handler= general_log_handler_list;
 
477
  char user_host_buff[MAX_USER_HOST_SIZE];
 
478
  Security_context *sctx= thd->security_ctx;
 
479
  ulong id;
 
480
  uint user_host_len= 0;
 
481
  time_t current_time;
 
482
 
 
483
  if (thd)
 
484
    id= thd->thread_id;                 /* Normal thread */
 
485
  else
 
486
    id= 0;                              /* Log from connect handler */
 
487
 
 
488
  lock_shared();
 
489
  if (!opt_log)
 
490
  {
 
491
    unlock();
 
492
    return 0;
 
493
  }
 
494
  user_host_len= strxnmov(user_host_buff, MAX_USER_HOST_SIZE,
 
495
                          sctx->priv_user ? sctx->priv_user : "", "[",
 
496
                          sctx->user ? sctx->user : "", "] @ ",
 
497
                          sctx->host ? sctx->host : "", " [",
 
498
                          sctx->ip ? sctx->ip : "", "]", NullS) -
 
499
                                                          user_host_buff;
 
500
 
 
501
  current_time= my_time(0);
 
502
 
 
503
  while (*current_handler)
 
504
    error|= (*current_handler++)->
 
505
      log_general(thd, current_time, user_host_buff,
 
506
                  user_host_len, id,
 
507
                  command_name[(uint) command].str,
 
508
                  command_name[(uint) command].length,
 
509
                  query, query_length,
 
510
                  thd->variables.character_set_client) || error;
 
511
  unlock();
 
512
 
 
513
  return error;
 
514
}
 
515
 
 
516
bool LOGGER::general_log_print(THD *thd, enum enum_server_command command,
 
517
                               const char *format, va_list args)
 
518
{
 
519
  uint message_buff_len= 0;
 
520
  char message_buff[MAX_LOG_BUFFER_SIZE];
 
521
 
 
522
  /* prepare message */
 
523
  if (format)
 
524
    message_buff_len= vsnprintf(message_buff, sizeof(message_buff),
 
525
                                   format, args);
 
526
  else
 
527
    message_buff[0]= '\0';
 
528
 
 
529
  return general_log_write(thd, command, message_buff, message_buff_len);
 
530
}
 
531
 
 
532
void LOGGER::init_error_log(uint error_log_printer)
291
533
{
292
534
  if (error_log_printer & LOG_NONE)
293
535
  {
295
537
    return;
296
538
  }
297
539
 
298
 
}
299
 
 
300
 
int LOGGER::set_handlers(uint32_t error_log_printer)
 
540
  switch (error_log_printer) {
 
541
  case LOG_FILE:
 
542
    error_log_handler_list[0]= file_log_handler;
 
543
    error_log_handler_list[1]= 0;
 
544
    break;
 
545
    /* these two are disabled for now */
 
546
  case LOG_TABLE:
 
547
    assert(0);
 
548
    break;
 
549
  case LOG_TABLE|LOG_FILE:
 
550
    assert(0);
 
551
    break;
 
552
  }
 
553
}
 
554
 
 
555
void LOGGER::init_slow_log(uint slow_log_printer)
 
556
{
 
557
  if (slow_log_printer & LOG_NONE)
 
558
  {
 
559
    slow_log_handler_list[0]= 0;
 
560
    return;
 
561
  }
 
562
 
 
563
  slow_log_handler_list[0]= file_log_handler;
 
564
  slow_log_handler_list[1]= 0;
 
565
}
 
566
 
 
567
void LOGGER::init_general_log(uint general_log_printer)
 
568
{
 
569
  if (general_log_printer & LOG_NONE)
 
570
  {
 
571
    general_log_handler_list[0]= 0;
 
572
    return;
 
573
  }
 
574
 
 
575
  general_log_handler_list[0]= file_log_handler;
 
576
  general_log_handler_list[1]= 0;
 
577
}
 
578
 
 
579
 
 
580
bool LOGGER::activate_log_handler(THD* thd __attribute__((__unused__)),
 
581
                                  uint log_type)
 
582
{
 
583
  MYSQL_QUERY_LOG *file_log;
 
584
  bool res= false;
 
585
  lock_exclusive();
 
586
  switch (log_type) {
 
587
  case QUERY_LOG_SLOW:
 
588
    if (!opt_slow_log)
 
589
    {
 
590
      file_log= file_log_handler->get_mysql_slow_log();
 
591
 
 
592
      file_log->open_slow_log(sys_var_slow_log_path.value);
 
593
      init_slow_log(log_output_options);
 
594
      opt_slow_log= true;
 
595
    }
 
596
    break;
 
597
  case QUERY_LOG_GENERAL:
 
598
    if (!opt_log)
 
599
    {
 
600
      file_log= file_log_handler->get_mysql_log();
 
601
 
 
602
      file_log->open_query_log(sys_var_general_log_path.value);
 
603
      init_general_log(log_output_options);
 
604
      opt_log= true;
 
605
    }
 
606
    break;
 
607
  default:
 
608
    assert(0);
 
609
  }
 
610
  unlock();
 
611
  return res;
 
612
}
 
613
 
 
614
 
 
615
void LOGGER::deactivate_log_handler(THD *thd __attribute__((__unused__)),
 
616
                                    uint log_type)
 
617
{
 
618
  bool *tmp_opt= 0;
 
619
  MYSQL_LOG *file_log;
 
620
 
 
621
  switch (log_type) {
 
622
  case QUERY_LOG_SLOW:
 
623
    tmp_opt= &opt_slow_log;
 
624
    file_log= file_log_handler->get_mysql_slow_log();
 
625
    break;
 
626
  case QUERY_LOG_GENERAL:
 
627
    tmp_opt= &opt_log;
 
628
    file_log= file_log_handler->get_mysql_log();
 
629
    break;
 
630
  default:
 
631
    assert(0);                                  // Impossible
 
632
  }
 
633
 
 
634
  if (!(*tmp_opt))
 
635
    return;
 
636
 
 
637
  lock_exclusive();
 
638
  file_log->close(0);
 
639
  *tmp_opt= false;
 
640
  unlock();
 
641
}
 
642
 
 
643
int LOGGER::set_handlers(uint error_log_printer,
 
644
                         uint slow_log_printer,
 
645
                         uint general_log_printer)
301
646
{
302
647
  /* error log table is not supported yet */
 
648
  assert(error_log_printer < LOG_TABLE);
 
649
 
303
650
  lock_exclusive();
304
651
 
305
652
  init_error_log(error_log_printer);
 
653
  init_slow_log(slow_log_printer);
 
654
  init_general_log(general_log_printer);
 
655
 
306
656
  unlock();
307
657
 
308
658
  return 0;
369
719
 
370
720
/*
371
721
  this function is mostly a placeholder.
372
 
  conceptually, binlog initialization (now mostly done in DRIZZLE_BIN_LOG::open)
 
722
  conceptually, binlog initialization (now mostly done in MYSQL_BIN_LOG::open)
373
723
  should be moved here.
374
724
*/
375
725
 
377
727
{
378
728
  binlog_hton= (handlerton *)p;
379
729
  binlog_hton->state=opt_bin_log ? SHOW_OPTION_YES : SHOW_OPTION_NO;
 
730
  binlog_hton->db_type=DB_TYPE_BINLOG;
380
731
  binlog_hton->savepoint_offset= sizeof(my_off_t);
381
732
  binlog_hton->close_connection= binlog_close_connection;
382
733
  binlog_hton->savepoint_set= binlog_savepoint_set;
385
736
  binlog_hton->rollback= binlog_rollback;
386
737
  binlog_hton->prepare= binlog_prepare;
387
738
  binlog_hton->flags= HTON_NOT_USER_SELECTABLE | HTON_HIDDEN;
388
 
 
389
739
  return 0;
390
740
}
391
741
 
392
 
static int binlog_close_connection(handlerton *hton __attribute__((unused)),
 
742
static int binlog_close_connection(handlerton *hton __attribute__((__unused__)),
393
743
                                   THD *thd)
394
744
{
395
745
  binlog_trx_data *const trx_data=
397
747
  assert(trx_data->empty());
398
748
  thd_set_ha_data(thd, binlog_hton, NULL);
399
749
  trx_data->~binlog_trx_data();
400
 
  free((unsigned char*)trx_data);
 
750
  my_free((uchar*)trx_data, MYF(0));
401
751
  return 0;
402
752
}
403
753
 
496
846
  return(error);
497
847
}
498
848
 
499
 
static int binlog_prepare(handlerton *hton __attribute__((unused)),
500
 
                          THD *thd __attribute__((unused)),
501
 
                          bool all __attribute__((unused)))
 
849
static int binlog_prepare(handlerton *hton __attribute__((__unused__)),
 
850
                          THD *thd __attribute__((__unused__)),
 
851
                          bool all __attribute__((__unused__)))
502
852
{
503
853
  /*
504
854
    do nothing.
505
855
    just pretend we can do 2pc, so that MySQL won't
506
856
    switch to 1pc.
507
 
    real work will be done in DRIZZLE_BIN_LOG::log_xid()
 
857
    real work will be done in MYSQL_BIN_LOG::log_xid()
508
858
  */
509
859
  return 0;
510
860
}
524
874
 
525
875
  @see handlerton::commit
526
876
*/
527
 
static int binlog_commit(handlerton *hton __attribute__((unused)),
 
877
static int binlog_commit(handlerton *hton __attribute__((__unused__)),
528
878
                         THD *thd, bool all)
529
879
{
530
880
  binlog_trx_data *const trx_data=
532
882
 
533
883
  if (trx_data->empty())
534
884
  {
535
 
    // we're here because trans_log was flushed in DRIZZLE_BIN_LOG::log_xid()
 
885
    // we're here because trans_log was flushed in MYSQL_BIN_LOG::log_xid()
536
886
    trx_data->reset();
537
887
    return(0);
538
888
  }
600
950
  if ((in_transaction && (all || (!trx_data->at_least_one_stmt && thd->transaction.stmt.modified_non_trans_table))) || (!in_transaction && !all))
601
951
  {
602
952
    Query_log_event qev(thd, STRING_WITH_LEN("COMMIT"), true, false);
603
 
    qev.error_code= 0; // see comment in DRIZZLE_LOG::write(THD, IO_CACHE)
 
953
    qev.error_code= 0; // see comment in MYSQL_LOG::write(THD, IO_CACHE)
604
954
    int error= binlog_end_trans(thd, trx_data, &qev, all);
605
955
    return(error);
606
956
  }
622
972
 
623
973
  @see handlerton::rollback
624
974
*/
625
 
static int binlog_rollback(handlerton *hton __attribute__((unused)),
 
975
static int binlog_rollback(handlerton *hton __attribute__((__unused__)),
626
976
                           THD *thd, bool all)
627
977
{
628
978
  int error=0;
647
997
      rolled back on the slave.
648
998
    */
649
999
    Query_log_event qev(thd, STRING_WITH_LEN("ROLLBACK"), true, false);
650
 
    qev.error_code= 0; // see comment in DRIZZLE_LOG::write(THD, IO_CACHE)
 
1000
    qev.error_code= 0; // see comment in MYSQL_LOG::write(THD, IO_CACHE)
651
1001
    error= binlog_end_trans(thd, trx_data, &qev, all);
652
1002
  }
653
1003
  else if ((all && !thd->transaction.all.modified_non_trans_table) ||
687
1037
  that case there is no need to have it in the binlog).
688
1038
*/
689
1039
 
690
 
static int binlog_savepoint_set(handlerton *hton __attribute__((unused)),
 
1040
static int binlog_savepoint_set(handlerton *hton __attribute__((__unused__)),
691
1041
                                THD *thd, void *sv)
692
1042
{
693
1043
  binlog_trans_log_savepos(thd, (my_off_t*) sv);
699
1049
  return(error);
700
1050
}
701
1051
 
702
 
static int binlog_savepoint_rollback(handlerton *hton __attribute__((unused)),
 
1052
static int binlog_savepoint_rollback(handlerton *hton __attribute__((__unused__)),
703
1053
                                     THD *thd, void *sv)
704
1054
{
705
1055
  /*
725
1075
  char magic[4];
726
1076
  assert(my_b_tell(log) == 0);
727
1077
 
728
 
  if (my_b_read(log, (unsigned char*) magic, sizeof(magic)))
 
1078
  if (my_b_read(log, (uchar*) magic, sizeof(magic)))
729
1079
  {
730
 
    *errmsg = _("I/O error reading the header from the binary log");
 
1080
    *errmsg = "I/O error reading the header from the binary log";
731
1081
    sql_print_error("%s, errno=%d, io cache code=%d", *errmsg, my_errno,
732
1082
                    log->error);
733
1083
    return 1;
734
1084
  }
735
1085
  if (memcmp(magic, BINLOG_MAGIC, sizeof(magic)))
736
1086
  {
737
 
    *errmsg = _("Binlog has bad magic number;  It's not a binary log file "
738
 
                "that can be used by this version of Drizzle");
 
1087
    *errmsg = "Binlog has bad magic number;  It's not a binary log file that can be used by this version of MySQL";
739
1088
    return 1;
740
1089
  }
741
1090
  return 0;
746
1095
{
747
1096
  File file;
748
1097
 
749
 
  if ((file = my_open(log_file_name, O_RDONLY, 
 
1098
  if ((file = my_open(log_file_name, O_RDONLY | O_BINARY | O_SHARE, 
750
1099
                      MYF(MY_WME))) < 0)
751
1100
  {
752
 
    sql_print_error(_("Failed to open log (file '%s', errno %d)"),
 
1101
    sql_print_error("Failed to open log (file '%s', errno %d)",
753
1102
                    log_file_name, my_errno);
754
 
    *errmsg = _("Could not open log file");
 
1103
    *errmsg = "Could not open log file";
755
1104
    goto err;
756
1105
  }
757
1106
  if (init_io_cache(log, file, IO_SIZE*2, READ_CACHE, 0, 0,
758
1107
                    MYF(MY_WME|MY_DONT_CHECK_FILESIZE)))
759
1108
  {
760
 
    sql_print_error(_("Failed to create a cache on log (file '%s')"),
 
1109
    sql_print_error("Failed to create a cache on log (file '%s')",
761
1110
                    log_file_name);
762
 
    *errmsg = _("Could not open log file");
 
1111
    *errmsg = "Could not open log file";
763
1112
    goto err;
764
1113
  }
765
1114
  if (check_binlog_magic(log,errmsg))
788
1137
static int find_uniq_filename(char *name)
789
1138
{
790
1139
  long                  number;
791
 
  uint32_t                  i;
 
1140
  uint                  i;
792
1141
  char                  buff[FN_REFLEN];
793
1142
  struct st_my_dir     *dir_info;
794
1143
  register struct fileinfo *file_info;
798
1147
 
799
1148
  length= dirname_part(buff, name, &buf_length);
800
1149
  start=  name + length;
801
 
  end= strchr(start, '\0');
 
1150
  end=    strend(start);
802
1151
 
803
1152
  *end='.';
804
1153
  length= (size_t) (end-start+1);
805
1154
 
806
1155
  if (!(dir_info = my_dir(buff,MYF(MY_DONT_SORT))))
807
1156
  {                                             // This shouldn't happen
808
 
    my_stpcpy(end,".1");                                // use name+1
 
1157
    strmov(end,".1");                           // use name+1
809
1158
    return(0);
810
1159
  }
811
1160
  file_info= dir_info->dir_entry;
812
1161
  for (i=dir_info->number_off_files ; i-- ; file_info++)
813
1162
  {
814
 
    if (memcmp(file_info->name, start, length) == 0 &&
 
1163
    if (bcmp((uchar*) file_info->name, (uchar*) start, length) == 0 &&
815
1164
        test_if_number(file_info->name+length, &number,0))
816
1165
    {
817
1166
      set_if_bigger(max_found,(ulong) number);
825
1174
}
826
1175
 
827
1176
 
828
 
void DRIZZLE_LOG::init(enum_log_type log_type_arg,
 
1177
void MYSQL_LOG::init(enum_log_type log_type_arg,
829
1178
                     enum cache_type io_cache_type_arg)
830
1179
{
831
1180
  log_type= log_type_arg;
855
1204
    1   error
856
1205
*/
857
1206
 
858
 
bool DRIZZLE_LOG::open(const char *log_name, enum_log_type log_type_arg,
 
1207
bool MYSQL_LOG::open(const char *log_name, enum_log_type log_type_arg,
859
1208
                     const char *new_name, enum cache_type io_cache_type_arg)
860
1209
{
861
1210
  char buff[FN_REFLEN];
862
1211
  File file= -1;
863
 
  int open_flags= O_CREAT;
 
1212
  int open_flags= O_CREAT | O_BINARY;
864
1213
 
865
1214
  write_error= 0;
866
1215
 
873
1222
  }
874
1223
 
875
1224
  if (new_name)
876
 
    my_stpcpy(log_file_name, new_name);
 
1225
    strmov(log_file_name, new_name);
877
1226
  else if (generate_new_name(log_file_name, name))
878
1227
    goto err;
879
1228
 
897
1246
    char *end;
898
1247
    int len=snprintf(buff, sizeof(buff), "%s, Version: %s (%s). "
899
1248
                     "started with:\nTCP Port: %d, Named Pipe: %s\n",
900
 
                     my_progname, server_version, DRIZZLE_COMPILATION_COMMENT,
 
1249
                     my_progname, server_version, MYSQL_COMPILATION_COMMENT,
901
1250
                     mysqld_port, ""
902
1251
                     );
903
 
    end= my_stpncpy(buff + len, "Time                 Id Command    Argument\n",
 
1252
    end= strnmov(buff + len, "Time                 Id Command    Argument\n",
904
1253
                 sizeof(buff) - len);
905
 
    if (my_b_write(&log_file, (unsigned char*) buff, (uint) (end-buff)) ||
 
1254
    if (my_b_write(&log_file, (uchar*) buff, (uint) (end-buff)) ||
906
1255
        flush_io_cache(&log_file))
907
1256
      goto err;
908
1257
  }
911
1260
  return(0);
912
1261
 
913
1262
err:
914
 
  sql_print_error(_("Could not use %s for logging (error %d). "
915
 
                    "Turning logging off for the whole duration of the "
916
 
                    "Drizzle server process. "
917
 
                    "To turn it on again: fix the cause, "
918
 
                    "shutdown the Drizzle server and restart it."),
919
 
                    name, errno);
 
1263
  sql_print_error("Could not use %s for logging (error %d). \
 
1264
Turning logging off for the whole duration of the MySQL server process. \
 
1265
To turn it on again: fix the cause, \
 
1266
shutdown the MySQL server and restart it.", name, errno);
920
1267
  if (file >= 0)
921
1268
    my_close(file, MYF(0));
922
1269
  end_io_cache(&log_file);
923
 
  if (name)
924
 
  {
925
 
    free(name);
926
 
    name= NULL;
927
 
  }
 
1270
  safeFree(name);
928
1271
  log_state= LOG_CLOSED;
929
1272
  return(1);
930
1273
}
931
1274
 
932
 
DRIZZLE_LOG::DRIZZLE_LOG()
 
1275
MYSQL_LOG::MYSQL_LOG()
933
1276
  : name(0), write_error(false), inited(false), log_type(LOG_UNKNOWN),
934
1277
    log_state(LOG_CLOSED)
935
1278
{
939
1282
    called only in main(). Doing initialization here would make it happen
940
1283
    before main().
941
1284
  */
942
 
  memset(&log_file, 0, sizeof(log_file));
 
1285
  bzero((char*) &log_file, sizeof(log_file));
943
1286
}
944
1287
 
945
 
void DRIZZLE_LOG::init_pthread_objects()
 
1288
void MYSQL_LOG::init_pthread_objects()
946
1289
{
947
1290
  assert(inited == 0);
948
1291
  inited= 1;
963
1306
    The internal structures are not freed until cleanup() is called
964
1307
*/
965
1308
 
966
 
void DRIZZLE_LOG::close(uint32_t exiting)
 
1309
void MYSQL_LOG::close(uint exiting)
967
1310
{                                       // One can't set log_type here!
968
1311
  if (log_state == LOG_OPENED)
969
1312
  {
983
1326
  }
984
1327
 
985
1328
  log_state= (exiting & LOG_CLOSE_TO_BE_OPENED) ? LOG_TO_BE_OPENED : LOG_CLOSED;
986
 
  if (name)
987
 
  {
988
 
    free(name);
989
 
    name= NULL;
990
 
  }
 
1329
  safeFree(name);
991
1330
  return;
992
1331
}
993
1332
 
994
1333
/** This is called only once. */
995
1334
 
996
 
void DRIZZLE_LOG::cleanup()
 
1335
void MYSQL_LOG::cleanup()
997
1336
{
998
1337
  if (inited)
999
1338
  {
1005
1344
}
1006
1345
 
1007
1346
 
1008
 
int DRIZZLE_LOG::generate_new_name(char *new_name, const char *log_name)
 
1347
int MYSQL_LOG::generate_new_name(char *new_name, const char *log_name)
1009
1348
{
1010
1349
  fn_format(new_name, log_name, mysql_data_home, "", 4);
1011
1350
  if (log_type == LOG_BIN)
1023
1362
}
1024
1363
 
1025
1364
 
 
1365
/*
 
1366
  Reopen the log file
 
1367
 
 
1368
  SYNOPSIS
 
1369
    reopen_file()
 
1370
 
 
1371
  DESCRIPTION
 
1372
    Reopen the log file. The method is used during FLUSH LOGS
 
1373
    and locks LOCK_log mutex
 
1374
*/
 
1375
 
 
1376
 
 
1377
void MYSQL_QUERY_LOG::reopen_file()
 
1378
{
 
1379
  char *save_name;
 
1380
 
 
1381
  if (!is_open())
 
1382
  {
 
1383
    return;
 
1384
  }
 
1385
 
 
1386
  pthread_mutex_lock(&LOCK_log);
 
1387
 
 
1388
  save_name= name;
 
1389
  name= 0;                              // Don't free name
 
1390
  close(LOG_CLOSE_TO_BE_OPENED);
 
1391
 
 
1392
  /*
 
1393
     Note that at this point, log_state != LOG_CLOSED (important for is_open()).
 
1394
  */
 
1395
 
 
1396
  open(save_name, log_type, 0, io_cache_type);
 
1397
  my_free(save_name, MYF(0));
 
1398
 
 
1399
  pthread_mutex_unlock(&LOCK_log);
 
1400
 
 
1401
  return;
 
1402
}
 
1403
 
 
1404
 
 
1405
/*
 
1406
  Write a command to traditional general log file
 
1407
 
 
1408
  SYNOPSIS
 
1409
    write()
 
1410
 
 
1411
    event_time        command start timestamp
 
1412
    user_host         the pointer to the string with user@host info
 
1413
    user_host_len     length of the user_host string. this is computed once
 
1414
                      and passed to all general log  event handlers
 
1415
    thread_id         Id of the thread, issued a query
 
1416
    command_type      the type of the command being logged
 
1417
    command_type_len  the length of the string above
 
1418
    sql_text          the very text of the query being executed
 
1419
    sql_text_len      the length of sql_text string
 
1420
 
 
1421
  DESCRIPTION
 
1422
 
 
1423
   Log given command to to normal (not rotable) log file
 
1424
 
 
1425
  RETURN
 
1426
    FASE - OK
 
1427
    TRUE - error occured
 
1428
*/
 
1429
 
 
1430
bool MYSQL_QUERY_LOG::write(time_t event_time,
 
1431
                            const char *user_host __attribute__((__unused__)),
 
1432
                            uint user_host_len __attribute__((__unused__)),
 
1433
                            int thread_id,
 
1434
                            const char *command_type, uint command_type_len,
 
1435
                            const char *sql_text, uint sql_text_len)
 
1436
{
 
1437
  char buff[32];
 
1438
  uint length= 0;
 
1439
  char local_time_buff[MAX_TIME_SIZE];
 
1440
  struct tm start;
 
1441
  uint time_buff_len= 0;
 
1442
 
 
1443
  (void) pthread_mutex_lock(&LOCK_log);
 
1444
 
 
1445
  /* Test if someone closed between the is_open test and lock */
 
1446
  if (is_open())
 
1447
  {
 
1448
    /* Note that my_b_write() assumes it knows the length for this */
 
1449
      if (event_time != last_time)
 
1450
      {
 
1451
        last_time= event_time;
 
1452
 
 
1453
        localtime_r(&event_time, &start);
 
1454
 
 
1455
        time_buff_len= snprintf(local_time_buff, MAX_TIME_SIZE,
 
1456
                                "%02d%02d%02d %2d:%02d:%02d",
 
1457
                                start.tm_year % 100, start.tm_mon + 1,
 
1458
                                start.tm_mday, start.tm_hour,
 
1459
                                start.tm_min, start.tm_sec);
 
1460
 
 
1461
        if (my_b_write(&log_file, (uchar*) local_time_buff, time_buff_len))
 
1462
          goto err;
 
1463
      }
 
1464
      else
 
1465
        if (my_b_write(&log_file, (uchar*) "\t\t" ,2) < 0)
 
1466
          goto err;
 
1467
 
 
1468
      /* command_type, thread_id */
 
1469
      length= snprintf(buff, 32, "%5ld ", (long) thread_id);
 
1470
 
 
1471
    if (my_b_write(&log_file, (uchar*) buff, length))
 
1472
      goto err;
 
1473
 
 
1474
    if (my_b_write(&log_file, (uchar*) command_type, command_type_len))
 
1475
      goto err;
 
1476
 
 
1477
    if (my_b_write(&log_file, (uchar*) "\t", 1))
 
1478
      goto err;
 
1479
 
 
1480
    /* sql_text */
 
1481
    if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len))
 
1482
      goto err;
 
1483
 
 
1484
    if (my_b_write(&log_file, (uchar*) "\n", 1) ||
 
1485
        flush_io_cache(&log_file))
 
1486
      goto err;
 
1487
  }
 
1488
 
 
1489
  (void) pthread_mutex_unlock(&LOCK_log);
 
1490
  return false;
 
1491
err:
 
1492
 
 
1493
  if (!write_error)
 
1494
  {
 
1495
    write_error= 1;
 
1496
    sql_print_error(ER(ER_ERROR_ON_WRITE), name, errno);
 
1497
  }
 
1498
  (void) pthread_mutex_unlock(&LOCK_log);
 
1499
  return true;
 
1500
}
 
1501
 
 
1502
 
 
1503
/*
 
1504
  Log a query to the traditional slow log file
 
1505
 
 
1506
  SYNOPSIS
 
1507
    write()
 
1508
 
 
1509
    thd               THD of the query
 
1510
    current_time      current timestamp
 
1511
    query_start_arg   command start timestamp
 
1512
    user_host         the pointer to the string with user@host info
 
1513
    user_host_len     length of the user_host string. this is computed once
 
1514
                      and passed to all general log event handlers
 
1515
    query_utime       Amount of time the query took to execute (in microseconds)
 
1516
    lock_utime        Amount of time the query was locked (in microseconds)
 
1517
    is_command        The flag, which determines, whether the sql_text is a
 
1518
                      query or an administrator command.
 
1519
    sql_text          the very text of the query or administrator command
 
1520
                      processed
 
1521
    sql_text_len      the length of sql_text string
 
1522
 
 
1523
  DESCRIPTION
 
1524
 
 
1525
   Log a query to the slow log file.
 
1526
 
 
1527
  RETURN
 
1528
    FALSE - OK
 
1529
    TRUE - error occured
 
1530
*/
 
1531
 
 
1532
bool MYSQL_QUERY_LOG::write(THD *thd, time_t current_time,
 
1533
                            time_t query_start_arg __attribute__((__unused__)),
 
1534
                            const char *user_host,
 
1535
                            uint user_host_len, uint64_t query_utime,
 
1536
                            uint64_t lock_utime, bool is_command,
 
1537
                            const char *sql_text, uint sql_text_len)
 
1538
{
 
1539
  bool error= 0;
 
1540
 
 
1541
  (void) pthread_mutex_lock(&LOCK_log);
 
1542
 
 
1543
  if (!is_open())
 
1544
  {
 
1545
    (void) pthread_mutex_unlock(&LOCK_log);
 
1546
    return(0);
 
1547
  }
 
1548
 
 
1549
  if (is_open())
 
1550
  {                                             // Safety agains reopen
 
1551
    int tmp_errno= 0;
 
1552
    char buff[80], *end;
 
1553
    char query_time_buff[22+7], lock_time_buff[22+7];
 
1554
    uint buff_len;
 
1555
    end= buff;
 
1556
 
 
1557
    if (!(specialflag & SPECIAL_SHORT_LOG_FORMAT))
 
1558
    {
 
1559
      if (current_time != last_time)
 
1560
      {
 
1561
        last_time= current_time;
 
1562
        struct tm start;
 
1563
        localtime_r(&current_time, &start);
 
1564
 
 
1565
        buff_len= snprintf(buff, sizeof buff,
 
1566
                           "# Time: %02d%02d%02d %2d:%02d:%02d\n",
 
1567
                           start.tm_year % 100, start.tm_mon + 1,
 
1568
                           start.tm_mday, start.tm_hour,
 
1569
                           start.tm_min, start.tm_sec);
 
1570
 
 
1571
        /* Note that my_b_write() assumes it knows the length for this */
 
1572
        if (my_b_write(&log_file, (uchar*) buff, buff_len))
 
1573
          tmp_errno= errno;
 
1574
      }
 
1575
      const uchar uh[]= "# User@Host: ";
 
1576
      if (my_b_write(&log_file, uh, sizeof(uh) - 1))
 
1577
        tmp_errno= errno;
 
1578
      if (my_b_write(&log_file, (uchar*) user_host, user_host_len))
 
1579
        tmp_errno= errno;
 
1580
      if (my_b_write(&log_file, (uchar*) "\n", 1))
 
1581
        tmp_errno= errno;
 
1582
    }
 
1583
    /* For slow query log */
 
1584
    sprintf(query_time_buff, "%.6f", uint64_t2double(query_utime)/1000000.0);
 
1585
    sprintf(lock_time_buff,  "%.6f", uint64_t2double(lock_utime)/1000000.0);
 
1586
    if (my_b_printf(&log_file,
 
1587
                    "# Query_time: %s  Lock_time: %s"
 
1588
                    " Rows_sent: %lu  Rows_examined: %lu\n",
 
1589
                    query_time_buff, lock_time_buff,
 
1590
                    (ulong) thd->sent_row_count,
 
1591
                    (ulong) thd->examined_row_count) == (uint) -1)
 
1592
      tmp_errno= errno;
 
1593
    if (thd->db && strcmp(thd->db, db))
 
1594
    {                                           // Database changed
 
1595
      if (my_b_printf(&log_file,"use %s;\n",thd->db) == (uint) -1)
 
1596
        tmp_errno= errno;
 
1597
      strmov(db,thd->db);
 
1598
    }
 
1599
    if (thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt)
 
1600
    {
 
1601
      end=strmov(end, ",last_insert_id=");
 
1602
      end=int64_t10_to_str((int64_t)
 
1603
                            thd->first_successful_insert_id_in_prev_stmt_for_binlog,
 
1604
                            end, -10);
 
1605
    }
 
1606
    // Save value if we do an insert.
 
1607
    if (thd->auto_inc_intervals_in_cur_stmt_for_binlog.nb_elements() > 0)
 
1608
    {
 
1609
      if (!(specialflag & SPECIAL_SHORT_LOG_FORMAT))
 
1610
      {
 
1611
        end=strmov(end,",insert_id=");
 
1612
        end=int64_t10_to_str((int64_t)
 
1613
                              thd->auto_inc_intervals_in_cur_stmt_for_binlog.minimum(),
 
1614
                              end, -10);
 
1615
      }
 
1616
    }
 
1617
 
 
1618
    /*
 
1619
      This info used to show up randomly, depending on whether the query
 
1620
      checked the query start time or not. now we always write current
 
1621
      timestamp to the slow log
 
1622
    */
 
1623
    end= strmov(end, ",timestamp=");
 
1624
    end= int10_to_str((long) current_time, end, 10);
 
1625
 
 
1626
    if (end != buff)
 
1627
    {
 
1628
      *end++=';';
 
1629
      *end='\n';
 
1630
      if (my_b_write(&log_file, (uchar*) "SET ", 4) ||
 
1631
          my_b_write(&log_file, (uchar*) buff + 1, (uint) (end-buff)))
 
1632
        tmp_errno= errno;
 
1633
    }
 
1634
    if (is_command)
 
1635
    {
 
1636
      end= strxmov(buff, "# administrator command: ", NullS);
 
1637
      buff_len= (ulong) (end - buff);
 
1638
      my_b_write(&log_file, (uchar*) buff, buff_len);
 
1639
    }
 
1640
    if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len) ||
 
1641
        my_b_write(&log_file, (uchar*) ";\n",2) ||
 
1642
        flush_io_cache(&log_file))
 
1643
      tmp_errno= errno;
 
1644
    if (tmp_errno)
 
1645
    {
 
1646
      error= 1;
 
1647
      if (! write_error)
 
1648
      {
 
1649
        write_error= 1;
 
1650
        sql_print_error(ER(ER_ERROR_ON_WRITE), name, error);
 
1651
      }
 
1652
    }
 
1653
  }
 
1654
  (void) pthread_mutex_unlock(&LOCK_log);
 
1655
  return(error);
 
1656
}
 
1657
 
 
1658
 
1026
1659
/**
1027
1660
  @todo
1028
1661
  The following should be using fn_format();  We just need to
1029
1662
  first change fn_format() to cut the file name if it's too long.
1030
1663
*/
1031
 
const char *DRIZZLE_LOG::generate_name(const char *log_name,
 
1664
const char *MYSQL_LOG::generate_name(const char *log_name,
1032
1665
                                      const char *suffix,
1033
1666
                                      bool strip_ext, char *buff)
1034
1667
{
1042
1675
  if (strip_ext)
1043
1676
  {
1044
1677
    char *p= fn_ext(log_name);
1045
 
    uint32_t length= (uint) (p - log_name);
1046
 
    strmake(buff, log_name, cmin(length, (uint)FN_REFLEN));
 
1678
    uint length= (uint) (p - log_name);
 
1679
    strmake(buff, log_name, min(length, FN_REFLEN));
1047
1680
    return (const char*)buff;
1048
1681
  }
1049
1682
  return log_name;
1051
1684
 
1052
1685
 
1053
1686
 
1054
 
DRIZZLE_BIN_LOG::DRIZZLE_BIN_LOG()
 
1687
MYSQL_BIN_LOG::MYSQL_BIN_LOG()
1055
1688
  :bytes_written(0), prepared_xids(0), file_id(1), open_count(1),
1056
1689
   need_start_event(true), m_table_map_version(0),
1057
1690
   description_event_for_exec(0), description_event_for_queue(0)
1063
1696
    before main().
1064
1697
  */
1065
1698
  index_file_name[0] = 0;
1066
 
  memset(&index_file, 0, sizeof(index_file));
 
1699
  bzero((char*) &index_file, sizeof(index_file));
1067
1700
}
1068
1701
 
1069
1702
/* this is called only once */
1070
1703
 
1071
 
void DRIZZLE_BIN_LOG::cleanup()
 
1704
void MYSQL_BIN_LOG::cleanup()
1072
1705
{
1073
1706
  if (inited)
1074
1707
  {
1085
1718
 
1086
1719
 
1087
1720
/* Init binlog-specific vars */
1088
 
void DRIZZLE_BIN_LOG::init(bool no_auto_events_arg, ulong max_size_arg)
 
1721
void MYSQL_BIN_LOG::init(bool no_auto_events_arg, ulong max_size_arg)
1089
1722
{
1090
1723
  no_auto_events= no_auto_events_arg;
1091
1724
  max_size= max_size_arg;
1093
1726
}
1094
1727
 
1095
1728
 
1096
 
void DRIZZLE_BIN_LOG::init_pthread_objects()
 
1729
void MYSQL_BIN_LOG::init_pthread_objects()
1097
1730
{
1098
1731
  assert(inited == 0);
1099
1732
  inited= 1;
1103
1736
}
1104
1737
 
1105
1738
 
1106
 
bool DRIZZLE_BIN_LOG::open_index_file(const char *index_file_name_arg,
 
1739
bool MYSQL_BIN_LOG::open_index_file(const char *index_file_name_arg,
1107
1740
                                const char *log_name)
1108
1741
{
1109
1742
  File index_file_nr= -1;
1123
1756
  fn_format(index_file_name, index_file_name_arg, mysql_data_home,
1124
1757
            ".index", opt);
1125
1758
  if ((index_file_nr= my_open(index_file_name,
1126
 
                              O_RDWR | O_CREAT,
 
1759
                              O_RDWR | O_CREAT | O_BINARY ,
1127
1760
                              MYF(MY_WME))) < 0 ||
1128
1761
       my_sync(index_file_nr, MYF(MY_WME)) ||
1129
1762
       init_io_cache(&index_file, index_file_nr,
1158
1791
    1   error
1159
1792
*/
1160
1793
 
1161
 
bool DRIZZLE_BIN_LOG::open(const char *log_name,
 
1794
bool MYSQL_BIN_LOG::open(const char *log_name,
1162
1795
                         enum_log_type log_type_arg,
1163
1796
                         const char *new_name,
1164
1797
                         enum cache_type io_cache_type_arg,
1171
1804
  write_error=0;
1172
1805
 
1173
1806
  /* open the main log file */
1174
 
  if (DRIZZLE_LOG::open(log_name, log_type_arg, new_name, io_cache_type_arg))
 
1807
  if (MYSQL_LOG::open(log_name, log_type_arg, new_name, io_cache_type_arg))
1175
1808
    return(1);                            /* all warnings issued */
1176
1809
 
1177
1810
  init(no_auto_events_arg, max_size_arg);
1191
1824
        an extension for the binary log files.
1192
1825
        In this case we write a standard header to it.
1193
1826
      */
1194
 
      if (my_b_safe_write(&log_file, (unsigned char*) BINLOG_MAGIC,
 
1827
      if (my_b_safe_write(&log_file, (uchar*) BINLOG_MAGIC,
1195
1828
                          BIN_LOG_HEADER_SIZE))
1196
1829
        goto err;
1197
1830
      bytes_written+= BIN_LOG_HEADER_SIZE;
1262
1895
        As this is a new log file, we write the file name to the index
1263
1896
        file. As every time we write to the index file, we sync it.
1264
1897
      */
1265
 
      if (my_b_write(&index_file, (unsigned char*) log_file_name,
 
1898
      if (my_b_write(&index_file, (uchar*) log_file_name,
1266
1899
                     strlen(log_file_name)) ||
1267
 
          my_b_write(&index_file, (unsigned char*) "\n", 1) ||
 
1900
          my_b_write(&index_file, (uchar*) "\n", 1) ||
1268
1901
          flush_io_cache(&index_file) ||
1269
1902
          my_sync(index_file.file, MYF(MY_WME)))
1270
1903
        goto err;
1275
1908
  return(0);
1276
1909
 
1277
1910
err:
1278
 
  sql_print_error(_("Could not use %s for logging (error %d). "
1279
 
                    "Turning logging off for the whole duration of the "
1280
 
                    "Drizzle server process. "
1281
 
                    "To turn it on again: fix the cause, "
1282
 
                    "shutdown the Drizzle server and restart it."),
1283
 
                    name, errno);
 
1911
  sql_print_error("Could not use %s for logging (error %d). \
 
1912
Turning logging off for the whole duration of the MySQL server process. \
 
1913
To turn it on again: fix the cause, \
 
1914
shutdown the MySQL server and restart it.", name, errno);
1284
1915
  if (file >= 0)
1285
1916
    my_close(file,MYF(0));
1286
1917
  end_io_cache(&log_file);
1287
1918
  end_io_cache(&index_file);
1288
 
  if (name)
1289
 
  {
1290
 
    free(name);
1291
 
    name= NULL;
1292
 
  }
 
1919
  safeFree(name);
1293
1920
  log_state= LOG_CLOSED;
1294
1921
  return(1);
1295
1922
}
1296
1923
 
1297
1924
 
1298
 
int DRIZZLE_BIN_LOG::get_current_log(LOG_INFO* linfo)
 
1925
int MYSQL_BIN_LOG::get_current_log(LOG_INFO* linfo)
1299
1926
{
1300
1927
  pthread_mutex_lock(&LOCK_log);
1301
1928
  int ret = raw_get_current_log(linfo);
1303
1930
  return ret;
1304
1931
}
1305
1932
 
1306
 
int DRIZZLE_BIN_LOG::raw_get_current_log(LOG_INFO* linfo)
 
1933
int MYSQL_BIN_LOG::raw_get_current_log(LOG_INFO* linfo)
1307
1934
{
1308
1935
  strmake(linfo->log_file_name, log_file_name, sizeof(linfo->log_file_name)-1);
1309
1936
  linfo->pos = my_b_tell(&log_file);
1327
1954
    0   ok
1328
1955
*/
1329
1956
 
 
1957
#ifdef HAVE_REPLICATION
 
1958
 
1330
1959
static bool copy_up_file_and_fill(IO_CACHE *index_file, my_off_t offset)
1331
1960
{
1332
1961
  int bytes_read;
1333
1962
  my_off_t init_offset= offset;
1334
1963
  File file= index_file->file;
1335
 
  unsigned char io_buf[IO_SIZE*2];
 
1964
  uchar io_buf[IO_SIZE*2];
1336
1965
 
1337
1966
  for (;; offset+= bytes_read)
1338
1967
  {
1358
1987
  return(1);
1359
1988
}
1360
1989
 
 
1990
#endif /* HAVE_REPLICATION */
 
1991
 
1361
1992
/**
1362
1993
  Find the position in the log-index-file for the given log name.
1363
1994
 
1380
2011
    LOG_INFO_IO         Got IO error while reading file
1381
2012
*/
1382
2013
 
1383
 
int DRIZZLE_BIN_LOG::find_log_pos(LOG_INFO *linfo, const char *log_name,
 
2014
int MYSQL_BIN_LOG::find_log_pos(LOG_INFO *linfo, const char *log_name,
1384
2015
                            bool need_lock)
1385
2016
{
1386
2017
  int error= 0;
1387
2018
  char *fname= linfo->log_file_name;
1388
 
  uint32_t log_name_len= log_name ? (uint) strlen(log_name) : 0;
 
2019
  uint log_name_len= log_name ? (uint) strlen(log_name) : 0;
1389
2020
 
1390
2021
  /*
1391
2022
    Mutex needed because we need to make sure the file pointer does not
1400
2031
 
1401
2032
  for (;;)
1402
2033
  {
1403
 
    uint32_t length;
 
2034
    uint length;
1404
2035
    my_off_t offset= my_b_tell(&index_file);
1405
2036
    /* If we get 0 or 1 characters, this is the end of the file */
1406
2037
 
1453
2084
    LOG_INFO_IO         Got IO error while reading file
1454
2085
*/
1455
2086
 
1456
 
int DRIZZLE_BIN_LOG::find_next_log(LOG_INFO* linfo, bool need_lock)
 
2087
int MYSQL_BIN_LOG::find_next_log(LOG_INFO* linfo, bool need_lock)
1457
2088
{
1458
2089
  int error= 0;
1459
 
  uint32_t length;
 
2090
  uint length;
1460
2091
  char *fname= linfo->log_file_name;
1461
2092
 
1462
2093
  if (need_lock)
1500
2131
    1   error
1501
2132
*/
1502
2133
 
1503
 
bool DRIZZLE_BIN_LOG::reset_logs(THD* thd)
 
2134
bool MYSQL_BIN_LOG::reset_logs(THD* thd)
1504
2135
{
1505
2136
  LOG_INFO linfo;
1506
2137
  bool error=0;
1507
2138
  const char* save_name;
1508
2139
 
 
2140
  ha_reset_logs(thd);
1509
2141
  /*
1510
2142
    We need to get both locks to be sure that no one is trying to
1511
2143
    write to the index log file.
1519
2151
    thread. If the transaction involved MyISAM tables, it should go
1520
2152
    into binlog even on rollback.
1521
2153
  */
1522
 
  pthread_mutex_lock(&LOCK_thread_count);
 
2154
  VOID(pthread_mutex_lock(&LOCK_thread_count));
1523
2155
 
1524
2156
  /* Save variables so that we can reopen the log */
1525
2157
  save_name=name;
1528
2160
 
1529
2161
  /* First delete all old log files */
1530
2162
 
1531
 
  if (find_log_pos(&linfo, NULL, 0))
 
2163
  if (find_log_pos(&linfo, NullS, 0))
1532
2164
  {
1533
2165
    error=1;
1534
2166
    goto err;
1540
2172
    {
1541
2173
      if (my_errno == ENOENT) 
1542
2174
      {
1543
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2175
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1544
2176
                            ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1545
2177
                            linfo.log_file_name);
1546
 
        sql_print_information(_("Failed to delete file '%s'"),
 
2178
        sql_print_information("Failed to delete file '%s'",
1547
2179
                              linfo.log_file_name);
1548
2180
        my_errno= 0;
1549
2181
        error= 0;
1550
2182
      }
1551
2183
      else
1552
2184
      {
1553
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2185
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1554
2186
                            ER_BINLOG_PURGE_FATAL_ERR,
1555
 
                            _("a problem with deleting %s; "
 
2187
                            "a problem with deleting %s; "
1556
2188
                            "consider examining correspondence "
1557
2189
                            "of your binlog index file "
1558
 
                            "to the actual binlog files"),
 
2190
                            "to the actual binlog files",
1559
2191
                            linfo.log_file_name);
1560
2192
        error= 1;
1561
2193
        goto err;
1571
2203
  {
1572
2204
    if (my_errno == ENOENT) 
1573
2205
    {
1574
 
      push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2206
      push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1575
2207
                          ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1576
2208
                          index_file_name);
1577
 
      sql_print_information(_("Failed to delete file '%s'"),
 
2209
      sql_print_information("Failed to delete file '%s'",
1578
2210
                            index_file_name);
1579
2211
      my_errno= 0;
1580
2212
      error= 0;
1581
2213
    }
1582
2214
    else
1583
2215
    {
1584
 
      push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2216
      push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1585
2217
                          ER_BINLOG_PURGE_FATAL_ERR,
1586
2218
                          "a problem with deleting %s; "
1587
2219
                          "consider examining correspondence "
1596
2228
    need_start_event=1;
1597
2229
  if (!open_index_file(index_file_name, 0))
1598
2230
    open(save_name, log_type, 0, io_cache_type, no_auto_events, max_size, 0);
1599
 
  free((unsigned char*) save_name);
 
2231
  my_free((uchar*) save_name, MYF(0));
1600
2232
 
1601
2233
err:
1602
 
  pthread_mutex_unlock(&LOCK_thread_count);
 
2234
  VOID(pthread_mutex_unlock(&LOCK_thread_count));
1603
2235
  pthread_mutex_unlock(&LOCK_index);
1604
2236
  pthread_mutex_unlock(&LOCK_log);
1605
2237
  return(error);
1643
2275
    LOG_INFO_IO         Got IO error while reading file
1644
2276
*/
1645
2277
 
 
2278
#ifdef HAVE_REPLICATION
1646
2279
 
1647
 
int DRIZZLE_BIN_LOG::purge_first_log(Relay_log_info* rli, bool included)
 
2280
int MYSQL_BIN_LOG::purge_first_log(Relay_log_info* rli, bool included)
1648
2281
{
1649
2282
  int error;
1650
2283
 
1651
2284
  assert(is_open());
1652
2285
  assert(rli->slave_running == 1);
1653
 
  assert(!strcmp(rli->linfo.log_file_name,rli->event_relay_log_name.c_str()));
 
2286
  assert(!strcmp(rli->linfo.log_file_name,rli->event_relay_log_name));
1654
2287
 
1655
2288
  pthread_mutex_lock(&LOCK_index);
1656
2289
  pthread_mutex_lock(&rli->log_space_lock);
1657
 
  rli->relay_log.purge_logs(rli->group_relay_log_name.c_str(), included,
 
2290
  rli->relay_log.purge_logs(rli->group_relay_log_name, included,
1658
2291
                            0, 0, &rli->log_space_total);
1659
2292
  // Tell the I/O thread to take the relay_log_space_limit into account
1660
2293
  rli->ignore_log_space_limit= 0;
1673
2306
    If included is true, we want the first relay log;
1674
2307
    otherwise we want the one after event_relay_log_name.
1675
2308
  */
1676
 
  if ((included && (error=find_log_pos(&rli->linfo, NULL, 0))) ||
 
2309
  if ((included && (error=find_log_pos(&rli->linfo, NullS, 0))) ||
1677
2310
      (!included &&
1678
 
       ((error=find_log_pos(&rli->linfo, rli->event_relay_log_name.c_str(), 0)) ||
 
2311
       ((error=find_log_pos(&rli->linfo, rli->event_relay_log_name, 0)) ||
1679
2312
        (error=find_next_log(&rli->linfo, 0)))))
1680
2313
  {
1681
2314
    char buff[22];
1682
 
    sql_print_error(_("next log error: %d  offset: %s  log: %s included: %d"),
 
2315
    sql_print_error("next log error: %d  offset: %s  log: %s included: %d",
1683
2316
                    error,
1684
2317
                    llstr(rli->linfo.index_file_offset,buff),
1685
 
                    rli->group_relay_log_name.c_str(),
 
2318
                    rli->group_relay_log_name,
1686
2319
                    included);
1687
2320
    goto err;
1688
2321
  }
1691
2324
    Reset rli's coordinates to the current log.
1692
2325
  */
1693
2326
  rli->event_relay_log_pos= BIN_LOG_HEADER_SIZE;
1694
 
  rli->event_relay_log_name.assign(rli->linfo.log_file_name);
 
2327
  strmake(rli->event_relay_log_name,rli->linfo.log_file_name,
 
2328
          sizeof(rli->event_relay_log_name)-1);
1695
2329
 
1696
2330
  /*
1697
2331
    If we removed the rli->group_relay_log_name file,
1701
2335
  if (included)
1702
2336
  {
1703
2337
    rli->group_relay_log_pos = BIN_LOG_HEADER_SIZE;
1704
 
    rli->group_relay_log_name.assign(rli->linfo.log_file_name);
 
2338
    strmake(rli->group_relay_log_name,rli->linfo.log_file_name,
 
2339
            sizeof(rli->group_relay_log_name)-1);
1705
2340
    rli->notify_group_relay_log_name_update();
1706
2341
  }
1707
2342
 
1717
2352
  Update log index_file.
1718
2353
*/
1719
2354
 
1720
 
int DRIZZLE_BIN_LOG::update_log_index(LOG_INFO* log_info, bool need_update_threads)
 
2355
int MYSQL_BIN_LOG::update_log_index(LOG_INFO* log_info, bool need_update_threads)
1721
2356
{
1722
2357
  if (copy_up_file_and_fill(&index_file, log_info->index_file_start_offset))
1723
2358
    return LOG_INFO_IO;
1752
2387
                                stat() or my_delete()
1753
2388
*/
1754
2389
 
1755
 
int DRIZZLE_BIN_LOG::purge_logs(const char *to_log, 
 
2390
int MYSQL_BIN_LOG::purge_logs(const char *to_log, 
1756
2391
                          bool included,
1757
2392
                          bool need_mutex, 
1758
2393
                          bool need_update_threads, 
1772
2407
    File name exists in index file; delete until we find this file
1773
2408
    or a file that is used.
1774
2409
  */
1775
 
  if ((error=find_log_pos(&log_info, NULL, 0 /*no mutex*/)))
 
2410
  if ((error=find_log_pos(&log_info, NullS, 0 /*no mutex*/)))
1776
2411
    goto err;
1777
2412
  while ((strcmp(to_log,log_info.log_file_name) || (exit_loop=included)) &&
1778
2413
         !log_in_use(log_info.log_file_name))
1786
2421
          It's not fatal if we can't stat a log file that does not exist;
1787
2422
          If we could not stat, we won't delete.
1788
2423
        */     
1789
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2424
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1790
2425
                            ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1791
2426
                            log_info.log_file_name);
1792
 
        sql_print_information(_("Failed to execute stat() on file '%s'"),
 
2427
        sql_print_information("Failed to execute stat on file '%s'",
1793
2428
                              log_info.log_file_name);
1794
2429
        my_errno= 0;
1795
2430
      }
1798
2433
        /*
1799
2434
          Other than ENOENT are fatal
1800
2435
        */
1801
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2436
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1802
2437
                            ER_BINLOG_PURGE_FATAL_ERR,
1803
 
                            _("a problem with getting info on being purged %s; "
 
2438
                            "a problem with getting info on being purged %s; "
1804
2439
                            "consider examining correspondence "
1805
2440
                            "of your binlog index file "
1806
 
                            "to the actual binlog files"),
 
2441
                            "to the actual binlog files",
1807
2442
                            log_info.log_file_name);
1808
2443
        error= LOG_INFO_FATAL;
1809
2444
        goto err;
1820
2455
      {
1821
2456
        if (my_errno == ENOENT) 
1822
2457
        {
1823
 
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2458
          push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1824
2459
                              ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1825
2460
                              log_info.log_file_name);
1826
 
          sql_print_information(_("Failed to delete file '%s'"),
 
2461
          sql_print_information("Failed to delete file '%s'",
1827
2462
                                log_info.log_file_name);
1828
2463
          my_errno= 0;
1829
2464
        }
1830
2465
        else
1831
2466
        {
1832
 
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2467
          push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1833
2468
                              ER_BINLOG_PURGE_FATAL_ERR,
1834
 
                              _("a problem with deleting %s; "
 
2469
                              "a problem with deleting %s; "
1835
2470
                              "consider examining correspondence "
1836
2471
                              "of your binlog index file "
1837
 
                              "to the actual binlog files"),
 
2472
                              "to the actual binlog files",
1838
2473
                              log_info.log_file_name);
1839
2474
          if (my_errno == EMFILE)
1840
2475
          {
1846
2481
      }
1847
2482
    }
1848
2483
 
 
2484
    ha_binlog_index_purge_file(current_thd, log_info.log_file_name);
 
2485
 
1849
2486
    if (find_next_log(&log_info, 0) || exit_loop)
1850
2487
      break;
1851
2488
  }
1884
2521
                                stat() or my_delete()
1885
2522
*/
1886
2523
 
1887
 
int DRIZZLE_BIN_LOG::purge_logs_before_date(time_t purge_time)
 
2524
int MYSQL_BIN_LOG::purge_logs_before_date(time_t purge_time)
1888
2525
{
1889
2526
  int error;
1890
2527
  LOG_INFO log_info;
1897
2534
    or a file that is used or a file
1898
2535
    that is older than purge_time.
1899
2536
  */
1900
 
  if ((error=find_log_pos(&log_info, NULL, 0 /*no mutex*/)))
 
2537
  if ((error=find_log_pos(&log_info, NullS, 0 /*no mutex*/)))
1901
2538
    goto err;
1902
2539
 
1903
2540
  while (strcmp(log_file_name, log_info.log_file_name) &&
1910
2547
        /*
1911
2548
          It's not fatal if we can't stat a log file that does not exist.
1912
2549
        */     
1913
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2550
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1914
2551
                            ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1915
2552
                            log_info.log_file_name);
1916
 
        sql_print_information(_("Failed to execute stat() on file '%s'"),
 
2553
        sql_print_information("Failed to execute stat on file '%s'",
1917
2554
                              log_info.log_file_name);
1918
2555
        my_errno= 0;
1919
2556
      }
1922
2559
        /*
1923
2560
          Other than ENOENT are fatal
1924
2561
        */
1925
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2562
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1926
2563
                            ER_BINLOG_PURGE_FATAL_ERR,
1927
 
                            _("a problem with getting info on being purged %s; "
 
2564
                            "a problem with getting info on being purged %s; "
1928
2565
                            "consider examining correspondence "
1929
2566
                            "of your binlog index file "
1930
 
                            "to the actual binlog files"),
 
2567
                            "to the actual binlog files",
1931
2568
                            log_info.log_file_name);
1932
2569
        error= LOG_INFO_FATAL;
1933
2570
        goto err;
1942
2579
        if (my_errno == ENOENT) 
1943
2580
        {
1944
2581
          /* It's not fatal even if we can't delete a log file */
1945
 
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2582
          push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1946
2583
                              ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1947
2584
                              log_info.log_file_name);
1948
 
          sql_print_information(_("Failed to delete file '%s'"),
 
2585
          sql_print_information("Failed to delete file '%s'",
1949
2586
                                log_info.log_file_name);
1950
2587
          my_errno= 0;
1951
2588
        }
1952
2589
        else
1953
2590
        {
1954
 
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2591
          push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1955
2592
                              ER_BINLOG_PURGE_FATAL_ERR,
1956
 
                              _("a problem with deleting %s; "
 
2593
                              "a problem with deleting %s; "
1957
2594
                              "consider examining correspondence "
1958
2595
                              "of your binlog index file "
1959
 
                              "to the actual binlog files"),
 
2596
                              "to the actual binlog files",
1960
2597
                              log_info.log_file_name);
1961
2598
          error= LOG_INFO_FATAL;
1962
2599
          goto err;
1963
2600
        }
1964
2601
      }
 
2602
      ha_binlog_index_purge_file(current_thd, log_info.log_file_name);
1965
2603
    }
1966
2604
    if (find_next_log(&log_info, 0))
1967
2605
      break;
1977
2615
  pthread_mutex_unlock(&LOCK_index);
1978
2616
  return(error);
1979
2617
}
 
2618
#endif /* HAVE_REPLICATION */
1980
2619
 
1981
2620
 
1982
2621
/**
1988
2627
    If file name will be longer then FN_REFLEN it will be truncated
1989
2628
*/
1990
2629
 
1991
 
void DRIZZLE_BIN_LOG::make_log_name(char* buf, const char* log_ident)
 
2630
void MYSQL_BIN_LOG::make_log_name(char* buf, const char* log_ident)
1992
2631
{
1993
 
  uint32_t dir_len = dirname_length(log_file_name); 
 
2632
  uint dir_len = dirname_length(log_file_name); 
1994
2633
  if (dir_len >= FN_REFLEN)
1995
2634
    dir_len=FN_REFLEN-1;
1996
 
  my_stpncpy(buf, log_file_name, dir_len);
 
2635
  strnmov(buf, log_file_name, dir_len);
1997
2636
  strmake(buf+dir_len, log_ident, FN_REFLEN - dir_len -1);
1998
2637
}
1999
2638
 
2002
2641
  Check if we are writing/reading to the given log file.
2003
2642
*/
2004
2643
 
2005
 
bool DRIZZLE_BIN_LOG::is_active(const char *log_file_name_arg)
 
2644
bool MYSQL_BIN_LOG::is_active(const char *log_file_name_arg)
2006
2645
{
2007
2646
  return !strcmp(log_file_name, log_file_name_arg);
2008
2647
}
2016
2655
  method).
2017
2656
*/
2018
2657
 
2019
 
void DRIZZLE_BIN_LOG::new_file()
 
2658
void MYSQL_BIN_LOG::new_file()
2020
2659
{
2021
2660
  new_file_impl(1);
2022
2661
}
2023
2662
 
2024
2663
 
2025
 
void DRIZZLE_BIN_LOG::new_file_without_locking()
 
2664
void MYSQL_BIN_LOG::new_file_without_locking()
2026
2665
{
2027
2666
  new_file_impl(0);
2028
2667
}
2037
2676
    The new file name is stored last in the index file
2038
2677
*/
2039
2678
 
2040
 
void DRIZZLE_BIN_LOG::new_file_impl(bool need_lock)
 
2679
void MYSQL_BIN_LOG::new_file_impl(bool need_lock)
2041
2680
{
2042
2681
  char new_name[FN_REFLEN], *new_name_ptr, *old_name;
2043
2682
 
2123
2762
 
2124
2763
  open(old_name, log_type, new_name_ptr,
2125
2764
       io_cache_type, no_auto_events, max_size, 1);
2126
 
  free(old_name);
 
2765
  my_free(old_name,MYF(0));
2127
2766
 
2128
2767
end:
2129
2768
  if (need_lock)
2134
2773
}
2135
2774
 
2136
2775
 
2137
 
bool DRIZZLE_BIN_LOG::append(Log_event* ev)
 
2776
bool MYSQL_BIN_LOG::append(Log_event* ev)
2138
2777
{
2139
2778
  bool error = 0;
2140
2779
  pthread_mutex_lock(&LOCK_log);
2160
2799
}
2161
2800
 
2162
2801
 
2163
 
bool DRIZZLE_BIN_LOG::appendv(const char* buf, uint32_t len,...)
 
2802
bool MYSQL_BIN_LOG::appendv(const char* buf, uint len,...)
2164
2803
{
2165
2804
  bool error= 0;
2166
2805
  va_list(args);
2171
2810
  safe_mutex_assert_owner(&LOCK_log);
2172
2811
  do
2173
2812
  {
2174
 
    if (my_b_append(&log_file,(unsigned char*) buf,len))
 
2813
    if (my_b_append(&log_file,(uchar*) buf,len))
2175
2814
    {
2176
2815
      error= 1;
2177
2816
      goto err;
2188
2827
}
2189
2828
 
2190
2829
 
2191
 
bool DRIZZLE_BIN_LOG::flush_and_sync()
 
2830
bool MYSQL_BIN_LOG::flush_and_sync()
2192
2831
{
2193
2832
  int err=0, fd=log_file.file;
2194
2833
  safe_mutex_assert_owner(&LOCK_log);
2202
2841
  return err;
2203
2842
}
2204
2843
 
2205
 
void DRIZZLE_BIN_LOG::start_union_events(THD *thd, query_id_t query_id_param)
 
2844
void MYSQL_BIN_LOG::start_union_events(THD *thd, query_id_t query_id_param)
2206
2845
{
2207
2846
  assert(!thd->binlog_evt_union.do_union);
2208
2847
  thd->binlog_evt_union.do_union= true;
2211
2850
  thd->binlog_evt_union.first_query_id= query_id_param;
2212
2851
}
2213
2852
 
2214
 
void DRIZZLE_BIN_LOG::stop_union_events(THD *thd)
 
2853
void MYSQL_BIN_LOG::stop_union_events(THD *thd)
2215
2854
{
2216
2855
  assert(thd->binlog_evt_union.do_union);
2217
2856
  thd->binlog_evt_union.do_union= false;
2218
2857
}
2219
2858
 
2220
 
bool DRIZZLE_BIN_LOG::is_query_in_union(THD *thd, query_id_t query_id_param)
 
2859
bool MYSQL_BIN_LOG::is_query_in_union(THD *thd, query_id_t query_id_param)
2221
2860
{
2222
2861
  return (thd->binlog_evt_union.do_union && 
2223
2862
          query_id_param >= thd->binlog_evt_union.first_query_id);
2242
2881
      open_cached_file(&trx_data->trans_log, mysql_tmpdir,
2243
2882
                       LOG_PREFIX, binlog_cache_size, MYF(MY_WME)))
2244
2883
  {
2245
 
    free((unsigned char*)trx_data);
 
2884
    my_free((uchar*)trx_data, MYF(MY_ALLOW_ZERO_PTR));
2246
2885
    return(1);                      // Didn't manage to set it up
2247
2886
  }
2248
2887
  thd_set_ha_data(this, binlog_hton, trx_data);
2327
2966
  Write a table map to the binary log.
2328
2967
 */
2329
2968
 
2330
 
int THD::binlog_write_table_map(Table *table, bool is_trans)
 
2969
int THD::binlog_write_table_map(TABLE *table, bool is_trans)
2331
2970
{
2332
2971
  int error;
2333
2972
 
2334
2973
  /* Pre-conditions */
2335
2974
  assert(current_stmt_binlog_row_based && mysql_bin_log.is_open());
2336
 
  assert(table->s->table_map_id != UINT32_MAX);
 
2975
  assert(table->s->table_map_id != ULONG_MAX);
2337
2976
 
2338
2977
  Table_map_log_event::flag_set const
2339
2978
    flags= Table_map_log_event::TM_NO_FLAGS;
2386
3025
  event.
2387
3026
*/
2388
3027
int
2389
 
DRIZZLE_BIN_LOG::flush_and_set_pending_rows_event(THD *thd,
 
3028
MYSQL_BIN_LOG::flush_and_set_pending_rows_event(THD *thd,
2390
3029
                                                Rows_log_event* event)
2391
3030
{
2392
3031
  assert(mysql_bin_log.is_open());
2468
3107
  Write an event to the binary log.
2469
3108
*/
2470
3109
 
2471
 
bool DRIZZLE_BIN_LOG::write(Log_event *event_info)
 
3110
bool MYSQL_BIN_LOG::write(Log_event *event_info)
2472
3111
{
2473
3112
  THD *thd= event_info->thd;
2474
3113
  bool error= 1;
2516
3155
    if ((thd && !(thd->options & OPTION_BIN_LOG)) ||
2517
3156
        (!binlog_filter->db_ok(local_db)))
2518
3157
    {
2519
 
      pthread_mutex_unlock(&LOCK_log);
 
3158
      VOID(pthread_mutex_unlock(&LOCK_log));
2520
3159
      return(0);
2521
3160
    }
2522
3161
 
2572
3211
      {
2573
3212
        if (thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt)
2574
3213
        {
2575
 
          Intvar_log_event e(thd,(unsigned char) LAST_INSERT_ID_EVENT,
 
3214
          Intvar_log_event e(thd,(uchar) LAST_INSERT_ID_EVENT,
2576
3215
                             thd->first_successful_insert_id_in_prev_stmt_for_binlog);
2577
3216
          if (e.write(file))
2578
3217
            goto err;
2584
3223
            MyISAM or BDB) (table->next_number_keypart != 0), such event is
2585
3224
            in fact not necessary. We could avoid logging it.
2586
3225
          */
2587
 
          Intvar_log_event e(thd, (unsigned char) INSERT_ID_EVENT,
 
3226
          Intvar_log_event e(thd, (uchar) INSERT_ID_EVENT,
2588
3227
                             thd->auto_inc_intervals_in_cur_stmt_for_binlog.
2589
3228
                             minimum());
2590
3229
          if (e.write(file))
2598
3237
        }
2599
3238
        if (thd->user_var_events.elements)
2600
3239
        {
2601
 
          for (uint32_t i= 0; i < thd->user_var_events.elements; i++)
 
3240
          for (uint i= 0; i < thd->user_var_events.elements; i++)
2602
3241
          {
2603
3242
            BINLOG_USER_VAR_EVENT *user_var_event;
2604
 
            get_dynamic(&thd->user_var_events,(unsigned char*) &user_var_event, i);
 
3243
            get_dynamic(&thd->user_var_events,(uchar*) &user_var_event, i);
2605
3244
            User_var_log_event e(thd, user_var_event->user_var_event->name.str,
2606
3245
                                 user_var_event->user_var_event->name.length,
2607
3246
                                 user_var_event->value,
2656
3295
  return logger.error_log_print(level, format, args);
2657
3296
}
2658
3297
 
2659
 
void DRIZZLE_BIN_LOG::rotate_and_purge(uint32_t flags)
 
3298
 
 
3299
bool slow_log_print(THD *thd, const char *query, uint query_length,
 
3300
                    uint64_t current_utime)
 
3301
{
 
3302
  return logger.slow_log_print(thd, query, query_length, current_utime);
 
3303
}
 
3304
 
 
3305
 
 
3306
bool LOGGER::log_command(THD *thd, enum enum_server_command command)
 
3307
{
 
3308
  /*
 
3309
    Log command if we have at least one log event handler enabled and want
 
3310
    to log this king of commands
 
3311
  */
 
3312
  if (*general_log_handler_list && (what_to_log & (1L << (uint) command)))
 
3313
  {
 
3314
    if (thd->options & OPTION_LOG_OFF)
 
3315
    {
 
3316
      /* No logging */
 
3317
      return false;
 
3318
    }
 
3319
 
 
3320
    return true;
 
3321
  }
 
3322
 
 
3323
  return false;
 
3324
}
 
3325
 
 
3326
 
 
3327
bool general_log_print(THD *thd, enum enum_server_command command,
 
3328
                       const char *format, ...)
 
3329
{
 
3330
  va_list args;
 
3331
  uint error= 0;
 
3332
 
 
3333
  /* Print the message to the buffer if we want to log this king of commands */
 
3334
  if (! logger.log_command(thd, command))
 
3335
    return false;
 
3336
 
 
3337
  va_start(args, format);
 
3338
  error= logger.general_log_print(thd, command, format, args);
 
3339
  va_end(args);
 
3340
 
 
3341
  return error;
 
3342
}
 
3343
 
 
3344
bool general_log_write(THD *thd, enum enum_server_command command,
 
3345
                       const char *query, uint query_length)
 
3346
{
 
3347
  /* Write the message to the log if we want to log this king of commands */
 
3348
  if (logger.log_command(thd, command))
 
3349
    return logger.general_log_write(thd, command, query, query_length);
 
3350
 
 
3351
  return false;
 
3352
}
 
3353
 
 
3354
void MYSQL_BIN_LOG::rotate_and_purge(uint flags)
2660
3355
{
2661
3356
  if (!(flags & RP_LOCK_LOG_IS_ALREADY_LOCKED))
2662
3357
    pthread_mutex_lock(&LOCK_log);
2664
3359
      (my_b_tell(&log_file) >= (my_off_t) max_size))
2665
3360
  {
2666
3361
    new_file_without_locking();
 
3362
#ifdef HAVE_REPLICATION
2667
3363
    if (expire_logs_days)
2668
3364
    {
2669
3365
      time_t purge_time= my_time(0) - expire_logs_days*24*60*60;
2670
3366
      if (purge_time >= 0)
2671
3367
        purge_logs_before_date(purge_time);
2672
3368
    }
 
3369
#endif
2673
3370
  }
2674
3371
  if (!(flags & RP_LOCK_LOG_IS_ALREADY_LOCKED))
2675
3372
    pthread_mutex_unlock(&LOCK_log);
2676
3373
}
2677
3374
 
2678
 
uint32_t DRIZZLE_BIN_LOG::next_file_id()
 
3375
uint MYSQL_BIN_LOG::next_file_id()
2679
3376
{
2680
 
  uint32_t res;
 
3377
  uint res;
2681
3378
  pthread_mutex_lock(&LOCK_log);
2682
3379
  res = file_id++;
2683
3380
  pthread_mutex_unlock(&LOCK_log);
2699
3396
    be reset as a READ_CACHE to be able to read the contents from it.
2700
3397
 */
2701
3398
 
2702
 
int DRIZZLE_BIN_LOG::write_cache(IO_CACHE *cache, bool lock_log, bool sync_log)
 
3399
int MYSQL_BIN_LOG::write_cache(IO_CACHE *cache, bool lock_log, bool sync_log)
2703
3400
{
2704
3401
  Mutex_sentry sentry(lock_log ? &LOCK_log : NULL);
2705
3402
 
2706
3403
  if (reinit_io_cache(cache, READ_CACHE, 0, 0, 0))
2707
3404
    return ER_ERROR_ON_WRITE;
2708
 
  uint32_t length= my_b_bytes_in_cache(cache), group, carry, hdr_offs;
 
3405
  uint length= my_b_bytes_in_cache(cache), group, carry, hdr_offs;
2709
3406
  long val;
2710
 
  unsigned char header[LOG_EVENT_HEADER_LEN];
 
3407
  uchar header[LOG_EVENT_HEADER_LEN];
2711
3408
 
2712
3409
  /*
2713
3410
    The events in the buffer have incorrect end_log_pos data
2738
3435
      assert(carry < LOG_EVENT_HEADER_LEN);
2739
3436
 
2740
3437
      /* assemble both halves */
2741
 
      memcpy(&header[carry], cache->read_pos, LOG_EVENT_HEADER_LEN - carry);
 
3438
      memcpy(&header[carry], (char *)cache->read_pos, LOG_EVENT_HEADER_LEN - carry);
2742
3439
 
2743
3440
      /* fix end_log_pos */
2744
3441
      val= uint4korr(&header[LOG_POS_OFFSET]) + group;
2752
3449
        copy fixed second half of header to cache so the correct
2753
3450
        version will be written later.
2754
3451
      */
2755
 
      memcpy(cache->read_pos, &header[carry], LOG_EVENT_HEADER_LEN - carry);
 
3452
      memcpy((char *)cache->read_pos, &header[carry], LOG_EVENT_HEADER_LEN - carry);
2756
3453
 
2757
3454
      /* next event header at ... */
2758
3455
      hdr_offs = uint4korr(&header[EVENT_LEN_OFFSET]) - carry;
2781
3478
        if (hdr_offs + LOG_EVENT_HEADER_LEN > length)
2782
3479
        {
2783
3480
          carry= length - hdr_offs;
2784
 
          memcpy(header, cache->read_pos + hdr_offs, carry);
 
3481
          memcpy(header, (char *)cache->read_pos + hdr_offs, carry);
2785
3482
          length= hdr_offs;
2786
3483
        }
2787
3484
        else
2788
3485
        {
2789
3486
          /* we've got a full event-header, and it came in one piece */
2790
3487
 
2791
 
          unsigned char *log_pos= (unsigned char *)cache->read_pos + hdr_offs + LOG_POS_OFFSET;
 
3488
          uchar *log_pos= (uchar *)cache->read_pos + hdr_offs + LOG_POS_OFFSET;
2792
3489
 
2793
3490
          /* fix end_log_pos */
2794
3491
          val= uint4korr(log_pos) + group;
2795
3492
          int4store(log_pos, val);
2796
3493
 
2797
3494
          /* next event header at ... */
2798
 
          log_pos= (unsigned char *)cache->read_pos + hdr_offs + EVENT_LEN_OFFSET;
 
3495
          log_pos= (uchar *)cache->read_pos + hdr_offs + EVENT_LEN_OFFSET;
2799
3496
          hdr_offs += uint4korr(log_pos);
2800
3497
 
2801
3498
        }
2847
3544
    'cache' needs to be reinitialized after this functions returns.
2848
3545
*/
2849
3546
 
2850
 
bool DRIZZLE_BIN_LOG::write(THD *thd, IO_CACHE *cache, Log_event *commit_event)
 
3547
bool MYSQL_BIN_LOG::write(THD *thd, IO_CACHE *cache, Log_event *commit_event)
2851
3548
{
2852
 
  pthread_mutex_lock(&LOCK_log);
 
3549
  VOID(pthread_mutex_lock(&LOCK_log));
2853
3550
 
2854
3551
  /* NULL would represent nothing to replicate after ROLLBACK */
2855
3552
  assert(commit_event != NULL);
2925
3622
    else
2926
3623
      rotate_and_purge(RP_LOCK_LOG_IS_ALREADY_LOCKED);
2927
3624
  }
2928
 
  pthread_mutex_unlock(&LOCK_log);
 
3625
  VOID(pthread_mutex_unlock(&LOCK_log));
2929
3626
 
2930
3627
  return(0);
2931
3628
 
2935
3632
    write_error= 1;
2936
3633
    sql_print_error(ER(ER_ERROR_ON_WRITE), name, errno);
2937
3634
  }
2938
 
  pthread_mutex_unlock(&LOCK_log);
 
3635
  VOID(pthread_mutex_unlock(&LOCK_log));
2939
3636
  return(1);
2940
3637
}
2941
3638
 
2949
3646
    It will be released at the end of the function.
2950
3647
*/
2951
3648
 
2952
 
void DRIZZLE_BIN_LOG::wait_for_update_relay_log(THD* thd)
 
3649
void MYSQL_BIN_LOG::wait_for_update_relay_log(THD* thd)
2953
3650
{
2954
3651
  const char *old_msg;
2955
3652
  old_msg= thd->enter_cond(&update_cond, &LOCK_log,
2978
3675
    LOCK_log is released by the caller.
2979
3676
*/
2980
3677
 
2981
 
int DRIZZLE_BIN_LOG::wait_for_update_bin_log(THD* thd,
 
3678
int MYSQL_BIN_LOG::wait_for_update_bin_log(THD* thd,
2982
3679
                                           const struct timespec *timeout)
2983
3680
{
2984
3681
  int ret= 0;
2985
 
  const char* old_msg = thd->get_proc_info();
 
3682
  const char* old_msg = thd->proc_info;
2986
3683
  old_msg= thd->enter_cond(&update_cond, &LOCK_log,
2987
3684
                           "Master has sent all binlog to slave; "
2988
3685
                           "waiting for binlog to be updated");
3009
3706
    The internal structures are not freed until cleanup() is called
3010
3707
*/
3011
3708
 
3012
 
void DRIZZLE_BIN_LOG::close(uint32_t exiting)
 
3709
void MYSQL_BIN_LOG::close(uint exiting)
3013
3710
{                                       // One can't set log_type here!
3014
3711
  if (log_state == LOG_OPENED)
3015
3712
  {
 
3713
#ifdef HAVE_REPLICATION
3016
3714
    if (log_type == LOG_BIN && !no_auto_events &&
3017
3715
        (exiting & LOG_CLOSE_STOP_EVENT))
3018
3716
    {
3021
3719
      bytes_written+= s.data_written;
3022
3720
      signal_update();
3023
3721
    }
 
3722
#endif /* HAVE_REPLICATION */
3024
3723
 
3025
3724
    /* don't pwrite in a file opened with O_APPEND - it doesn't work */
3026
3725
    if (log_file.type == WRITE_CACHE && log_type == LOG_BIN)
3027
3726
    {
3028
3727
      my_off_t offset= BIN_LOG_HEADER_SIZE + FLAGS_OFFSET;
3029
 
      unsigned char flags= 0;            // clearing LOG_EVENT_BINLOG_IN_USE_F
 
3728
      uchar flags= 0;            // clearing LOG_EVENT_BINLOG_IN_USE_F
3030
3729
      pwrite(log_file.file, &flags, 1, offset);
3031
3730
    }
3032
3731
 
3033
3732
    /* this will cleanup IO_CACHE, sync and close the file */
3034
 
    DRIZZLE_LOG::close(exiting);
 
3733
    MYSQL_LOG::close(exiting);
3035
3734
  }
3036
3735
 
3037
3736
  /*
3049
3748
    }
3050
3749
  }
3051
3750
  log_state= (exiting & LOG_CLOSE_TO_BE_OPENED) ? LOG_TO_BE_OPENED : LOG_CLOSED;
3052
 
  if (name)
3053
 
  {
3054
 
    free(name);
3055
 
    name= NULL;
3056
 
  }
 
3751
  safeFree(name);
3057
3752
  return;
3058
3753
}
3059
3754
 
3060
3755
 
3061
 
void DRIZZLE_BIN_LOG::set_max_size(ulong max_size_arg)
 
3756
void MYSQL_BIN_LOG::set_max_size(ulong max_size_arg)
3062
3757
{
3063
3758
  /*
3064
3759
    We need to take locks, otherwise this may happen:
3126
3821
 
3127
3822
void sql_perror(const char *message)
3128
3823
{
 
3824
#ifdef HAVE_STRERROR
3129
3825
  sql_print_error("%s: %s",message, strerror(errno));
 
3826
#else
 
3827
  perror(message);
 
3828
#endif
3130
3829
}
3131
3830
 
3132
3831
 
3137
3836
  {
3138
3837
    char err_renamed[FN_REFLEN], *end;
3139
3838
    end= strmake(err_renamed,log_error_file,FN_REFLEN-4);
3140
 
    my_stpcpy(end, "-old");
3141
 
    pthread_mutex_lock(&LOCK_error_log);
 
3839
    strmov(end, "-old");
 
3840
    VOID(pthread_mutex_lock(&LOCK_error_log));
3142
3841
    char err_temp[FN_REFLEN+4];
3143
3842
    /*
3144
3843
     On Windows is necessary a temporary file for to rename
3145
3844
     the current error file.
3146
3845
    */
3147
 
    strxmov(err_temp, err_renamed,"-tmp",NULL);
 
3846
    strxmov(err_temp, err_renamed,"-tmp",NullS);
3148
3847
    (void) my_delete(err_temp, MYF(0)); 
3149
3848
    if (freopen(err_temp,"a+",stdout))
3150
3849
    {
3151
3850
      int fd;
3152
3851
      size_t bytes;
3153
 
      unsigned char buf[IO_SIZE];
 
3852
      uchar buf[IO_SIZE];
3154
3853
 
3155
3854
      freopen(err_temp,"a+",stderr);
3156
3855
      (void) my_delete(err_renamed, MYF(0));
3169
3868
    }
3170
3869
    else
3171
3870
     result= 1;
3172
 
    pthread_mutex_unlock(&LOCK_error_log);
 
3871
    VOID(pthread_mutex_unlock(&LOCK_error_log));
3173
3872
  }
3174
3873
   return result;
3175
3874
}
3176
3875
 
3177
 
void DRIZZLE_BIN_LOG::signal_update()
 
3876
void MYSQL_BIN_LOG::signal_update()
3178
3877
{
3179
3878
  pthread_cond_broadcast(&update_cond);
3180
3879
  return;
3197
3896
    return an error (e.g. logging to the log tables)
3198
3897
*/
3199
3898
static void print_buffer_to_file(enum loglevel level,
3200
 
                                 int error_code __attribute__((unused)),
 
3899
                                 int error_code __attribute__((__unused__)),
3201
3900
                                 const char *buffer,
3202
 
                                 size_t buffer_length __attribute__((unused)))
 
3901
                                 size_t buffer_length __attribute__((__unused__)))
3203
3902
{
3204
3903
  time_t skr;
3205
3904
  struct tm tm_tmp;
3206
3905
  struct tm *start;
3207
3906
 
3208
 
  pthread_mutex_lock(&LOCK_error_log);
 
3907
  VOID(pthread_mutex_lock(&LOCK_error_log));
3209
3908
 
3210
3909
  skr= my_time(0);
3211
3910
  localtime_r(&skr, &tm_tmp);
3224
3923
 
3225
3924
  fflush(stderr);
3226
3925
 
3227
 
  pthread_mutex_unlock(&LOCK_error_log);
 
3926
  VOID(pthread_mutex_unlock(&LOCK_error_log));
3228
3927
  return;
3229
3928
}
3230
3929
 
3330
4029
 
3331
4030
int TC_LOG_MMAP::open(const char *opt_name)
3332
4031
{
3333
 
  uint32_t i;
 
4032
  uint i;
3334
4033
  bool crashed= false;
3335
4034
  PAGE *pg;
3336
4035
 
3337
4036
  assert(total_ha_2pc > 1);
3338
4037
  assert(opt_name && opt_name[0]);
3339
4038
 
3340
 
  tc_log_page_size= getpagesize();
 
4039
  tc_log_page_size= my_getpagesize();
3341
4040
  assert(TC_LOG_PAGE_SIZE % tc_log_page_size == 0);
3342
4041
 
3343
4042
  fn_format(logname,opt_name,mysql_data_home,"",MY_UNPACK_FILENAME);
3358
4057
  {
3359
4058
    inited= 1;
3360
4059
    crashed= true;
3361
 
    sql_print_information(_("Recovering after a crash using %s"), opt_name);
 
4060
    sql_print_information("Recovering after a crash using %s", opt_name);
3362
4061
    if (tc_heuristic_recover)
3363
4062
    {
3364
 
      sql_print_error(_("Cannot perform automatic crash recovery when "
3365
 
                      "--tc-heuristic-recover is used"));
 
4063
      sql_print_error("Cannot perform automatic crash recovery when "
 
4064
                      "--tc-heuristic-recover is used");
3366
4065
      goto err;
3367
4066
    }
3368
4067
    file_length= my_seek(fd, 0L, MY_SEEK_END, MYF(MY_WME+MY_FAE));
3370
4069
      goto err;
3371
4070
  }
3372
4071
 
3373
 
  data= (unsigned char *)my_mmap(0, (size_t)file_length, PROT_READ|PROT_WRITE,
 
4072
  data= (uchar *)my_mmap(0, (size_t)file_length, PROT_READ|PROT_WRITE,
3374
4073
                        MAP_NOSYNC|MAP_SHARED, fd, 0);
3375
4074
  if (data == MAP_FAILED)
3376
4075
  {
3406
4105
      goto err;
3407
4106
 
3408
4107
  memcpy(data, tc_log_magic, sizeof(tc_log_magic));
3409
 
  data[sizeof(tc_log_magic)]= (unsigned char)total_ha_2pc;
 
4108
  data[sizeof(tc_log_magic)]= (uchar)total_ha_2pc;
3410
4109
  msync(data, tc_log_page_size, MS_SYNC);
3411
4110
  my_sync(fd, MYF(0));
3412
4111
  inited=5;
3514
4213
    threads waiting for a page, but then all these threads will be waiting
3515
4214
    for a fsync() anyway
3516
4215
 
3517
 
   If tc_log == DRIZZLE_LOG then tc_log writes transaction to binlog and
 
4216
   If tc_log == MYSQL_LOG then tc_log writes transaction to binlog and
3518
4217
   records XID in a special Xid_log_event.
3519
4218
   If tc_log = TC_LOG_MMAP then xid is written in a special memory-mapped
3520
4219
   log.
3528
4227
    to the position in memory where xid was logged to.
3529
4228
*/
3530
4229
 
3531
 
int TC_LOG_MMAP::log_xid(THD *thd __attribute__((unused)), my_xid xid)
 
4230
int TC_LOG_MMAP::log_xid(THD *thd __attribute__((__unused__)), my_xid xid)
3532
4231
{
3533
4232
  int err;
3534
4233
  PAGE *p;
3561
4260
  }
3562
4261
 
3563
4262
  /* found! store xid there and mark the page dirty */
3564
 
  cookie= (ulong)((unsigned char *)p->ptr - data);      // can never be zero
 
4263
  cookie= (ulong)((uchar *)p->ptr - data);      // can never be zero
3565
4264
  *p->ptr++= xid;
3566
4265
  p->free--;
3567
4266
  p->state= DIRTY;
3640
4339
  cookie points directly to the memory where xid was logged.
3641
4340
*/
3642
4341
 
3643
 
void TC_LOG_MMAP::unlog(ulong cookie, my_xid xid __attribute__((unused)))
 
4342
void TC_LOG_MMAP::unlog(ulong cookie, my_xid xid __attribute__((__unused__)))
3644
4343
{
3645
4344
  PAGE *p=pages+(cookie/tc_log_page_size);
3646
4345
  my_xid *x=(my_xid *)(data+cookie);
3662
4361
 
3663
4362
void TC_LOG_MMAP::close()
3664
4363
{
3665
 
  uint32_t i;
 
4364
  uint i;
3666
4365
  switch (inited) {
3667
4366
  case 6:
3668
4367
    pthread_mutex_destroy(&LOCK_sync);
3680
4379
      pthread_cond_destroy(&pages[i].cond);
3681
4380
    }
3682
4381
  case 3:
3683
 
    free((unsigned char*)pages);
 
4382
    my_free((uchar*)pages, MYF(0));
3684
4383
  case 2:
3685
4384
    my_munmap((char*)data, (size_t)file_length);
3686
4385
  case 1:
3698
4397
 
3699
4398
  if (memcmp(data, tc_log_magic, sizeof(tc_log_magic)))
3700
4399
  {
3701
 
    sql_print_error(_("Bad magic header in tc log"));
 
4400
    sql_print_error("Bad magic header in tc log");
3702
4401
    goto err1;
3703
4402
  }
3704
4403
 
3708
4407
  */
3709
4408
  if (data[sizeof(tc_log_magic)] != total_ha_2pc)
3710
4409
  {
3711
 
    sql_print_error(_("Recovery failed! You must enable "
 
4410
    sql_print_error("Recovery failed! You must enable "
3712
4411
                    "exactly %d storage engines that support "
3713
 
                    "two-phase commit protocol"),
 
4412
                    "two-phase commit protocol",
3714
4413
                    data[sizeof(tc_log_magic)]);
3715
4414
    goto err1;
3716
4415
  }
3722
4421
  for ( ; p < end_p ; p++)
3723
4422
  {
3724
4423
    for (my_xid *x=p->start; x < p->end; x++)
3725
 
      if (*x && my_hash_insert(&xids, (unsigned char *)x))
 
4424
      if (*x && my_hash_insert(&xids, (uchar *)x))
3726
4425
        goto err2; // OOM
3727
4426
  }
3728
4427
 
3730
4429
    goto err2;
3731
4430
 
3732
4431
  hash_free(&xids);
3733
 
  memset(data, 0, (size_t)file_length);
 
4432
  bzero(data, (size_t)file_length);
3734
4433
  return 0;
3735
4434
 
3736
4435
err2:
3737
4436
  hash_free(&xids);
3738
4437
err1:
3739
 
  sql_print_error(_("Crash recovery failed. Either correct the problem "
 
4438
  sql_print_error("Crash recovery failed. Either correct the problem "
3740
4439
                  "(if it's, for example, out of memory error) and restart, "
3741
 
                  "or delete tc log and start drizzled with "
3742
 
                  "--tc-heuristic-recover={commit|rollback}"));
 
4440
                  "or delete tc log and start mysqld with "
 
4441
                  "--tc-heuristic-recover={commit|rollback}");
3743
4442
  return 1;
3744
4443
}
3745
4444
#endif
3766
4465
  if (!tc_heuristic_recover)
3767
4466
    return 0;
3768
4467
 
3769
 
  sql_print_information(_("Heuristic crash recovery mode"));
 
4468
  sql_print_information("Heuristic crash recovery mode");
3770
4469
  if (ha_recover(0))
3771
 
    sql_print_error(_("Heuristic crash recovery failed"));
3772
 
  sql_print_information(_("Please restart mysqld without --tc-heuristic-recover"));
 
4470
    sql_print_error("Heuristic crash recovery failed");
 
4471
  sql_print_information("Please restart mysqld without --tc-heuristic-recover");
3773
4472
  return 1;
3774
4473
}
3775
4474
 
3776
4475
/****** transaction coordinator log for 2pc - binlog() based solution ******/
3777
 
#define TC_LOG_BINLOG DRIZZLE_BIN_LOG
 
4476
#define TC_LOG_BINLOG MYSQL_BIN_LOG
3778
4477
 
3779
4478
/**
3780
4479
  @todo
3810
4509
    return 1;
3811
4510
  }
3812
4511
 
3813
 
  if ((error= find_log_pos(&log_info, NULL, 1)))
 
4512
  if ((error= find_log_pos(&log_info, NullS, 1)))
3814
4513
  {
3815
4514
    if (error != LOG_INFO_EOF)
3816
 
      sql_print_error(_("find_log_pos() failed (error: %d)"), error);
 
4515
      sql_print_error("find_log_pos() failed (error: %d)", error);
3817
4516
    else
3818
4517
      error= 0;
3819
4518
    goto err;
3837
4536
 
3838
4537
    if (error !=  LOG_INFO_EOF)
3839
4538
    {
3840
 
      sql_print_error(_("find_log_pos() failed (error: %d)"), error);
 
4539
      sql_print_error("find_log_pos() failed (error: %d)", error);
3841
4540
      goto err;
3842
4541
    }
3843
4542
 
3851
4550
        ev->get_type_code() == FORMAT_DESCRIPTION_EVENT &&
3852
4551
        ev->flags & LOG_EVENT_BINLOG_IN_USE_F)
3853
4552
    {
3854
 
      sql_print_information(_("Recovering after a crash using %s"), opt_name);
 
4553
      sql_print_information("Recovering after a crash using %s", opt_name);
3855
4554
      error= recover(&log, (Format_description_log_event *)ev);
3856
4555
    }
3857
4556
    else
3898
4597
  return(!binlog_end_trans(thd, trx_data, &xle, true));
3899
4598
}
3900
4599
 
3901
 
void TC_LOG_BINLOG::unlog(ulong cookie __attribute__((unused)),
3902
 
                          my_xid xid __attribute__((unused)))
 
4600
void TC_LOG_BINLOG::unlog(ulong cookie __attribute__((__unused__)),
 
4601
                          my_xid xid __attribute__((__unused__)))
3903
4602
{
3904
4603
  pthread_mutex_lock(&LOCK_prep_xids);
3905
4604
  assert(prepared_xids > 0);
3930
4629
    if (ev->get_type_code() == XID_EVENT)
3931
4630
    {
3932
4631
      Xid_log_event *xev=(Xid_log_event *)ev;
3933
 
      unsigned char *x= (unsigned char *) memdup_root(&mem_root, (unsigned char*) &xev->xid,
 
4632
      uchar *x= (uchar *) memdup_root(&mem_root, (uchar*) &xev->xid,
3934
4633
                                      sizeof(xev->xid));
3935
4634
      if (! x)
3936
4635
        goto err2;
3950
4649
  free_root(&mem_root, MYF(0));
3951
4650
  hash_free(&xids);
3952
4651
err1:
3953
 
  sql_print_error(_("Crash recovery failed. Either correct the problem "
 
4652
  sql_print_error("Crash recovery failed. Either correct the problem "
3954
4653
                  "(if it's, for example, out of memory error) and restart, "
3955
4654
                  "or delete (or rename) binary log and start mysqld with "
3956
 
                  "--tc-heuristic-recover={commit|rollback}"));
 
4655
                  "--tc-heuristic-recover={commit|rollback}");
3957
4656
  return 1;
3958
4657
}
3959
4658
 
3980
4679
#endif /* INNODB_COMPATIBILITY_HOOKS */
3981
4680
 
3982
4681
 
 
4682
struct st_mysql_storage_engine binlog_storage_engine=
 
4683
{ MYSQL_HANDLERTON_INTERFACE_VERSION };
 
4684
 
3983
4685
mysql_declare_plugin(binlog)
3984
4686
{
3985
 
  DRIZZLE_STORAGE_ENGINE_PLUGIN,
 
4687
  MYSQL_STORAGE_ENGINE_PLUGIN,
 
4688
  &binlog_storage_engine,
3986
4689
  "binlog",
3987
 
  "1.0",
3988
4690
  "MySQL AB",
3989
4691
  "This is a pseudo storage engine to represent the binlog in a transaction",
3990
4692
  PLUGIN_LICENSE_GPL,
3991
4693
  binlog_init, /* Plugin Init */
3992
4694
  NULL, /* Plugin Deinit */
 
4695
  0x0100 /* 1.0 */,
3993
4696
  NULL,                       /* status variables                */
3994
4697
  NULL,                       /* system variables                */
3995
4698
  NULL                        /* config options                  */