~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/log.cc

  • Committer: Brian Aker
  • Date: 2008-09-04 19:31:00 UTC
  • Revision ID: brian@tangent.org-20080904193100-l849hgghfy4urj43
Changing default character set from this point on.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
*/
26
26
 
27
27
#include <drizzled/server_includes.h>
28
 
#include <drizzled/sql_repl.h>
29
 
#include <drizzled/rpl_filter.h>
30
 
#include <drizzled/rpl_rli.h>
 
28
#include "sql_repl.h"
 
29
#include "rpl_filter.h"
 
30
#include "rpl_rli.h"
31
31
 
32
32
#include <mysys/my_dir.h>
33
33
#include <stdarg.h>
34
34
 
35
35
#include <drizzled/plugin.h>
36
 
#include <drizzled/error.h>
37
 
#include <drizzled/gettext.h>
38
 
#include <drizzled/data_home.h>
39
 
 
 
36
#include <drizzled/drizzled_error_messages.h>
 
37
#include <libdrizzle/gettext.h>
40
38
 
41
39
/* max size of the log message */
 
40
#define MAX_LOG_BUFFER_SIZE 1024
 
41
#define MAX_USER_HOST_SIZE 512
 
42
#define MAX_TIME_SIZE 32
42
43
#define MY_OFF_T_UNDEF (~(my_off_t)0UL)
43
44
 
 
45
#define FLAGSTR(V,F) ((V)&(F)?#F" ":"")
 
46
 
44
47
LOGGER logger;
45
48
 
46
49
DRIZZLE_BIN_LOG mysql_bin_log;
47
50
ulong sync_binlog_counter= 0;
48
51
 
49
52
static bool test_if_number(const char *str,
50
 
                           long *res, bool allow_wildcards);
 
53
                           long *res, bool allow_wildcards);
51
54
static int binlog_init(void *p);
52
 
static int binlog_close_connection(handlerton *hton, Session *session);
53
 
static int binlog_savepoint_set(handlerton *hton, Session *session, void *sv);
54
 
static int binlog_savepoint_rollback(handlerton *hton, Session *session, void *sv);
55
 
static int binlog_commit(handlerton *hton, Session *session, bool all);
56
 
static int binlog_rollback(handlerton *hton, Session *session, bool all);
57
 
static int binlog_prepare(handlerton *hton, Session *session, bool all);
 
55
static int binlog_close_connection(handlerton *hton, THD *thd);
 
56
static int binlog_savepoint_set(handlerton *hton, THD *thd, void *sv);
 
57
static int binlog_savepoint_rollback(handlerton *hton, THD *thd, void *sv);
 
58
static int binlog_commit(handlerton *hton, THD *thd, bool all);
 
59
static int binlog_rollback(handlerton *hton, THD *thd, bool all);
 
60
static int binlog_prepare(handlerton *hton, THD *thd, bool all);
58
61
 
59
62
 
60
63
sql_print_message_func sql_print_message_handlers[3] =
200
203
handlerton *binlog_hton;
201
204
 
202
205
 
 
206
/* Check if a given table is opened log table */
 
207
int check_if_log_table(uint db_len __attribute__((unused)),
 
208
                       const char *db __attribute__((unused)),
 
209
                       uint table_name_len __attribute__((unused)),
 
210
                       const char *table_name __attribute__((unused)),
 
211
                       uint check_if_opened __attribute__((unused)))
 
212
{
 
213
  return 0;
 
214
}
 
215
 
 
216
/* log event handlers */
 
217
 
 
218
bool Log_to_file_event_handler::
 
219
  log_error(enum loglevel level, const char *format,
 
220
            va_list args)
 
221
{
 
222
  return vprint_msg_to_log(level, format, args);
 
223
}
 
224
 
 
225
void Log_to_file_event_handler::init_pthread_objects()
 
226
{
 
227
  mysql_log.init_pthread_objects();
 
228
  mysql_slow_log.init_pthread_objects();
 
229
}
 
230
 
 
231
 
 
232
/** Wrapper around DRIZZLE_LOG::write() for slow log. */
 
233
 
 
234
bool Log_to_file_event_handler::
 
235
  log_slow(THD *thd, time_t current_time, time_t query_start_arg,
 
236
           const char *user_host, uint user_host_len,
 
237
           uint64_t query_utime, uint64_t lock_utime, bool is_command,
 
238
           const char *sql_text, uint sql_text_len)
 
239
{
 
240
  return mysql_slow_log.write(thd, current_time, query_start_arg,
 
241
                              user_host, user_host_len,
 
242
                              query_utime, lock_utime, is_command,
 
243
                              sql_text, sql_text_len);
 
244
}
 
245
 
 
246
 
 
247
/**
 
248
   Wrapper around DRIZZLE_LOG::write() for general log. We need it since we
 
249
   want all log event handlers to have the same signature.
 
250
*/
 
251
 
 
252
bool Log_to_file_event_handler::
 
253
  log_general(THD *thd __attribute__((unused)),
 
254
              time_t event_time, const char *user_host,
 
255
              uint user_host_len, int thread_id,
 
256
              const char *command_type, uint command_type_len,
 
257
              const char *sql_text, uint sql_text_len,
 
258
              const CHARSET_INFO * const client_cs __attribute__((unused)))
 
259
{
 
260
  return mysql_log.write(event_time, user_host, user_host_len,
 
261
                         thread_id, command_type, command_type_len,
 
262
                         sql_text, sql_text_len);
 
263
}
 
264
 
 
265
 
 
266
bool Log_to_file_event_handler::init()
 
267
{
 
268
  if (!is_initialized)
 
269
  {
 
270
    if (opt_slow_log)
 
271
      mysql_slow_log.open_slow_log(sys_var_slow_log_path.value);
 
272
 
 
273
    if (opt_log)
 
274
      mysql_log.open_query_log(sys_var_general_log_path.value);
 
275
 
 
276
    is_initialized= true;
 
277
  }
 
278
 
 
279
  return false;
 
280
}
 
281
 
 
282
 
 
283
void Log_to_file_event_handler::cleanup()
 
284
{
 
285
  mysql_log.cleanup();
 
286
  mysql_slow_log.cleanup();
 
287
}
 
288
 
 
289
void Log_to_file_event_handler::flush()
 
290
{
 
291
  /* reopen log files */
 
292
  if (opt_log)
 
293
    mysql_log.reopen_file();
 
294
  if (opt_slow_log)
 
295
    mysql_slow_log.reopen_file();
 
296
}
 
297
 
203
298
/*
204
299
  Log error with all enabled log event handlers
205
300
 
234
329
{
235
330
  assert(inited == 1);
236
331
  rwlock_destroy(&LOCK_logger);
 
332
  if (file_log_handler)
 
333
    file_log_handler->cleanup();
237
334
}
238
335
 
239
336
 
240
337
void LOGGER::cleanup_end()
241
338
{
242
339
  assert(inited == 1);
 
340
  if (file_log_handler)
 
341
    delete file_log_handler;
243
342
}
244
343
 
245
344
 
252
351
  assert(inited == 0);
253
352
  inited= 1;
254
353
 
 
354
  /*
 
355
    Here we create file log handler. We don't do it for the table log handler
 
356
    here as it cannot be created so early. The reason is THD initialization,
 
357
    which depends on the system variables (parsed later).
 
358
  */
 
359
  if (!file_log_handler)
 
360
    file_log_handler= new Log_to_file_event_handler;
 
361
 
255
362
  /* by default we use traditional error log */
256
363
  init_error_log(LOG_FILE);
257
364
 
 
365
  file_log_handler->init_pthread_objects();
258
366
  my_rwlock_init(&LOCK_logger, NULL);
259
367
}
260
368
 
261
369
 
262
 
bool LOGGER::flush_logs(Session *)
 
370
bool LOGGER::flush_logs(THD *thd __attribute__((unused)))
263
371
{
264
372
  int rc= 0;
265
373
 
269
377
  */
270
378
  logger.lock_exclusive();
271
379
 
 
380
  /* reopen log files */
 
381
  file_log_handler->flush();
 
382
 
272
383
  /* end of log flush */
273
384
  logger.unlock();
274
385
  return rc;
275
386
}
276
387
 
277
 
void LOGGER::init_error_log(uint32_t error_log_printer)
 
388
 
 
389
/*
 
390
  Log slow query with all enabled log event handlers
 
391
 
 
392
  SYNOPSIS
 
393
    slow_log_print()
 
394
 
 
395
    thd                 THD of the query being logged
 
396
    query               The query being logged
 
397
    query_length        The length of the query string
 
398
    current_utime       Current time in microseconds (from undefined start)
 
399
 
 
400
  RETURN
 
401
    FALSE   OK
 
402
    TRUE    error occured
 
403
*/
 
404
 
 
405
bool LOGGER::slow_log_print(THD *thd, const char *query, uint query_length,
 
406
                            uint64_t current_utime)
 
407
 
 
408
{
 
409
  bool error= false;
 
410
  Log_event_handler **current_handler;
 
411
  bool is_command= false;
 
412
  char user_host_buff[MAX_USER_HOST_SIZE];
 
413
  Security_context *sctx= thd->security_ctx;
 
414
  uint user_host_len= 0;
 
415
  uint64_t query_utime, lock_utime;
 
416
 
 
417
  /*
 
418
    Print the message to the buffer if we have slow log enabled
 
419
  */
 
420
 
 
421
  if (*slow_log_handler_list)
 
422
  {
 
423
    time_t current_time;
 
424
 
 
425
    /* do not log slow queries from replication threads */
 
426
    if (thd->slave_thread && !opt_log_slow_slave_statements)
 
427
      return 0;
 
428
 
 
429
    lock_shared();
 
430
    if (!opt_slow_log)
 
431
    {
 
432
      unlock();
 
433
      return 0;
 
434
    }
 
435
 
 
436
    /* fill in user_host value: the format is "%s[%s] @ %s [%s]" */
 
437
    user_host_len= (strxnmov(user_host_buff, MAX_USER_HOST_SIZE,
 
438
                             sctx->user, "[", sctx->user, "] @ ",
 
439
                             sctx->ip, " [",
 
440
                             sctx->ip, "]", NullS) -
 
441
                    user_host_buff);
 
442
 
 
443
    current_time= my_time_possible_from_micro(current_utime);
 
444
    if (thd->start_utime)
 
445
    {
 
446
      query_utime= (current_utime - thd->start_utime);
 
447
      lock_utime=  (thd->utime_after_lock - thd->start_utime);
 
448
    }
 
449
    else
 
450
    {
 
451
      query_utime= lock_utime= 0;
 
452
    }
 
453
 
 
454
    if (!query)
 
455
    {
 
456
      is_command= true;
 
457
      query= command_name[thd->command].str;
 
458
      query_length= command_name[thd->command].length;
 
459
    }
 
460
 
 
461
    for (current_handler= slow_log_handler_list; *current_handler ;)
 
462
      error= (*current_handler++)->log_slow(thd, current_time, thd->start_time,
 
463
                                            user_host_buff, user_host_len,
 
464
                                            query_utime, lock_utime, is_command,
 
465
                                            query, query_length) || error;
 
466
 
 
467
    unlock();
 
468
  }
 
469
  return error;
 
470
}
 
471
 
 
472
bool LOGGER::general_log_write(THD *thd, enum enum_server_command command,
 
473
                               const char *query, uint query_length)
 
474
{
 
475
  bool error= false;
 
476
  Log_event_handler **current_handler= general_log_handler_list;
 
477
  char user_host_buff[MAX_USER_HOST_SIZE];
 
478
  Security_context *sctx= thd->security_ctx;
 
479
  ulong id;
 
480
  uint user_host_len= 0;
 
481
  time_t current_time;
 
482
 
 
483
  if (thd)
 
484
    id= thd->thread_id;                 /* Normal thread */
 
485
  else
 
486
    id= 0;                              /* Log from connect handler */
 
487
 
 
488
  lock_shared();
 
489
  if (!opt_log)
 
490
  {
 
491
    unlock();
 
492
    return 0;
 
493
  }
 
494
  user_host_len= strxnmov(user_host_buff, MAX_USER_HOST_SIZE,
 
495
                          sctx->user, "[",
 
496
                          sctx->user, "] @ ",
 
497
                          sctx->ip, " [",
 
498
                          sctx->ip, "]", NullS) -
 
499
                                                          user_host_buff;
 
500
 
 
501
  current_time= my_time(0);
 
502
 
 
503
  while (*current_handler)
 
504
    error|= (*current_handler++)->
 
505
      log_general(thd, current_time, user_host_buff,
 
506
                  user_host_len, id,
 
507
                  command_name[(uint) command].str,
 
508
                  command_name[(uint) command].length,
 
509
                  query, query_length,
 
510
                  thd->variables.character_set_client) || error;
 
511
  unlock();
 
512
 
 
513
  return error;
 
514
}
 
515
 
 
516
bool LOGGER::general_log_print(THD *thd, enum enum_server_command command,
 
517
                               const char *format, va_list args)
 
518
{
 
519
  uint message_buff_len= 0;
 
520
  char message_buff[MAX_LOG_BUFFER_SIZE];
 
521
 
 
522
  /* prepare message */
 
523
  if (format)
 
524
    message_buff_len= vsnprintf(message_buff, sizeof(message_buff),
 
525
                                   format, args);
 
526
  else
 
527
    message_buff[0]= '\0';
 
528
 
 
529
  return general_log_write(thd, command, message_buff, message_buff_len);
 
530
}
 
531
 
 
532
void LOGGER::init_error_log(uint error_log_printer)
278
533
{
279
534
  if (error_log_printer & LOG_NONE)
280
535
  {
282
537
    return;
283
538
  }
284
539
 
285
 
}
286
 
 
287
 
int LOGGER::set_handlers(uint32_t error_log_printer)
 
540
  switch (error_log_printer) {
 
541
  case LOG_FILE:
 
542
    error_log_handler_list[0]= file_log_handler;
 
543
    error_log_handler_list[1]= 0;
 
544
    break;
 
545
  }
 
546
}
 
547
 
 
548
void LOGGER::init_slow_log(uint slow_log_printer)
 
549
{
 
550
  if (slow_log_printer & LOG_NONE)
 
551
  {
 
552
    slow_log_handler_list[0]= 0;
 
553
    return;
 
554
  }
 
555
 
 
556
  slow_log_handler_list[0]= file_log_handler;
 
557
  slow_log_handler_list[1]= 0;
 
558
}
 
559
 
 
560
void LOGGER::init_general_log(uint general_log_printer)
 
561
{
 
562
  if (general_log_printer & LOG_NONE)
 
563
  {
 
564
    general_log_handler_list[0]= 0;
 
565
    return;
 
566
  }
 
567
 
 
568
  general_log_handler_list[0]= file_log_handler;
 
569
  general_log_handler_list[1]= 0;
 
570
}
 
571
 
 
572
 
 
573
bool LOGGER::activate_log_handler(THD* thd __attribute__((unused)),
 
574
                                  uint log_type)
 
575
{
 
576
  DRIZZLE_QUERY_LOG *file_log;
 
577
  bool res= false;
 
578
  lock_exclusive();
 
579
  switch (log_type) {
 
580
  case QUERY_LOG_SLOW:
 
581
    if (!opt_slow_log)
 
582
    {
 
583
      file_log= file_log_handler->get_mysql_slow_log();
 
584
 
 
585
      file_log->open_slow_log(sys_var_slow_log_path.value);
 
586
      init_slow_log(log_output_options);
 
587
      opt_slow_log= true;
 
588
    }
 
589
    break;
 
590
  case QUERY_LOG_GENERAL:
 
591
    if (!opt_log)
 
592
    {
 
593
      file_log= file_log_handler->get_mysql_log();
 
594
 
 
595
      file_log->open_query_log(sys_var_general_log_path.value);
 
596
      init_general_log(log_output_options);
 
597
      opt_log= true;
 
598
    }
 
599
    break;
 
600
  default:
 
601
    assert(0);
 
602
  }
 
603
  unlock();
 
604
  return res;
 
605
}
 
606
 
 
607
 
 
608
void LOGGER::deactivate_log_handler(THD *thd __attribute__((unused)),
 
609
                                    uint log_type)
 
610
{
 
611
  bool *tmp_opt= 0;
 
612
  DRIZZLE_LOG *file_log;
 
613
 
 
614
  switch (log_type) {
 
615
  case QUERY_LOG_SLOW:
 
616
    tmp_opt= &opt_slow_log;
 
617
    file_log= file_log_handler->get_mysql_slow_log();
 
618
    break;
 
619
  case QUERY_LOG_GENERAL:
 
620
    tmp_opt= &opt_log;
 
621
    file_log= file_log_handler->get_mysql_log();
 
622
    break;
 
623
  default:
 
624
    assert(0);                                  // Impossible
 
625
  }
 
626
 
 
627
  if (!(*tmp_opt))
 
628
    return;
 
629
 
 
630
  lock_exclusive();
 
631
  file_log->close(0);
 
632
  *tmp_opt= false;
 
633
  unlock();
 
634
}
 
635
 
 
636
int LOGGER::set_handlers(uint error_log_printer,
 
637
                         uint slow_log_printer,
 
638
                         uint general_log_printer)
288
639
{
289
640
  /* error log table is not supported yet */
290
641
  lock_exclusive();
291
642
 
292
643
  init_error_log(error_log_printer);
 
644
  init_slow_log(slow_log_printer);
 
645
  init_general_log(general_log_printer);
 
646
 
293
647
  unlock();
294
648
 
295
649
  return 0;
302
656
  SYNPOSIS
303
657
    binlog_trans_log_savepos()
304
658
 
305
 
    session      The thread to take the binlog data from
 
659
    thd      The thread to take the binlog data from
306
660
    pos      Pointer to variable where the position will be stored
307
661
 
308
662
  DESCRIPTION
312
666
 */
313
667
 
314
668
static void
315
 
binlog_trans_log_savepos(Session *session, my_off_t *pos)
 
669
binlog_trans_log_savepos(THD *thd, my_off_t *pos)
316
670
{
317
671
  assert(pos != NULL);
318
 
  if (session_get_ha_data(session, binlog_hton) == NULL)
319
 
    session->binlog_setup_trx_data();
 
672
  if (thd_get_ha_data(thd, binlog_hton) == NULL)
 
673
    thd->binlog_setup_trx_data();
320
674
  binlog_trx_data *const trx_data=
321
 
    (binlog_trx_data*) session_get_ha_data(session, binlog_hton);
 
675
    (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
322
676
  assert(mysql_bin_log.is_open());
323
677
  *pos= trx_data->position();
324
678
  return;
331
685
  SYNPOSIS
332
686
    binlog_trans_log_truncate()
333
687
 
334
 
    session      The thread to take the binlog data from
 
688
    thd      The thread to take the binlog data from
335
689
    pos      Position to truncate to
336
690
 
337
691
  DESCRIPTION
341
695
 
342
696
 */
343
697
static void
344
 
binlog_trans_log_truncate(Session *session, my_off_t pos)
 
698
binlog_trans_log_truncate(THD *thd, my_off_t pos)
345
699
{
346
 
  assert(session_get_ha_data(session, binlog_hton) != NULL);
 
700
  assert(thd_get_ha_data(thd, binlog_hton) != NULL);
347
701
  /* Only true if binlog_trans_log_savepos() wasn't called before */
348
702
  assert(pos != ~(my_off_t) 0);
349
703
 
350
704
  binlog_trx_data *const trx_data=
351
 
    (binlog_trx_data*) session_get_ha_data(session, binlog_hton);
 
705
    (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
352
706
  trx_data->truncate(pos);
353
707
  return;
354
708
}
364
718
{
365
719
  binlog_hton= (handlerton *)p;
366
720
  binlog_hton->state=opt_bin_log ? SHOW_OPTION_YES : SHOW_OPTION_NO;
 
721
  binlog_hton->db_type=DB_TYPE_BINLOG;
367
722
  binlog_hton->savepoint_offset= sizeof(my_off_t);
368
723
  binlog_hton->close_connection= binlog_close_connection;
369
724
  binlog_hton->savepoint_set= binlog_savepoint_set;
376
731
  return 0;
377
732
}
378
733
 
379
 
static int binlog_close_connection(handlerton *, Session *session)
 
734
static int binlog_close_connection(handlerton *hton __attribute__((unused)),
 
735
                                   THD *thd)
380
736
{
381
737
  binlog_trx_data *const trx_data=
382
 
    (binlog_trx_data*) session_get_ha_data(session, binlog_hton);
 
738
    (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
383
739
  assert(trx_data->empty());
384
 
  session_set_ha_data(session, binlog_hton, NULL);
 
740
  thd_set_ha_data(thd, binlog_hton, NULL);
385
741
  trx_data->~binlog_trx_data();
386
 
  free((unsigned char*)trx_data);
 
742
  my_free((uchar*)trx_data, MYF(0));
387
743
  return 0;
388
744
}
389
745
 
393
749
  SYNOPSIS
394
750
    binlog_end_trans()
395
751
 
396
 
    session      The thread whose transaction should be ended
 
752
    thd      The thread whose transaction should be ended
397
753
    trx_data Pointer to the transaction data to use
398
754
    end_ev   The end event to use, or NULL
399
755
    all      True if the entire transaction should be ended, false if
411
767
    'all' is false), or reset completely (if 'all' is true).
412
768
 */
413
769
static int
414
 
binlog_end_trans(Session *session, binlog_trx_data *trx_data,
 
770
binlog_end_trans(THD *thd, binlog_trx_data *trx_data,
415
771
                 Log_event *end_ev, bool all)
416
772
{
417
773
  int error=0;
435
791
      were, we would have to ensure that we're not ending a statement
436
792
      inside a stored function.
437
793
     */
438
 
    session->binlog_flush_pending_rows_event(true);
 
794
    thd->binlog_flush_pending_rows_event(true);
439
795
 
440
 
    error= mysql_bin_log.write(session, &trx_data->trans_log, end_ev);
 
796
    error= mysql_bin_log.write(thd, &trx_data->trans_log, end_ev);
441
797
    trx_data->reset();
442
798
 
443
799
    /*
461
817
      If rolling back a statement in a transaction, we truncate the
462
818
      transaction cache to remove the statement.
463
819
     */
464
 
    if (all || !(session->options & (OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT)))
 
820
    if (all || !(thd->options & (OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT)))
465
821
    {
466
822
      trx_data->reset();
467
823
 
468
 
      assert(!session->binlog_get_pending_rows_event());
469
 
      session->clear_binlog_table_maps();
 
824
      assert(!thd->binlog_get_pending_rows_event());
 
825
      thd->clear_binlog_table_maps();
470
826
    }
471
827
    else                                        // ...statement
472
828
      trx_data->truncate(trx_data->before_stmt_pos);
482
838
  return(error);
483
839
}
484
840
 
485
 
static int binlog_prepare(handlerton *, Session *, bool)
 
841
static int binlog_prepare(handlerton *hton __attribute__((unused)),
 
842
                          THD *thd __attribute__((unused)),
 
843
                          bool all __attribute__((unused)))
486
844
{
487
845
  /*
488
846
    do nothing.
493
851
  return 0;
494
852
}
495
853
 
 
854
#define YESNO(X) ((X) ? "yes" : "no")
 
855
 
496
856
/**
497
857
  This function is called once after each statement.
498
858
 
500
860
  binlog file on commits.
501
861
 
502
862
  @param hton  The binlog handlerton.
503
 
  @param session   The client thread that executes the transaction.
 
863
  @param thd   The client thread that executes the transaction.
504
864
  @param all   This is @c true if this is a real transaction commit, and
505
865
               @false otherwise.
506
866
 
507
867
  @see handlerton::commit
508
868
*/
509
 
static int binlog_commit(handlerton *, Session *session, bool all)
 
869
static int binlog_commit(handlerton *hton __attribute__((unused)),
 
870
                         THD *thd, bool all)
510
871
{
511
872
  binlog_trx_data *const trx_data=
512
 
    (binlog_trx_data*) session_get_ha_data(session, binlog_hton);
 
873
    (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
513
874
 
514
875
  if (trx_data->empty())
515
876
  {
577
938
    Otherwise, we accumulate the statement
578
939
  */
579
940
  uint64_t const in_transaction=
580
 
    session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN);
581
 
  if ((in_transaction && (all || (!trx_data->at_least_one_stmt && session->transaction.stmt.modified_non_trans_table))) || (!in_transaction && !all))
 
941
    thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN);
 
942
  if ((in_transaction && (all || (!trx_data->at_least_one_stmt && thd->transaction.stmt.modified_non_trans_table))) || (!in_transaction && !all))
582
943
  {
583
 
    Query_log_event qev(session, STRING_WITH_LEN("COMMIT"), true, false);
584
 
    qev.error_code= 0; // see comment in DRIZZLE_LOG::write(Session, IO_CACHE)
585
 
    int error= binlog_end_trans(session, trx_data, &qev, all);
 
944
    Query_log_event qev(thd, STRING_WITH_LEN("COMMIT"), true, false);
 
945
    qev.error_code= 0; // see comment in DRIZZLE_LOG::write(THD, IO_CACHE)
 
946
    int error= binlog_end_trans(thd, trx_data, &qev, all);
586
947
    return(error);
587
948
  }
588
949
  return(0);
597
958
  non-transactional tables, nothing needs to be logged.
598
959
 
599
960
  @param hton  The binlog handlerton.
600
 
  @param session   The client thread that executes the transaction.
 
961
  @param thd   The client thread that executes the transaction.
601
962
  @param all   This is @c true if this is a real transaction rollback, and
602
963
               @false otherwise.
603
964
 
604
965
  @see handlerton::rollback
605
966
*/
606
 
static int binlog_rollback(handlerton *, Session *session, bool all)
 
967
static int binlog_rollback(handlerton *hton __attribute__((unused)),
 
968
                           THD *thd, bool all)
607
969
{
608
970
  int error=0;
609
971
  binlog_trx_data *const trx_data=
610
 
    (binlog_trx_data*) session_get_ha_data(session, binlog_hton);
 
972
    (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
611
973
 
612
974
  if (trx_data->empty()) {
613
975
    trx_data->reset();
614
976
    return(0);
615
977
  }
616
978
 
617
 
  if ((all && session->transaction.all.modified_non_trans_table) ||
618
 
      (!all && session->transaction.stmt.modified_non_trans_table) ||
619
 
      (session->options & OPTION_KEEP_LOG))
 
979
  if ((all && thd->transaction.all.modified_non_trans_table) ||
 
980
      (!all && thd->transaction.stmt.modified_non_trans_table) ||
 
981
      (thd->options & OPTION_KEEP_LOG))
620
982
  {
621
983
    /*
622
984
      We write the transaction cache with a rollback last if we have
626
988
      transactional table in that statement as well, which needs to be
627
989
      rolled back on the slave.
628
990
    */
629
 
    Query_log_event qev(session, STRING_WITH_LEN("ROLLBACK"), true, false);
630
 
    qev.error_code= 0; // see comment in DRIZZLE_LOG::write(Session, IO_CACHE)
631
 
    error= binlog_end_trans(session, trx_data, &qev, all);
 
991
    Query_log_event qev(thd, STRING_WITH_LEN("ROLLBACK"), true, false);
 
992
    qev.error_code= 0; // see comment in DRIZZLE_LOG::write(THD, IO_CACHE)
 
993
    error= binlog_end_trans(thd, trx_data, &qev, all);
632
994
  }
633
 
  else if ((all && !session->transaction.all.modified_non_trans_table) ||
634
 
           (!all && !session->transaction.stmt.modified_non_trans_table))
 
995
  else if ((all && !thd->transaction.all.modified_non_trans_table) ||
 
996
           (!all && !thd->transaction.stmt.modified_non_trans_table))
635
997
  {
636
998
    /*
637
999
      If we have modified only transactional tables, we can truncate
638
1000
      the transaction cache without writing anything to the binary
639
1001
      log.
640
1002
     */
641
 
    error= binlog_end_trans(session, trx_data, 0, all);
 
1003
    error= binlog_end_trans(thd, trx_data, 0, all);
642
1004
  }
643
1005
  return(error);
644
1006
}
667
1029
  that case there is no need to have it in the binlog).
668
1030
*/
669
1031
 
670
 
static int binlog_savepoint_set(handlerton *, Session *session, void *sv)
 
1032
static int binlog_savepoint_set(handlerton *hton __attribute__((unused)),
 
1033
                                THD *thd, void *sv)
671
1034
{
672
 
  binlog_trans_log_savepos(session, (my_off_t*) sv);
 
1035
  binlog_trans_log_savepos(thd, (my_off_t*) sv);
673
1036
  /* Write it to the binary log */
674
 
 
 
1037
  
675
1038
  int const error=
676
 
    session->binlog_query(Session::STMT_QUERY_TYPE,
677
 
                      session->query, session->query_length, true, false);
 
1039
    thd->binlog_query(THD::STMT_QUERY_TYPE,
 
1040
                      thd->query, thd->query_length, true, false);
678
1041
  return(error);
679
1042
}
680
1043
 
681
 
static int binlog_savepoint_rollback(handlerton *, Session *session, void *sv)
 
1044
static int binlog_savepoint_rollback(handlerton *hton __attribute__((unused)),
 
1045
                                     THD *thd, void *sv)
682
1046
{
683
1047
  /*
684
1048
    Write ROLLBACK TO SAVEPOINT to the binlog cache if we have updated some
685
1049
    non-transactional table. Otherwise, truncate the binlog cache starting
686
1050
    from the SAVEPOINT command.
687
1051
  */
688
 
  if (unlikely(session->transaction.all.modified_non_trans_table || 
689
 
               (session->options & OPTION_KEEP_LOG)))
 
1052
  if (unlikely(thd->transaction.all.modified_non_trans_table || 
 
1053
               (thd->options & OPTION_KEEP_LOG)))
690
1054
  {
691
1055
    int error=
692
 
      session->binlog_query(Session::STMT_QUERY_TYPE,
693
 
                        session->query, session->query_length, true, false);
 
1056
      thd->binlog_query(THD::STMT_QUERY_TYPE,
 
1057
                        thd->query, thd->query_length, true, false);
694
1058
    return(error);
695
1059
  }
696
 
  binlog_trans_log_truncate(session, *(my_off_t*)sv);
 
1060
  binlog_trans_log_truncate(thd, *(my_off_t*)sv);
697
1061
  return(0);
698
1062
}
699
1063
 
703
1067
  char magic[4];
704
1068
  assert(my_b_tell(log) == 0);
705
1069
 
706
 
  if (my_b_read(log, (unsigned char*) magic, sizeof(magic)))
 
1070
  if (my_b_read(log, (uchar*) magic, sizeof(magic)))
707
1071
  {
708
1072
    *errmsg = _("I/O error reading the header from the binary log");
709
1073
    sql_print_error("%s, errno=%d, io cache code=%d", *errmsg, my_errno,
724
1088
{
725
1089
  File file;
726
1090
 
727
 
  if ((file = my_open(log_file_name, O_RDONLY, 
 
1091
  if ((file = my_open(log_file_name, O_RDONLY | O_BINARY | O_SHARE, 
728
1092
                      MYF(MY_WME))) < 0)
729
1093
  {
730
1094
    sql_print_error(_("Failed to open log (file '%s', errno %d)"),
766
1130
static int find_uniq_filename(char *name)
767
1131
{
768
1132
  long                  number;
769
 
  uint32_t                  i;
 
1133
  uint                  i;
770
1134
  char                  buff[FN_REFLEN];
771
1135
  struct st_my_dir     *dir_info;
772
1136
  register struct fileinfo *file_info;
783
1147
 
784
1148
  if (!(dir_info = my_dir(buff,MYF(MY_DONT_SORT))))
785
1149
  {                                             // This shouldn't happen
786
 
    my_stpcpy(end,".1");                                // use name+1
 
1150
    stpcpy(end,".1");                           // use name+1
787
1151
    return(0);
788
1152
  }
789
1153
  file_info= dir_info->dir_entry;
838
1202
{
839
1203
  char buff[FN_REFLEN];
840
1204
  File file= -1;
841
 
  int open_flags= O_CREAT;
 
1205
  int open_flags= O_CREAT | O_BINARY;
842
1206
 
843
1207
  write_error= 0;
844
1208
 
851
1215
  }
852
1216
 
853
1217
  if (new_name)
854
 
    my_stpcpy(log_file_name, new_name);
 
1218
    stpcpy(log_file_name, new_name);
855
1219
  else if (generate_new_name(log_file_name, name))
856
1220
    goto err;
857
1221
 
875
1239
    char *end;
876
1240
    int len=snprintf(buff, sizeof(buff), "%s, Version: %s (%s). "
877
1241
                     "started with:\nTCP Port: %d, Named Pipe: %s\n",
878
 
                     my_progname, server_version, COMPILATION_COMMENT,
879
 
                     drizzled_port, ""
 
1242
                     my_progname, server_version, DRIZZLE_COMPILATION_COMMENT,
 
1243
                     mysqld_port, ""
880
1244
                     );
881
 
    end= my_stpncpy(buff + len, "Time                 Id Command    Argument\n",
 
1245
    end= stpncpy(buff + len, "Time                 Id Command    Argument\n",
882
1246
                 sizeof(buff) - len);
883
 
    if (my_b_write(&log_file, (unsigned char*) buff, (uint) (end-buff)) ||
 
1247
    if (my_b_write(&log_file, (uchar*) buff, (uint) (end-buff)) ||
884
1248
        flush_io_cache(&log_file))
885
1249
      goto err;
886
1250
  }
898
1262
  if (file >= 0)
899
1263
    my_close(file, MYF(0));
900
1264
  end_io_cache(&log_file);
901
 
  if (name)
902
 
  {
903
 
    free(name);
904
 
    name= NULL;
905
 
  }
 
1265
  safeFree(name);
906
1266
  log_state= LOG_CLOSED;
907
1267
  return(1);
908
1268
}
941
1301
    The internal structures are not freed until cleanup() is called
942
1302
*/
943
1303
 
944
 
void DRIZZLE_LOG::close(uint32_t exiting)
 
1304
void DRIZZLE_LOG::close(uint exiting)
945
1305
{                                       // One can't set log_type here!
946
1306
  if (log_state == LOG_OPENED)
947
1307
  {
961
1321
  }
962
1322
 
963
1323
  log_state= (exiting & LOG_CLOSE_TO_BE_OPENED) ? LOG_TO_BE_OPENED : LOG_CLOSED;
964
 
  if (name)
965
 
  {
966
 
    free(name);
967
 
    name= NULL;
968
 
  }
 
1324
  safeFree(name);
969
1325
  return;
970
1326
}
971
1327
 
1001
1357
}
1002
1358
 
1003
1359
 
 
1360
/*
 
1361
  Reopen the log file
 
1362
 
 
1363
  SYNOPSIS
 
1364
    reopen_file()
 
1365
 
 
1366
  DESCRIPTION
 
1367
    Reopen the log file. The method is used during FLUSH LOGS
 
1368
    and locks LOCK_log mutex
 
1369
*/
 
1370
 
 
1371
 
 
1372
void DRIZZLE_QUERY_LOG::reopen_file()
 
1373
{
 
1374
  char *save_name;
 
1375
 
 
1376
  if (!is_open())
 
1377
  {
 
1378
    return;
 
1379
  }
 
1380
 
 
1381
  pthread_mutex_lock(&LOCK_log);
 
1382
 
 
1383
  save_name= name;
 
1384
  name= 0;                              // Don't free name
 
1385
  close(LOG_CLOSE_TO_BE_OPENED);
 
1386
 
 
1387
  /*
 
1388
     Note that at this point, log_state != LOG_CLOSED (important for is_open()).
 
1389
  */
 
1390
 
 
1391
  open(save_name, log_type, 0, io_cache_type);
 
1392
  my_free(save_name, MYF(0));
 
1393
 
 
1394
  pthread_mutex_unlock(&LOCK_log);
 
1395
 
 
1396
  return;
 
1397
}
 
1398
 
 
1399
 
 
1400
/*
 
1401
  Write a command to traditional general log file
 
1402
 
 
1403
  SYNOPSIS
 
1404
    write()
 
1405
 
 
1406
    event_time        command start timestamp
 
1407
    user_host         the pointer to the string with user@host info
 
1408
    user_host_len     length of the user_host string. this is computed once
 
1409
                      and passed to all general log  event handlers
 
1410
    thread_id         Id of the thread, issued a query
 
1411
    command_type      the type of the command being logged
 
1412
    command_type_len  the length of the string above
 
1413
    sql_text          the very text of the query being executed
 
1414
    sql_text_len      the length of sql_text string
 
1415
 
 
1416
  DESCRIPTION
 
1417
 
 
1418
   Log given command to to normal (not rotable) log file
 
1419
 
 
1420
  RETURN
 
1421
    FASE - OK
 
1422
    TRUE - error occured
 
1423
*/
 
1424
 
 
1425
bool DRIZZLE_QUERY_LOG::write(time_t event_time,
 
1426
                            const char *user_host __attribute__((unused)),
 
1427
                            uint user_host_len __attribute__((unused)),
 
1428
                            int thread_id,
 
1429
                            const char *command_type, uint command_type_len,
 
1430
                            const char *sql_text, uint sql_text_len)
 
1431
{
 
1432
  char buff[32];
 
1433
  uint length= 0;
 
1434
  char local_time_buff[MAX_TIME_SIZE];
 
1435
  struct tm start;
 
1436
  uint time_buff_len= 0;
 
1437
 
 
1438
  (void) pthread_mutex_lock(&LOCK_log);
 
1439
 
 
1440
  /* Test if someone closed between the is_open test and lock */
 
1441
  if (is_open())
 
1442
  {
 
1443
    /* Note that my_b_write() assumes it knows the length for this */
 
1444
      if (event_time != last_time)
 
1445
      {
 
1446
        last_time= event_time;
 
1447
 
 
1448
        localtime_r(&event_time, &start);
 
1449
 
 
1450
        time_buff_len= snprintf(local_time_buff, MAX_TIME_SIZE,
 
1451
                                "%02d%02d%02d %2d:%02d:%02d",
 
1452
                                start.tm_year % 100, start.tm_mon + 1,
 
1453
                                start.tm_mday, start.tm_hour,
 
1454
                                start.tm_min, start.tm_sec);
 
1455
 
 
1456
        if (my_b_write(&log_file, (uchar*) local_time_buff, time_buff_len))
 
1457
          goto err;
 
1458
      }
 
1459
      else
 
1460
        if (my_b_write(&log_file, (uchar*) "\t\t" ,2) < 0)
 
1461
          goto err;
 
1462
 
 
1463
      /* command_type, thread_id */
 
1464
      length= snprintf(buff, 32, "%5ld ", (long) thread_id);
 
1465
 
 
1466
    if (my_b_write(&log_file, (uchar*) buff, length))
 
1467
      goto err;
 
1468
 
 
1469
    if (my_b_write(&log_file, (uchar*) command_type, command_type_len))
 
1470
      goto err;
 
1471
 
 
1472
    if (my_b_write(&log_file, (uchar*) "\t", 1))
 
1473
      goto err;
 
1474
 
 
1475
    /* sql_text */
 
1476
    if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len))
 
1477
      goto err;
 
1478
 
 
1479
    if (my_b_write(&log_file, (uchar*) "\n", 1) ||
 
1480
        flush_io_cache(&log_file))
 
1481
      goto err;
 
1482
  }
 
1483
 
 
1484
  (void) pthread_mutex_unlock(&LOCK_log);
 
1485
  return false;
 
1486
err:
 
1487
 
 
1488
  if (!write_error)
 
1489
  {
 
1490
    write_error= 1;
 
1491
    sql_print_error(ER(ER_ERROR_ON_WRITE), name, errno);
 
1492
  }
 
1493
  (void) pthread_mutex_unlock(&LOCK_log);
 
1494
  return true;
 
1495
}
 
1496
 
 
1497
 
 
1498
/*
 
1499
  Log a query to the traditional slow log file
 
1500
 
 
1501
  SYNOPSIS
 
1502
    write()
 
1503
 
 
1504
    thd               THD of the query
 
1505
    current_time      current timestamp
 
1506
    query_start_arg   command start timestamp
 
1507
    user_host         the pointer to the string with user@host info
 
1508
    user_host_len     length of the user_host string. this is computed once
 
1509
                      and passed to all general log event handlers
 
1510
    query_utime       Amount of time the query took to execute (in microseconds)
 
1511
    lock_utime        Amount of time the query was locked (in microseconds)
 
1512
    is_command        The flag, which determines, whether the sql_text is a
 
1513
                      query or an administrator command.
 
1514
    sql_text          the very text of the query or administrator command
 
1515
                      processed
 
1516
    sql_text_len      the length of sql_text string
 
1517
 
 
1518
  DESCRIPTION
 
1519
 
 
1520
   Log a query to the slow log file.
 
1521
 
 
1522
  RETURN
 
1523
    FALSE - OK
 
1524
    TRUE - error occured
 
1525
*/
 
1526
 
 
1527
bool DRIZZLE_QUERY_LOG::write(THD *thd, time_t current_time,
 
1528
                            time_t query_start_arg __attribute__((unused)),
 
1529
                            const char *user_host,
 
1530
                            uint user_host_len, uint64_t query_utime,
 
1531
                            uint64_t lock_utime, bool is_command,
 
1532
                            const char *sql_text, uint sql_text_len)
 
1533
{
 
1534
  bool error= 0;
 
1535
 
 
1536
  (void) pthread_mutex_lock(&LOCK_log);
 
1537
 
 
1538
  if (!is_open())
 
1539
  {
 
1540
    (void) pthread_mutex_unlock(&LOCK_log);
 
1541
    return(0);
 
1542
  }
 
1543
 
 
1544
  if (is_open())
 
1545
  {                                             // Safety agains reopen
 
1546
    int tmp_errno= 0;
 
1547
    char buff[80], *end;
 
1548
    char query_time_buff[22+7], lock_time_buff[22+7];
 
1549
    uint buff_len;
 
1550
    end= buff;
 
1551
 
 
1552
    {
 
1553
      if (current_time != last_time)
 
1554
      {
 
1555
        last_time= current_time;
 
1556
        struct tm start;
 
1557
        localtime_r(&current_time, &start);
 
1558
 
 
1559
        buff_len= snprintf(buff, sizeof buff,
 
1560
                           "# Time: %02d%02d%02d %2d:%02d:%02d\n",
 
1561
                           start.tm_year % 100, start.tm_mon + 1,
 
1562
                           start.tm_mday, start.tm_hour,
 
1563
                           start.tm_min, start.tm_sec);
 
1564
 
 
1565
        /* Note that my_b_write() assumes it knows the length for this */
 
1566
        if (my_b_write(&log_file, (uchar*) buff, buff_len))
 
1567
          tmp_errno= errno;
 
1568
      }
 
1569
      const uchar uh[]= "# User@Host: ";
 
1570
      if (my_b_write(&log_file, uh, sizeof(uh) - 1))
 
1571
        tmp_errno= errno;
 
1572
      if (my_b_write(&log_file, (uchar*) user_host, user_host_len))
 
1573
        tmp_errno= errno;
 
1574
      if (my_b_write(&log_file, (uchar*) "\n", 1))
 
1575
        tmp_errno= errno;
 
1576
    }
 
1577
    /* For slow query log */
 
1578
    sprintf(query_time_buff, "%.6f", uint64_t2double(query_utime)/1000000.0);
 
1579
    sprintf(lock_time_buff,  "%.6f", uint64_t2double(lock_utime)/1000000.0);
 
1580
    if (my_b_printf(&log_file,
 
1581
                    "# Query_time: %s  Lock_time: %s"
 
1582
                    " Rows_sent: %lu  Rows_examined: %lu\n",
 
1583
                    query_time_buff, lock_time_buff,
 
1584
                    (ulong) thd->sent_row_count,
 
1585
                    (ulong) thd->examined_row_count) == (uint) -1)
 
1586
      tmp_errno= errno;
 
1587
    if (thd->db && strcmp(thd->db, db))
 
1588
    {                                           // Database changed
 
1589
      if (my_b_printf(&log_file,"use %s;\n",thd->db) == (uint) -1)
 
1590
        tmp_errno= errno;
 
1591
      stpcpy(db,thd->db);
 
1592
    }
 
1593
    if (thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt)
 
1594
    {
 
1595
      end=stpcpy(end, ",last_insert_id=");
 
1596
      end=int64_t10_to_str((int64_t)
 
1597
                            thd->first_successful_insert_id_in_prev_stmt_for_binlog,
 
1598
                            end, -10);
 
1599
    }
 
1600
    // Save value if we do an insert.
 
1601
    if (thd->auto_inc_intervals_in_cur_stmt_for_binlog.nb_elements() > 0)
 
1602
    {
 
1603
      {
 
1604
        end=stpcpy(end,",insert_id=");
 
1605
        end=int64_t10_to_str((int64_t)
 
1606
                              thd->auto_inc_intervals_in_cur_stmt_for_binlog.minimum(),
 
1607
                              end, -10);
 
1608
      }
 
1609
    }
 
1610
 
 
1611
    /*
 
1612
      This info used to show up randomly, depending on whether the query
 
1613
      checked the query start time or not. now we always write current
 
1614
      timestamp to the slow log
 
1615
    */
 
1616
    end= stpcpy(end, ",timestamp=");
 
1617
    end= int10_to_str((long) current_time, end, 10);
 
1618
 
 
1619
    if (end != buff)
 
1620
    {
 
1621
      *end++=';';
 
1622
      *end='\n';
 
1623
      if (my_b_write(&log_file, (uchar*) "SET ", 4) ||
 
1624
          my_b_write(&log_file, (uchar*) buff + 1, (uint) (end-buff)))
 
1625
        tmp_errno= errno;
 
1626
    }
 
1627
    if (is_command)
 
1628
    {
 
1629
      end= strxmov(buff, "# administrator command: ", NullS);
 
1630
      buff_len= (ulong) (end - buff);
 
1631
      my_b_write(&log_file, (uchar*) buff, buff_len);
 
1632
    }
 
1633
    if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len) ||
 
1634
        my_b_write(&log_file, (uchar*) ";\n",2) ||
 
1635
        flush_io_cache(&log_file))
 
1636
      tmp_errno= errno;
 
1637
    if (tmp_errno)
 
1638
    {
 
1639
      error= 1;
 
1640
      if (! write_error)
 
1641
      {
 
1642
        write_error= 1;
 
1643
        sql_print_error(ER(ER_ERROR_ON_WRITE), name, error);
 
1644
      }
 
1645
    }
 
1646
  }
 
1647
  (void) pthread_mutex_unlock(&LOCK_log);
 
1648
  return(error);
 
1649
}
 
1650
 
 
1651
 
1004
1652
/**
1005
1653
  @todo
1006
1654
  The following should be using fn_format();  We just need to
1020
1668
  if (strip_ext)
1021
1669
  {
1022
1670
    char *p= fn_ext(log_name);
1023
 
    uint32_t length= (uint) (p - log_name);
1024
 
    strmake(buff, log_name, cmin(length, (uint)FN_REFLEN));
 
1671
    uint length= (uint) (p - log_name);
 
1672
    strmake(buff, log_name, min(length, (uint)FN_REFLEN));
1025
1673
    return (const char*)buff;
1026
1674
  }
1027
1675
  return log_name;
1101
1749
  fn_format(index_file_name, index_file_name_arg, mysql_data_home,
1102
1750
            ".index", opt);
1103
1751
  if ((index_file_nr= my_open(index_file_name,
1104
 
                              O_RDWR | O_CREAT,
 
1752
                              O_RDWR | O_CREAT | O_BINARY ,
1105
1753
                              MYF(MY_WME))) < 0 ||
1106
1754
       my_sync(index_file_nr, MYF(MY_WME)) ||
1107
1755
       init_io_cache(&index_file, index_file_nr,
1169
1817
        an extension for the binary log files.
1170
1818
        In this case we write a standard header to it.
1171
1819
      */
1172
 
      if (my_b_safe_write(&log_file, (unsigned char*) BINLOG_MAGIC,
 
1820
      if (my_b_safe_write(&log_file, (uchar*) BINLOG_MAGIC,
1173
1821
                          BIN_LOG_HEADER_SIZE))
1174
1822
        goto err;
1175
1823
      bytes_written+= BIN_LOG_HEADER_SIZE;
1240
1888
        As this is a new log file, we write the file name to the index
1241
1889
        file. As every time we write to the index file, we sync it.
1242
1890
      */
1243
 
      if (my_b_write(&index_file, (unsigned char*) log_file_name,
 
1891
      if (my_b_write(&index_file, (uchar*) log_file_name,
1244
1892
                     strlen(log_file_name)) ||
1245
 
          my_b_write(&index_file, (unsigned char*) "\n", 1) ||
 
1893
          my_b_write(&index_file, (uchar*) "\n", 1) ||
1246
1894
          flush_io_cache(&index_file) ||
1247
1895
          my_sync(index_file.file, MYF(MY_WME)))
1248
1896
        goto err;
1263
1911
    my_close(file,MYF(0));
1264
1912
  end_io_cache(&log_file);
1265
1913
  end_io_cache(&index_file);
1266
 
  if (name)
1267
 
  {
1268
 
    free(name);
1269
 
    name= NULL;
1270
 
  }
 
1914
  safeFree(name);
1271
1915
  log_state= LOG_CLOSED;
1272
1916
  return(1);
1273
1917
}
1305
1949
    0   ok
1306
1950
*/
1307
1951
 
 
1952
#ifdef HAVE_REPLICATION
 
1953
 
1308
1954
static bool copy_up_file_and_fill(IO_CACHE *index_file, my_off_t offset)
1309
1955
{
1310
1956
  int bytes_read;
1311
1957
  my_off_t init_offset= offset;
1312
1958
  File file= index_file->file;
1313
 
  unsigned char io_buf[IO_SIZE*2];
 
1959
  uchar io_buf[IO_SIZE*2];
1314
1960
 
1315
1961
  for (;; offset+= bytes_read)
1316
1962
  {
1336
1982
  return(1);
1337
1983
}
1338
1984
 
 
1985
#endif /* HAVE_REPLICATION */
 
1986
 
1339
1987
/**
1340
1988
  Find the position in the log-index-file for the given log name.
1341
1989
 
1363
2011
{
1364
2012
  int error= 0;
1365
2013
  char *fname= linfo->log_file_name;
1366
 
  uint32_t log_name_len= log_name ? (uint) strlen(log_name) : 0;
 
2014
  uint log_name_len= log_name ? (uint) strlen(log_name) : 0;
1367
2015
 
1368
2016
  /*
1369
2017
    Mutex needed because we need to make sure the file pointer does not
1378
2026
 
1379
2027
  for (;;)
1380
2028
  {
1381
 
    uint32_t length;
 
2029
    uint length;
1382
2030
    my_off_t offset= my_b_tell(&index_file);
1383
2031
    /* If we get 0 or 1 characters, this is the end of the file */
1384
2032
 
1434
2082
int DRIZZLE_BIN_LOG::find_next_log(LOG_INFO* linfo, bool need_lock)
1435
2083
{
1436
2084
  int error= 0;
1437
 
  uint32_t length;
 
2085
  uint length;
1438
2086
  char *fname= linfo->log_file_name;
1439
2087
 
1440
2088
  if (need_lock)
1467
2115
 
1468
2116
  The new index file will only contain this file.
1469
2117
 
1470
 
  @param session                Thread
 
2118
  @param thd            Thread
1471
2119
 
1472
2120
  @note
1473
2121
    If not called from slave thread, write start event to new log
1478
2126
    1   error
1479
2127
*/
1480
2128
 
1481
 
bool DRIZZLE_BIN_LOG::reset_logs(Session* session)
 
2129
bool DRIZZLE_BIN_LOG::reset_logs(THD* thd)
1482
2130
{
1483
2131
  LOG_INFO linfo;
1484
2132
  bool error=0;
1485
2133
  const char* save_name;
1486
2134
 
 
2135
  ha_reset_logs(thd);
1487
2136
  /*
1488
2137
    We need to get both locks to be sure that no one is trying to
1489
2138
    write to the index log file.
1493
2142
 
1494
2143
  /*
1495
2144
    The following mutex is needed to ensure that no threads call
1496
 
    'delete session' as we would then risk missing a 'rollback' from this
 
2145
    'delete thd' as we would then risk missing a 'rollback' from this
1497
2146
    thread. If the transaction involved MyISAM tables, it should go
1498
2147
    into binlog even on rollback.
1499
2148
  */
1500
 
  pthread_mutex_lock(&LOCK_thread_count);
 
2149
  VOID(pthread_mutex_lock(&LOCK_thread_count));
1501
2150
 
1502
2151
  /* Save variables so that we can reopen the log */
1503
2152
  save_name=name;
1506
2155
 
1507
2156
  /* First delete all old log files */
1508
2157
 
1509
 
  if (find_log_pos(&linfo, NULL, 0))
 
2158
  if (find_log_pos(&linfo, NullS, 0))
1510
2159
  {
1511
2160
    error=1;
1512
2161
    goto err;
1518
2167
    {
1519
2168
      if (my_errno == ENOENT) 
1520
2169
      {
1521
 
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2170
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1522
2171
                            ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1523
2172
                            linfo.log_file_name);
1524
2173
        sql_print_information(_("Failed to delete file '%s'"),
1528
2177
      }
1529
2178
      else
1530
2179
      {
1531
 
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2180
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
1532
2181
                            ER_BINLOG_PURGE_FATAL_ERR,
1533
2182
                            _("a problem with deleting %s; "
1534
2183
                            "consider examining correspondence "
1549
2198
  {
1550
2199
    if (my_errno == ENOENT) 
1551
2200
    {
1552
 
      push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2201
      push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1553
2202
                          ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1554
2203
                          index_file_name);
1555
2204
      sql_print_information(_("Failed to delete file '%s'"),
1559
2208
    }
1560
2209
    else
1561
2210
    {
1562
 
      push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2211
      push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
1563
2212
                          ER_BINLOG_PURGE_FATAL_ERR,
1564
2213
                          "a problem with deleting %s; "
1565
2214
                          "consider examining correspondence "
1570
2219
      goto err;
1571
2220
    }
1572
2221
  }
1573
 
  if (!session->slave_thread)
 
2222
  if (!thd->slave_thread)
1574
2223
    need_start_event=1;
1575
2224
  if (!open_index_file(index_file_name, 0))
1576
2225
    open(save_name, log_type, 0, io_cache_type, no_auto_events, max_size, 0);
1577
 
  free((unsigned char*) save_name);
 
2226
  my_free((uchar*) save_name, MYF(0));
1578
2227
 
1579
2228
err:
1580
 
  pthread_mutex_unlock(&LOCK_thread_count);
 
2229
  VOID(pthread_mutex_unlock(&LOCK_thread_count));
1581
2230
  pthread_mutex_unlock(&LOCK_index);
1582
2231
  pthread_mutex_unlock(&LOCK_log);
1583
2232
  return(error);
1621
2270
    LOG_INFO_IO         Got IO error while reading file
1622
2271
*/
1623
2272
 
 
2273
#ifdef HAVE_REPLICATION
1624
2274
 
1625
2275
int DRIZZLE_BIN_LOG::purge_first_log(Relay_log_info* rli, bool included)
1626
2276
{
1628
2278
 
1629
2279
  assert(is_open());
1630
2280
  assert(rli->slave_running == 1);
1631
 
  assert(!strcmp(rli->linfo.log_file_name,rli->event_relay_log_name.c_str()));
 
2281
  assert(!strcmp(rli->linfo.log_file_name,rli->event_relay_log_name));
1632
2282
 
1633
2283
  pthread_mutex_lock(&LOCK_index);
1634
2284
  pthread_mutex_lock(&rli->log_space_lock);
1635
 
  rli->relay_log.purge_logs(rli->group_relay_log_name.c_str(), included,
 
2285
  rli->relay_log.purge_logs(rli->group_relay_log_name, included,
1636
2286
                            0, 0, &rli->log_space_total);
1637
2287
  // Tell the I/O thread to take the relay_log_space_limit into account
1638
2288
  rli->ignore_log_space_limit= 0;
1651
2301
    If included is true, we want the first relay log;
1652
2302
    otherwise we want the one after event_relay_log_name.
1653
2303
  */
1654
 
  if ((included && (error=find_log_pos(&rli->linfo, NULL, 0))) ||
 
2304
  if ((included && (error=find_log_pos(&rli->linfo, NullS, 0))) ||
1655
2305
      (!included &&
1656
 
       ((error=find_log_pos(&rli->linfo, rli->event_relay_log_name.c_str(), 0)) ||
 
2306
       ((error=find_log_pos(&rli->linfo, rli->event_relay_log_name, 0)) ||
1657
2307
        (error=find_next_log(&rli->linfo, 0)))))
1658
2308
  {
1659
2309
    char buff[22];
1660
2310
    sql_print_error(_("next log error: %d  offset: %s  log: %s included: %d"),
1661
2311
                    error,
1662
2312
                    llstr(rli->linfo.index_file_offset,buff),
1663
 
                    rli->group_relay_log_name.c_str(),
 
2313
                    rli->group_relay_log_name,
1664
2314
                    included);
1665
2315
    goto err;
1666
2316
  }
1669
2319
    Reset rli's coordinates to the current log.
1670
2320
  */
1671
2321
  rli->event_relay_log_pos= BIN_LOG_HEADER_SIZE;
1672
 
  rli->event_relay_log_name.assign(rli->linfo.log_file_name);
 
2322
  strmake(rli->event_relay_log_name,rli->linfo.log_file_name,
 
2323
          sizeof(rli->event_relay_log_name)-1);
1673
2324
 
1674
2325
  /*
1675
2326
    If we removed the rli->group_relay_log_name file,
1679
2330
  if (included)
1680
2331
  {
1681
2332
    rli->group_relay_log_pos = BIN_LOG_HEADER_SIZE;
1682
 
    rli->group_relay_log_name.assign(rli->linfo.log_file_name);
 
2333
    strmake(rli->group_relay_log_name,rli->linfo.log_file_name,
 
2334
            sizeof(rli->group_relay_log_name)-1);
1683
2335
    rli->notify_group_relay_log_name_update();
1684
2336
  }
1685
2337
 
1750
2402
    File name exists in index file; delete until we find this file
1751
2403
    or a file that is used.
1752
2404
  */
1753
 
  if ((error=find_log_pos(&log_info, NULL, 0 /*no mutex*/)))
 
2405
  if ((error=find_log_pos(&log_info, NullS, 0 /*no mutex*/)))
1754
2406
    goto err;
1755
2407
  while ((strcmp(to_log,log_info.log_file_name) || (exit_loop=included)) &&
1756
2408
         !log_in_use(log_info.log_file_name))
1764
2416
          It's not fatal if we can't stat a log file that does not exist;
1765
2417
          If we could not stat, we won't delete.
1766
2418
        */     
1767
 
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2419
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1768
2420
                            ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1769
2421
                            log_info.log_file_name);
1770
2422
        sql_print_information(_("Failed to execute stat() on file '%s'"),
1776
2428
        /*
1777
2429
          Other than ENOENT are fatal
1778
2430
        */
1779
 
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2431
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
1780
2432
                            ER_BINLOG_PURGE_FATAL_ERR,
1781
2433
                            _("a problem with getting info on being purged %s; "
1782
2434
                            "consider examining correspondence "
1798
2450
      {
1799
2451
        if (my_errno == ENOENT) 
1800
2452
        {
1801
 
          push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2453
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1802
2454
                              ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1803
2455
                              log_info.log_file_name);
1804
2456
          sql_print_information(_("Failed to delete file '%s'"),
1807
2459
        }
1808
2460
        else
1809
2461
        {
1810
 
          push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2462
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
1811
2463
                              ER_BINLOG_PURGE_FATAL_ERR,
1812
2464
                              _("a problem with deleting %s; "
1813
2465
                              "consider examining correspondence "
1824
2476
      }
1825
2477
    }
1826
2478
 
 
2479
    ha_binlog_index_purge_file(current_thd, log_info.log_file_name);
 
2480
 
1827
2481
    if (find_next_log(&log_info, 0) || exit_loop)
1828
2482
      break;
1829
2483
  }
1847
2501
  Remove all logs before the given file date from disk and from the
1848
2502
  index file.
1849
2503
 
1850
 
  @param session                Thread pointer
 
2504
  @param thd            Thread pointer
1851
2505
  @param before_date    Delete all log files before given date.
1852
2506
 
1853
2507
  @note
1875
2529
    or a file that is used or a file
1876
2530
    that is older than purge_time.
1877
2531
  */
1878
 
  if ((error=find_log_pos(&log_info, NULL, 0 /*no mutex*/)))
 
2532
  if ((error=find_log_pos(&log_info, NullS, 0 /*no mutex*/)))
1879
2533
    goto err;
1880
2534
 
1881
2535
  while (strcmp(log_file_name, log_info.log_file_name) &&
1888
2542
        /*
1889
2543
          It's not fatal if we can't stat a log file that does not exist.
1890
2544
        */     
1891
 
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2545
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1892
2546
                            ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1893
2547
                            log_info.log_file_name);
1894
2548
        sql_print_information(_("Failed to execute stat() on file '%s'"),
1900
2554
        /*
1901
2555
          Other than ENOENT are fatal
1902
2556
        */
1903
 
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2557
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
1904
2558
                            ER_BINLOG_PURGE_FATAL_ERR,
1905
2559
                            _("a problem with getting info on being purged %s; "
1906
2560
                            "consider examining correspondence "
1920
2574
        if (my_errno == ENOENT) 
1921
2575
        {
1922
2576
          /* It's not fatal even if we can't delete a log file */
1923
 
          push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2577
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1924
2578
                              ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1925
2579
                              log_info.log_file_name);
1926
2580
          sql_print_information(_("Failed to delete file '%s'"),
1929
2583
        }
1930
2584
        else
1931
2585
        {
1932
 
          push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2586
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
1933
2587
                              ER_BINLOG_PURGE_FATAL_ERR,
1934
2588
                              _("a problem with deleting %s; "
1935
2589
                              "consider examining correspondence "
1940
2594
          goto err;
1941
2595
        }
1942
2596
      }
 
2597
      ha_binlog_index_purge_file(current_thd, log_info.log_file_name);
1943
2598
    }
1944
2599
    if (find_next_log(&log_info, 0))
1945
2600
      break;
1955
2610
  pthread_mutex_unlock(&LOCK_index);
1956
2611
  return(error);
1957
2612
}
 
2613
#endif /* HAVE_REPLICATION */
1958
2614
 
1959
2615
 
1960
2616
/**
1968
2624
 
1969
2625
void DRIZZLE_BIN_LOG::make_log_name(char* buf, const char* log_ident)
1970
2626
{
1971
 
  uint32_t dir_len = dirname_length(log_file_name); 
 
2627
  uint dir_len = dirname_length(log_file_name); 
1972
2628
  if (dir_len >= FN_REFLEN)
1973
2629
    dir_len=FN_REFLEN-1;
1974
 
  my_stpncpy(buf, log_file_name, dir_len);
 
2630
  stpncpy(buf, log_file_name, dir_len);
1975
2631
  strmake(buf+dir_len, log_ident, FN_REFLEN - dir_len -1);
1976
2632
}
1977
2633
 
2101
2757
 
2102
2758
  open(old_name, log_type, new_name_ptr,
2103
2759
       io_cache_type, no_auto_events, max_size, 1);
2104
 
  free(old_name);
 
2760
  my_free(old_name,MYF(0));
2105
2761
 
2106
2762
end:
2107
2763
  if (need_lock)
2138
2794
}
2139
2795
 
2140
2796
 
2141
 
bool DRIZZLE_BIN_LOG::appendv(const char* buf, uint32_t len,...)
 
2797
bool DRIZZLE_BIN_LOG::appendv(const char* buf, uint len,...)
2142
2798
{
2143
2799
  bool error= 0;
2144
2800
  va_list(args);
2149
2805
  safe_mutex_assert_owner(&LOCK_log);
2150
2806
  do
2151
2807
  {
2152
 
    if (my_b_append(&log_file,(unsigned char*) buf,len))
 
2808
    if (my_b_append(&log_file,(uchar*) buf,len))
2153
2809
    {
2154
2810
      error= 1;
2155
2811
      goto err;
2180
2836
  return err;
2181
2837
}
2182
2838
 
2183
 
void DRIZZLE_BIN_LOG::start_union_events(Session *session, query_id_t query_id_param)
2184
 
{
2185
 
  assert(!session->binlog_evt_union.do_union);
2186
 
  session->binlog_evt_union.do_union= true;
2187
 
  session->binlog_evt_union.unioned_events= false;
2188
 
  session->binlog_evt_union.unioned_events_trans= false;
2189
 
  session->binlog_evt_union.first_query_id= query_id_param;
2190
 
}
2191
 
 
2192
 
void DRIZZLE_BIN_LOG::stop_union_events(Session *session)
2193
 
{
2194
 
  assert(session->binlog_evt_union.do_union);
2195
 
  session->binlog_evt_union.do_union= false;
2196
 
}
2197
 
 
2198
 
bool DRIZZLE_BIN_LOG::is_query_in_union(Session *session, query_id_t query_id_param)
2199
 
{
2200
 
  return (session->binlog_evt_union.do_union && 
2201
 
          query_id_param >= session->binlog_evt_union.first_query_id);
 
2839
void DRIZZLE_BIN_LOG::start_union_events(THD *thd, query_id_t query_id_param)
 
2840
{
 
2841
  assert(!thd->binlog_evt_union.do_union);
 
2842
  thd->binlog_evt_union.do_union= true;
 
2843
  thd->binlog_evt_union.unioned_events= false;
 
2844
  thd->binlog_evt_union.unioned_events_trans= false;
 
2845
  thd->binlog_evt_union.first_query_id= query_id_param;
 
2846
}
 
2847
 
 
2848
void DRIZZLE_BIN_LOG::stop_union_events(THD *thd)
 
2849
{
 
2850
  assert(thd->binlog_evt_union.do_union);
 
2851
  thd->binlog_evt_union.do_union= false;
 
2852
}
 
2853
 
 
2854
bool DRIZZLE_BIN_LOG::is_query_in_union(THD *thd, query_id_t query_id_param)
 
2855
{
 
2856
  return (thd->binlog_evt_union.do_union && 
 
2857
          query_id_param >= thd->binlog_evt_union.first_query_id);
2202
2858
}
2203
2859
 
2204
2860
 
2207
2863
  binlog_hton, which has internal linkage.
2208
2864
*/
2209
2865
 
2210
 
int Session::binlog_setup_trx_data()
 
2866
int THD::binlog_setup_trx_data()
2211
2867
{
2212
2868
  binlog_trx_data *trx_data=
2213
 
    (binlog_trx_data*) session_get_ha_data(this, binlog_hton);
 
2869
    (binlog_trx_data*) thd_get_ha_data(this, binlog_hton);
2214
2870
 
2215
2871
  if (trx_data)
2216
2872
    return(0);                             // Already set up
2220
2876
      open_cached_file(&trx_data->trans_log, mysql_tmpdir,
2221
2877
                       LOG_PREFIX, binlog_cache_size, MYF(MY_WME)))
2222
2878
  {
2223
 
    free((unsigned char*)trx_data);
 
2879
    my_free((uchar*)trx_data, MYF(MY_ALLOW_ZERO_PTR));
2224
2880
    return(1);                      // Didn't manage to set it up
2225
2881
  }
2226
 
  session_set_ha_data(this, binlog_hton, trx_data);
 
2882
  thd_set_ha_data(this, binlog_hton, trx_data);
2227
2883
 
2228
 
  trx_data= new (session_get_ha_data(this, binlog_hton)) binlog_trx_data;
 
2884
  trx_data= new (thd_get_ha_data(this, binlog_hton)) binlog_trx_data;
2229
2885
 
2230
2886
  return(0);
2231
2887
}
2252
2908
      We only update the saved position if the old one was undefined,
2253
2909
      the reason is that there are some cases (e.g., for CREATE-SELECT)
2254
2910
      where the position is saved twice (e.g., both in
2255
 
      select_create::prepare() and Session::binlog_write_table_map()) , but
 
2911
      select_create::prepare() and THD::binlog_write_table_map()) , but
2256
2912
      we should use the first. This means that calls to this function
2257
2913
      can be used to start the statement before the first table map
2258
2914
      event, to include some extra events.
2259
2915
 */
2260
2916
 
2261
2917
void
2262
 
Session::binlog_start_trans_and_stmt()
 
2918
THD::binlog_start_trans_and_stmt()
2263
2919
{
2264
 
  binlog_trx_data *trx_data= (binlog_trx_data*) session_get_ha_data(this, binlog_hton);
 
2920
  binlog_trx_data *trx_data= (binlog_trx_data*) thd_get_ha_data(this, binlog_hton);
2265
2921
 
2266
2922
  if (trx_data == NULL ||
2267
2923
      trx_data->before_stmt_pos == MY_OFF_T_UNDEF)
2284
2940
  return;
2285
2941
}
2286
2942
 
2287
 
void Session::binlog_set_stmt_begin() {
 
2943
void THD::binlog_set_stmt_begin() {
2288
2944
  binlog_trx_data *trx_data=
2289
 
    (binlog_trx_data*) session_get_ha_data(this, binlog_hton);
 
2945
    (binlog_trx_data*) thd_get_ha_data(this, binlog_hton);
2290
2946
 
2291
2947
  /*
2292
2948
    The call to binlog_trans_log_savepos() might create the trx_data
2296
2952
  */
2297
2953
  my_off_t pos= 0;
2298
2954
  binlog_trans_log_savepos(this, &pos);
2299
 
  trx_data= (binlog_trx_data*) session_get_ha_data(this, binlog_hton);
 
2955
  trx_data= (binlog_trx_data*) thd_get_ha_data(this, binlog_hton);
2300
2956
  trx_data->before_stmt_pos= pos;
2301
2957
}
2302
2958
 
2305
2961
  Write a table map to the binary log.
2306
2962
 */
2307
2963
 
2308
 
int Session::binlog_write_table_map(Table *table, bool is_trans)
 
2964
int THD::binlog_write_table_map(Table *table, bool is_trans)
2309
2965
{
2310
2966
  int error;
2311
2967
 
2331
2987
}
2332
2988
 
2333
2989
Rows_log_event*
2334
 
Session::binlog_get_pending_rows_event() const
 
2990
THD::binlog_get_pending_rows_event() const
2335
2991
{
2336
2992
  binlog_trx_data *const trx_data=
2337
 
    (binlog_trx_data*) session_get_ha_data(this, binlog_hton);
 
2993
    (binlog_trx_data*) thd_get_ha_data(this, binlog_hton);
2338
2994
  /*
2339
2995
    This is less than ideal, but here's the story: If there is no
2340
2996
    trx_data, prepare_pending_rows_event() has never been called
2345
3001
}
2346
3002
 
2347
3003
void
2348
 
Session::binlog_set_pending_rows_event(Rows_log_event* ev)
 
3004
THD::binlog_set_pending_rows_event(Rows_log_event* ev)
2349
3005
{
2350
 
  if (session_get_ha_data(this, binlog_hton) == NULL)
 
3006
  if (thd_get_ha_data(this, binlog_hton) == NULL)
2351
3007
    binlog_setup_trx_data();
2352
3008
 
2353
3009
  binlog_trx_data *const trx_data=
2354
 
    (binlog_trx_data*) session_get_ha_data(this, binlog_hton);
 
3010
    (binlog_trx_data*) thd_get_ha_data(this, binlog_hton);
2355
3011
 
2356
3012
  assert(trx_data);
2357
3013
  trx_data->set_pending(ev);
2364
3020
  event.
2365
3021
*/
2366
3022
int
2367
 
DRIZZLE_BIN_LOG::flush_and_set_pending_rows_event(Session *session,
 
3023
DRIZZLE_BIN_LOG::flush_and_set_pending_rows_event(THD *thd,
2368
3024
                                                Rows_log_event* event)
2369
3025
{
2370
3026
  assert(mysql_bin_log.is_open());
2372
3028
  int error= 0;
2373
3029
 
2374
3030
  binlog_trx_data *const trx_data=
2375
 
    (binlog_trx_data*) session_get_ha_data(session, binlog_hton);
 
3031
    (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
2376
3032
 
2377
3033
  assert(trx_data);
2378
3034
 
2437
3093
    pthread_mutex_unlock(&LOCK_log);
2438
3094
  }
2439
3095
 
2440
 
  session->binlog_set_pending_rows_event(event);
 
3096
  thd->binlog_set_pending_rows_event(event);
2441
3097
 
2442
3098
  return(error);
2443
3099
}
2448
3104
 
2449
3105
bool DRIZZLE_BIN_LOG::write(Log_event *event_info)
2450
3106
{
2451
 
  Session *session= event_info->session;
 
3107
  THD *thd= event_info->thd;
2452
3108
  bool error= 1;
2453
3109
 
2454
 
  if (session->binlog_evt_union.do_union)
 
3110
  if (thd->binlog_evt_union.do_union)
2455
3111
  {
2456
3112
    /*
2457
3113
      In Stored function; Remember that function call caused an update.
2458
3114
      We will log the function call to the binary log on function exit
2459
3115
    */
2460
 
    session->binlog_evt_union.unioned_events= true;
2461
 
    session->binlog_evt_union.unioned_events_trans |= event_info->cache_stmt;
 
3116
    thd->binlog_evt_union.unioned_events= true;
 
3117
    thd->binlog_evt_union.unioned_events_trans |= event_info->cache_stmt;
2462
3118
    return(0);
2463
3119
  }
2464
3120
 
2473
3129
    this will close all tables on the slave.
2474
3130
  */
2475
3131
  bool const end_stmt= false;
2476
 
  session->binlog_flush_pending_rows_event(end_stmt);
 
3132
  thd->binlog_flush_pending_rows_event(end_stmt);
2477
3133
 
2478
3134
  pthread_mutex_lock(&LOCK_log);
2479
3135
 
2491
3147
      binlog_[wild_]{do|ignore}_table?" (WL#1049)"
2492
3148
    */
2493
3149
    const char *local_db= event_info->get_db();
2494
 
    if ((session && !(session->options & OPTION_BIN_LOG)) ||
 
3150
    if ((thd && !(thd->options & OPTION_BIN_LOG)) ||
2495
3151
        (!binlog_filter->db_ok(local_db)))
2496
3152
    {
2497
 
      pthread_mutex_unlock(&LOCK_log);
 
3153
      VOID(pthread_mutex_unlock(&LOCK_log));
2498
3154
      return(0);
2499
3155
    }
2500
3156
 
2507
3163
     trans/non-trans table types the best possible in binlogging)
2508
3164
      - or if the event asks for it (cache_stmt == TRUE).
2509
3165
    */
2510
 
    if (opt_using_transactions && session)
 
3166
    if (opt_using_transactions && thd)
2511
3167
    {
2512
 
      if (session->binlog_setup_trx_data())
 
3168
      if (thd->binlog_setup_trx_data())
2513
3169
        goto err;
2514
3170
 
2515
3171
      binlog_trx_data *const trx_data=
2516
 
        (binlog_trx_data*) session_get_ha_data(session, binlog_hton);
 
3172
        (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
2517
3173
      IO_CACHE *trans_log= &trx_data->trans_log;
2518
3174
      my_off_t trans_log_pos= my_b_tell(trans_log);
2519
3175
      if (event_info->get_cache_stmt() || trans_log_pos != 0)
2520
3176
      {
2521
3177
        if (trans_log_pos == 0)
2522
 
          session->binlog_start_trans_and_stmt();
 
3178
          thd->binlog_start_trans_and_stmt();
2523
3179
        file= trans_log;
2524
3180
      }
2525
3181
      /*
2544
3200
      If row-based binlogging, Insert_id, Rand and other kind of "setting
2545
3201
      context" events are not needed.
2546
3202
    */
2547
 
    if (session)
 
3203
    if (thd)
2548
3204
    {
2549
 
      if (!session->current_stmt_binlog_row_based)
 
3205
      if (!thd->current_stmt_binlog_row_based)
2550
3206
      {
2551
 
        if (session->stmt_depends_on_first_successful_insert_id_in_prev_stmt)
 
3207
        if (thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt)
2552
3208
        {
2553
 
          Intvar_log_event e(session,(unsigned char) LAST_INSERT_ID_EVENT,
2554
 
                             session->first_successful_insert_id_in_prev_stmt_for_binlog);
 
3209
          Intvar_log_event e(thd,(uchar) LAST_INSERT_ID_EVENT,
 
3210
                             thd->first_successful_insert_id_in_prev_stmt_for_binlog);
2555
3211
          if (e.write(file))
2556
3212
            goto err;
2557
3213
        }
2558
 
        if (session->auto_inc_intervals_in_cur_stmt_for_binlog.nb_elements() > 0)
 
3214
        if (thd->auto_inc_intervals_in_cur_stmt_for_binlog.nb_elements() > 0)
2559
3215
        {
2560
3216
          /*
2561
3217
            If the auto_increment was second in a table's index (possible with
2562
3218
            MyISAM or BDB) (table->next_number_keypart != 0), such event is
2563
3219
            in fact not necessary. We could avoid logging it.
2564
3220
          */
2565
 
          Intvar_log_event e(session, (unsigned char) INSERT_ID_EVENT,
2566
 
                             session->auto_inc_intervals_in_cur_stmt_for_binlog.
 
3221
          Intvar_log_event e(thd, (uchar) INSERT_ID_EVENT,
 
3222
                             thd->auto_inc_intervals_in_cur_stmt_for_binlog.
2567
3223
                             minimum());
2568
3224
          if (e.write(file))
2569
3225
            goto err;
2570
3226
        }
2571
 
        if (session->rand_used)
 
3227
        if (thd->rand_used)
2572
3228
        {
2573
 
          Rand_log_event e(session,session->rand_saved_seed1,session->rand_saved_seed2);
 
3229
          Rand_log_event e(thd,thd->rand_saved_seed1,thd->rand_saved_seed2);
2574
3230
          if (e.write(file))
2575
3231
            goto err;
2576
3232
        }
2577
 
        if (session->user_var_events.elements)
 
3233
        if (thd->user_var_events.elements)
2578
3234
        {
2579
 
          for (uint32_t i= 0; i < session->user_var_events.elements; i++)
 
3235
          for (uint i= 0; i < thd->user_var_events.elements; i++)
2580
3236
          {
2581
3237
            BINLOG_USER_VAR_EVENT *user_var_event;
2582
 
            get_dynamic(&session->user_var_events,(unsigned char*) &user_var_event, i);
2583
 
            User_var_log_event e(session, user_var_event->user_var_event->name.str,
 
3238
            get_dynamic(&thd->user_var_events,(uchar*) &user_var_event, i);
 
3239
            User_var_log_event e(thd, user_var_event->user_var_event->name.str,
2584
3240
                                 user_var_event->user_var_event->name.length,
2585
3241
                                 user_var_event->value,
2586
3242
                                 user_var_event->length,
2634
3290
  return logger.error_log_print(level, format, args);
2635
3291
}
2636
3292
 
2637
 
void DRIZZLE_BIN_LOG::rotate_and_purge(uint32_t flags)
 
3293
 
 
3294
bool slow_log_print(THD *thd, const char *query, uint query_length,
 
3295
                    uint64_t current_utime)
 
3296
{
 
3297
  return logger.slow_log_print(thd, query, query_length, current_utime);
 
3298
}
 
3299
 
 
3300
 
 
3301
bool LOGGER::log_command(THD *thd, enum enum_server_command command)
 
3302
{
 
3303
  /*
 
3304
    Log command if we have at least one log event handler enabled and want
 
3305
    to log this king of commands
 
3306
  */
 
3307
  if (*general_log_handler_list && (what_to_log & (1L << (uint) command)))
 
3308
  {
 
3309
    if (thd->options & OPTION_LOG_OFF)
 
3310
    {
 
3311
      /* No logging */
 
3312
      return false;
 
3313
    }
 
3314
 
 
3315
    return true;
 
3316
  }
 
3317
 
 
3318
  return false;
 
3319
}
 
3320
 
 
3321
 
 
3322
bool general_log_print(THD *thd, enum enum_server_command command,
 
3323
                       const char *format, ...)
 
3324
{
 
3325
  va_list args;
 
3326
  uint error= 0;
 
3327
 
 
3328
  /* Print the message to the buffer if we want to log this king of commands */
 
3329
  if (! logger.log_command(thd, command))
 
3330
    return false;
 
3331
 
 
3332
  va_start(args, format);
 
3333
  error= logger.general_log_print(thd, command, format, args);
 
3334
  va_end(args);
 
3335
 
 
3336
  return error;
 
3337
}
 
3338
 
 
3339
bool general_log_write(THD *thd, enum enum_server_command command,
 
3340
                       const char *query, uint query_length)
 
3341
{
 
3342
  /* Write the message to the log if we want to log this king of commands */
 
3343
  if (logger.log_command(thd, command))
 
3344
    return logger.general_log_write(thd, command, query, query_length);
 
3345
 
 
3346
  return false;
 
3347
}
 
3348
 
 
3349
void DRIZZLE_BIN_LOG::rotate_and_purge(uint flags)
2638
3350
{
2639
3351
  if (!(flags & RP_LOCK_LOG_IS_ALREADY_LOCKED))
2640
3352
    pthread_mutex_lock(&LOCK_log);
2642
3354
      (my_b_tell(&log_file) >= (my_off_t) max_size))
2643
3355
  {
2644
3356
    new_file_without_locking();
 
3357
#ifdef HAVE_REPLICATION
2645
3358
    if (expire_logs_days)
2646
3359
    {
2647
3360
      time_t purge_time= my_time(0) - expire_logs_days*24*60*60;
2648
3361
      if (purge_time >= 0)
2649
3362
        purge_logs_before_date(purge_time);
2650
3363
    }
 
3364
#endif
2651
3365
  }
2652
3366
  if (!(flags & RP_LOCK_LOG_IS_ALREADY_LOCKED))
2653
3367
    pthread_mutex_unlock(&LOCK_log);
2654
3368
}
2655
3369
 
2656
 
uint32_t DRIZZLE_BIN_LOG::next_file_id()
 
3370
uint DRIZZLE_BIN_LOG::next_file_id()
2657
3371
{
2658
 
  uint32_t res;
 
3372
  uint res;
2659
3373
  pthread_mutex_lock(&LOCK_log);
2660
3374
  res = file_id++;
2661
3375
  pthread_mutex_unlock(&LOCK_log);
2683
3397
 
2684
3398
  if (reinit_io_cache(cache, READ_CACHE, 0, 0, 0))
2685
3399
    return ER_ERROR_ON_WRITE;
2686
 
  uint32_t length= my_b_bytes_in_cache(cache), group, carry, hdr_offs;
 
3400
  uint length= my_b_bytes_in_cache(cache), group, carry, hdr_offs;
2687
3401
  long val;
2688
 
  unsigned char header[LOG_EVENT_HEADER_LEN];
 
3402
  uchar header[LOG_EVENT_HEADER_LEN];
2689
3403
 
2690
3404
  /*
2691
3405
    The events in the buffer have incorrect end_log_pos data
2766
3480
        {
2767
3481
          /* we've got a full event-header, and it came in one piece */
2768
3482
 
2769
 
          unsigned char *log_pos= (unsigned char *)cache->read_pos + hdr_offs + LOG_POS_OFFSET;
 
3483
          uchar *log_pos= (uchar *)cache->read_pos + hdr_offs + LOG_POS_OFFSET;
2770
3484
 
2771
3485
          /* fix end_log_pos */
2772
3486
          val= uint4korr(log_pos) + group;
2773
3487
          int4store(log_pos, val);
2774
3488
 
2775
3489
          /* next event header at ... */
2776
 
          log_pos= (unsigned char *)cache->read_pos + hdr_offs + EVENT_LEN_OFFSET;
 
3490
          log_pos= (uchar *)cache->read_pos + hdr_offs + EVENT_LEN_OFFSET;
2777
3491
          hdr_offs += uint4korr(log_pos);
2778
3492
 
2779
3493
        }
2812
3526
  was updated in a transaction which was rolled back. This is to ensure
2813
3527
  that the same updates are run on the slave.
2814
3528
 
2815
 
  @param session
 
3529
  @param thd
2816
3530
  @param cache          The cache to copy to the binlog
2817
3531
  @param commit_event   The commit event to print after writing the
2818
3532
                        contents of the cache.
2825
3539
    'cache' needs to be reinitialized after this functions returns.
2826
3540
*/
2827
3541
 
2828
 
bool DRIZZLE_BIN_LOG::write(Session *session, IO_CACHE *cache, Log_event *commit_event)
 
3542
bool DRIZZLE_BIN_LOG::write(THD *thd, IO_CACHE *cache, Log_event *commit_event)
2829
3543
{
2830
 
  pthread_mutex_lock(&LOCK_log);
 
3544
  VOID(pthread_mutex_lock(&LOCK_log));
2831
3545
 
2832
3546
  /* NULL would represent nothing to replicate after ROLLBACK */
2833
3547
  assert(commit_event != NULL);
2846
3560
        transaction is either a BEGIN..COMMIT block or a single
2847
3561
        statement in autocommit mode.
2848
3562
      */
2849
 
      Query_log_event qinfo(session, STRING_WITH_LEN("BEGIN"), true, false);
 
3563
      Query_log_event qinfo(thd, STRING_WITH_LEN("BEGIN"), true, false);
2850
3564
      /*
2851
3565
        Imagine this is rollback due to net timeout, after all
2852
3566
        statements of the transaction succeeded. Then we want a
2856
3570
        generated event, and as this event is generated late it would
2857
3571
        lead to false alarms.
2858
3572
 
2859
 
        This is safer than session->clear_error() against kills at shutdown.
 
3573
        This is safer than thd->clear_error() against kills at shutdown.
2860
3574
      */
2861
3575
      qinfo.error_code= 0;
2862
3576
      /*
2903
3617
    else
2904
3618
      rotate_and_purge(RP_LOCK_LOG_IS_ALREADY_LOCKED);
2905
3619
  }
2906
 
  pthread_mutex_unlock(&LOCK_log);
 
3620
  VOID(pthread_mutex_unlock(&LOCK_log));
2907
3621
 
2908
3622
  return(0);
2909
3623
 
2913
3627
    write_error= 1;
2914
3628
    sql_print_error(ER(ER_ERROR_ON_WRITE), name, errno);
2915
3629
  }
2916
 
  pthread_mutex_unlock(&LOCK_log);
 
3630
  VOID(pthread_mutex_unlock(&LOCK_log));
2917
3631
  return(1);
2918
3632
}
2919
3633
 
2921
3635
/**
2922
3636
  Wait until we get a signal that the relay log has been updated
2923
3637
 
2924
 
  @param[in] session   a Session struct
 
3638
  @param[in] thd   a THD struct
2925
3639
  @note
2926
3640
    LOCK_log must be taken before calling this function.
2927
3641
    It will be released at the end of the function.
2928
3642
*/
2929
3643
 
2930
 
void DRIZZLE_BIN_LOG::wait_for_update_relay_log(Session* session)
 
3644
void DRIZZLE_BIN_LOG::wait_for_update_relay_log(THD* thd)
2931
3645
{
2932
3646
  const char *old_msg;
2933
 
  old_msg= session->enter_cond(&update_cond, &LOCK_log,
 
3647
  old_msg= thd->enter_cond(&update_cond, &LOCK_log,
2934
3648
                           "Slave has read all relay log; " 
2935
3649
                           "waiting for the slave I/O "
2936
3650
                           "thread to update it" );
2937
3651
  pthread_cond_wait(&update_cond, &LOCK_log);
2938
 
  session->exit_cond(old_msg);
 
3652
  thd->exit_cond(old_msg);
2939
3653
  return;
2940
3654
}
2941
3655
 
2945
3659
  Applies to master only.
2946
3660
     
2947
3661
  NOTES
2948
 
  @param[in] session        a Session struct
 
3662
  @param[in] thd        a THD struct
2949
3663
  @param[in] timeout    a pointer to a timespec;
2950
3664
                        NULL means to wait w/o timeout.
2951
3665
  @retval    0          if got signalled on update
2956
3670
    LOCK_log is released by the caller.
2957
3671
*/
2958
3672
 
2959
 
int DRIZZLE_BIN_LOG::wait_for_update_bin_log(Session* session,
 
3673
int DRIZZLE_BIN_LOG::wait_for_update_bin_log(THD* thd,
2960
3674
                                           const struct timespec *timeout)
2961
3675
{
2962
3676
  int ret= 0;
2963
 
  const char* old_msg = session->get_proc_info();
2964
 
  old_msg= session->enter_cond(&update_cond, &LOCK_log,
 
3677
  const char* old_msg = thd->get_proc_info();
 
3678
  old_msg= thd->enter_cond(&update_cond, &LOCK_log,
2965
3679
                           "Master has sent all binlog to slave; "
2966
3680
                           "waiting for binlog to be updated");
2967
3681
  if (!timeout)
2987
3701
    The internal structures are not freed until cleanup() is called
2988
3702
*/
2989
3703
 
2990
 
void DRIZZLE_BIN_LOG::close(uint32_t exiting)
 
3704
void DRIZZLE_BIN_LOG::close(uint exiting)
2991
3705
{                                       // One can't set log_type here!
2992
3706
  if (log_state == LOG_OPENED)
2993
3707
  {
 
3708
#ifdef HAVE_REPLICATION
2994
3709
    if (log_type == LOG_BIN && !no_auto_events &&
2995
3710
        (exiting & LOG_CLOSE_STOP_EVENT))
2996
3711
    {
2999
3714
      bytes_written+= s.data_written;
3000
3715
      signal_update();
3001
3716
    }
 
3717
#endif /* HAVE_REPLICATION */
3002
3718
 
3003
3719
    /* don't pwrite in a file opened with O_APPEND - it doesn't work */
3004
3720
    if (log_file.type == WRITE_CACHE && log_type == LOG_BIN)
3005
3721
    {
3006
3722
      my_off_t offset= BIN_LOG_HEADER_SIZE + FLAGS_OFFSET;
3007
 
      unsigned char flags= 0;            // clearing LOG_EVENT_BINLOG_IN_USE_F
3008
 
      assert(pwrite(log_file.file, &flags, 1, offset)==1);
 
3723
      uchar flags= 0;            // clearing LOG_EVENT_BINLOG_IN_USE_F
 
3724
      pwrite(log_file.file, &flags, 1, offset);
3009
3725
    }
3010
3726
 
3011
3727
    /* this will cleanup IO_CACHE, sync and close the file */
3027
3743
    }
3028
3744
  }
3029
3745
  log_state= (exiting & LOG_CLOSE_TO_BE_OPENED) ? LOG_TO_BE_OPENED : LOG_CLOSED;
3030
 
  if (name)
3031
 
  {
3032
 
    free(name);
3033
 
    name= NULL;
3034
 
  }
 
3746
  safeFree(name);
3035
3747
  return;
3036
3748
}
3037
3749
 
3104
3816
 
3105
3817
void sql_perror(const char *message)
3106
3818
{
 
3819
#ifdef HAVE_STRERROR
3107
3820
  sql_print_error("%s: %s",message, strerror(errno));
 
3821
#else
 
3822
  perror(message);
 
3823
#endif
3108
3824
}
3109
3825
 
3110
3826
 
3115
3831
  {
3116
3832
    char err_renamed[FN_REFLEN], *end;
3117
3833
    end= strmake(err_renamed,log_error_file,FN_REFLEN-4);
3118
 
    my_stpcpy(end, "-old");
3119
 
    pthread_mutex_lock(&LOCK_error_log);
 
3834
    stpcpy(end, "-old");
 
3835
    VOID(pthread_mutex_lock(&LOCK_error_log));
3120
3836
    char err_temp[FN_REFLEN+4];
3121
3837
    /*
3122
3838
     On Windows is necessary a temporary file for to rename
3123
3839
     the current error file.
3124
3840
    */
3125
 
    strxmov(err_temp, err_renamed,"-tmp",NULL);
 
3841
    strxmov(err_temp, err_renamed,"-tmp",NullS);
3126
3842
    (void) my_delete(err_temp, MYF(0)); 
3127
3843
    if (freopen(err_temp,"a+",stdout))
3128
3844
    {
3129
3845
      int fd;
3130
3846
      size_t bytes;
3131
 
      unsigned char buf[IO_SIZE];
 
3847
      uchar buf[IO_SIZE];
3132
3848
 
3133
 
      if(freopen(err_temp,"a+",stderr)==NULL)
3134
 
        return 1;
 
3849
      freopen(err_temp,"a+",stderr);
3135
3850
      (void) my_delete(err_renamed, MYF(0));
3136
3851
      my_rename(log_error_file,err_renamed,MYF(0));
3137
 
      if (freopen(log_error_file,"a+",stdout)==NULL)
3138
 
        return 1;
3139
 
      else
3140
 
        if(freopen(log_error_file,"a+",stderr)==NULL)
3141
 
          return 1;
 
3852
      if (freopen(log_error_file,"a+",stdout))
 
3853
        freopen(log_error_file,"a+",stderr);
3142
3854
 
3143
3855
      if ((fd = my_open(err_temp, O_RDONLY, MYF(0))) >= 0)
3144
3856
      {
3151
3863
    }
3152
3864
    else
3153
3865
     result= 1;
3154
 
    pthread_mutex_unlock(&LOCK_error_log);
 
3866
    VOID(pthread_mutex_unlock(&LOCK_error_log));
3155
3867
  }
3156
3868
   return result;
3157
3869
}
3178
3890
    signature to be compatible with other logging routines, which could
3179
3891
    return an error (e.g. logging to the log tables)
3180
3892
*/
3181
 
static void print_buffer_to_file(enum loglevel level, int,
3182
 
                                 const char *buffer, size_t)
 
3893
static void print_buffer_to_file(enum loglevel level,
 
3894
                                 int error_code __attribute__((unused)),
 
3895
                                 const char *buffer,
 
3896
                                 size_t buffer_length __attribute__((unused)))
3183
3897
{
3184
3898
  time_t skr;
3185
3899
  struct tm tm_tmp;
3186
3900
  struct tm *start;
3187
3901
 
3188
 
  pthread_mutex_lock(&LOCK_error_log);
 
3902
  VOID(pthread_mutex_lock(&LOCK_error_log));
3189
3903
 
3190
3904
  skr= my_time(0);
3191
3905
  localtime_r(&skr, &tm_tmp);
3204
3918
 
3205
3919
  fflush(stderr);
3206
3920
 
3207
 
  pthread_mutex_unlock(&LOCK_error_log);
 
3921
  VOID(pthread_mutex_unlock(&LOCK_error_log));
3208
3922
  return;
3209
3923
}
3210
3924
 
3310
4024
 
3311
4025
int TC_LOG_MMAP::open(const char *opt_name)
3312
4026
{
3313
 
  uint32_t i;
 
4027
  uint i;
3314
4028
  bool crashed= false;
3315
4029
  PAGE *pg;
3316
4030
 
3350
4064
      goto err;
3351
4065
  }
3352
4066
 
3353
 
  data= (unsigned char *)my_mmap(0, (size_t)file_length, PROT_READ|PROT_WRITE,
 
4067
  data= (uchar *)my_mmap(0, (size_t)file_length, PROT_READ|PROT_WRITE,
3354
4068
                        MAP_NOSYNC|MAP_SHARED, fd, 0);
3355
4069
  if (data == MAP_FAILED)
3356
4070
  {
3386
4100
      goto err;
3387
4101
 
3388
4102
  memcpy(data, tc_log_magic, sizeof(tc_log_magic));
3389
 
  data[sizeof(tc_log_magic)]= (unsigned char)total_ha_2pc;
3390
 
  // must cast data to (char *) for solaris. Arg1 is (void *) on linux
3391
 
  //   so the cast should be fine. 
3392
 
  msync((char *)data, tc_log_page_size, MS_SYNC);
 
4103
  data[sizeof(tc_log_magic)]= (uchar)total_ha_2pc;
 
4104
  msync(data, tc_log_page_size, MS_SYNC);
3393
4105
  my_sync(fd, MYF(0));
3394
4106
  inited=5;
3395
4107
 
3510
4222
    to the position in memory where xid was logged to.
3511
4223
*/
3512
4224
 
3513
 
int TC_LOG_MMAP::log_xid(Session *, my_xid xid)
 
4225
int TC_LOG_MMAP::log_xid(THD *thd __attribute__((unused)), my_xid xid)
3514
4226
{
3515
4227
  int err;
3516
4228
  PAGE *p;
3543
4255
  }
3544
4256
 
3545
4257
  /* found! store xid there and mark the page dirty */
3546
 
  cookie= (ulong)((unsigned char *)p->ptr - data);      // can never be zero
 
4258
  cookie= (ulong)((uchar *)p->ptr - data);      // can never be zero
3547
4259
  *p->ptr++= xid;
3548
4260
  p->free--;
3549
4261
  p->state= DIRTY;
3595
4307
    sit down and relax - this can take a while...
3596
4308
    note - no locks are held at this point
3597
4309
  */
3598
 
  // must cast data to (char *) for solaris. Arg1 is (void *) on linux
3599
 
  //   so the cast should be fine. 
3600
 
  err= msync((char *)syncing->start, 1, MS_SYNC);
 
4310
  err= msync(syncing->start, 1, MS_SYNC);
3601
4311
  if(err==0)
3602
4312
    err= my_sync(fd, MYF(0));
3603
4313
 
3624
4334
  cookie points directly to the memory where xid was logged.
3625
4335
*/
3626
4336
 
3627
 
void TC_LOG_MMAP::unlog(ulong cookie, my_xid xid)
 
4337
void TC_LOG_MMAP::unlog(ulong cookie, my_xid xid __attribute__((unused)))
3628
4338
{
3629
4339
  PAGE *p=pages+(cookie/tc_log_page_size);
3630
4340
  my_xid *x=(my_xid *)(data+cookie);
3646
4356
 
3647
4357
void TC_LOG_MMAP::close()
3648
4358
{
3649
 
  uint32_t i;
 
4359
  uint i;
3650
4360
  switch (inited) {
3651
4361
  case 6:
3652
4362
    pthread_mutex_destroy(&LOCK_sync);
3664
4374
      pthread_cond_destroy(&pages[i].cond);
3665
4375
    }
3666
4376
  case 3:
3667
 
    free((unsigned char*)pages);
 
4377
    my_free((uchar*)pages, MYF(0));
3668
4378
  case 2:
3669
4379
    my_munmap((char*)data, (size_t)file_length);
3670
4380
  case 1:
3706
4416
  for ( ; p < end_p ; p++)
3707
4417
  {
3708
4418
    for (my_xid *x=p->start; x < p->end; x++)
3709
 
      if (*x && my_hash_insert(&xids, (unsigned char *)x))
 
4419
      if (*x && my_hash_insert(&xids, (uchar *)x))
3710
4420
        goto err2; // OOM
3711
4421
  }
3712
4422
 
3794
4504
    return 1;
3795
4505
  }
3796
4506
 
3797
 
  if ((error= find_log_pos(&log_info, NULL, 1)))
 
4507
  if ((error= find_log_pos(&log_info, NullS, 1)))
3798
4508
  {
3799
4509
    if (error != LOG_INFO_EOF)
3800
4510
      sql_print_error(_("find_log_pos() failed (error: %d)"), error);
3870
4580
  @retval
3871
4581
    1    success
3872
4582
*/
3873
 
int TC_LOG_BINLOG::log_xid(Session *session, my_xid xid)
 
4583
int TC_LOG_BINLOG::log_xid(THD *thd, my_xid xid)
3874
4584
{
3875
 
  Xid_log_event xle(session, xid);
 
4585
  Xid_log_event xle(thd, xid);
3876
4586
  binlog_trx_data *trx_data=
3877
 
    (binlog_trx_data*) session_get_ha_data(session, binlog_hton);
 
4587
    (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
3878
4588
  /*
3879
4589
    We always commit the entire transaction when writing an XID. Also
3880
4590
    note that the return value is inverted.
3881
4591
   */
3882
 
  return(!binlog_end_trans(session, trx_data, &xle, true));
 
4592
  return(!binlog_end_trans(thd, trx_data, &xle, true));
3883
4593
}
3884
4594
 
3885
 
void TC_LOG_BINLOG::unlog(ulong, my_xid)
 
4595
void TC_LOG_BINLOG::unlog(ulong cookie __attribute__((unused)),
 
4596
                          my_xid xid __attribute__((unused)))
3886
4597
{
3887
4598
  pthread_mutex_lock(&LOCK_prep_xids);
3888
4599
  assert(prepared_xids > 0);
3913
4624
    if (ev->get_type_code() == XID_EVENT)
3914
4625
    {
3915
4626
      Xid_log_event *xev=(Xid_log_event *)ev;
3916
 
      unsigned char *x= (unsigned char *) memdup_root(&mem_root, (unsigned char*) &xev->xid,
 
4627
      uchar *x= (uchar *) memdup_root(&mem_root, (uchar*) &xev->xid,
3917
4628
                                      sizeof(xev->xid));
3918
4629
      if (! x)
3919
4630
        goto err2;