~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/log.cc

Removed/replaced DBUG symbols and standardized TRUE/FALSE

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;
746
1129
{
747
1130
  File file;
748
1131
 
749
 
  if ((file = my_open(log_file_name, O_RDONLY, 
 
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];
862
1245
  File file= -1;
863
 
  int open_flags= O_CREAT;
 
1246
  int open_flags= O_CREAT | O_BINARY;
864
1247
 
865
1248
  write_error= 0;
866
1249
 
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;
1123
1790
  fn_format(index_file_name, index_file_name_arg, mysql_data_home,
1124
1791
            ".index", opt);
1125
1792
  if ((index_file_nr= my_open(index_file_name,
1126
 
                              O_RDWR | O_CREAT,
 
1793
                              O_RDWR | O_CREAT | O_BINARY ,
1127
1794
                              MYF(MY_WME))) < 0 ||
1128
1795
       my_sync(index_file_nr, MYF(MY_WME)) ||
1129
1796
       init_io_cache(&index_file, index_file_nr,
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
 
1651
2318
  assert(is_open());
1652
2319
  assert(rli->slave_running == 1);
1653
 
  assert(!strcmp(rli->linfo.log_file_name,rli->event_relay_log_name.c_str()));
 
2320
  assert(!strcmp(rli->linfo.log_file_name,rli->event_relay_log_name));
1654
2321
 
1655
2322
  pthread_mutex_lock(&LOCK_index);
1656
2323
  pthread_mutex_lock(&rli->log_space_lock);
1657
 
  rli->relay_log.purge_logs(rli->group_relay_log_name.c_str(), included,
 
2324
  rli->relay_log.purge_logs(rli->group_relay_log_name, included,
1658
2325
                            0, 0, &rli->log_space_total);
1659
2326
  // Tell the I/O thread to take the relay_log_space_limit into account
1660
2327
  rli->ignore_log_space_limit= 0;
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
 
       ((error=find_log_pos(&rli->linfo, rli->event_relay_log_name.c_str(), 0)) ||
 
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
 
                    rli->group_relay_log_name.c_str(),
 
2352
                    rli->group_relay_log_name,
1686
2353
                    included);
1687
2354
    goto err;
1688
2355
  }
1691
2358
    Reset rli's coordinates to the current log.
1692
2359
  */
1693
2360
  rli->event_relay_log_pos= BIN_LOG_HEADER_SIZE;
1694
 
  rli->event_relay_log_name.assign(rli->linfo.log_file_name);
 
2361
  strmake(rli->event_relay_log_name,rli->linfo.log_file_name,
 
2362
          sizeof(rli->event_relay_log_name)-1);
1695
2363
 
1696
2364
  /*
1697
2365
    If we removed the rli->group_relay_log_name file,
1701
2369
  if (included)
1702
2370
  {
1703
2371
    rli->group_relay_log_pos = BIN_LOG_HEADER_SIZE;
1704
 
    rli->group_relay_log_name.assign(rli->linfo.log_file_name);
 
2372
    strmake(rli->group_relay_log_name,rli->linfo.log_file_name,
 
2373
            sizeof(rli->group_relay_log_name)-1);
1705
2374
    rli->notify_group_relay_log_name_update();
1706
2375
  }
1707
2376
 
1717
2386
  Update log index_file.
1718
2387
*/
1719
2388
 
1720
 
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)
1721
2390
{
1722
2391
  if (copy_up_file_and_fill(&index_file, log_info->index_file_start_offset))
1723
2392
    return LOG_INFO_IO;
1752
2421
                                stat() or my_delete()
1753
2422
*/
1754
2423
 
1755
 
int DRIZZLE_BIN_LOG::purge_logs(const char *to_log, 
 
2424
int MYSQL_BIN_LOG::purge_logs(const char *to_log, 
1756
2425
                          bool included,
1757
2426
                          bool need_mutex, 
1758
2427
                          bool need_update_threads, 
1759
 
                          uint64_t *decrease_log_space)
 
2428
                          ulonglong *decrease_log_space)
1760
2429
{
1761
2430
  int error;
1762
2431
  int ret = 0;
1772
2441
    File name exists in index file; delete until we find this file
1773
2442
    or a file that is used.
1774
2443
  */
1775
 
  if ((error=find_log_pos(&log_info, NULL, 0 /*no mutex*/)))
 
2444
  if ((error=find_log_pos(&log_info, NullS, 0 /*no mutex*/)))
1776
2445
    goto err;
1777
2446
  while ((strcmp(to_log,log_info.log_file_name) || (exit_loop=included)) &&
1778
2447
         !log_in_use(log_info.log_file_name))
1786
2455
          It's not fatal if we can't stat a log file that does not exist;
1787
2456
          If we could not stat, we won't delete.
1788
2457
        */     
1789
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2458
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1790
2459
                            ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1791
2460
                            log_info.log_file_name);
1792
 
        sql_print_information(_("Failed to execute stat() on file '%s'"),
 
2461
        sql_print_information("Failed to execute stat on file '%s'",
1793
2462
                              log_info.log_file_name);
1794
2463
        my_errno= 0;
1795
2464
      }
1798
2467
        /*
1799
2468
          Other than ENOENT are fatal
1800
2469
        */
1801
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2470
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1802
2471
                            ER_BINLOG_PURGE_FATAL_ERR,
1803
 
                            _("a problem with getting info on being purged %s; "
 
2472
                            "a problem with getting info on being purged %s; "
1804
2473
                            "consider examining correspondence "
1805
2474
                            "of your binlog index file "
1806
 
                            "to the actual binlog files"),
 
2475
                            "to the actual binlog files",
1807
2476
                            log_info.log_file_name);
1808
2477
        error= LOG_INFO_FATAL;
1809
2478
        goto err;
1820
2489
      {
1821
2490
        if (my_errno == ENOENT) 
1822
2491
        {
1823
 
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2492
          push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1824
2493
                              ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1825
2494
                              log_info.log_file_name);
1826
 
          sql_print_information(_("Failed to delete file '%s'"),
 
2495
          sql_print_information("Failed to delete file '%s'",
1827
2496
                                log_info.log_file_name);
1828
2497
          my_errno= 0;
1829
2498
        }
1830
2499
        else
1831
2500
        {
1832
 
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2501
          push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1833
2502
                              ER_BINLOG_PURGE_FATAL_ERR,
1834
 
                              _("a problem with deleting %s; "
 
2503
                              "a problem with deleting %s; "
1835
2504
                              "consider examining correspondence "
1836
2505
                              "of your binlog index file "
1837
 
                              "to the actual binlog files"),
 
2506
                              "to the actual binlog files",
1838
2507
                              log_info.log_file_name);
1839
2508
          if (my_errno == EMFILE)
1840
2509
          {
1846
2515
      }
1847
2516
    }
1848
2517
 
 
2518
    ha_binlog_index_purge_file(current_thd, log_info.log_file_name);
 
2519
 
1849
2520
    if (find_next_log(&log_info, 0) || exit_loop)
1850
2521
      break;
1851
2522
  }
1884
2555
                                stat() or my_delete()
1885
2556
*/
1886
2557
 
1887
 
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)
1888
2559
{
1889
2560
  int error;
1890
2561
  LOG_INFO log_info;
1897
2568
    or a file that is used or a file
1898
2569
    that is older than purge_time.
1899
2570
  */
1900
 
  if ((error=find_log_pos(&log_info, NULL, 0 /*no mutex*/)))
 
2571
  if ((error=find_log_pos(&log_info, NullS, 0 /*no mutex*/)))
1901
2572
    goto err;
1902
2573
 
1903
2574
  while (strcmp(log_file_name, log_info.log_file_name) &&
1910
2581
        /*
1911
2582
          It's not fatal if we can't stat a log file that does not exist.
1912
2583
        */     
1913
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2584
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1914
2585
                            ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1915
2586
                            log_info.log_file_name);
1916
 
        sql_print_information(_("Failed to execute stat() on file '%s'"),
 
2587
        sql_print_information("Failed to execute stat on file '%s'",
1917
2588
                              log_info.log_file_name);
1918
2589
        my_errno= 0;
1919
2590
      }
1922
2593
        /*
1923
2594
          Other than ENOENT are fatal
1924
2595
        */
1925
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2596
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1926
2597
                            ER_BINLOG_PURGE_FATAL_ERR,
1927
 
                            _("a problem with getting info on being purged %s; "
 
2598
                            "a problem with getting info on being purged %s; "
1928
2599
                            "consider examining correspondence "
1929
2600
                            "of your binlog index file "
1930
 
                            "to the actual binlog files"),
 
2601
                            "to the actual binlog files",
1931
2602
                            log_info.log_file_name);
1932
2603
        error= LOG_INFO_FATAL;
1933
2604
        goto err;
1942
2613
        if (my_errno == ENOENT) 
1943
2614
        {
1944
2615
          /* It's not fatal even if we can't delete a log file */
1945
 
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2616
          push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1946
2617
                              ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1947
2618
                              log_info.log_file_name);
1948
 
          sql_print_information(_("Failed to delete file '%s'"),
 
2619
          sql_print_information("Failed to delete file '%s'",
1949
2620
                                log_info.log_file_name);
1950
2621
          my_errno= 0;
1951
2622
        }
1952
2623
        else
1953
2624
        {
1954
 
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2625
          push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1955
2626
                              ER_BINLOG_PURGE_FATAL_ERR,
1956
 
                              _("a problem with deleting %s; "
 
2627
                              "a problem with deleting %s; "
1957
2628
                              "consider examining correspondence "
1958
2629
                              "of your binlog index file "
1959
 
                              "to the actual binlog files"),
 
2630
                              "to the actual binlog files",
1960
2631
                              log_info.log_file_name);
1961
2632
          error= LOG_INFO_FATAL;
1962
2633
          goto err;
1963
2634
        }
1964
2635
      }
 
2636
      ha_binlog_index_purge_file(current_thd, log_info.log_file_name);
1965
2637
    }
1966
2638
    if (find_next_log(&log_info, 0))
1967
2639
      break;
1977
2649
  pthread_mutex_unlock(&LOCK_index);
1978
2650
  return(error);
1979
2651
}
 
2652
#endif /* HAVE_REPLICATION */
1980
2653
 
1981
2654
 
1982
2655
/**
1988
2661
    If file name will be longer then FN_REFLEN it will be truncated
1989
2662
*/
1990
2663
 
1991
 
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)
1992
2665
{
1993
 
  uint32_t dir_len = dirname_length(log_file_name); 
 
2666
  uint dir_len = dirname_length(log_file_name); 
1994
2667
  if (dir_len >= FN_REFLEN)
1995
2668
    dir_len=FN_REFLEN-1;
1996
 
  my_stpncpy(buf, log_file_name, dir_len);
 
2669
  strnmov(buf, log_file_name, dir_len);
1997
2670
  strmake(buf+dir_len, log_ident, FN_REFLEN - dir_len -1);
1998
2671
}
1999
2672
 
2002
2675
  Check if we are writing/reading to the given log file.
2003
2676
*/
2004
2677
 
2005
 
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)
2006
2679
{
2007
2680
  return !strcmp(log_file_name, log_file_name_arg);
2008
2681
}
2016
2689
  method).
2017
2690
*/
2018
2691
 
2019
 
void DRIZZLE_BIN_LOG::new_file()
 
2692
void MYSQL_BIN_LOG::new_file()
2020
2693
{
2021
2694
  new_file_impl(1);
2022
2695
}
2023
2696
 
2024
2697
 
2025
 
void DRIZZLE_BIN_LOG::new_file_without_locking()
 
2698
void MYSQL_BIN_LOG::new_file_without_locking()
2026
2699
{
2027
2700
  new_file_impl(0);
2028
2701
}
2037
2710
    The new file name is stored last in the index file
2038
2711
*/
2039
2712
 
2040
 
void DRIZZLE_BIN_LOG::new_file_impl(bool need_lock)
 
2713
void MYSQL_BIN_LOG::new_file_impl(bool need_lock)
2041
2714
{
2042
2715
  char new_name[FN_REFLEN], *new_name_ptr, *old_name;
2043
2716
 
2123
2796
 
2124
2797
  open(old_name, log_type, new_name_ptr,
2125
2798
       io_cache_type, no_auto_events, max_size, 1);
2126
 
  free(old_name);
 
2799
  my_free(old_name,MYF(0));
2127
2800
 
2128
2801
end:
2129
2802
  if (need_lock)
2134
2807
}
2135
2808
 
2136
2809
 
2137
 
bool DRIZZLE_BIN_LOG::append(Log_event* ev)
 
2810
bool MYSQL_BIN_LOG::append(Log_event* ev)
2138
2811
{
2139
2812
  bool error = 0;
2140
2813
  pthread_mutex_lock(&LOCK_log);
2160
2833
}
2161
2834
 
2162
2835
 
2163
 
bool DRIZZLE_BIN_LOG::appendv(const char* buf, uint32_t len,...)
 
2836
bool MYSQL_BIN_LOG::appendv(const char* buf, uint len,...)
2164
2837
{
2165
2838
  bool error= 0;
2166
2839
  va_list(args);
2171
2844
  safe_mutex_assert_owner(&LOCK_log);
2172
2845
  do
2173
2846
  {
2174
 
    if (my_b_append(&log_file,(unsigned char*) buf,len))
 
2847
    if (my_b_append(&log_file,(uchar*) buf,len))
2175
2848
    {
2176
2849
      error= 1;
2177
2850
      goto err;
2188
2861
}
2189
2862
 
2190
2863
 
2191
 
bool DRIZZLE_BIN_LOG::flush_and_sync()
 
2864
bool MYSQL_BIN_LOG::flush_and_sync()
2192
2865
{
2193
2866
  int err=0, fd=log_file.file;
2194
2867
  safe_mutex_assert_owner(&LOCK_log);
2202
2875
  return err;
2203
2876
}
2204
2877
 
2205
 
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)
2206
2879
{
2207
2880
  assert(!thd->binlog_evt_union.do_union);
2208
2881
  thd->binlog_evt_union.do_union= true;
2211
2884
  thd->binlog_evt_union.first_query_id= query_id_param;
2212
2885
}
2213
2886
 
2214
 
void DRIZZLE_BIN_LOG::stop_union_events(THD *thd)
 
2887
void MYSQL_BIN_LOG::stop_union_events(THD *thd)
2215
2888
{
2216
2889
  assert(thd->binlog_evt_union.do_union);
2217
2890
  thd->binlog_evt_union.do_union= false;
2218
2891
}
2219
2892
 
2220
 
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)
2221
2894
{
2222
2895
  return (thd->binlog_evt_union.do_union && 
2223
2896
          query_id_param >= thd->binlog_evt_union.first_query_id);
2242
2915
      open_cached_file(&trx_data->trans_log, mysql_tmpdir,
2243
2916
                       LOG_PREFIX, binlog_cache_size, MYF(MY_WME)))
2244
2917
  {
2245
 
    free((unsigned char*)trx_data);
 
2918
    my_free((uchar*)trx_data, MYF(MY_ALLOW_ZERO_PTR));
2246
2919
    return(1);                      // Didn't manage to set it up
2247
2920
  }
2248
2921
  thd_set_ha_data(this, binlog_hton, trx_data);
2327
3000
  Write a table map to the binary log.
2328
3001
 */
2329
3002
 
2330
 
int THD::binlog_write_table_map(Table *table, bool is_trans)
 
3003
int THD::binlog_write_table_map(TABLE *table, bool is_trans)
2331
3004
{
2332
3005
  int error;
2333
3006
 
2334
3007
  /* Pre-conditions */
2335
3008
  assert(current_stmt_binlog_row_based && mysql_bin_log.is_open());
2336
 
  assert(table->s->table_map_id != UINT32_MAX);
 
3009
  assert(table->s->table_map_id != ULONG_MAX);
2337
3010
 
2338
3011
  Table_map_log_event::flag_set const
2339
3012
    flags= Table_map_log_event::TM_NO_FLAGS;
2386
3059
  event.
2387
3060
*/
2388
3061
int
2389
 
DRIZZLE_BIN_LOG::flush_and_set_pending_rows_event(THD *thd,
 
3062
MYSQL_BIN_LOG::flush_and_set_pending_rows_event(THD *thd,
2390
3063
                                                Rows_log_event* event)
2391
3064
{
2392
3065
  assert(mysql_bin_log.is_open());
2468
3141
  Write an event to the binary log.
2469
3142
*/
2470
3143
 
2471
 
bool DRIZZLE_BIN_LOG::write(Log_event *event_info)
 
3144
bool MYSQL_BIN_LOG::write(Log_event *event_info)
2472
3145
{
2473
3146
  THD *thd= event_info->thd;
2474
3147
  bool error= 1;
2516
3189
    if ((thd && !(thd->options & OPTION_BIN_LOG)) ||
2517
3190
        (!binlog_filter->db_ok(local_db)))
2518
3191
    {
2519
 
      pthread_mutex_unlock(&LOCK_log);
 
3192
      VOID(pthread_mutex_unlock(&LOCK_log));
2520
3193
      return(0);
2521
3194
    }
2522
3195
 
2572
3245
      {
2573
3246
        if (thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt)
2574
3247
        {
2575
 
          Intvar_log_event e(thd,(unsigned char) LAST_INSERT_ID_EVENT,
 
3248
          Intvar_log_event e(thd,(uchar) LAST_INSERT_ID_EVENT,
2576
3249
                             thd->first_successful_insert_id_in_prev_stmt_for_binlog);
2577
3250
          if (e.write(file))
2578
3251
            goto err;
2584
3257
            MyISAM or BDB) (table->next_number_keypart != 0), such event is
2585
3258
            in fact not necessary. We could avoid logging it.
2586
3259
          */
2587
 
          Intvar_log_event e(thd, (unsigned char) INSERT_ID_EVENT,
 
3260
          Intvar_log_event e(thd, (uchar) INSERT_ID_EVENT,
2588
3261
                             thd->auto_inc_intervals_in_cur_stmt_for_binlog.
2589
3262
                             minimum());
2590
3263
          if (e.write(file))
2598
3271
        }
2599
3272
        if (thd->user_var_events.elements)
2600
3273
        {
2601
 
          for (uint32_t i= 0; i < thd->user_var_events.elements; i++)
 
3274
          for (uint i= 0; i < thd->user_var_events.elements; i++)
2602
3275
          {
2603
3276
            BINLOG_USER_VAR_EVENT *user_var_event;
2604
 
            get_dynamic(&thd->user_var_events,(unsigned char*) &user_var_event, i);
 
3277
            get_dynamic(&thd->user_var_events,(uchar*) &user_var_event, i);
2605
3278
            User_var_log_event e(thd, user_var_event->user_var_event->name.str,
2606
3279
                                 user_var_event->user_var_event->name.length,
2607
3280
                                 user_var_event->value,
2656
3329
  return logger.error_log_print(level, format, args);
2657
3330
}
2658
3331
 
2659
 
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)
2660
3389
{
2661
3390
  if (!(flags & RP_LOCK_LOG_IS_ALREADY_LOCKED))
2662
3391
    pthread_mutex_lock(&LOCK_log);
2664
3393
      (my_b_tell(&log_file) >= (my_off_t) max_size))
2665
3394
  {
2666
3395
    new_file_without_locking();
 
3396
#ifdef HAVE_REPLICATION
2667
3397
    if (expire_logs_days)
2668
3398
    {
2669
3399
      time_t purge_time= my_time(0) - expire_logs_days*24*60*60;
2670
3400
      if (purge_time >= 0)
2671
3401
        purge_logs_before_date(purge_time);
2672
3402
    }
 
3403
#endif
2673
3404
  }
2674
3405
  if (!(flags & RP_LOCK_LOG_IS_ALREADY_LOCKED))
2675
3406
    pthread_mutex_unlock(&LOCK_log);
2676
3407
}
2677
3408
 
2678
 
uint32_t DRIZZLE_BIN_LOG::next_file_id()
 
3409
uint MYSQL_BIN_LOG::next_file_id()
2679
3410
{
2680
 
  uint32_t res;
 
3411
  uint res;
2681
3412
  pthread_mutex_lock(&LOCK_log);
2682
3413
  res = file_id++;
2683
3414
  pthread_mutex_unlock(&LOCK_log);
2699
3430
    be reset as a READ_CACHE to be able to read the contents from it.
2700
3431
 */
2701
3432
 
2702
 
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)
2703
3434
{
2704
3435
  Mutex_sentry sentry(lock_log ? &LOCK_log : NULL);
2705
3436
 
2706
3437
  if (reinit_io_cache(cache, READ_CACHE, 0, 0, 0))
2707
3438
    return ER_ERROR_ON_WRITE;
2708
 
  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;
2709
3440
  long val;
2710
 
  unsigned char header[LOG_EVENT_HEADER_LEN];
 
3441
  uchar header[LOG_EVENT_HEADER_LEN];
2711
3442
 
2712
3443
  /*
2713
3444
    The events in the buffer have incorrect end_log_pos data
2738
3469
      assert(carry < LOG_EVENT_HEADER_LEN);
2739
3470
 
2740
3471
      /* assemble both halves */
2741
 
      memcpy(&header[carry], cache->read_pos, LOG_EVENT_HEADER_LEN - carry);
 
3472
      memcpy(&header[carry], (char *)cache->read_pos, LOG_EVENT_HEADER_LEN - carry);
2742
3473
 
2743
3474
      /* fix end_log_pos */
2744
3475
      val= uint4korr(&header[LOG_POS_OFFSET]) + group;
2752
3483
        copy fixed second half of header to cache so the correct
2753
3484
        version will be written later.
2754
3485
      */
2755
 
      memcpy(cache->read_pos, &header[carry], LOG_EVENT_HEADER_LEN - carry);
 
3486
      memcpy((char *)cache->read_pos, &header[carry], LOG_EVENT_HEADER_LEN - carry);
2756
3487
 
2757
3488
      /* next event header at ... */
2758
3489
      hdr_offs = uint4korr(&header[EVENT_LEN_OFFSET]) - carry;
2781
3512
        if (hdr_offs + LOG_EVENT_HEADER_LEN > length)
2782
3513
        {
2783
3514
          carry= length - hdr_offs;
2784
 
          memcpy(header, cache->read_pos + hdr_offs, carry);
 
3515
          memcpy(header, (char *)cache->read_pos + hdr_offs, carry);
2785
3516
          length= hdr_offs;
2786
3517
        }
2787
3518
        else
2788
3519
        {
2789
3520
          /* we've got a full event-header, and it came in one piece */
2790
3521
 
2791
 
          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;
2792
3523
 
2793
3524
          /* fix end_log_pos */
2794
3525
          val= uint4korr(log_pos) + group;
2795
3526
          int4store(log_pos, val);
2796
3527
 
2797
3528
          /* next event header at ... */
2798
 
          log_pos= (unsigned char *)cache->read_pos + hdr_offs + EVENT_LEN_OFFSET;
 
3529
          log_pos= (uchar *)cache->read_pos + hdr_offs + EVENT_LEN_OFFSET;
2799
3530
          hdr_offs += uint4korr(log_pos);
2800
3531
 
2801
3532
        }
2847
3578
    'cache' needs to be reinitialized after this functions returns.
2848
3579
*/
2849
3580
 
2850
 
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)
2851
3582
{
2852
 
  pthread_mutex_lock(&LOCK_log);
 
3583
  VOID(pthread_mutex_lock(&LOCK_log));
2853
3584
 
2854
3585
  /* NULL would represent nothing to replicate after ROLLBACK */
2855
3586
  assert(commit_event != NULL);
2925
3656
    else
2926
3657
      rotate_and_purge(RP_LOCK_LOG_IS_ALREADY_LOCKED);
2927
3658
  }
2928
 
  pthread_mutex_unlock(&LOCK_log);
 
3659
  VOID(pthread_mutex_unlock(&LOCK_log));
2929
3660
 
2930
3661
  return(0);
2931
3662
 
2935
3666
    write_error= 1;
2936
3667
    sql_print_error(ER(ER_ERROR_ON_WRITE), name, errno);
2937
3668
  }
2938
 
  pthread_mutex_unlock(&LOCK_log);
 
3669
  VOID(pthread_mutex_unlock(&LOCK_log));
2939
3670
  return(1);
2940
3671
}
2941
3672
 
2949
3680
    It will be released at the end of the function.
2950
3681
*/
2951
3682
 
2952
 
void DRIZZLE_BIN_LOG::wait_for_update_relay_log(THD* thd)
 
3683
void MYSQL_BIN_LOG::wait_for_update_relay_log(THD* thd)
2953
3684
{
2954
3685
  const char *old_msg;
2955
3686
  old_msg= thd->enter_cond(&update_cond, &LOCK_log,
2978
3709
    LOCK_log is released by the caller.
2979
3710
*/
2980
3711
 
2981
 
int DRIZZLE_BIN_LOG::wait_for_update_bin_log(THD* thd,
 
3712
int MYSQL_BIN_LOG::wait_for_update_bin_log(THD* thd,
2982
3713
                                           const struct timespec *timeout)
2983
3714
{
2984
3715
  int ret= 0;
2985
 
  const char* old_msg = thd->get_proc_info();
 
3716
  const char* old_msg = thd->proc_info;
2986
3717
  old_msg= thd->enter_cond(&update_cond, &LOCK_log,
2987
3718
                           "Master has sent all binlog to slave; "
2988
3719
                           "waiting for binlog to be updated");
3009
3740
    The internal structures are not freed until cleanup() is called
3010
3741
*/
3011
3742
 
3012
 
void DRIZZLE_BIN_LOG::close(uint32_t exiting)
 
3743
void MYSQL_BIN_LOG::close(uint exiting)
3013
3744
{                                       // One can't set log_type here!
3014
3745
  if (log_state == LOG_OPENED)
3015
3746
  {
 
3747
#ifdef HAVE_REPLICATION
3016
3748
    if (log_type == LOG_BIN && !no_auto_events &&
3017
3749
        (exiting & LOG_CLOSE_STOP_EVENT))
3018
3750
    {
3021
3753
      bytes_written+= s.data_written;
3022
3754
      signal_update();
3023
3755
    }
 
3756
#endif /* HAVE_REPLICATION */
3024
3757
 
3025
3758
    /* don't pwrite in a file opened with O_APPEND - it doesn't work */
3026
3759
    if (log_file.type == WRITE_CACHE && log_type == LOG_BIN)
3027
3760
    {
3028
3761
      my_off_t offset= BIN_LOG_HEADER_SIZE + FLAGS_OFFSET;
3029
 
      unsigned char flags= 0;            // clearing LOG_EVENT_BINLOG_IN_USE_F
 
3762
      uchar flags= 0;            // clearing LOG_EVENT_BINLOG_IN_USE_F
3030
3763
      pwrite(log_file.file, &flags, 1, offset);
3031
3764
    }
3032
3765
 
3033
3766
    /* this will cleanup IO_CACHE, sync and close the file */
3034
 
    DRIZZLE_LOG::close(exiting);
 
3767
    MYSQL_LOG::close(exiting);
3035
3768
  }
3036
3769
 
3037
3770
  /*
3049
3782
    }
3050
3783
  }
3051
3784
  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
 
  }
 
3785
  safeFree(name);
3057
3786
  return;
3058
3787
}
3059
3788
 
3060
3789
 
3061
 
void DRIZZLE_BIN_LOG::set_max_size(ulong max_size_arg)
 
3790
void MYSQL_BIN_LOG::set_max_size(ulong max_size_arg)
3062
3791
{
3063
3792
  /*
3064
3793
    We need to take locks, otherwise this may happen:
3126
3855
 
3127
3856
void sql_perror(const char *message)
3128
3857
{
 
3858
#ifdef HAVE_STRERROR
3129
3859
  sql_print_error("%s: %s",message, strerror(errno));
 
3860
#else
 
3861
  perror(message);
 
3862
#endif
3130
3863
}
3131
3864
 
3132
3865
 
3137
3870
  {
3138
3871
    char err_renamed[FN_REFLEN], *end;
3139
3872
    end= strmake(err_renamed,log_error_file,FN_REFLEN-4);
3140
 
    my_stpcpy(end, "-old");
3141
 
    pthread_mutex_lock(&LOCK_error_log);
 
3873
    strmov(end, "-old");
 
3874
    VOID(pthread_mutex_lock(&LOCK_error_log));
3142
3875
    char err_temp[FN_REFLEN+4];
3143
3876
    /*
3144
3877
     On Windows is necessary a temporary file for to rename
3145
3878
     the current error file.
3146
3879
    */
3147
 
    strxmov(err_temp, err_renamed,"-tmp",NULL);
 
3880
    strxmov(err_temp, err_renamed,"-tmp",NullS);
3148
3881
    (void) my_delete(err_temp, MYF(0)); 
3149
3882
    if (freopen(err_temp,"a+",stdout))
3150
3883
    {
3151
3884
      int fd;
3152
3885
      size_t bytes;
3153
 
      unsigned char buf[IO_SIZE];
 
3886
      uchar buf[IO_SIZE];
3154
3887
 
3155
3888
      freopen(err_temp,"a+",stderr);
3156
3889
      (void) my_delete(err_renamed, MYF(0));
3169
3902
    }
3170
3903
    else
3171
3904
     result= 1;
3172
 
    pthread_mutex_unlock(&LOCK_error_log);
 
3905
    VOID(pthread_mutex_unlock(&LOCK_error_log));
3173
3906
  }
3174
3907
   return result;
3175
3908
}
3176
3909
 
3177
 
void DRIZZLE_BIN_LOG::signal_update()
 
3910
void MYSQL_BIN_LOG::signal_update()
3178
3911
{
3179
3912
  pthread_cond_broadcast(&update_cond);
3180
3913
  return;
3197
3930
    return an error (e.g. logging to the log tables)
3198
3931
*/
3199
3932
static void print_buffer_to_file(enum loglevel level,
3200
 
                                 int error_code __attribute__((unused)),
 
3933
                                 int error_code __attribute__((__unused__)),
3201
3934
                                 const char *buffer,
3202
 
                                 size_t buffer_length __attribute__((unused)))
 
3935
                                 size_t buffer_length __attribute__((__unused__)))
3203
3936
{
3204
3937
  time_t skr;
3205
3938
  struct tm tm_tmp;
3206
3939
  struct tm *start;
3207
3940
 
3208
 
  pthread_mutex_lock(&LOCK_error_log);
 
3941
  VOID(pthread_mutex_lock(&LOCK_error_log));
3209
3942
 
3210
3943
  skr= my_time(0);
3211
3944
  localtime_r(&skr, &tm_tmp);
3224
3957
 
3225
3958
  fflush(stderr);
3226
3959
 
3227
 
  pthread_mutex_unlock(&LOCK_error_log);
 
3960
  VOID(pthread_mutex_unlock(&LOCK_error_log));
3228
3961
  return;
3229
3962
}
3230
3963
 
3330
4063
 
3331
4064
int TC_LOG_MMAP::open(const char *opt_name)
3332
4065
{
3333
 
  uint32_t i;
 
4066
  uint i;
3334
4067
  bool crashed= false;
3335
4068
  PAGE *pg;
3336
4069
 
3337
4070
  assert(total_ha_2pc > 1);
3338
4071
  assert(opt_name && opt_name[0]);
3339
4072
 
3340
 
  tc_log_page_size= getpagesize();
 
4073
  tc_log_page_size= my_getpagesize();
3341
4074
  assert(TC_LOG_PAGE_SIZE % tc_log_page_size == 0);
3342
4075
 
3343
4076
  fn_format(logname,opt_name,mysql_data_home,"",MY_UNPACK_FILENAME);
3358
4091
  {
3359
4092
    inited= 1;
3360
4093
    crashed= true;
3361
 
    sql_print_information(_("Recovering after a crash using %s"), opt_name);
 
4094
    sql_print_information("Recovering after a crash using %s", opt_name);
3362
4095
    if (tc_heuristic_recover)
3363
4096
    {
3364
 
      sql_print_error(_("Cannot perform automatic crash recovery when "
3365
 
                      "--tc-heuristic-recover is used"));
 
4097
      sql_print_error("Cannot perform automatic crash recovery when "
 
4098
                      "--tc-heuristic-recover is used");
3366
4099
      goto err;
3367
4100
    }
3368
4101
    file_length= my_seek(fd, 0L, MY_SEEK_END, MYF(MY_WME+MY_FAE));
3370
4103
      goto err;
3371
4104
  }
3372
4105
 
3373
 
  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,
3374
4107
                        MAP_NOSYNC|MAP_SHARED, fd, 0);
3375
4108
  if (data == MAP_FAILED)
3376
4109
  {
3406
4139
      goto err;
3407
4140
 
3408
4141
  memcpy(data, tc_log_magic, sizeof(tc_log_magic));
3409
 
  data[sizeof(tc_log_magic)]= (unsigned char)total_ha_2pc;
 
4142
  data[sizeof(tc_log_magic)]= (uchar)total_ha_2pc;
3410
4143
  msync(data, tc_log_page_size, MS_SYNC);
3411
4144
  my_sync(fd, MYF(0));
3412
4145
  inited=5;
3514
4247
    threads waiting for a page, but then all these threads will be waiting
3515
4248
    for a fsync() anyway
3516
4249
 
3517
 
   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
3518
4251
   records XID in a special Xid_log_event.
3519
4252
   If tc_log = TC_LOG_MMAP then xid is written in a special memory-mapped
3520
4253
   log.
3528
4261
    to the position in memory where xid was logged to.
3529
4262
*/
3530
4263
 
3531
 
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)
3532
4265
{
3533
4266
  int err;
3534
4267
  PAGE *p;
3561
4294
  }
3562
4295
 
3563
4296
  /* found! store xid there and mark the page dirty */
3564
 
  cookie= (ulong)((unsigned char *)p->ptr - data);      // can never be zero
 
4297
  cookie= (ulong)((uchar *)p->ptr - data);      // can never be zero
3565
4298
  *p->ptr++= xid;
3566
4299
  p->free--;
3567
4300
  p->state= DIRTY;
3640
4373
  cookie points directly to the memory where xid was logged.
3641
4374
*/
3642
4375
 
3643
 
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__)))
3644
4377
{
3645
4378
  PAGE *p=pages+(cookie/tc_log_page_size);
3646
4379
  my_xid *x=(my_xid *)(data+cookie);
3662
4395
 
3663
4396
void TC_LOG_MMAP::close()
3664
4397
{
3665
 
  uint32_t i;
 
4398
  uint i;
3666
4399
  switch (inited) {
3667
4400
  case 6:
3668
4401
    pthread_mutex_destroy(&LOCK_sync);
3680
4413
      pthread_cond_destroy(&pages[i].cond);
3681
4414
    }
3682
4415
  case 3:
3683
 
    free((unsigned char*)pages);
 
4416
    my_free((uchar*)pages, MYF(0));
3684
4417
  case 2:
3685
4418
    my_munmap((char*)data, (size_t)file_length);
3686
4419
  case 1:
3698
4431
 
3699
4432
  if (memcmp(data, tc_log_magic, sizeof(tc_log_magic)))
3700
4433
  {
3701
 
    sql_print_error(_("Bad magic header in tc log"));
 
4434
    sql_print_error("Bad magic header in tc log");
3702
4435
    goto err1;
3703
4436
  }
3704
4437
 
3708
4441
  */
3709
4442
  if (data[sizeof(tc_log_magic)] != total_ha_2pc)
3710
4443
  {
3711
 
    sql_print_error(_("Recovery failed! You must enable "
 
4444
    sql_print_error("Recovery failed! You must enable "
3712
4445
                    "exactly %d storage engines that support "
3713
 
                    "two-phase commit protocol"),
 
4446
                    "two-phase commit protocol",
3714
4447
                    data[sizeof(tc_log_magic)]);
3715
4448
    goto err1;
3716
4449
  }
3722
4455
  for ( ; p < end_p ; p++)
3723
4456
  {
3724
4457
    for (my_xid *x=p->start; x < p->end; x++)
3725
 
      if (*x && my_hash_insert(&xids, (unsigned char *)x))
 
4458
      if (*x && my_hash_insert(&xids, (uchar *)x))
3726
4459
        goto err2; // OOM
3727
4460
  }
3728
4461
 
3730
4463
    goto err2;
3731
4464
 
3732
4465
  hash_free(&xids);
3733
 
  memset(data, 0, (size_t)file_length);
 
4466
  bzero(data, (size_t)file_length);
3734
4467
  return 0;
3735
4468
 
3736
4469
err2:
3737
4470
  hash_free(&xids);
3738
4471
err1:
3739
 
  sql_print_error(_("Crash recovery failed. Either correct the problem "
 
4472
  sql_print_error("Crash recovery failed. Either correct the problem "
3740
4473
                  "(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}"));
 
4474
                  "or delete tc log and start mysqld with "
 
4475
                  "--tc-heuristic-recover={commit|rollback}");
3743
4476
  return 1;
3744
4477
}
3745
4478
#endif
3766
4499
  if (!tc_heuristic_recover)
3767
4500
    return 0;
3768
4501
 
3769
 
  sql_print_information(_("Heuristic crash recovery mode"));
 
4502
  sql_print_information("Heuristic crash recovery mode");
3770
4503
  if (ha_recover(0))
3771
 
    sql_print_error(_("Heuristic crash recovery failed"));
3772
 
  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");
3773
4506
  return 1;
3774
4507
}
3775
4508
 
3776
4509
/****** transaction coordinator log for 2pc - binlog() based solution ******/
3777
 
#define TC_LOG_BINLOG DRIZZLE_BIN_LOG
 
4510
#define TC_LOG_BINLOG MYSQL_BIN_LOG
3778
4511
 
3779
4512
/**
3780
4513
  @todo
3810
4543
    return 1;
3811
4544
  }
3812
4545
 
3813
 
  if ((error= find_log_pos(&log_info, NULL, 1)))
 
4546
  if ((error= find_log_pos(&log_info, NullS, 1)))
3814
4547
  {
3815
4548
    if (error != LOG_INFO_EOF)
3816
 
      sql_print_error(_("find_log_pos() failed (error: %d)"), error);
 
4549
      sql_print_error("find_log_pos() failed (error: %d)", error);
3817
4550
    else
3818
4551
      error= 0;
3819
4552
    goto err;
3837
4570
 
3838
4571
    if (error !=  LOG_INFO_EOF)
3839
4572
    {
3840
 
      sql_print_error(_("find_log_pos() failed (error: %d)"), error);
 
4573
      sql_print_error("find_log_pos() failed (error: %d)", error);
3841
4574
      goto err;
3842
4575
    }
3843
4576
 
3851
4584
        ev->get_type_code() == FORMAT_DESCRIPTION_EVENT &&
3852
4585
        ev->flags & LOG_EVENT_BINLOG_IN_USE_F)
3853
4586
    {
3854
 
      sql_print_information(_("Recovering after a crash using %s"), opt_name);
 
4587
      sql_print_information("Recovering after a crash using %s", opt_name);
3855
4588
      error= recover(&log, (Format_description_log_event *)ev);
3856
4589
    }
3857
4590
    else
3898
4631
  return(!binlog_end_trans(thd, trx_data, &xle, true));
3899
4632
}
3900
4633
 
3901
 
void TC_LOG_BINLOG::unlog(ulong cookie __attribute__((unused)),
3902
 
                          my_xid xid __attribute__((unused)))
 
4634
void TC_LOG_BINLOG::unlog(ulong cookie __attribute__((__unused__)),
 
4635
                          my_xid xid __attribute__((__unused__)))
3903
4636
{
3904
4637
  pthread_mutex_lock(&LOCK_prep_xids);
3905
4638
  assert(prepared_xids > 0);
3930
4663
    if (ev->get_type_code() == XID_EVENT)
3931
4664
    {
3932
4665
      Xid_log_event *xev=(Xid_log_event *)ev;
3933
 
      unsigned char *x= (unsigned char *) memdup_root(&mem_root, (unsigned char*) &xev->xid,
 
4666
      uchar *x= (uchar *) memdup_root(&mem_root, (uchar*) &xev->xid,
3934
4667
                                      sizeof(xev->xid));
3935
4668
      if (! x)
3936
4669
        goto err2;
3950
4683
  free_root(&mem_root, MYF(0));
3951
4684
  hash_free(&xids);
3952
4685
err1:
3953
 
  sql_print_error(_("Crash recovery failed. Either correct the problem "
 
4686
  sql_print_error("Crash recovery failed. Either correct the problem "
3954
4687
                  "(if it's, for example, out of memory error) and restart, "
3955
4688
                  "or delete (or rename) binary log and start mysqld with "
3956
 
                  "--tc-heuristic-recover={commit|rollback}"));
 
4689
                  "--tc-heuristic-recover={commit|rollback}");
3957
4690
  return 1;
3958
4691
}
3959
4692
 
3973
4706
  @return byte offset from the beginning of the binlog
3974
4707
*/
3975
4708
extern "C"
3976
 
uint64_t mysql_bin_log_file_pos(void)
 
4709
ulonglong mysql_bin_log_file_pos(void)
3977
4710
{
3978
 
  return (uint64_t) mysql_bin_log.get_log_file()->pos_in_file;
 
4711
  return (ulonglong) mysql_bin_log.get_log_file()->pos_in_file;
3979
4712
}
3980
4713
#endif /* INNODB_COMPATIBILITY_HOOKS */
3981
4714
 
3982
4715
 
 
4716
struct st_mysql_storage_engine binlog_storage_engine=
 
4717
{ MYSQL_HANDLERTON_INTERFACE_VERSION };
 
4718
 
3983
4719
mysql_declare_plugin(binlog)
3984
4720
{
3985
 
  DRIZZLE_STORAGE_ENGINE_PLUGIN,
 
4721
  MYSQL_STORAGE_ENGINE_PLUGIN,
 
4722
  &binlog_storage_engine,
3986
4723
  "binlog",
3987
 
  "1.0",
3988
4724
  "MySQL AB",
3989
4725
  "This is a pseudo storage engine to represent the binlog in a transaction",
3990
4726
  PLUGIN_LICENSE_GPL,
3991
4727
  binlog_init, /* Plugin Init */
3992
4728
  NULL, /* Plugin Deinit */
 
4729
  0x0100 /* 1.0 */,
3993
4730
  NULL,                       /* status variables                */
3994
4731
  NULL,                       /* system variables                */
3995
4732
  NULL                        /* config options                  */