~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/log.cc

  • Committer: Mark Atwood
  • Date: 2008-07-12 07:25:25 UTC
  • mto: This revision was merged to the branch mainline in revision 139.
  • Revision ID: me@mark.atwood.name-20080712072525-s1dq9mtwo5td7af7
more hackery to get plugin UDFs working

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,
59
58
static int binlog_rollback(handlerton *hton, THD *thd, bool all);
60
59
static int binlog_prepare(handlerton *hton, THD *thd, bool all);
61
60
 
 
61
/**
 
62
  Silence all errors and warnings reported when performing a write
 
63
  to a log table.
 
64
  Errors and warnings are not reported to the client or SQL exception
 
65
  handlers, so that the presence of logging does not interfere and affect
 
66
  the logic of an application.
 
67
*/
 
68
class Silence_log_table_errors : public Internal_error_handler
 
69
{
 
70
  char m_message[MYSQL_ERRMSG_SIZE];
 
71
public:
 
72
  Silence_log_table_errors()
 
73
  {
 
74
    m_message[0]= '\0';
 
75
  }
 
76
 
 
77
  virtual ~Silence_log_table_errors() {}
 
78
 
 
79
  virtual bool handle_error(uint sql_errno, const char *message,
 
80
                            MYSQL_ERROR::enum_warning_level level,
 
81
                            THD *thd);
 
82
  const char *message() const { return m_message; }
 
83
};
 
84
 
 
85
bool
 
86
Silence_log_table_errors::handle_error(uint /* sql_errno */,
 
87
                                       const char *message_arg,
 
88
                                       MYSQL_ERROR::enum_warning_level /* level */,
 
89
                                       THD * /* thd */)
 
90
{
 
91
  strmake(m_message, message_arg, sizeof(m_message)-1);
 
92
  return true;
 
93
}
 
94
 
62
95
 
63
96
sql_print_message_func sql_print_message_handlers[3] =
64
97
{
204
237
 
205
238
 
206
239
/* 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)))
 
240
int check_if_log_table(uint db_len __attribute__((__unused__)),
 
241
                       const char *db __attribute__((__unused__)),
 
242
                       uint table_name_len __attribute__((__unused__)),
 
243
                       const char *table_name __attribute__((__unused__)),
 
244
                       uint check_if_opened __attribute__((__unused__)))
212
245
{
213
246
  return 0;
214
247
}
215
248
 
 
249
/* log event handlers */
 
250
 
 
251
bool Log_to_file_event_handler::
 
252
  log_error(enum loglevel level, const char *format,
 
253
            va_list args)
 
254
{
 
255
  return vprint_msg_to_log(level, format, args);
 
256
}
 
257
 
 
258
void Log_to_file_event_handler::init_pthread_objects()
 
259
{
 
260
  mysql_log.init_pthread_objects();
 
261
  mysql_slow_log.init_pthread_objects();
 
262
}
 
263
 
 
264
 
 
265
/** Wrapper around MYSQL_LOG::write() for slow log. */
 
266
 
 
267
bool Log_to_file_event_handler::
 
268
  log_slow(THD *thd, time_t current_time, time_t query_start_arg,
 
269
           const char *user_host, uint user_host_len,
 
270
           ulonglong query_utime, ulonglong lock_utime, bool is_command,
 
271
           const char *sql_text, uint sql_text_len)
 
272
{
 
273
  return mysql_slow_log.write(thd, current_time, query_start_arg,
 
274
                              user_host, user_host_len,
 
275
                              query_utime, lock_utime, is_command,
 
276
                              sql_text, sql_text_len);
 
277
}
 
278
 
 
279
 
 
280
/**
 
281
   Wrapper around MYSQL_LOG::write() for general log. We need it since we
 
282
   want all log event handlers to have the same signature.
 
283
*/
 
284
 
 
285
bool Log_to_file_event_handler::
 
286
  log_general(THD *thd __attribute__((__unused__)),
 
287
              time_t event_time, const char *user_host,
 
288
              uint user_host_len, int thread_id,
 
289
              const char *command_type, uint command_type_len,
 
290
              const char *sql_text, uint sql_text_len,
 
291
              CHARSET_INFO *client_cs __attribute__((__unused__)))
 
292
{
 
293
  return mysql_log.write(event_time, user_host, user_host_len,
 
294
                         thread_id, command_type, command_type_len,
 
295
                         sql_text, sql_text_len);
 
296
}
 
297
 
 
298
 
 
299
bool Log_to_file_event_handler::init()
 
300
{
 
301
  if (!is_initialized)
 
302
  {
 
303
    if (opt_slow_log)
 
304
      mysql_slow_log.open_slow_log(sys_var_slow_log_path.value);
 
305
 
 
306
    if (opt_log)
 
307
      mysql_log.open_query_log(sys_var_general_log_path.value);
 
308
 
 
309
    is_initialized= true;
 
310
  }
 
311
 
 
312
  return false;
 
313
}
 
314
 
 
315
 
 
316
void Log_to_file_event_handler::cleanup()
 
317
{
 
318
  mysql_log.cleanup();
 
319
  mysql_slow_log.cleanup();
 
320
}
 
321
 
 
322
void Log_to_file_event_handler::flush()
 
323
{
 
324
  /* reopen log files */
 
325
  if (opt_log)
 
326
    mysql_log.reopen_file();
 
327
  if (opt_slow_log)
 
328
    mysql_slow_log.reopen_file();
 
329
}
 
330
 
216
331
/*
217
332
  Log error with all enabled log event handlers
218
333
 
247
362
{
248
363
  assert(inited == 1);
249
364
  rwlock_destroy(&LOCK_logger);
 
365
  if (file_log_handler)
 
366
    file_log_handler->cleanup();
250
367
}
251
368
 
252
369
 
253
370
void LOGGER::cleanup_end()
254
371
{
255
372
  assert(inited == 1);
 
373
  if (file_log_handler)
 
374
    delete file_log_handler;
256
375
}
257
376
 
258
377
 
265
384
  assert(inited == 0);
266
385
  inited= 1;
267
386
 
 
387
  /*
 
388
    Here we create file log handler. We don't do it for the table log handler
 
389
    here as it cannot be created so early. The reason is THD initialization,
 
390
    which depends on the system variables (parsed later).
 
391
  */
 
392
  if (!file_log_handler)
 
393
    file_log_handler= new Log_to_file_event_handler;
 
394
 
268
395
  /* by default we use traditional error log */
269
396
  init_error_log(LOG_FILE);
270
397
 
 
398
  file_log_handler->init_pthread_objects();
271
399
  my_rwlock_init(&LOCK_logger, NULL);
272
400
}
273
401
 
274
402
 
275
 
bool LOGGER::flush_logs(THD *thd __attribute__((unused)))
 
403
bool LOGGER::flush_logs(THD *thd __attribute__((__unused__)))
276
404
{
277
405
  int rc= 0;
278
406
 
282
410
  */
283
411
  logger.lock_exclusive();
284
412
 
 
413
  /* reopen log files */
 
414
  file_log_handler->flush();
 
415
 
285
416
  /* end of log flush */
286
417
  logger.unlock();
287
418
  return rc;
288
419
}
289
420
 
290
 
void LOGGER::init_error_log(uint32_t error_log_printer)
 
421
 
 
422
/*
 
423
  Log slow query with all enabled log event handlers
 
424
 
 
425
  SYNOPSIS
 
426
    slow_log_print()
 
427
 
 
428
    thd                 THD of the query being logged
 
429
    query               The query being logged
 
430
    query_length        The length of the query string
 
431
    current_utime       Current time in microseconds (from undefined start)
 
432
 
 
433
  RETURN
 
434
    FALSE   OK
 
435
    TRUE    error occured
 
436
*/
 
437
 
 
438
bool LOGGER::slow_log_print(THD *thd, const char *query, uint query_length,
 
439
                            ulonglong current_utime)
 
440
 
 
441
{
 
442
  bool error= false;
 
443
  Log_event_handler **current_handler;
 
444
  bool is_command= false;
 
445
  char user_host_buff[MAX_USER_HOST_SIZE];
 
446
  Security_context *sctx= thd->security_ctx;
 
447
  uint user_host_len= 0;
 
448
  ulonglong query_utime, lock_utime;
 
449
 
 
450
  /*
 
451
    Print the message to the buffer if we have slow log enabled
 
452
  */
 
453
 
 
454
  if (*slow_log_handler_list)
 
455
  {
 
456
    time_t current_time;
 
457
 
 
458
    /* do not log slow queries from replication threads */
 
459
    if (thd->slave_thread && !opt_log_slow_slave_statements)
 
460
      return 0;
 
461
 
 
462
    lock_shared();
 
463
    if (!opt_slow_log)
 
464
    {
 
465
      unlock();
 
466
      return 0;
 
467
    }
 
468
 
 
469
    /* fill in user_host value: the format is "%s[%s] @ %s [%s]" */
 
470
    user_host_len= (strxnmov(user_host_buff, MAX_USER_HOST_SIZE,
 
471
                             sctx->priv_user ? sctx->priv_user : "", "[",
 
472
                             sctx->user ? sctx->user : "", "] @ ",
 
473
                             sctx->host ? sctx->host : "", " [",
 
474
                             sctx->ip ? sctx->ip : "", "]", NullS) -
 
475
                    user_host_buff);
 
476
 
 
477
    current_time= my_time_possible_from_micro(current_utime);
 
478
    if (thd->start_utime)
 
479
    {
 
480
      query_utime= (current_utime - thd->start_utime);
 
481
      lock_utime=  (thd->utime_after_lock - thd->start_utime);
 
482
    }
 
483
    else
 
484
    {
 
485
      query_utime= lock_utime= 0;
 
486
    }
 
487
 
 
488
    if (!query)
 
489
    {
 
490
      is_command= true;
 
491
      query= command_name[thd->command].str;
 
492
      query_length= command_name[thd->command].length;
 
493
    }
 
494
 
 
495
    for (current_handler= slow_log_handler_list; *current_handler ;)
 
496
      error= (*current_handler++)->log_slow(thd, current_time, thd->start_time,
 
497
                                            user_host_buff, user_host_len,
 
498
                                            query_utime, lock_utime, is_command,
 
499
                                            query, query_length) || error;
 
500
 
 
501
    unlock();
 
502
  }
 
503
  return error;
 
504
}
 
505
 
 
506
bool LOGGER::general_log_write(THD *thd, enum enum_server_command command,
 
507
                               const char *query, uint query_length)
 
508
{
 
509
  bool error= false;
 
510
  Log_event_handler **current_handler= general_log_handler_list;
 
511
  char user_host_buff[MAX_USER_HOST_SIZE];
 
512
  Security_context *sctx= thd->security_ctx;
 
513
  ulong id;
 
514
  uint user_host_len= 0;
 
515
  time_t current_time;
 
516
 
 
517
  if (thd)
 
518
    id= thd->thread_id;                 /* Normal thread */
 
519
  else
 
520
    id= 0;                              /* Log from connect handler */
 
521
 
 
522
  lock_shared();
 
523
  if (!opt_log)
 
524
  {
 
525
    unlock();
 
526
    return 0;
 
527
  }
 
528
  user_host_len= strxnmov(user_host_buff, MAX_USER_HOST_SIZE,
 
529
                          sctx->priv_user ? sctx->priv_user : "", "[",
 
530
                          sctx->user ? sctx->user : "", "] @ ",
 
531
                          sctx->host ? sctx->host : "", " [",
 
532
                          sctx->ip ? sctx->ip : "", "]", NullS) -
 
533
                                                          user_host_buff;
 
534
 
 
535
  current_time= my_time(0);
 
536
 
 
537
  while (*current_handler)
 
538
    error|= (*current_handler++)->
 
539
      log_general(thd, current_time, user_host_buff,
 
540
                  user_host_len, id,
 
541
                  command_name[(uint) command].str,
 
542
                  command_name[(uint) command].length,
 
543
                  query, query_length,
 
544
                  thd->variables.character_set_client) || error;
 
545
  unlock();
 
546
 
 
547
  return error;
 
548
}
 
549
 
 
550
bool LOGGER::general_log_print(THD *thd, enum enum_server_command command,
 
551
                               const char *format, va_list args)
 
552
{
 
553
  uint message_buff_len= 0;
 
554
  char message_buff[MAX_LOG_BUFFER_SIZE];
 
555
 
 
556
  /* prepare message */
 
557
  if (format)
 
558
    message_buff_len= vsnprintf(message_buff, sizeof(message_buff),
 
559
                                   format, args);
 
560
  else
 
561
    message_buff[0]= '\0';
 
562
 
 
563
  return general_log_write(thd, command, message_buff, message_buff_len);
 
564
}
 
565
 
 
566
void LOGGER::init_error_log(uint error_log_printer)
291
567
{
292
568
  if (error_log_printer & LOG_NONE)
293
569
  {
295
571
    return;
296
572
  }
297
573
 
298
 
}
299
 
 
300
 
int LOGGER::set_handlers(uint32_t error_log_printer)
 
574
  switch (error_log_printer) {
 
575
  case LOG_FILE:
 
576
    error_log_handler_list[0]= file_log_handler;
 
577
    error_log_handler_list[1]= 0;
 
578
    break;
 
579
    /* these two are disabled for now */
 
580
  case LOG_TABLE:
 
581
    assert(0);
 
582
    break;
 
583
  case LOG_TABLE|LOG_FILE:
 
584
    assert(0);
 
585
    break;
 
586
  }
 
587
}
 
588
 
 
589
void LOGGER::init_slow_log(uint slow_log_printer)
 
590
{
 
591
  if (slow_log_printer & LOG_NONE)
 
592
  {
 
593
    slow_log_handler_list[0]= 0;
 
594
    return;
 
595
  }
 
596
 
 
597
  slow_log_handler_list[0]= file_log_handler;
 
598
  slow_log_handler_list[1]= 0;
 
599
}
 
600
 
 
601
void LOGGER::init_general_log(uint general_log_printer)
 
602
{
 
603
  if (general_log_printer & LOG_NONE)
 
604
  {
 
605
    general_log_handler_list[0]= 0;
 
606
    return;
 
607
  }
 
608
 
 
609
  general_log_handler_list[0]= file_log_handler;
 
610
  general_log_handler_list[1]= 0;
 
611
}
 
612
 
 
613
 
 
614
bool LOGGER::activate_log_handler(THD* thd __attribute__((__unused__)),
 
615
                                  uint log_type)
 
616
{
 
617
  MYSQL_QUERY_LOG *file_log;
 
618
  bool res= false;
 
619
  lock_exclusive();
 
620
  switch (log_type) {
 
621
  case QUERY_LOG_SLOW:
 
622
    if (!opt_slow_log)
 
623
    {
 
624
      file_log= file_log_handler->get_mysql_slow_log();
 
625
 
 
626
      file_log->open_slow_log(sys_var_slow_log_path.value);
 
627
      init_slow_log(log_output_options);
 
628
      opt_slow_log= true;
 
629
    }
 
630
    break;
 
631
  case QUERY_LOG_GENERAL:
 
632
    if (!opt_log)
 
633
    {
 
634
      file_log= file_log_handler->get_mysql_log();
 
635
 
 
636
      file_log->open_query_log(sys_var_general_log_path.value);
 
637
      init_general_log(log_output_options);
 
638
      opt_log= true;
 
639
    }
 
640
    break;
 
641
  default:
 
642
    assert(0);
 
643
  }
 
644
  unlock();
 
645
  return res;
 
646
}
 
647
 
 
648
 
 
649
void LOGGER::deactivate_log_handler(THD *thd __attribute__((__unused__)),
 
650
                                    uint log_type)
 
651
{
 
652
  my_bool *tmp_opt= 0;
 
653
  MYSQL_LOG *file_log;
 
654
 
 
655
  switch (log_type) {
 
656
  case QUERY_LOG_SLOW:
 
657
    tmp_opt= &opt_slow_log;
 
658
    file_log= file_log_handler->get_mysql_slow_log();
 
659
    break;
 
660
  case QUERY_LOG_GENERAL:
 
661
    tmp_opt= &opt_log;
 
662
    file_log= file_log_handler->get_mysql_log();
 
663
    break;
 
664
  default:
 
665
    assert(0);                                  // Impossible
 
666
  }
 
667
 
 
668
  if (!(*tmp_opt))
 
669
    return;
 
670
 
 
671
  lock_exclusive();
 
672
  file_log->close(0);
 
673
  *tmp_opt= false;
 
674
  unlock();
 
675
}
 
676
 
 
677
int LOGGER::set_handlers(uint error_log_printer,
 
678
                         uint slow_log_printer,
 
679
                         uint general_log_printer)
301
680
{
302
681
  /* error log table is not supported yet */
 
682
  assert(error_log_printer < LOG_TABLE);
 
683
 
303
684
  lock_exclusive();
304
685
 
305
686
  init_error_log(error_log_printer);
 
687
  init_slow_log(slow_log_printer);
 
688
  init_general_log(general_log_printer);
 
689
 
306
690
  unlock();
307
691
 
308
692
  return 0;
369
753
 
370
754
/*
371
755
  this function is mostly a placeholder.
372
 
  conceptually, binlog initialization (now mostly done in DRIZZLE_BIN_LOG::open)
 
756
  conceptually, binlog initialization (now mostly done in MYSQL_BIN_LOG::open)
373
757
  should be moved here.
374
758
*/
375
759
 
377
761
{
378
762
  binlog_hton= (handlerton *)p;
379
763
  binlog_hton->state=opt_bin_log ? SHOW_OPTION_YES : SHOW_OPTION_NO;
 
764
  binlog_hton->db_type=DB_TYPE_BINLOG;
380
765
  binlog_hton->savepoint_offset= sizeof(my_off_t);
381
766
  binlog_hton->close_connection= binlog_close_connection;
382
767
  binlog_hton->savepoint_set= binlog_savepoint_set;
385
770
  binlog_hton->rollback= binlog_rollback;
386
771
  binlog_hton->prepare= binlog_prepare;
387
772
  binlog_hton->flags= HTON_NOT_USER_SELECTABLE | HTON_HIDDEN;
388
 
 
389
773
  return 0;
390
774
}
391
775
 
392
 
static int binlog_close_connection(handlerton *hton __attribute__((unused)),
 
776
static int binlog_close_connection(handlerton *hton __attribute__((__unused__)),
393
777
                                   THD *thd)
394
778
{
395
779
  binlog_trx_data *const trx_data=
397
781
  assert(trx_data->empty());
398
782
  thd_set_ha_data(thd, binlog_hton, NULL);
399
783
  trx_data->~binlog_trx_data();
400
 
  free((unsigned char*)trx_data);
 
784
  my_free((uchar*)trx_data, MYF(0));
401
785
  return 0;
402
786
}
403
787
 
496
880
  return(error);
497
881
}
498
882
 
499
 
static int binlog_prepare(handlerton *hton __attribute__((unused)),
500
 
                          THD *thd __attribute__((unused)),
501
 
                          bool all __attribute__((unused)))
 
883
static int binlog_prepare(handlerton *hton __attribute__((__unused__)),
 
884
                          THD *thd __attribute__((__unused__)),
 
885
                          bool all __attribute__((__unused__)))
502
886
{
503
887
  /*
504
888
    do nothing.
505
889
    just pretend we can do 2pc, so that MySQL won't
506
890
    switch to 1pc.
507
 
    real work will be done in DRIZZLE_BIN_LOG::log_xid()
 
891
    real work will be done in MYSQL_BIN_LOG::log_xid()
508
892
  */
509
893
  return 0;
510
894
}
524
908
 
525
909
  @see handlerton::commit
526
910
*/
527
 
static int binlog_commit(handlerton *hton __attribute__((unused)),
 
911
static int binlog_commit(handlerton *hton __attribute__((__unused__)),
528
912
                         THD *thd, bool all)
529
913
{
530
914
  binlog_trx_data *const trx_data=
532
916
 
533
917
  if (trx_data->empty())
534
918
  {
535
 
    // we're here because trans_log was flushed in DRIZZLE_BIN_LOG::log_xid()
 
919
    // we're here because trans_log was flushed in MYSQL_BIN_LOG::log_xid()
536
920
    trx_data->reset();
537
921
    return(0);
538
922
  }
595
979
 
596
980
    Otherwise, we accumulate the statement
597
981
  */
598
 
  uint64_t const in_transaction=
 
982
  ulonglong const in_transaction=
599
983
    thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN);
600
984
  if ((in_transaction && (all || (!trx_data->at_least_one_stmt && thd->transaction.stmt.modified_non_trans_table))) || (!in_transaction && !all))
601
985
  {
602
986
    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)
 
987
    qev.error_code= 0; // see comment in MYSQL_LOG::write(THD, IO_CACHE)
604
988
    int error= binlog_end_trans(thd, trx_data, &qev, all);
605
989
    return(error);
606
990
  }
622
1006
 
623
1007
  @see handlerton::rollback
624
1008
*/
625
 
static int binlog_rollback(handlerton *hton __attribute__((unused)),
 
1009
static int binlog_rollback(handlerton *hton __attribute__((__unused__)),
626
1010
                           THD *thd, bool all)
627
1011
{
628
1012
  int error=0;
647
1031
      rolled back on the slave.
648
1032
    */
649
1033
    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)
 
1034
    qev.error_code= 0; // see comment in MYSQL_LOG::write(THD, IO_CACHE)
651
1035
    error= binlog_end_trans(thd, trx_data, &qev, all);
652
1036
  }
653
1037
  else if ((all && !thd->transaction.all.modified_non_trans_table) ||
687
1071
  that case there is no need to have it in the binlog).
688
1072
*/
689
1073
 
690
 
static int binlog_savepoint_set(handlerton *hton __attribute__((unused)),
 
1074
static int binlog_savepoint_set(handlerton *hton __attribute__((__unused__)),
691
1075
                                THD *thd, void *sv)
692
1076
{
693
1077
  binlog_trans_log_savepos(thd, (my_off_t*) sv);
699
1083
  return(error);
700
1084
}
701
1085
 
702
 
static int binlog_savepoint_rollback(handlerton *hton __attribute__((unused)),
 
1086
static int binlog_savepoint_rollback(handlerton *hton __attribute__((__unused__)),
703
1087
                                     THD *thd, void *sv)
704
1088
{
705
1089
  /*
725
1109
  char magic[4];
726
1110
  assert(my_b_tell(log) == 0);
727
1111
 
728
 
  if (my_b_read(log, (unsigned char*) magic, sizeof(magic)))
 
1112
  if (my_b_read(log, (uchar*) magic, sizeof(magic)))
729
1113
  {
730
 
    *errmsg = _("I/O error reading the header from the binary log");
 
1114
    *errmsg = "I/O error reading the header from the binary log";
731
1115
    sql_print_error("%s, errno=%d, io cache code=%d", *errmsg, my_errno,
732
1116
                    log->error);
733
1117
    return 1;
734
1118
  }
735
1119
  if (memcmp(magic, BINLOG_MAGIC, sizeof(magic)))
736
1120
  {
737
 
    *errmsg = _("Binlog has bad magic number;  It's not a binary log file "
738
 
                "that can be used by this version of Drizzle");
 
1121
    *errmsg = "Binlog has bad magic number;  It's not a binary log file that can be used by this version of MySQL";
739
1122
    return 1;
740
1123
  }
741
1124
  return 0;
749
1132
  if ((file = my_open(log_file_name, O_RDONLY | O_BINARY | O_SHARE, 
750
1133
                      MYF(MY_WME))) < 0)
751
1134
  {
752
 
    sql_print_error(_("Failed to open log (file '%s', errno %d)"),
 
1135
    sql_print_error("Failed to open log (file '%s', errno %d)",
753
1136
                    log_file_name, my_errno);
754
 
    *errmsg = _("Could not open log file");
 
1137
    *errmsg = "Could not open log file";
755
1138
    goto err;
756
1139
  }
757
1140
  if (init_io_cache(log, file, IO_SIZE*2, READ_CACHE, 0, 0,
758
1141
                    MYF(MY_WME|MY_DONT_CHECK_FILESIZE)))
759
1142
  {
760
 
    sql_print_error(_("Failed to create a cache on log (file '%s')"),
 
1143
    sql_print_error("Failed to create a cache on log (file '%s')",
761
1144
                    log_file_name);
762
 
    *errmsg = _("Could not open log file");
 
1145
    *errmsg = "Could not open log file";
763
1146
    goto err;
764
1147
  }
765
1148
  if (check_binlog_magic(log,errmsg))
788
1171
static int find_uniq_filename(char *name)
789
1172
{
790
1173
  long                  number;
791
 
  uint32_t                  i;
 
1174
  uint                  i;
792
1175
  char                  buff[FN_REFLEN];
793
1176
  struct st_my_dir     *dir_info;
794
1177
  register struct fileinfo *file_info;
798
1181
 
799
1182
  length= dirname_part(buff, name, &buf_length);
800
1183
  start=  name + length;
801
 
  end= strchr(start, '\0');
 
1184
  end=    strend(start);
802
1185
 
803
1186
  *end='.';
804
1187
  length= (size_t) (end-start+1);
805
1188
 
806
1189
  if (!(dir_info = my_dir(buff,MYF(MY_DONT_SORT))))
807
1190
  {                                             // This shouldn't happen
808
 
    my_stpcpy(end,".1");                                // use name+1
 
1191
    strmov(end,".1");                           // use name+1
809
1192
    return(0);
810
1193
  }
811
1194
  file_info= dir_info->dir_entry;
812
1195
  for (i=dir_info->number_off_files ; i-- ; file_info++)
813
1196
  {
814
 
    if (memcmp(file_info->name, start, length) == 0 &&
 
1197
    if (bcmp((uchar*) file_info->name, (uchar*) start, length) == 0 &&
815
1198
        test_if_number(file_info->name+length, &number,0))
816
1199
    {
817
1200
      set_if_bigger(max_found,(ulong) number);
825
1208
}
826
1209
 
827
1210
 
828
 
void DRIZZLE_LOG::init(enum_log_type log_type_arg,
 
1211
void MYSQL_LOG::init(enum_log_type log_type_arg,
829
1212
                     enum cache_type io_cache_type_arg)
830
1213
{
831
1214
  log_type= log_type_arg;
855
1238
    1   error
856
1239
*/
857
1240
 
858
 
bool DRIZZLE_LOG::open(const char *log_name, enum_log_type log_type_arg,
 
1241
bool MYSQL_LOG::open(const char *log_name, enum_log_type log_type_arg,
859
1242
                     const char *new_name, enum cache_type io_cache_type_arg)
860
1243
{
861
1244
  char buff[FN_REFLEN];
873
1256
  }
874
1257
 
875
1258
  if (new_name)
876
 
    my_stpcpy(log_file_name, new_name);
 
1259
    strmov(log_file_name, new_name);
877
1260
  else if (generate_new_name(log_file_name, name))
878
1261
    goto err;
879
1262
 
897
1280
    char *end;
898
1281
    int len=snprintf(buff, sizeof(buff), "%s, Version: %s (%s). "
899
1282
                     "started with:\nTCP Port: %d, Named Pipe: %s\n",
900
 
                     my_progname, server_version, DRIZZLE_COMPILATION_COMMENT,
 
1283
                     my_progname, server_version, MYSQL_COMPILATION_COMMENT,
901
1284
                     mysqld_port, ""
902
1285
                     );
903
 
    end= my_stpncpy(buff + len, "Time                 Id Command    Argument\n",
 
1286
    end= strnmov(buff + len, "Time                 Id Command    Argument\n",
904
1287
                 sizeof(buff) - len);
905
 
    if (my_b_write(&log_file, (unsigned char*) buff, (uint) (end-buff)) ||
 
1288
    if (my_b_write(&log_file, (uchar*) buff, (uint) (end-buff)) ||
906
1289
        flush_io_cache(&log_file))
907
1290
      goto err;
908
1291
  }
911
1294
  return(0);
912
1295
 
913
1296
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);
 
1297
  sql_print_error("Could not use %s for logging (error %d). \
 
1298
Turning logging off for the whole duration of the MySQL server process. \
 
1299
To turn it on again: fix the cause, \
 
1300
shutdown the MySQL server and restart it.", name, errno);
920
1301
  if (file >= 0)
921
1302
    my_close(file, MYF(0));
922
1303
  end_io_cache(&log_file);
923
 
  if (name)
924
 
  {
925
 
    free(name);
926
 
    name= NULL;
927
 
  }
 
1304
  safeFree(name);
928
1305
  log_state= LOG_CLOSED;
929
1306
  return(1);
930
1307
}
931
1308
 
932
 
DRIZZLE_LOG::DRIZZLE_LOG()
 
1309
MYSQL_LOG::MYSQL_LOG()
933
1310
  : name(0), write_error(false), inited(false), log_type(LOG_UNKNOWN),
934
1311
    log_state(LOG_CLOSED)
935
1312
{
939
1316
    called only in main(). Doing initialization here would make it happen
940
1317
    before main().
941
1318
  */
942
 
  memset(&log_file, 0, sizeof(log_file));
 
1319
  bzero((char*) &log_file, sizeof(log_file));
943
1320
}
944
1321
 
945
 
void DRIZZLE_LOG::init_pthread_objects()
 
1322
void MYSQL_LOG::init_pthread_objects()
946
1323
{
947
1324
  assert(inited == 0);
948
1325
  inited= 1;
963
1340
    The internal structures are not freed until cleanup() is called
964
1341
*/
965
1342
 
966
 
void DRIZZLE_LOG::close(uint32_t exiting)
 
1343
void MYSQL_LOG::close(uint exiting)
967
1344
{                                       // One can't set log_type here!
968
1345
  if (log_state == LOG_OPENED)
969
1346
  {
983
1360
  }
984
1361
 
985
1362
  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
 
  }
 
1363
  safeFree(name);
991
1364
  return;
992
1365
}
993
1366
 
994
1367
/** This is called only once. */
995
1368
 
996
 
void DRIZZLE_LOG::cleanup()
 
1369
void MYSQL_LOG::cleanup()
997
1370
{
998
1371
  if (inited)
999
1372
  {
1005
1378
}
1006
1379
 
1007
1380
 
1008
 
int DRIZZLE_LOG::generate_new_name(char *new_name, const char *log_name)
 
1381
int MYSQL_LOG::generate_new_name(char *new_name, const char *log_name)
1009
1382
{
1010
1383
  fn_format(new_name, log_name, mysql_data_home, "", 4);
1011
1384
  if (log_type == LOG_BIN)
1023
1396
}
1024
1397
 
1025
1398
 
 
1399
/*
 
1400
  Reopen the log file
 
1401
 
 
1402
  SYNOPSIS
 
1403
    reopen_file()
 
1404
 
 
1405
  DESCRIPTION
 
1406
    Reopen the log file. The method is used during FLUSH LOGS
 
1407
    and locks LOCK_log mutex
 
1408
*/
 
1409
 
 
1410
 
 
1411
void MYSQL_QUERY_LOG::reopen_file()
 
1412
{
 
1413
  char *save_name;
 
1414
 
 
1415
  if (!is_open())
 
1416
  {
 
1417
    return;
 
1418
  }
 
1419
 
 
1420
  pthread_mutex_lock(&LOCK_log);
 
1421
 
 
1422
  save_name= name;
 
1423
  name= 0;                              // Don't free name
 
1424
  close(LOG_CLOSE_TO_BE_OPENED);
 
1425
 
 
1426
  /*
 
1427
     Note that at this point, log_state != LOG_CLOSED (important for is_open()).
 
1428
  */
 
1429
 
 
1430
  open(save_name, log_type, 0, io_cache_type);
 
1431
  my_free(save_name, MYF(0));
 
1432
 
 
1433
  pthread_mutex_unlock(&LOCK_log);
 
1434
 
 
1435
  return;
 
1436
}
 
1437
 
 
1438
 
 
1439
/*
 
1440
  Write a command to traditional general log file
 
1441
 
 
1442
  SYNOPSIS
 
1443
    write()
 
1444
 
 
1445
    event_time        command start timestamp
 
1446
    user_host         the pointer to the string with user@host info
 
1447
    user_host_len     length of the user_host string. this is computed once
 
1448
                      and passed to all general log  event handlers
 
1449
    thread_id         Id of the thread, issued a query
 
1450
    command_type      the type of the command being logged
 
1451
    command_type_len  the length of the string above
 
1452
    sql_text          the very text of the query being executed
 
1453
    sql_text_len      the length of sql_text string
 
1454
 
 
1455
  DESCRIPTION
 
1456
 
 
1457
   Log given command to to normal (not rotable) log file
 
1458
 
 
1459
  RETURN
 
1460
    FASE - OK
 
1461
    TRUE - error occured
 
1462
*/
 
1463
 
 
1464
bool MYSQL_QUERY_LOG::write(time_t event_time,
 
1465
                            const char *user_host __attribute__((__unused__)),
 
1466
                            uint user_host_len __attribute__((__unused__)),
 
1467
                            int thread_id,
 
1468
                            const char *command_type, uint command_type_len,
 
1469
                            const char *sql_text, uint sql_text_len)
 
1470
{
 
1471
  char buff[32];
 
1472
  uint length= 0;
 
1473
  char local_time_buff[MAX_TIME_SIZE];
 
1474
  struct tm start;
 
1475
  uint time_buff_len= 0;
 
1476
 
 
1477
  (void) pthread_mutex_lock(&LOCK_log);
 
1478
 
 
1479
  /* Test if someone closed between the is_open test and lock */
 
1480
  if (is_open())
 
1481
  {
 
1482
    /* Note that my_b_write() assumes it knows the length for this */
 
1483
      if (event_time != last_time)
 
1484
      {
 
1485
        last_time= event_time;
 
1486
 
 
1487
        localtime_r(&event_time, &start);
 
1488
 
 
1489
        time_buff_len= snprintf(local_time_buff, MAX_TIME_SIZE,
 
1490
                                "%02d%02d%02d %2d:%02d:%02d",
 
1491
                                start.tm_year % 100, start.tm_mon + 1,
 
1492
                                start.tm_mday, start.tm_hour,
 
1493
                                start.tm_min, start.tm_sec);
 
1494
 
 
1495
        if (my_b_write(&log_file, (uchar*) local_time_buff, time_buff_len))
 
1496
          goto err;
 
1497
      }
 
1498
      else
 
1499
        if (my_b_write(&log_file, (uchar*) "\t\t" ,2) < 0)
 
1500
          goto err;
 
1501
 
 
1502
      /* command_type, thread_id */
 
1503
      length= snprintf(buff, 32, "%5ld ", (long) thread_id);
 
1504
 
 
1505
    if (my_b_write(&log_file, (uchar*) buff, length))
 
1506
      goto err;
 
1507
 
 
1508
    if (my_b_write(&log_file, (uchar*) command_type, command_type_len))
 
1509
      goto err;
 
1510
 
 
1511
    if (my_b_write(&log_file, (uchar*) "\t", 1))
 
1512
      goto err;
 
1513
 
 
1514
    /* sql_text */
 
1515
    if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len))
 
1516
      goto err;
 
1517
 
 
1518
    if (my_b_write(&log_file, (uchar*) "\n", 1) ||
 
1519
        flush_io_cache(&log_file))
 
1520
      goto err;
 
1521
  }
 
1522
 
 
1523
  (void) pthread_mutex_unlock(&LOCK_log);
 
1524
  return false;
 
1525
err:
 
1526
 
 
1527
  if (!write_error)
 
1528
  {
 
1529
    write_error= 1;
 
1530
    sql_print_error(ER(ER_ERROR_ON_WRITE), name, errno);
 
1531
  }
 
1532
  (void) pthread_mutex_unlock(&LOCK_log);
 
1533
  return true;
 
1534
}
 
1535
 
 
1536
 
 
1537
/*
 
1538
  Log a query to the traditional slow log file
 
1539
 
 
1540
  SYNOPSIS
 
1541
    write()
 
1542
 
 
1543
    thd               THD of the query
 
1544
    current_time      current timestamp
 
1545
    query_start_arg   command start timestamp
 
1546
    user_host         the pointer to the string with user@host info
 
1547
    user_host_len     length of the user_host string. this is computed once
 
1548
                      and passed to all general log event handlers
 
1549
    query_utime       Amount of time the query took to execute (in microseconds)
 
1550
    lock_utime        Amount of time the query was locked (in microseconds)
 
1551
    is_command        The flag, which determines, whether the sql_text is a
 
1552
                      query or an administrator command.
 
1553
    sql_text          the very text of the query or administrator command
 
1554
                      processed
 
1555
    sql_text_len      the length of sql_text string
 
1556
 
 
1557
  DESCRIPTION
 
1558
 
 
1559
   Log a query to the slow log file.
 
1560
 
 
1561
  RETURN
 
1562
    FALSE - OK
 
1563
    TRUE - error occured
 
1564
*/
 
1565
 
 
1566
bool MYSQL_QUERY_LOG::write(THD *thd, time_t current_time,
 
1567
                            time_t query_start_arg __attribute__((__unused__)),
 
1568
                            const char *user_host,
 
1569
                            uint user_host_len, ulonglong query_utime,
 
1570
                            ulonglong lock_utime, bool is_command,
 
1571
                            const char *sql_text, uint sql_text_len)
 
1572
{
 
1573
  bool error= 0;
 
1574
 
 
1575
  (void) pthread_mutex_lock(&LOCK_log);
 
1576
 
 
1577
  if (!is_open())
 
1578
  {
 
1579
    (void) pthread_mutex_unlock(&LOCK_log);
 
1580
    return(0);
 
1581
  }
 
1582
 
 
1583
  if (is_open())
 
1584
  {                                             // Safety agains reopen
 
1585
    int tmp_errno= 0;
 
1586
    char buff[80], *end;
 
1587
    char query_time_buff[22+7], lock_time_buff[22+7];
 
1588
    uint buff_len;
 
1589
    end= buff;
 
1590
 
 
1591
    if (!(specialflag & SPECIAL_SHORT_LOG_FORMAT))
 
1592
    {
 
1593
      if (current_time != last_time)
 
1594
      {
 
1595
        last_time= current_time;
 
1596
        struct tm start;
 
1597
        localtime_r(&current_time, &start);
 
1598
 
 
1599
        buff_len= snprintf(buff, sizeof buff,
 
1600
                           "# Time: %02d%02d%02d %2d:%02d:%02d\n",
 
1601
                           start.tm_year % 100, start.tm_mon + 1,
 
1602
                           start.tm_mday, start.tm_hour,
 
1603
                           start.tm_min, start.tm_sec);
 
1604
 
 
1605
        /* Note that my_b_write() assumes it knows the length for this */
 
1606
        if (my_b_write(&log_file, (uchar*) buff, buff_len))
 
1607
          tmp_errno= errno;
 
1608
      }
 
1609
      const uchar uh[]= "# User@Host: ";
 
1610
      if (my_b_write(&log_file, uh, sizeof(uh) - 1))
 
1611
        tmp_errno= errno;
 
1612
      if (my_b_write(&log_file, (uchar*) user_host, user_host_len))
 
1613
        tmp_errno= errno;
 
1614
      if (my_b_write(&log_file, (uchar*) "\n", 1))
 
1615
        tmp_errno= errno;
 
1616
    }
 
1617
    /* For slow query log */
 
1618
    sprintf(query_time_buff, "%.6f", ulonglong2double(query_utime)/1000000.0);
 
1619
    sprintf(lock_time_buff,  "%.6f", ulonglong2double(lock_utime)/1000000.0);
 
1620
    if (my_b_printf(&log_file,
 
1621
                    "# Query_time: %s  Lock_time: %s"
 
1622
                    " Rows_sent: %lu  Rows_examined: %lu\n",
 
1623
                    query_time_buff, lock_time_buff,
 
1624
                    (ulong) thd->sent_row_count,
 
1625
                    (ulong) thd->examined_row_count) == (uint) -1)
 
1626
      tmp_errno= errno;
 
1627
    if (thd->db && strcmp(thd->db, db))
 
1628
    {                                           // Database changed
 
1629
      if (my_b_printf(&log_file,"use %s;\n",thd->db) == (uint) -1)
 
1630
        tmp_errno= errno;
 
1631
      strmov(db,thd->db);
 
1632
    }
 
1633
    if (thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt)
 
1634
    {
 
1635
      end=strmov(end, ",last_insert_id=");
 
1636
      end=longlong10_to_str((longlong)
 
1637
                            thd->first_successful_insert_id_in_prev_stmt_for_binlog,
 
1638
                            end, -10);
 
1639
    }
 
1640
    // Save value if we do an insert.
 
1641
    if (thd->auto_inc_intervals_in_cur_stmt_for_binlog.nb_elements() > 0)
 
1642
    {
 
1643
      if (!(specialflag & SPECIAL_SHORT_LOG_FORMAT))
 
1644
      {
 
1645
        end=strmov(end,",insert_id=");
 
1646
        end=longlong10_to_str((longlong)
 
1647
                              thd->auto_inc_intervals_in_cur_stmt_for_binlog.minimum(),
 
1648
                              end, -10);
 
1649
      }
 
1650
    }
 
1651
 
 
1652
    /*
 
1653
      This info used to show up randomly, depending on whether the query
 
1654
      checked the query start time or not. now we always write current
 
1655
      timestamp to the slow log
 
1656
    */
 
1657
    end= strmov(end, ",timestamp=");
 
1658
    end= int10_to_str((long) current_time, end, 10);
 
1659
 
 
1660
    if (end != buff)
 
1661
    {
 
1662
      *end++=';';
 
1663
      *end='\n';
 
1664
      if (my_b_write(&log_file, (uchar*) "SET ", 4) ||
 
1665
          my_b_write(&log_file, (uchar*) buff + 1, (uint) (end-buff)))
 
1666
        tmp_errno= errno;
 
1667
    }
 
1668
    if (is_command)
 
1669
    {
 
1670
      end= strxmov(buff, "# administrator command: ", NullS);
 
1671
      buff_len= (ulong) (end - buff);
 
1672
      my_b_write(&log_file, (uchar*) buff, buff_len);
 
1673
    }
 
1674
    if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len) ||
 
1675
        my_b_write(&log_file, (uchar*) ";\n",2) ||
 
1676
        flush_io_cache(&log_file))
 
1677
      tmp_errno= errno;
 
1678
    if (tmp_errno)
 
1679
    {
 
1680
      error= 1;
 
1681
      if (! write_error)
 
1682
      {
 
1683
        write_error= 1;
 
1684
        sql_print_error(ER(ER_ERROR_ON_WRITE), name, error);
 
1685
      }
 
1686
    }
 
1687
  }
 
1688
  (void) pthread_mutex_unlock(&LOCK_log);
 
1689
  return(error);
 
1690
}
 
1691
 
 
1692
 
1026
1693
/**
1027
1694
  @todo
1028
1695
  The following should be using fn_format();  We just need to
1029
1696
  first change fn_format() to cut the file name if it's too long.
1030
1697
*/
1031
 
const char *DRIZZLE_LOG::generate_name(const char *log_name,
 
1698
const char *MYSQL_LOG::generate_name(const char *log_name,
1032
1699
                                      const char *suffix,
1033
1700
                                      bool strip_ext, char *buff)
1034
1701
{
1042
1709
  if (strip_ext)
1043
1710
  {
1044
1711
    char *p= fn_ext(log_name);
1045
 
    uint32_t length= (uint) (p - log_name);
1046
 
    strmake(buff, log_name, cmin(length, (uint)FN_REFLEN));
 
1712
    uint length= (uint) (p - log_name);
 
1713
    strmake(buff, log_name, min(length, FN_REFLEN));
1047
1714
    return (const char*)buff;
1048
1715
  }
1049
1716
  return log_name;
1051
1718
 
1052
1719
 
1053
1720
 
1054
 
DRIZZLE_BIN_LOG::DRIZZLE_BIN_LOG()
 
1721
MYSQL_BIN_LOG::MYSQL_BIN_LOG()
1055
1722
  :bytes_written(0), prepared_xids(0), file_id(1), open_count(1),
1056
1723
   need_start_event(true), m_table_map_version(0),
1057
1724
   description_event_for_exec(0), description_event_for_queue(0)
1063
1730
    before main().
1064
1731
  */
1065
1732
  index_file_name[0] = 0;
1066
 
  memset(&index_file, 0, sizeof(index_file));
 
1733
  bzero((char*) &index_file, sizeof(index_file));
1067
1734
}
1068
1735
 
1069
1736
/* this is called only once */
1070
1737
 
1071
 
void DRIZZLE_BIN_LOG::cleanup()
 
1738
void MYSQL_BIN_LOG::cleanup()
1072
1739
{
1073
1740
  if (inited)
1074
1741
  {
1085
1752
 
1086
1753
 
1087
1754
/* Init binlog-specific vars */
1088
 
void DRIZZLE_BIN_LOG::init(bool no_auto_events_arg, ulong max_size_arg)
 
1755
void MYSQL_BIN_LOG::init(bool no_auto_events_arg, ulong max_size_arg)
1089
1756
{
1090
1757
  no_auto_events= no_auto_events_arg;
1091
1758
  max_size= max_size_arg;
1093
1760
}
1094
1761
 
1095
1762
 
1096
 
void DRIZZLE_BIN_LOG::init_pthread_objects()
 
1763
void MYSQL_BIN_LOG::init_pthread_objects()
1097
1764
{
1098
1765
  assert(inited == 0);
1099
1766
  inited= 1;
1103
1770
}
1104
1771
 
1105
1772
 
1106
 
bool DRIZZLE_BIN_LOG::open_index_file(const char *index_file_name_arg,
 
1773
bool MYSQL_BIN_LOG::open_index_file(const char *index_file_name_arg,
1107
1774
                                const char *log_name)
1108
1775
{
1109
1776
  File index_file_nr= -1;
1158
1825
    1   error
1159
1826
*/
1160
1827
 
1161
 
bool DRIZZLE_BIN_LOG::open(const char *log_name,
 
1828
bool MYSQL_BIN_LOG::open(const char *log_name,
1162
1829
                         enum_log_type log_type_arg,
1163
1830
                         const char *new_name,
1164
1831
                         enum cache_type io_cache_type_arg,
1171
1838
  write_error=0;
1172
1839
 
1173
1840
  /* open the main log file */
1174
 
  if (DRIZZLE_LOG::open(log_name, log_type_arg, new_name, io_cache_type_arg))
 
1841
  if (MYSQL_LOG::open(log_name, log_type_arg, new_name, io_cache_type_arg))
1175
1842
    return(1);                            /* all warnings issued */
1176
1843
 
1177
1844
  init(no_auto_events_arg, max_size_arg);
1191
1858
        an extension for the binary log files.
1192
1859
        In this case we write a standard header to it.
1193
1860
      */
1194
 
      if (my_b_safe_write(&log_file, (unsigned char*) BINLOG_MAGIC,
 
1861
      if (my_b_safe_write(&log_file, (uchar*) BINLOG_MAGIC,
1195
1862
                          BIN_LOG_HEADER_SIZE))
1196
1863
        goto err;
1197
1864
      bytes_written+= BIN_LOG_HEADER_SIZE;
1262
1929
        As this is a new log file, we write the file name to the index
1263
1930
        file. As every time we write to the index file, we sync it.
1264
1931
      */
1265
 
      if (my_b_write(&index_file, (unsigned char*) log_file_name,
 
1932
      if (my_b_write(&index_file, (uchar*) log_file_name,
1266
1933
                     strlen(log_file_name)) ||
1267
 
          my_b_write(&index_file, (unsigned char*) "\n", 1) ||
 
1934
          my_b_write(&index_file, (uchar*) "\n", 1) ||
1268
1935
          flush_io_cache(&index_file) ||
1269
1936
          my_sync(index_file.file, MYF(MY_WME)))
1270
1937
        goto err;
1275
1942
  return(0);
1276
1943
 
1277
1944
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);
 
1945
  sql_print_error("Could not use %s for logging (error %d). \
 
1946
Turning logging off for the whole duration of the MySQL server process. \
 
1947
To turn it on again: fix the cause, \
 
1948
shutdown the MySQL server and restart it.", name, errno);
1284
1949
  if (file >= 0)
1285
1950
    my_close(file,MYF(0));
1286
1951
  end_io_cache(&log_file);
1287
1952
  end_io_cache(&index_file);
1288
 
  if (name)
1289
 
  {
1290
 
    free(name);
1291
 
    name= NULL;
1292
 
  }
 
1953
  safeFree(name);
1293
1954
  log_state= LOG_CLOSED;
1294
1955
  return(1);
1295
1956
}
1296
1957
 
1297
1958
 
1298
 
int DRIZZLE_BIN_LOG::get_current_log(LOG_INFO* linfo)
 
1959
int MYSQL_BIN_LOG::get_current_log(LOG_INFO* linfo)
1299
1960
{
1300
1961
  pthread_mutex_lock(&LOCK_log);
1301
1962
  int ret = raw_get_current_log(linfo);
1303
1964
  return ret;
1304
1965
}
1305
1966
 
1306
 
int DRIZZLE_BIN_LOG::raw_get_current_log(LOG_INFO* linfo)
 
1967
int MYSQL_BIN_LOG::raw_get_current_log(LOG_INFO* linfo)
1307
1968
{
1308
1969
  strmake(linfo->log_file_name, log_file_name, sizeof(linfo->log_file_name)-1);
1309
1970
  linfo->pos = my_b_tell(&log_file);
1327
1988
    0   ok
1328
1989
*/
1329
1990
 
 
1991
#ifdef HAVE_REPLICATION
 
1992
 
1330
1993
static bool copy_up_file_and_fill(IO_CACHE *index_file, my_off_t offset)
1331
1994
{
1332
1995
  int bytes_read;
1333
1996
  my_off_t init_offset= offset;
1334
1997
  File file= index_file->file;
1335
 
  unsigned char io_buf[IO_SIZE*2];
 
1998
  uchar io_buf[IO_SIZE*2];
1336
1999
 
1337
2000
  for (;; offset+= bytes_read)
1338
2001
  {
1358
2021
  return(1);
1359
2022
}
1360
2023
 
 
2024
#endif /* HAVE_REPLICATION */
 
2025
 
1361
2026
/**
1362
2027
  Find the position in the log-index-file for the given log name.
1363
2028
 
1380
2045
    LOG_INFO_IO         Got IO error while reading file
1381
2046
*/
1382
2047
 
1383
 
int DRIZZLE_BIN_LOG::find_log_pos(LOG_INFO *linfo, const char *log_name,
 
2048
int MYSQL_BIN_LOG::find_log_pos(LOG_INFO *linfo, const char *log_name,
1384
2049
                            bool need_lock)
1385
2050
{
1386
2051
  int error= 0;
1387
2052
  char *fname= linfo->log_file_name;
1388
 
  uint32_t log_name_len= log_name ? (uint) strlen(log_name) : 0;
 
2053
  uint log_name_len= log_name ? (uint) strlen(log_name) : 0;
1389
2054
 
1390
2055
  /*
1391
2056
    Mutex needed because we need to make sure the file pointer does not
1400
2065
 
1401
2066
  for (;;)
1402
2067
  {
1403
 
    uint32_t length;
 
2068
    uint length;
1404
2069
    my_off_t offset= my_b_tell(&index_file);
1405
2070
    /* If we get 0 or 1 characters, this is the end of the file */
1406
2071
 
1453
2118
    LOG_INFO_IO         Got IO error while reading file
1454
2119
*/
1455
2120
 
1456
 
int DRIZZLE_BIN_LOG::find_next_log(LOG_INFO* linfo, bool need_lock)
 
2121
int MYSQL_BIN_LOG::find_next_log(LOG_INFO* linfo, bool need_lock)
1457
2122
{
1458
2123
  int error= 0;
1459
 
  uint32_t length;
 
2124
  uint length;
1460
2125
  char *fname= linfo->log_file_name;
1461
2126
 
1462
2127
  if (need_lock)
1500
2165
    1   error
1501
2166
*/
1502
2167
 
1503
 
bool DRIZZLE_BIN_LOG::reset_logs(THD* thd)
 
2168
bool MYSQL_BIN_LOG::reset_logs(THD* thd)
1504
2169
{
1505
2170
  LOG_INFO linfo;
1506
2171
  bool error=0;
1507
2172
  const char* save_name;
1508
2173
 
 
2174
  ha_reset_logs(thd);
1509
2175
  /*
1510
2176
    We need to get both locks to be sure that no one is trying to
1511
2177
    write to the index log file.
1519
2185
    thread. If the transaction involved MyISAM tables, it should go
1520
2186
    into binlog even on rollback.
1521
2187
  */
1522
 
  pthread_mutex_lock(&LOCK_thread_count);
 
2188
  VOID(pthread_mutex_lock(&LOCK_thread_count));
1523
2189
 
1524
2190
  /* Save variables so that we can reopen the log */
1525
2191
  save_name=name;
1528
2194
 
1529
2195
  /* First delete all old log files */
1530
2196
 
1531
 
  if (find_log_pos(&linfo, NULL, 0))
 
2197
  if (find_log_pos(&linfo, NullS, 0))
1532
2198
  {
1533
2199
    error=1;
1534
2200
    goto err;
1540
2206
    {
1541
2207
      if (my_errno == ENOENT) 
1542
2208
      {
1543
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2209
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1544
2210
                            ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1545
2211
                            linfo.log_file_name);
1546
 
        sql_print_information(_("Failed to delete file '%s'"),
 
2212
        sql_print_information("Failed to delete file '%s'",
1547
2213
                              linfo.log_file_name);
1548
2214
        my_errno= 0;
1549
2215
        error= 0;
1550
2216
      }
1551
2217
      else
1552
2218
      {
1553
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2219
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1554
2220
                            ER_BINLOG_PURGE_FATAL_ERR,
1555
 
                            _("a problem with deleting %s; "
 
2221
                            "a problem with deleting %s; "
1556
2222
                            "consider examining correspondence "
1557
2223
                            "of your binlog index file "
1558
 
                            "to the actual binlog files"),
 
2224
                            "to the actual binlog files",
1559
2225
                            linfo.log_file_name);
1560
2226
        error= 1;
1561
2227
        goto err;
1571
2237
  {
1572
2238
    if (my_errno == ENOENT) 
1573
2239
    {
1574
 
      push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2240
      push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1575
2241
                          ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1576
2242
                          index_file_name);
1577
 
      sql_print_information(_("Failed to delete file '%s'"),
 
2243
      sql_print_information("Failed to delete file '%s'",
1578
2244
                            index_file_name);
1579
2245
      my_errno= 0;
1580
2246
      error= 0;
1581
2247
    }
1582
2248
    else
1583
2249
    {
1584
 
      push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2250
      push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1585
2251
                          ER_BINLOG_PURGE_FATAL_ERR,
1586
2252
                          "a problem with deleting %s; "
1587
2253
                          "consider examining correspondence "
1596
2262
    need_start_event=1;
1597
2263
  if (!open_index_file(index_file_name, 0))
1598
2264
    open(save_name, log_type, 0, io_cache_type, no_auto_events, max_size, 0);
1599
 
  free((unsigned char*) save_name);
 
2265
  my_free((uchar*) save_name, MYF(0));
1600
2266
 
1601
2267
err:
1602
 
  pthread_mutex_unlock(&LOCK_thread_count);
 
2268
  VOID(pthread_mutex_unlock(&LOCK_thread_count));
1603
2269
  pthread_mutex_unlock(&LOCK_index);
1604
2270
  pthread_mutex_unlock(&LOCK_log);
1605
2271
  return(error);
1643
2309
    LOG_INFO_IO         Got IO error while reading file
1644
2310
*/
1645
2311
 
 
2312
#ifdef HAVE_REPLICATION
1646
2313
 
1647
 
int DRIZZLE_BIN_LOG::purge_first_log(Relay_log_info* rli, bool included)
 
2314
int MYSQL_BIN_LOG::purge_first_log(Relay_log_info* rli, bool included)
1648
2315
{
1649
2316
  int error;
1650
2317
 
1673
2340
    If included is true, we want the first relay log;
1674
2341
    otherwise we want the one after event_relay_log_name.
1675
2342
  */
1676
 
  if ((included && (error=find_log_pos(&rli->linfo, NULL, 0))) ||
 
2343
  if ((included && (error=find_log_pos(&rli->linfo, NullS, 0))) ||
1677
2344
      (!included &&
1678
2345
       ((error=find_log_pos(&rli->linfo, rli->event_relay_log_name, 0)) ||
1679
2346
        (error=find_next_log(&rli->linfo, 0)))))
1680
2347
  {
1681
2348
    char buff[22];
1682
 
    sql_print_error(_("next log error: %d  offset: %s  log: %s included: %d"),
 
2349
    sql_print_error("next log error: %d  offset: %s  log: %s included: %d",
1683
2350
                    error,
1684
2351
                    llstr(rli->linfo.index_file_offset,buff),
1685
2352
                    rli->group_relay_log_name,
1719
2386
  Update log index_file.
1720
2387
*/
1721
2388
 
1722
 
int DRIZZLE_BIN_LOG::update_log_index(LOG_INFO* log_info, bool need_update_threads)
 
2389
int MYSQL_BIN_LOG::update_log_index(LOG_INFO* log_info, bool need_update_threads)
1723
2390
{
1724
2391
  if (copy_up_file_and_fill(&index_file, log_info->index_file_start_offset))
1725
2392
    return LOG_INFO_IO;
1754
2421
                                stat() or my_delete()
1755
2422
*/
1756
2423
 
1757
 
int DRIZZLE_BIN_LOG::purge_logs(const char *to_log, 
 
2424
int MYSQL_BIN_LOG::purge_logs(const char *to_log, 
1758
2425
                          bool included,
1759
2426
                          bool need_mutex, 
1760
2427
                          bool need_update_threads, 
1761
 
                          uint64_t *decrease_log_space)
 
2428
                          ulonglong *decrease_log_space)
1762
2429
{
1763
2430
  int error;
1764
2431
  int ret = 0;
1774
2441
    File name exists in index file; delete until we find this file
1775
2442
    or a file that is used.
1776
2443
  */
1777
 
  if ((error=find_log_pos(&log_info, NULL, 0 /*no mutex*/)))
 
2444
  if ((error=find_log_pos(&log_info, NullS, 0 /*no mutex*/)))
1778
2445
    goto err;
1779
2446
  while ((strcmp(to_log,log_info.log_file_name) || (exit_loop=included)) &&
1780
2447
         !log_in_use(log_info.log_file_name))
1788
2455
          It's not fatal if we can't stat a log file that does not exist;
1789
2456
          If we could not stat, we won't delete.
1790
2457
        */     
1791
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2458
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1792
2459
                            ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1793
2460
                            log_info.log_file_name);
1794
 
        sql_print_information(_("Failed to execute stat() on file '%s'"),
 
2461
        sql_print_information("Failed to execute stat on file '%s'",
1795
2462
                              log_info.log_file_name);
1796
2463
        my_errno= 0;
1797
2464
      }
1800
2467
        /*
1801
2468
          Other than ENOENT are fatal
1802
2469
        */
1803
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2470
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1804
2471
                            ER_BINLOG_PURGE_FATAL_ERR,
1805
 
                            _("a problem with getting info on being purged %s; "
 
2472
                            "a problem with getting info on being purged %s; "
1806
2473
                            "consider examining correspondence "
1807
2474
                            "of your binlog index file "
1808
 
                            "to the actual binlog files"),
 
2475
                            "to the actual binlog files",
1809
2476
                            log_info.log_file_name);
1810
2477
        error= LOG_INFO_FATAL;
1811
2478
        goto err;
1822
2489
      {
1823
2490
        if (my_errno == ENOENT) 
1824
2491
        {
1825
 
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2492
          push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1826
2493
                              ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1827
2494
                              log_info.log_file_name);
1828
 
          sql_print_information(_("Failed to delete file '%s'"),
 
2495
          sql_print_information("Failed to delete file '%s'",
1829
2496
                                log_info.log_file_name);
1830
2497
          my_errno= 0;
1831
2498
        }
1832
2499
        else
1833
2500
        {
1834
 
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2501
          push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1835
2502
                              ER_BINLOG_PURGE_FATAL_ERR,
1836
 
                              _("a problem with deleting %s; "
 
2503
                              "a problem with deleting %s; "
1837
2504
                              "consider examining correspondence "
1838
2505
                              "of your binlog index file "
1839
 
                              "to the actual binlog files"),
 
2506
                              "to the actual binlog files",
1840
2507
                              log_info.log_file_name);
1841
2508
          if (my_errno == EMFILE)
1842
2509
          {
1848
2515
      }
1849
2516
    }
1850
2517
 
 
2518
    ha_binlog_index_purge_file(current_thd, log_info.log_file_name);
 
2519
 
1851
2520
    if (find_next_log(&log_info, 0) || exit_loop)
1852
2521
      break;
1853
2522
  }
1886
2555
                                stat() or my_delete()
1887
2556
*/
1888
2557
 
1889
 
int DRIZZLE_BIN_LOG::purge_logs_before_date(time_t purge_time)
 
2558
int MYSQL_BIN_LOG::purge_logs_before_date(time_t purge_time)
1890
2559
{
1891
2560
  int error;
1892
2561
  LOG_INFO log_info;
1899
2568
    or a file that is used or a file
1900
2569
    that is older than purge_time.
1901
2570
  */
1902
 
  if ((error=find_log_pos(&log_info, NULL, 0 /*no mutex*/)))
 
2571
  if ((error=find_log_pos(&log_info, NullS, 0 /*no mutex*/)))
1903
2572
    goto err;
1904
2573
 
1905
2574
  while (strcmp(log_file_name, log_info.log_file_name) &&
1912
2581
        /*
1913
2582
          It's not fatal if we can't stat a log file that does not exist.
1914
2583
        */     
1915
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2584
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1916
2585
                            ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1917
2586
                            log_info.log_file_name);
1918
 
        sql_print_information(_("Failed to execute stat() on file '%s'"),
 
2587
        sql_print_information("Failed to execute stat on file '%s'",
1919
2588
                              log_info.log_file_name);
1920
2589
        my_errno= 0;
1921
2590
      }
1924
2593
        /*
1925
2594
          Other than ENOENT are fatal
1926
2595
        */
1927
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2596
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1928
2597
                            ER_BINLOG_PURGE_FATAL_ERR,
1929
 
                            _("a problem with getting info on being purged %s; "
 
2598
                            "a problem with getting info on being purged %s; "
1930
2599
                            "consider examining correspondence "
1931
2600
                            "of your binlog index file "
1932
 
                            "to the actual binlog files"),
 
2601
                            "to the actual binlog files",
1933
2602
                            log_info.log_file_name);
1934
2603
        error= LOG_INFO_FATAL;
1935
2604
        goto err;
1944
2613
        if (my_errno == ENOENT) 
1945
2614
        {
1946
2615
          /* It's not fatal even if we can't delete a log file */
1947
 
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2616
          push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1948
2617
                              ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1949
2618
                              log_info.log_file_name);
1950
 
          sql_print_information(_("Failed to delete file '%s'"),
 
2619
          sql_print_information("Failed to delete file '%s'",
1951
2620
                                log_info.log_file_name);
1952
2621
          my_errno= 0;
1953
2622
        }
1954
2623
        else
1955
2624
        {
1956
 
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2625
          push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1957
2626
                              ER_BINLOG_PURGE_FATAL_ERR,
1958
 
                              _("a problem with deleting %s; "
 
2627
                              "a problem with deleting %s; "
1959
2628
                              "consider examining correspondence "
1960
2629
                              "of your binlog index file "
1961
 
                              "to the actual binlog files"),
 
2630
                              "to the actual binlog files",
1962
2631
                              log_info.log_file_name);
1963
2632
          error= LOG_INFO_FATAL;
1964
2633
          goto err;
1965
2634
        }
1966
2635
      }
 
2636
      ha_binlog_index_purge_file(current_thd, log_info.log_file_name);
1967
2637
    }
1968
2638
    if (find_next_log(&log_info, 0))
1969
2639
      break;
1979
2649
  pthread_mutex_unlock(&LOCK_index);
1980
2650
  return(error);
1981
2651
}
 
2652
#endif /* HAVE_REPLICATION */
1982
2653
 
1983
2654
 
1984
2655
/**
1990
2661
    If file name will be longer then FN_REFLEN it will be truncated
1991
2662
*/
1992
2663
 
1993
 
void DRIZZLE_BIN_LOG::make_log_name(char* buf, const char* log_ident)
 
2664
void MYSQL_BIN_LOG::make_log_name(char* buf, const char* log_ident)
1994
2665
{
1995
 
  uint32_t dir_len = dirname_length(log_file_name); 
 
2666
  uint dir_len = dirname_length(log_file_name); 
1996
2667
  if (dir_len >= FN_REFLEN)
1997
2668
    dir_len=FN_REFLEN-1;
1998
 
  my_stpncpy(buf, log_file_name, dir_len);
 
2669
  strnmov(buf, log_file_name, dir_len);
1999
2670
  strmake(buf+dir_len, log_ident, FN_REFLEN - dir_len -1);
2000
2671
}
2001
2672
 
2004
2675
  Check if we are writing/reading to the given log file.
2005
2676
*/
2006
2677
 
2007
 
bool DRIZZLE_BIN_LOG::is_active(const char *log_file_name_arg)
 
2678
bool MYSQL_BIN_LOG::is_active(const char *log_file_name_arg)
2008
2679
{
2009
2680
  return !strcmp(log_file_name, log_file_name_arg);
2010
2681
}
2018
2689
  method).
2019
2690
*/
2020
2691
 
2021
 
void DRIZZLE_BIN_LOG::new_file()
 
2692
void MYSQL_BIN_LOG::new_file()
2022
2693
{
2023
2694
  new_file_impl(1);
2024
2695
}
2025
2696
 
2026
2697
 
2027
 
void DRIZZLE_BIN_LOG::new_file_without_locking()
 
2698
void MYSQL_BIN_LOG::new_file_without_locking()
2028
2699
{
2029
2700
  new_file_impl(0);
2030
2701
}
2039
2710
    The new file name is stored last in the index file
2040
2711
*/
2041
2712
 
2042
 
void DRIZZLE_BIN_LOG::new_file_impl(bool need_lock)
 
2713
void MYSQL_BIN_LOG::new_file_impl(bool need_lock)
2043
2714
{
2044
2715
  char new_name[FN_REFLEN], *new_name_ptr, *old_name;
2045
2716
 
2125
2796
 
2126
2797
  open(old_name, log_type, new_name_ptr,
2127
2798
       io_cache_type, no_auto_events, max_size, 1);
2128
 
  free(old_name);
 
2799
  my_free(old_name,MYF(0));
2129
2800
 
2130
2801
end:
2131
2802
  if (need_lock)
2136
2807
}
2137
2808
 
2138
2809
 
2139
 
bool DRIZZLE_BIN_LOG::append(Log_event* ev)
 
2810
bool MYSQL_BIN_LOG::append(Log_event* ev)
2140
2811
{
2141
2812
  bool error = 0;
2142
2813
  pthread_mutex_lock(&LOCK_log);
2162
2833
}
2163
2834
 
2164
2835
 
2165
 
bool DRIZZLE_BIN_LOG::appendv(const char* buf, uint32_t len,...)
 
2836
bool MYSQL_BIN_LOG::appendv(const char* buf, uint len,...)
2166
2837
{
2167
2838
  bool error= 0;
2168
2839
  va_list(args);
2173
2844
  safe_mutex_assert_owner(&LOCK_log);
2174
2845
  do
2175
2846
  {
2176
 
    if (my_b_append(&log_file,(unsigned char*) buf,len))
 
2847
    if (my_b_append(&log_file,(uchar*) buf,len))
2177
2848
    {
2178
2849
      error= 1;
2179
2850
      goto err;
2190
2861
}
2191
2862
 
2192
2863
 
2193
 
bool DRIZZLE_BIN_LOG::flush_and_sync()
 
2864
bool MYSQL_BIN_LOG::flush_and_sync()
2194
2865
{
2195
2866
  int err=0, fd=log_file.file;
2196
2867
  safe_mutex_assert_owner(&LOCK_log);
2204
2875
  return err;
2205
2876
}
2206
2877
 
2207
 
void DRIZZLE_BIN_LOG::start_union_events(THD *thd, query_id_t query_id_param)
 
2878
void MYSQL_BIN_LOG::start_union_events(THD *thd, query_id_t query_id_param)
2208
2879
{
2209
2880
  assert(!thd->binlog_evt_union.do_union);
2210
2881
  thd->binlog_evt_union.do_union= true;
2213
2884
  thd->binlog_evt_union.first_query_id= query_id_param;
2214
2885
}
2215
2886
 
2216
 
void DRIZZLE_BIN_LOG::stop_union_events(THD *thd)
 
2887
void MYSQL_BIN_LOG::stop_union_events(THD *thd)
2217
2888
{
2218
2889
  assert(thd->binlog_evt_union.do_union);
2219
2890
  thd->binlog_evt_union.do_union= false;
2220
2891
}
2221
2892
 
2222
 
bool DRIZZLE_BIN_LOG::is_query_in_union(THD *thd, query_id_t query_id_param)
 
2893
bool MYSQL_BIN_LOG::is_query_in_union(THD *thd, query_id_t query_id_param)
2223
2894
{
2224
2895
  return (thd->binlog_evt_union.do_union && 
2225
2896
          query_id_param >= thd->binlog_evt_union.first_query_id);
2244
2915
      open_cached_file(&trx_data->trans_log, mysql_tmpdir,
2245
2916
                       LOG_PREFIX, binlog_cache_size, MYF(MY_WME)))
2246
2917
  {
2247
 
    free((unsigned char*)trx_data);
 
2918
    my_free((uchar*)trx_data, MYF(MY_ALLOW_ZERO_PTR));
2248
2919
    return(1);                      // Didn't manage to set it up
2249
2920
  }
2250
2921
  thd_set_ha_data(this, binlog_hton, trx_data);
2329
3000
  Write a table map to the binary log.
2330
3001
 */
2331
3002
 
2332
 
int THD::binlog_write_table_map(Table *table, bool is_trans)
 
3003
int THD::binlog_write_table_map(TABLE *table, bool is_trans)
2333
3004
{
2334
3005
  int error;
2335
3006
 
2336
3007
  /* Pre-conditions */
2337
3008
  assert(current_stmt_binlog_row_based && mysql_bin_log.is_open());
2338
 
  assert(table->s->table_map_id != UINT32_MAX);
 
3009
  assert(table->s->table_map_id != ULONG_MAX);
2339
3010
 
2340
3011
  Table_map_log_event::flag_set const
2341
3012
    flags= Table_map_log_event::TM_NO_FLAGS;
2388
3059
  event.
2389
3060
*/
2390
3061
int
2391
 
DRIZZLE_BIN_LOG::flush_and_set_pending_rows_event(THD *thd,
 
3062
MYSQL_BIN_LOG::flush_and_set_pending_rows_event(THD *thd,
2392
3063
                                                Rows_log_event* event)
2393
3064
{
2394
3065
  assert(mysql_bin_log.is_open());
2470
3141
  Write an event to the binary log.
2471
3142
*/
2472
3143
 
2473
 
bool DRIZZLE_BIN_LOG::write(Log_event *event_info)
 
3144
bool MYSQL_BIN_LOG::write(Log_event *event_info)
2474
3145
{
2475
3146
  THD *thd= event_info->thd;
2476
3147
  bool error= 1;
2518
3189
    if ((thd && !(thd->options & OPTION_BIN_LOG)) ||
2519
3190
        (!binlog_filter->db_ok(local_db)))
2520
3191
    {
2521
 
      pthread_mutex_unlock(&LOCK_log);
 
3192
      VOID(pthread_mutex_unlock(&LOCK_log));
2522
3193
      return(0);
2523
3194
    }
2524
3195
 
2574
3245
      {
2575
3246
        if (thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt)
2576
3247
        {
2577
 
          Intvar_log_event e(thd,(unsigned char) LAST_INSERT_ID_EVENT,
 
3248
          Intvar_log_event e(thd,(uchar) LAST_INSERT_ID_EVENT,
2578
3249
                             thd->first_successful_insert_id_in_prev_stmt_for_binlog);
2579
3250
          if (e.write(file))
2580
3251
            goto err;
2586
3257
            MyISAM or BDB) (table->next_number_keypart != 0), such event is
2587
3258
            in fact not necessary. We could avoid logging it.
2588
3259
          */
2589
 
          Intvar_log_event e(thd, (unsigned char) INSERT_ID_EVENT,
 
3260
          Intvar_log_event e(thd, (uchar) INSERT_ID_EVENT,
2590
3261
                             thd->auto_inc_intervals_in_cur_stmt_for_binlog.
2591
3262
                             minimum());
2592
3263
          if (e.write(file))
2600
3271
        }
2601
3272
        if (thd->user_var_events.elements)
2602
3273
        {
2603
 
          for (uint32_t i= 0; i < thd->user_var_events.elements; i++)
 
3274
          for (uint i= 0; i < thd->user_var_events.elements; i++)
2604
3275
          {
2605
3276
            BINLOG_USER_VAR_EVENT *user_var_event;
2606
 
            get_dynamic(&thd->user_var_events,(unsigned char*) &user_var_event, i);
 
3277
            get_dynamic(&thd->user_var_events,(uchar*) &user_var_event, i);
2607
3278
            User_var_log_event e(thd, user_var_event->user_var_event->name.str,
2608
3279
                                 user_var_event->user_var_event->name.length,
2609
3280
                                 user_var_event->value,
2658
3329
  return logger.error_log_print(level, format, args);
2659
3330
}
2660
3331
 
2661
 
void DRIZZLE_BIN_LOG::rotate_and_purge(uint32_t flags)
 
3332
 
 
3333
bool slow_log_print(THD *thd, const char *query, uint query_length,
 
3334
                    ulonglong current_utime)
 
3335
{
 
3336
  return logger.slow_log_print(thd, query, query_length, current_utime);
 
3337
}
 
3338
 
 
3339
 
 
3340
bool LOGGER::log_command(THD *thd, enum enum_server_command command)
 
3341
{
 
3342
  /*
 
3343
    Log command if we have at least one log event handler enabled and want
 
3344
    to log this king of commands
 
3345
  */
 
3346
  if (*general_log_handler_list && (what_to_log & (1L << (uint) command)))
 
3347
  {
 
3348
    if (thd->options & OPTION_LOG_OFF)
 
3349
    {
 
3350
      /* No logging */
 
3351
      return false;
 
3352
    }
 
3353
 
 
3354
    return true;
 
3355
  }
 
3356
 
 
3357
  return false;
 
3358
}
 
3359
 
 
3360
 
 
3361
bool general_log_print(THD *thd, enum enum_server_command command,
 
3362
                       const char *format, ...)
 
3363
{
 
3364
  va_list args;
 
3365
  uint error= 0;
 
3366
 
 
3367
  /* Print the message to the buffer if we want to log this king of commands */
 
3368
  if (! logger.log_command(thd, command))
 
3369
    return false;
 
3370
 
 
3371
  va_start(args, format);
 
3372
  error= logger.general_log_print(thd, command, format, args);
 
3373
  va_end(args);
 
3374
 
 
3375
  return error;
 
3376
}
 
3377
 
 
3378
bool general_log_write(THD *thd, enum enum_server_command command,
 
3379
                       const char *query, uint query_length)
 
3380
{
 
3381
  /* Write the message to the log if we want to log this king of commands */
 
3382
  if (logger.log_command(thd, command))
 
3383
    return logger.general_log_write(thd, command, query, query_length);
 
3384
 
 
3385
  return false;
 
3386
}
 
3387
 
 
3388
void MYSQL_BIN_LOG::rotate_and_purge(uint flags)
2662
3389
{
2663
3390
  if (!(flags & RP_LOCK_LOG_IS_ALREADY_LOCKED))
2664
3391
    pthread_mutex_lock(&LOCK_log);
2666
3393
      (my_b_tell(&log_file) >= (my_off_t) max_size))
2667
3394
  {
2668
3395
    new_file_without_locking();
 
3396
#ifdef HAVE_REPLICATION
2669
3397
    if (expire_logs_days)
2670
3398
    {
2671
3399
      time_t purge_time= my_time(0) - expire_logs_days*24*60*60;
2672
3400
      if (purge_time >= 0)
2673
3401
        purge_logs_before_date(purge_time);
2674
3402
    }
 
3403
#endif
2675
3404
  }
2676
3405
  if (!(flags & RP_LOCK_LOG_IS_ALREADY_LOCKED))
2677
3406
    pthread_mutex_unlock(&LOCK_log);
2678
3407
}
2679
3408
 
2680
 
uint32_t DRIZZLE_BIN_LOG::next_file_id()
 
3409
uint MYSQL_BIN_LOG::next_file_id()
2681
3410
{
2682
 
  uint32_t res;
 
3411
  uint res;
2683
3412
  pthread_mutex_lock(&LOCK_log);
2684
3413
  res = file_id++;
2685
3414
  pthread_mutex_unlock(&LOCK_log);
2701
3430
    be reset as a READ_CACHE to be able to read the contents from it.
2702
3431
 */
2703
3432
 
2704
 
int DRIZZLE_BIN_LOG::write_cache(IO_CACHE *cache, bool lock_log, bool sync_log)
 
3433
int MYSQL_BIN_LOG::write_cache(IO_CACHE *cache, bool lock_log, bool sync_log)
2705
3434
{
2706
3435
  Mutex_sentry sentry(lock_log ? &LOCK_log : NULL);
2707
3436
 
2708
3437
  if (reinit_io_cache(cache, READ_CACHE, 0, 0, 0))
2709
3438
    return ER_ERROR_ON_WRITE;
2710
 
  uint32_t length= my_b_bytes_in_cache(cache), group, carry, hdr_offs;
 
3439
  uint length= my_b_bytes_in_cache(cache), group, carry, hdr_offs;
2711
3440
  long val;
2712
 
  unsigned char header[LOG_EVENT_HEADER_LEN];
 
3441
  uchar header[LOG_EVENT_HEADER_LEN];
2713
3442
 
2714
3443
  /*
2715
3444
    The events in the buffer have incorrect end_log_pos data
2740
3469
      assert(carry < LOG_EVENT_HEADER_LEN);
2741
3470
 
2742
3471
      /* assemble both halves */
2743
 
      memcpy(&header[carry], cache->read_pos, LOG_EVENT_HEADER_LEN - carry);
 
3472
      memcpy(&header[carry], (char *)cache->read_pos, LOG_EVENT_HEADER_LEN - carry);
2744
3473
 
2745
3474
      /* fix end_log_pos */
2746
3475
      val= uint4korr(&header[LOG_POS_OFFSET]) + group;
2754
3483
        copy fixed second half of header to cache so the correct
2755
3484
        version will be written later.
2756
3485
      */
2757
 
      memcpy(cache->read_pos, &header[carry], LOG_EVENT_HEADER_LEN - carry);
 
3486
      memcpy((char *)cache->read_pos, &header[carry], LOG_EVENT_HEADER_LEN - carry);
2758
3487
 
2759
3488
      /* next event header at ... */
2760
3489
      hdr_offs = uint4korr(&header[EVENT_LEN_OFFSET]) - carry;
2783
3512
        if (hdr_offs + LOG_EVENT_HEADER_LEN > length)
2784
3513
        {
2785
3514
          carry= length - hdr_offs;
2786
 
          memcpy(header, cache->read_pos + hdr_offs, carry);
 
3515
          memcpy(header, (char *)cache->read_pos + hdr_offs, carry);
2787
3516
          length= hdr_offs;
2788
3517
        }
2789
3518
        else
2790
3519
        {
2791
3520
          /* we've got a full event-header, and it came in one piece */
2792
3521
 
2793
 
          unsigned char *log_pos= (unsigned char *)cache->read_pos + hdr_offs + LOG_POS_OFFSET;
 
3522
          uchar *log_pos= (uchar *)cache->read_pos + hdr_offs + LOG_POS_OFFSET;
2794
3523
 
2795
3524
          /* fix end_log_pos */
2796
3525
          val= uint4korr(log_pos) + group;
2797
3526
          int4store(log_pos, val);
2798
3527
 
2799
3528
          /* next event header at ... */
2800
 
          log_pos= (unsigned char *)cache->read_pos + hdr_offs + EVENT_LEN_OFFSET;
 
3529
          log_pos= (uchar *)cache->read_pos + hdr_offs + EVENT_LEN_OFFSET;
2801
3530
          hdr_offs += uint4korr(log_pos);
2802
3531
 
2803
3532
        }
2849
3578
    'cache' needs to be reinitialized after this functions returns.
2850
3579
*/
2851
3580
 
2852
 
bool DRIZZLE_BIN_LOG::write(THD *thd, IO_CACHE *cache, Log_event *commit_event)
 
3581
bool MYSQL_BIN_LOG::write(THD *thd, IO_CACHE *cache, Log_event *commit_event)
2853
3582
{
2854
 
  pthread_mutex_lock(&LOCK_log);
 
3583
  VOID(pthread_mutex_lock(&LOCK_log));
2855
3584
 
2856
3585
  /* NULL would represent nothing to replicate after ROLLBACK */
2857
3586
  assert(commit_event != NULL);
2927
3656
    else
2928
3657
      rotate_and_purge(RP_LOCK_LOG_IS_ALREADY_LOCKED);
2929
3658
  }
2930
 
  pthread_mutex_unlock(&LOCK_log);
 
3659
  VOID(pthread_mutex_unlock(&LOCK_log));
2931
3660
 
2932
3661
  return(0);
2933
3662
 
2937
3666
    write_error= 1;
2938
3667
    sql_print_error(ER(ER_ERROR_ON_WRITE), name, errno);
2939
3668
  }
2940
 
  pthread_mutex_unlock(&LOCK_log);
 
3669
  VOID(pthread_mutex_unlock(&LOCK_log));
2941
3670
  return(1);
2942
3671
}
2943
3672
 
2951
3680
    It will be released at the end of the function.
2952
3681
*/
2953
3682
 
2954
 
void DRIZZLE_BIN_LOG::wait_for_update_relay_log(THD* thd)
 
3683
void MYSQL_BIN_LOG::wait_for_update_relay_log(THD* thd)
2955
3684
{
2956
3685
  const char *old_msg;
2957
3686
  old_msg= thd->enter_cond(&update_cond, &LOCK_log,
2980
3709
    LOCK_log is released by the caller.
2981
3710
*/
2982
3711
 
2983
 
int DRIZZLE_BIN_LOG::wait_for_update_bin_log(THD* thd,
 
3712
int MYSQL_BIN_LOG::wait_for_update_bin_log(THD* thd,
2984
3713
                                           const struct timespec *timeout)
2985
3714
{
2986
3715
  int ret= 0;
2987
 
  const char* old_msg = thd->get_proc_info();
 
3716
  const char* old_msg = thd->proc_info;
2988
3717
  old_msg= thd->enter_cond(&update_cond, &LOCK_log,
2989
3718
                           "Master has sent all binlog to slave; "
2990
3719
                           "waiting for binlog to be updated");
3011
3740
    The internal structures are not freed until cleanup() is called
3012
3741
*/
3013
3742
 
3014
 
void DRIZZLE_BIN_LOG::close(uint32_t exiting)
 
3743
void MYSQL_BIN_LOG::close(uint exiting)
3015
3744
{                                       // One can't set log_type here!
3016
3745
  if (log_state == LOG_OPENED)
3017
3746
  {
 
3747
#ifdef HAVE_REPLICATION
3018
3748
    if (log_type == LOG_BIN && !no_auto_events &&
3019
3749
        (exiting & LOG_CLOSE_STOP_EVENT))
3020
3750
    {
3023
3753
      bytes_written+= s.data_written;
3024
3754
      signal_update();
3025
3755
    }
 
3756
#endif /* HAVE_REPLICATION */
3026
3757
 
3027
3758
    /* don't pwrite in a file opened with O_APPEND - it doesn't work */
3028
3759
    if (log_file.type == WRITE_CACHE && log_type == LOG_BIN)
3029
3760
    {
3030
3761
      my_off_t offset= BIN_LOG_HEADER_SIZE + FLAGS_OFFSET;
3031
 
      unsigned char flags= 0;            // clearing LOG_EVENT_BINLOG_IN_USE_F
 
3762
      uchar flags= 0;            // clearing LOG_EVENT_BINLOG_IN_USE_F
3032
3763
      pwrite(log_file.file, &flags, 1, offset);
3033
3764
    }
3034
3765
 
3035
3766
    /* this will cleanup IO_CACHE, sync and close the file */
3036
 
    DRIZZLE_LOG::close(exiting);
 
3767
    MYSQL_LOG::close(exiting);
3037
3768
  }
3038
3769
 
3039
3770
  /*
3051
3782
    }
3052
3783
  }
3053
3784
  log_state= (exiting & LOG_CLOSE_TO_BE_OPENED) ? LOG_TO_BE_OPENED : LOG_CLOSED;
3054
 
  if (name)
3055
 
  {
3056
 
    free(name);
3057
 
    name= NULL;
3058
 
  }
 
3785
  safeFree(name);
3059
3786
  return;
3060
3787
}
3061
3788
 
3062
3789
 
3063
 
void DRIZZLE_BIN_LOG::set_max_size(ulong max_size_arg)
 
3790
void MYSQL_BIN_LOG::set_max_size(ulong max_size_arg)
3064
3791
{
3065
3792
  /*
3066
3793
    We need to take locks, otherwise this may happen:
3128
3855
 
3129
3856
void sql_perror(const char *message)
3130
3857
{
 
3858
#ifdef HAVE_STRERROR
3131
3859
  sql_print_error("%s: %s",message, strerror(errno));
 
3860
#else
 
3861
  perror(message);
 
3862
#endif
3132
3863
}
3133
3864
 
3134
3865
 
3139
3870
  {
3140
3871
    char err_renamed[FN_REFLEN], *end;
3141
3872
    end= strmake(err_renamed,log_error_file,FN_REFLEN-4);
3142
 
    my_stpcpy(end, "-old");
3143
 
    pthread_mutex_lock(&LOCK_error_log);
 
3873
    strmov(end, "-old");
 
3874
    VOID(pthread_mutex_lock(&LOCK_error_log));
3144
3875
    char err_temp[FN_REFLEN+4];
3145
3876
    /*
3146
3877
     On Windows is necessary a temporary file for to rename
3147
3878
     the current error file.
3148
3879
    */
3149
 
    strxmov(err_temp, err_renamed,"-tmp",NULL);
 
3880
    strxmov(err_temp, err_renamed,"-tmp",NullS);
3150
3881
    (void) my_delete(err_temp, MYF(0)); 
3151
3882
    if (freopen(err_temp,"a+",stdout))
3152
3883
    {
3153
3884
      int fd;
3154
3885
      size_t bytes;
3155
 
      unsigned char buf[IO_SIZE];
 
3886
      uchar buf[IO_SIZE];
3156
3887
 
3157
3888
      freopen(err_temp,"a+",stderr);
3158
3889
      (void) my_delete(err_renamed, MYF(0));
3171
3902
    }
3172
3903
    else
3173
3904
     result= 1;
3174
 
    pthread_mutex_unlock(&LOCK_error_log);
 
3905
    VOID(pthread_mutex_unlock(&LOCK_error_log));
3175
3906
  }
3176
3907
   return result;
3177
3908
}
3178
3909
 
3179
 
void DRIZZLE_BIN_LOG::signal_update()
 
3910
void MYSQL_BIN_LOG::signal_update()
3180
3911
{
3181
3912
  pthread_cond_broadcast(&update_cond);
3182
3913
  return;
3199
3930
    return an error (e.g. logging to the log tables)
3200
3931
*/
3201
3932
static void print_buffer_to_file(enum loglevel level,
3202
 
                                 int error_code __attribute__((unused)),
 
3933
                                 int error_code __attribute__((__unused__)),
3203
3934
                                 const char *buffer,
3204
 
                                 size_t buffer_length __attribute__((unused)))
 
3935
                                 size_t buffer_length __attribute__((__unused__)))
3205
3936
{
3206
3937
  time_t skr;
3207
3938
  struct tm tm_tmp;
3208
3939
  struct tm *start;
3209
3940
 
3210
 
  pthread_mutex_lock(&LOCK_error_log);
 
3941
  VOID(pthread_mutex_lock(&LOCK_error_log));
3211
3942
 
3212
3943
  skr= my_time(0);
3213
3944
  localtime_r(&skr, &tm_tmp);
3226
3957
 
3227
3958
  fflush(stderr);
3228
3959
 
3229
 
  pthread_mutex_unlock(&LOCK_error_log);
 
3960
  VOID(pthread_mutex_unlock(&LOCK_error_log));
3230
3961
  return;
3231
3962
}
3232
3963
 
3332
4063
 
3333
4064
int TC_LOG_MMAP::open(const char *opt_name)
3334
4065
{
3335
 
  uint32_t i;
 
4066
  uint i;
3336
4067
  bool crashed= false;
3337
4068
  PAGE *pg;
3338
4069
 
3339
4070
  assert(total_ha_2pc > 1);
3340
4071
  assert(opt_name && opt_name[0]);
3341
4072
 
3342
 
  tc_log_page_size= getpagesize();
 
4073
  tc_log_page_size= my_getpagesize();
3343
4074
  assert(TC_LOG_PAGE_SIZE % tc_log_page_size == 0);
3344
4075
 
3345
4076
  fn_format(logname,opt_name,mysql_data_home,"",MY_UNPACK_FILENAME);
3360
4091
  {
3361
4092
    inited= 1;
3362
4093
    crashed= true;
3363
 
    sql_print_information(_("Recovering after a crash using %s"), opt_name);
 
4094
    sql_print_information("Recovering after a crash using %s", opt_name);
3364
4095
    if (tc_heuristic_recover)
3365
4096
    {
3366
 
      sql_print_error(_("Cannot perform automatic crash recovery when "
3367
 
                      "--tc-heuristic-recover is used"));
 
4097
      sql_print_error("Cannot perform automatic crash recovery when "
 
4098
                      "--tc-heuristic-recover is used");
3368
4099
      goto err;
3369
4100
    }
3370
4101
    file_length= my_seek(fd, 0L, MY_SEEK_END, MYF(MY_WME+MY_FAE));
3372
4103
      goto err;
3373
4104
  }
3374
4105
 
3375
 
  data= (unsigned char *)my_mmap(0, (size_t)file_length, PROT_READ|PROT_WRITE,
 
4106
  data= (uchar *)my_mmap(0, (size_t)file_length, PROT_READ|PROT_WRITE,
3376
4107
                        MAP_NOSYNC|MAP_SHARED, fd, 0);
3377
4108
  if (data == MAP_FAILED)
3378
4109
  {
3408
4139
      goto err;
3409
4140
 
3410
4141
  memcpy(data, tc_log_magic, sizeof(tc_log_magic));
3411
 
  data[sizeof(tc_log_magic)]= (unsigned char)total_ha_2pc;
 
4142
  data[sizeof(tc_log_magic)]= (uchar)total_ha_2pc;
3412
4143
  msync(data, tc_log_page_size, MS_SYNC);
3413
4144
  my_sync(fd, MYF(0));
3414
4145
  inited=5;
3516
4247
    threads waiting for a page, but then all these threads will be waiting
3517
4248
    for a fsync() anyway
3518
4249
 
3519
 
   If tc_log == DRIZZLE_LOG then tc_log writes transaction to binlog and
 
4250
   If tc_log == MYSQL_LOG then tc_log writes transaction to binlog and
3520
4251
   records XID in a special Xid_log_event.
3521
4252
   If tc_log = TC_LOG_MMAP then xid is written in a special memory-mapped
3522
4253
   log.
3530
4261
    to the position in memory where xid was logged to.
3531
4262
*/
3532
4263
 
3533
 
int TC_LOG_MMAP::log_xid(THD *thd __attribute__((unused)), my_xid xid)
 
4264
int TC_LOG_MMAP::log_xid(THD *thd __attribute__((__unused__)), my_xid xid)
3534
4265
{
3535
4266
  int err;
3536
4267
  PAGE *p;
3563
4294
  }
3564
4295
 
3565
4296
  /* found! store xid there and mark the page dirty */
3566
 
  cookie= (ulong)((unsigned char *)p->ptr - data);      // can never be zero
 
4297
  cookie= (ulong)((uchar *)p->ptr - data);      // can never be zero
3567
4298
  *p->ptr++= xid;
3568
4299
  p->free--;
3569
4300
  p->state= DIRTY;
3642
4373
  cookie points directly to the memory where xid was logged.
3643
4374
*/
3644
4375
 
3645
 
void TC_LOG_MMAP::unlog(ulong cookie, my_xid xid __attribute__((unused)))
 
4376
void TC_LOG_MMAP::unlog(ulong cookie, my_xid xid __attribute__((__unused__)))
3646
4377
{
3647
4378
  PAGE *p=pages+(cookie/tc_log_page_size);
3648
4379
  my_xid *x=(my_xid *)(data+cookie);
3664
4395
 
3665
4396
void TC_LOG_MMAP::close()
3666
4397
{
3667
 
  uint32_t i;
 
4398
  uint i;
3668
4399
  switch (inited) {
3669
4400
  case 6:
3670
4401
    pthread_mutex_destroy(&LOCK_sync);
3682
4413
      pthread_cond_destroy(&pages[i].cond);
3683
4414
    }
3684
4415
  case 3:
3685
 
    free((unsigned char*)pages);
 
4416
    my_free((uchar*)pages, MYF(0));
3686
4417
  case 2:
3687
4418
    my_munmap((char*)data, (size_t)file_length);
3688
4419
  case 1:
3700
4431
 
3701
4432
  if (memcmp(data, tc_log_magic, sizeof(tc_log_magic)))
3702
4433
  {
3703
 
    sql_print_error(_("Bad magic header in tc log"));
 
4434
    sql_print_error("Bad magic header in tc log");
3704
4435
    goto err1;
3705
4436
  }
3706
4437
 
3710
4441
  */
3711
4442
  if (data[sizeof(tc_log_magic)] != total_ha_2pc)
3712
4443
  {
3713
 
    sql_print_error(_("Recovery failed! You must enable "
 
4444
    sql_print_error("Recovery failed! You must enable "
3714
4445
                    "exactly %d storage engines that support "
3715
 
                    "two-phase commit protocol"),
 
4446
                    "two-phase commit protocol",
3716
4447
                    data[sizeof(tc_log_magic)]);
3717
4448
    goto err1;
3718
4449
  }
3724
4455
  for ( ; p < end_p ; p++)
3725
4456
  {
3726
4457
    for (my_xid *x=p->start; x < p->end; x++)
3727
 
      if (*x && my_hash_insert(&xids, (unsigned char *)x))
 
4458
      if (*x && my_hash_insert(&xids, (uchar *)x))
3728
4459
        goto err2; // OOM
3729
4460
  }
3730
4461
 
3732
4463
    goto err2;
3733
4464
 
3734
4465
  hash_free(&xids);
3735
 
  memset(data, 0, (size_t)file_length);
 
4466
  bzero(data, (size_t)file_length);
3736
4467
  return 0;
3737
4468
 
3738
4469
err2:
3739
4470
  hash_free(&xids);
3740
4471
err1:
3741
 
  sql_print_error(_("Crash recovery failed. Either correct the problem "
 
4472
  sql_print_error("Crash recovery failed. Either correct the problem "
3742
4473
                  "(if it's, for example, out of memory error) and restart, "
3743
 
                  "or delete tc log and start drizzled with "
3744
 
                  "--tc-heuristic-recover={commit|rollback}"));
 
4474
                  "or delete tc log and start mysqld with "
 
4475
                  "--tc-heuristic-recover={commit|rollback}");
3745
4476
  return 1;
3746
4477
}
3747
4478
#endif
3768
4499
  if (!tc_heuristic_recover)
3769
4500
    return 0;
3770
4501
 
3771
 
  sql_print_information(_("Heuristic crash recovery mode"));
 
4502
  sql_print_information("Heuristic crash recovery mode");
3772
4503
  if (ha_recover(0))
3773
 
    sql_print_error(_("Heuristic crash recovery failed"));
3774
 
  sql_print_information(_("Please restart mysqld without --tc-heuristic-recover"));
 
4504
    sql_print_error("Heuristic crash recovery failed");
 
4505
  sql_print_information("Please restart mysqld without --tc-heuristic-recover");
3775
4506
  return 1;
3776
4507
}
3777
4508
 
3778
4509
/****** transaction coordinator log for 2pc - binlog() based solution ******/
3779
 
#define TC_LOG_BINLOG DRIZZLE_BIN_LOG
 
4510
#define TC_LOG_BINLOG MYSQL_BIN_LOG
3780
4511
 
3781
4512
/**
3782
4513
  @todo
3812
4543
    return 1;
3813
4544
  }
3814
4545
 
3815
 
  if ((error= find_log_pos(&log_info, NULL, 1)))
 
4546
  if ((error= find_log_pos(&log_info, NullS, 1)))
3816
4547
  {
3817
4548
    if (error != LOG_INFO_EOF)
3818
 
      sql_print_error(_("find_log_pos() failed (error: %d)"), error);
 
4549
      sql_print_error("find_log_pos() failed (error: %d)", error);
3819
4550
    else
3820
4551
      error= 0;
3821
4552
    goto err;
3839
4570
 
3840
4571
    if (error !=  LOG_INFO_EOF)
3841
4572
    {
3842
 
      sql_print_error(_("find_log_pos() failed (error: %d)"), error);
 
4573
      sql_print_error("find_log_pos() failed (error: %d)", error);
3843
4574
      goto err;
3844
4575
    }
3845
4576
 
3853
4584
        ev->get_type_code() == FORMAT_DESCRIPTION_EVENT &&
3854
4585
        ev->flags & LOG_EVENT_BINLOG_IN_USE_F)
3855
4586
    {
3856
 
      sql_print_information(_("Recovering after a crash using %s"), opt_name);
 
4587
      sql_print_information("Recovering after a crash using %s", opt_name);
3857
4588
      error= recover(&log, (Format_description_log_event *)ev);
3858
4589
    }
3859
4590
    else
3900
4631
  return(!binlog_end_trans(thd, trx_data, &xle, true));
3901
4632
}
3902
4633
 
3903
 
void TC_LOG_BINLOG::unlog(ulong cookie __attribute__((unused)),
3904
 
                          my_xid xid __attribute__((unused)))
 
4634
void TC_LOG_BINLOG::unlog(ulong cookie __attribute__((__unused__)),
 
4635
                          my_xid xid __attribute__((__unused__)))
3905
4636
{
3906
4637
  pthread_mutex_lock(&LOCK_prep_xids);
3907
4638
  assert(prepared_xids > 0);
3932
4663
    if (ev->get_type_code() == XID_EVENT)
3933
4664
    {
3934
4665
      Xid_log_event *xev=(Xid_log_event *)ev;
3935
 
      unsigned char *x= (unsigned char *) memdup_root(&mem_root, (unsigned char*) &xev->xid,
 
4666
      uchar *x= (uchar *) memdup_root(&mem_root, (uchar*) &xev->xid,
3936
4667
                                      sizeof(xev->xid));
3937
4668
      if (! x)
3938
4669
        goto err2;
3952
4683
  free_root(&mem_root, MYF(0));
3953
4684
  hash_free(&xids);
3954
4685
err1:
3955
 
  sql_print_error(_("Crash recovery failed. Either correct the problem "
 
4686
  sql_print_error("Crash recovery failed. Either correct the problem "
3956
4687
                  "(if it's, for example, out of memory error) and restart, "
3957
4688
                  "or delete (or rename) binary log and start mysqld with "
3958
 
                  "--tc-heuristic-recover={commit|rollback}"));
 
4689
                  "--tc-heuristic-recover={commit|rollback}");
3959
4690
  return 1;
3960
4691
}
3961
4692
 
3975
4706
  @return byte offset from the beginning of the binlog
3976
4707
*/
3977
4708
extern "C"
3978
 
uint64_t mysql_bin_log_file_pos(void)
 
4709
ulonglong mysql_bin_log_file_pos(void)
3979
4710
{
3980
 
  return (uint64_t) mysql_bin_log.get_log_file()->pos_in_file;
 
4711
  return (ulonglong) mysql_bin_log.get_log_file()->pos_in_file;
3981
4712
}
3982
4713
#endif /* INNODB_COMPATIBILITY_HOOKS */
3983
4714
 
3984
4715
 
 
4716
struct st_mysql_storage_engine binlog_storage_engine=
 
4717
{ MYSQL_HANDLERTON_INTERFACE_VERSION };
 
4718
 
3985
4719
mysql_declare_plugin(binlog)
3986
4720
{
3987
 
  DRIZZLE_STORAGE_ENGINE_PLUGIN,
 
4721
  MYSQL_STORAGE_ENGINE_PLUGIN,
 
4722
  &binlog_storage_engine,
3988
4723
  "binlog",
3989
 
  "1.0",
3990
4724
  "MySQL AB",
3991
4725
  "This is a pseudo storage engine to represent the binlog in a transaction",
3992
4726
  PLUGIN_LICENSE_GPL,
3993
4727
  binlog_init, /* Plugin Init */
3994
4728
  NULL, /* Plugin Deinit */
 
4729
  0x0100 /* 1.0 */,
3995
4730
  NULL,                       /* status variables                */
3996
4731
  NULL,                       /* system variables                */
3997
4732
  NULL                        /* config options                  */