~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/drizzled.cc

  • Committer: Patrick Crews
  • Date: 2010-07-19 22:50:20 UTC
  • mto: (1663.1.2 rollup)
  • mto: This revision was merged to the branch mainline in revision 1664.
  • Revision ID: gleebix@gmail.com-20100719225020-6lhrjnkq3k9lyq0f
Fix of optimizer.test - needed cleanup at the end + updated .result file

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
#include <signal.h>
29
29
#include <limits.h>
30
30
 
 
31
#include <boost/program_options.hpp>
 
32
 
31
33
#include "drizzled/internal/my_sys.h"
32
34
#include "drizzled/internal/my_bit.h"
33
35
#include <drizzled/my_hash.h>
34
 
#include <drizzled/stacktrace.h>
35
36
#include <drizzled/error.h>
36
37
#include <drizzled/errmsg_print.h>
37
38
#include <drizzled/tztime.h>
49
50
#include "drizzled/plugin/scheduler.h"
50
51
#include "drizzled/plugin/xa_resource_manager.h"
51
52
#include "drizzled/plugin/monitored_in_transaction.h"
 
53
#include "drizzled/replication_services.h" /* For ReplicationServices::evaluateRegisteredPlugins() */
52
54
#include "drizzled/probes.h"
53
55
#include "drizzled/session_list.h"
54
56
#include "drizzled/charset.h"
55
57
#include "plugin/myisam/myisam.h"
 
58
#include "drizzled/drizzled.h"
 
59
#include "drizzled/module/registry.h"
56
60
 
57
61
#include <google/protobuf/stubs/common.h>
58
62
 
72
76
#endif
73
77
#include <sys/socket.h>
74
78
 
75
 
#include <locale.h>
76
 
 
77
79
 
78
80
#include <errno.h>
79
81
#include <sys/stat.h>
84
86
#include <pwd.h>                                // For getpwent
85
87
#include <grp.h>
86
88
 
87
 
#include <sys/resource.h>
88
 
 
89
89
#ifdef HAVE_SELECT_H
90
90
#  include <select.h>
91
91
#endif
127
127
#define MAX_MEM_TABLE_SIZE SIZE_MAX
128
128
 
129
129
using namespace std;
 
130
namespace po=boost::program_options;
 
131
 
130
132
 
131
133
namespace drizzled
132
134
{
190
192
/*
191
193
  Used with --help for detailed option
192
194
*/
193
 
static bool opt_help= false;
194
 
static bool opt_help_extended= false;
 
195
bool opt_help= false;
 
196
bool opt_help_extended= false;
195
197
 
196
198
arg_cmp_func Arg_comparator::comparator_matrix[5][2] =
197
199
{{&Arg_comparator::compare_string,     &Arg_comparator::compare_e_string},
202
204
 
203
205
/* static variables */
204
206
 
205
 
static bool volatile select_thread_in_use;
206
 
static bool volatile ready_to_exit;
207
207
static bool opt_debugging= 0;
208
208
static uint32_t wake_thread;
209
 
static uint32_t killed_threads;
210
 
static char *drizzled_user, *drizzled_chroot;
 
209
static char *drizzled_chroot;
211
210
static char *language_ptr;
212
211
static const char *default_character_set_name;
213
212
static const char *character_set_filesystem_name;
218
217
 
219
218
/* Global variables */
220
219
 
 
220
bool volatile ready_to_exit;
 
221
char *drizzled_user;
 
222
bool volatile select_thread_in_use;
221
223
bool volatile abort_loop;
222
224
bool volatile shutdown_in_progress;
223
 
uint32_t max_used_connections;
224
 
const string opt_scheduler_default("multi_thread");
 
225
char *opt_scheduler_default;
225
226
char *opt_scheduler= NULL;
226
227
 
227
228
size_t my_thread_stack_size= 65536;
234
235
 
235
236
char* opt_secure_file_priv= 0;
236
237
 
237
 
static bool calling_initgroups= false; /**< Used in SIGSEGV handler. */
 
238
bool calling_initgroups= false; /**< Used in SIGSEGV handler. */
238
239
 
239
240
uint32_t drizzled_bind_timeout;
240
241
std::bitset<12> test_flags;
245
246
uint32_t server_id;
246
247
uint64_t table_cache_size;
247
248
size_t table_def_size;
248
 
uint64_t aborted_threads;
249
 
uint64_t aborted_connects;
250
249
uint64_t max_connect_errors;
251
250
uint32_t global_thread_id= 1UL;
252
251
pid_t current_pid;
293
292
char drizzle_home[FN_REFLEN], pidfile_name[FN_REFLEN], system_time_zone[30];
294
293
char *default_tz_name;
295
294
char glob_hostname[FN_REFLEN];
296
 
char drizzle_real_data_home[FN_REFLEN],
 
295
char data_home_real[FN_REFLEN],
297
296
     language[FN_REFLEN], 
298
297
     *opt_tc_log_file;
299
 
char drizzle_unpacked_real_data_home[FN_REFLEN];
 
298
char data_home_real_unpacked[FN_REFLEN];
300
299
const key_map key_map_empty(0);
301
300
key_map key_map_full(0);                        // Will be initialized later
302
301
 
303
 
uint32_t drizzle_data_home_len;
304
 
char drizzle_data_home_buff[2], *drizzle_data_home=drizzle_real_data_home;
305
 
char *drizzle_tmpdir= NULL;
 
302
uint32_t data_home_len;
 
303
char data_home_buff[2], *data_home=data_home_real;
 
304
std::string drizzle_tmpdir;
306
305
char *opt_drizzle_tmpdir= NULL;
307
306
 
308
307
/** name of reference on left espression in rewritten IN subquery */
318
317
 
319
318
struct system_variables global_system_variables;
320
319
struct system_variables max_system_variables;
321
 
struct system_status_var global_status_var;
 
320
struct global_counters current_global_counters;
322
321
 
323
322
const CHARSET_INFO *system_charset_info, *files_charset_info ;
324
323
const CHARSET_INFO *table_alias_charset;
346
345
 
347
346
/* Static variables */
348
347
 
349
 
static bool segfaulted;
350
348
int cleanup_done;
351
349
static char *drizzle_home_ptr, *pidfile_name_ptr;
352
350
static int defaults_argc;
353
351
static char **defaults_argv;
354
352
 
355
 
struct passwd *user_info;
356
 
static pthread_t select_thread;
357
 
static uint32_t thr_kill_signal;
 
353
passwd *user_info;
358
354
 
359
 
/**
360
 
  Number of currently active user connections. The variable is protected by
361
 
  LOCK_thread_count.
362
 
*/
363
355
atomic<uint32_t> connection_count;
364
356
 
365
357
/** 
370
362
/* Function declarations */
371
363
bool drizzle_rm_tmp_tables();
372
364
 
373
 
extern "C" pthread_handler_t signal_hand(void *arg);
374
365
static void drizzle_init_variables(void);
375
366
static void get_options(int *argc,char **argv);
376
367
int drizzled_get_one_option(int, const struct option *, char *);
377
 
static int init_thread_environment();
378
368
static const char *get_relative_path(const char *path);
379
369
static void fix_paths(string &progname);
380
 
extern "C" pthread_handler_t handle_slave(void *arg);
381
 
static void clean_up(bool print_message);
382
370
 
383
371
static void usage(void);
384
 
static void clean_up_mutexes(void);
385
372
void close_connections(void);
 
373
 
 
374
po::options_description long_options("Allowed Options");
 
375
po::variables_map vm;
 
376
 
 
377
po::variables_map &getVariablesMap()
 
378
{
 
379
  return vm;
 
380
}
386
381
 
387
382
/****************************************************************************
388
383
** Code to end drizzled
466
461
  }
467
462
}
468
463
 
469
 
extern "C" void print_signal_warning(int sig);
470
 
 
471
 
extern "C" void print_signal_warning(int sig)
472
 
{
473
 
  if (global_system_variables.log_warnings)
474
 
    errmsg_printf(ERRMSG_LVL_WARN, _("Got signal %d from thread %"PRIu64),
475
 
                  sig, global_thread_id);
476
 
#ifndef HAVE_BSD_SIGNALS
477
 
  my_sigset(sig,print_signal_warning);          /* int. thread system calls */
478
 
#endif
479
 
  if (sig == SIGALRM)
480
 
    alarm(2);                                   /* reschedule alarm */
481
 
}
482
 
 
483
464
/**
484
465
  cleanup all memory and end program nicely.
485
466
 
516
497
}
517
498
 
518
499
 
519
 
static void clean_up(bool print_message)
 
500
void clean_up(bool print_message)
520
501
{
521
502
  if (cleanup_done++)
522
503
    return;
525
506
  TableShare::cacheStop();
526
507
  set_var_free();
527
508
  free_charsets();
528
 
  plugin::Registry &plugins= plugin::Registry::singleton();
529
 
  plugin_shutdown(plugins);
 
509
  module::Registry &modules= module::Registry::singleton();
 
510
  modules.shutdownModules();
530
511
  xid_cache_free();
531
 
  free_status_vars();
532
512
  if (defaults_argv)
533
513
    internal::free_defaults(defaults_argv);
534
 
  free(drizzle_tmpdir);
535
514
  if (opt_secure_file_priv)
536
515
    free(opt_secure_file_priv);
537
516
 
558
537
} /* clean_up */
559
538
 
560
539
 
561
 
static void clean_up_mutexes()
 
540
void clean_up_mutexes()
562
541
{
563
542
  (void) pthread_mutex_destroy(&LOCK_create_db);
564
543
  (void) pthread_mutex_destroy(&LOCK_open);
576
555
 
577
556
/* Change to run as another user if started with --user */
578
557
 
579
 
static struct passwd *check_user(const char *user)
 
558
passwd *check_user(const char *user)
580
559
{
581
 
  struct passwd *tmp_user_info;
 
560
  passwd *tmp_user_info;
582
561
  uid_t user_id= geteuid();
583
562
 
584
563
  // Don't bother if we aren't superuser
636
615
 
637
616
}
638
617
 
639
 
static void set_user(const char *user, struct passwd *user_info_arg)
 
618
void set_user(const char *user, passwd *user_info_arg)
640
619
{
641
620
  assert(user_info_arg != 0);
642
621
  /*
661
640
}
662
641
 
663
642
 
 
643
 
664
644
/** Change root user if started with @c --chroot . */
665
645
static void set_root(const char *path)
666
646
{
671
651
  }
672
652
}
673
653
 
674
 
extern "C" void end_thread_signal(int );
675
 
 
676
 
/** Called when a thread is aborted. */
677
 
extern "C" void end_thread_signal(int )
678
 
{
679
 
  Session *session=current_session;
680
 
  if (session)
681
 
  {
682
 
    statistic_increment(killed_threads, &LOCK_status);
683
 
    session->scheduler->killSessionNow(session);
684
 
    DRIZZLE_CONNECTION_DONE(session->thread_id);
685
 
  }
686
 
  return;
687
 
}
688
 
 
689
654
 
690
655
/*
691
656
  Unlink session from global list of available connections and free session
718
683
}
719
684
 
720
685
 
721
 
#ifdef THREAD_SPECIFIC_SIGPIPE
722
 
/**
723
 
 
724
 
  @todo
725
 
    One should have to fix that thr_alarm know about this thread too.
726
 
*/
727
 
extern "C" void abort_thread(int )
728
 
{
729
 
  Session *session=current_session;
730
 
  if (session)
731
 
    session->killed= Session::KILL_CONNECTION;
732
 
  return;;
733
 
}
734
 
#endif
735
 
 
736
 
#if defined(BACKTRACE_DEMANGLE)
737
 
#include <cxxabi.h>
738
 
extern "C" char *my_demangle(const char *mangled_name, int *status)
739
 
{
740
 
  return abi::__cxa_demangle(mangled_name, NULL, NULL, status);
741
 
}
742
 
#endif
743
 
 
744
 
extern "C" void handle_segfault(int sig);
745
 
 
746
 
extern "C" void handle_segfault(int sig)
747
 
{
748
 
  time_t curr_time;
749
 
  struct tm tm;
750
 
 
751
 
  /*
752
 
    Strictly speaking, one needs a mutex here
753
 
    but since we have got SIGSEGV already, things are a mess
754
 
    so not having the mutex is not as bad as possibly using a buggy
755
 
    mutex - so we keep things simple
756
 
  */
757
 
  if (segfaulted)
758
 
  {
759
 
    fprintf(stderr, _("Fatal signal %d while backtracing\n"), sig);
760
 
    exit(1);
761
 
  }
762
 
 
763
 
  segfaulted = 1;
764
 
 
765
 
  curr_time= time(NULL);
766
 
  if(curr_time == (time_t)-1)
767
 
  {
768
 
    fprintf(stderr, "Fetal: time() call failed\n");
769
 
    exit(1);
770
 
  }
771
 
 
772
 
  localtime_r(&curr_time, &tm);
773
 
  
774
 
  fprintf(stderr,"%02d%02d%02d %2d:%02d:%02d - drizzled got signal %d;\n"
775
 
          "This could be because you hit a bug. It is also possible that "
776
 
          "this binary\n or one of the libraries it was linked against is "
777
 
          "corrupt, improperly built,\n or misconfigured. This error can "
778
 
          "also be caused by malfunctioning hardware.\n",
779
 
          tm.tm_year % 100, tm.tm_mon+1, tm.tm_mday,
780
 
          tm.tm_hour, tm.tm_min, tm.tm_sec,
781
 
          sig);
782
 
  fprintf(stderr, _("We will try our best to scrape up some info that "
783
 
                    "will hopefully help diagnose\n"
784
 
                    "the problem, but since we have already crashed, "
785
 
                    "something is definitely wrong\nand this may fail.\n\n"));
786
 
  fprintf(stderr, "key_buffer_size=%u\n",
787
 
          (uint32_t) dflt_key_cache->key_cache_mem_size);
788
 
  fprintf(stderr, "read_buffer_size=%ld\n", (long) global_system_variables.read_buff_size);
789
 
  fprintf(stderr, "max_used_connections=%u\n", max_used_connections);
790
 
  fprintf(stderr, "connection_count=%u\n", uint32_t(connection_count));
791
 
  fprintf(stderr, _("It is possible that drizzled could use up to \n"
792
 
                    "key_buffer_size + (read_buffer_size + "
793
 
                    "sort_buffer_size)*thread_count\n"
794
 
                    "bytes of memory\n"
795
 
                    "Hope that's ok; if not, decrease some variables in the "
796
 
                    "equation.\n\n"));
797
 
 
798
 
#ifdef HAVE_STACKTRACE
799
 
  Session *session= current_session;
800
 
 
801
 
  if (! (test_flags.test(TEST_NO_STACKTRACE)))
802
 
  {
803
 
    fprintf(stderr,"session: 0x%lx\n",(long) session);
804
 
    fprintf(stderr,_("Attempting backtrace. You can use the following "
805
 
                     "information to find out\n"
806
 
                     "where drizzled died. If you see no messages after this, "
807
 
                     "something went\n"
808
 
                     "terribly wrong...\n"));
809
 
    print_stacktrace(session ? (unsigned char*) session->thread_stack : (unsigned char*) 0,
810
 
                     my_thread_stack_size);
811
 
  }
812
 
  if (session)
813
 
  {
814
 
    const char *kreason= "UNKNOWN";
815
 
    switch (session->killed) {
816
 
    case Session::NOT_KILLED:
817
 
      kreason= "NOT_KILLED";
818
 
      break;
819
 
    case Session::KILL_BAD_DATA:
820
 
      kreason= "KILL_BAD_DATA";
821
 
      break;
822
 
    case Session::KILL_CONNECTION:
823
 
      kreason= "KILL_CONNECTION";
824
 
      break;
825
 
    case Session::KILL_QUERY:
826
 
      kreason= "KILL_QUERY";
827
 
      break;
828
 
    case Session::KILLED_NO_VALUE:
829
 
      kreason= "KILLED_NO_VALUE";
830
 
      break;
831
 
    }
832
 
    fprintf(stderr, _("Trying to get some variables.\n"
833
 
                      "Some pointers may be invalid and cause the "
834
 
                      "dump to abort...\n"));
835
 
    safe_print_str("session->query", session->query.c_str(), 1024);
836
 
    fprintf(stderr, "session->thread_id=%"PRIu32"\n", (uint32_t) session->thread_id);
837
 
    fprintf(stderr, "session->killed=%s\n", kreason);
838
 
  }
839
 
  fflush(stderr);
840
 
#endif /* HAVE_STACKTRACE */
841
 
 
842
 
  if (calling_initgroups)
843
 
    fprintf(stderr, _("\nThis crash occurred while the server was calling "
844
 
                      "initgroups(). This is\n"
845
 
                      "often due to the use of a drizzled that is statically "
846
 
                      "linked against glibc\n"
847
 
                      "and configured to use LDAP in /etc/nsswitch.conf. "
848
 
                      "You will need to either\n"
849
 
                      "upgrade to a version of glibc that does not have this "
850
 
                      "problem (2.3.4 or\n"
851
 
                      "later when used with nscd), disable LDAP in your "
852
 
                      "nsswitch.conf, or use a\n"
853
 
                      "drizzled that is not statically linked.\n"));
854
 
 
855
 
  if (internal::thd_lib_detected == THD_LIB_LT && !getenv("LD_ASSUME_KERNEL"))
856
 
    fprintf(stderr,
857
 
            _("\nYou are running a statically-linked LinuxThreads binary "
858
 
              "on an NPTL system.\n"
859
 
              "This can result in crashes on some distributions due "
860
 
              "to LT/NPTL conflicts.\n"
861
 
              "You should either build a dynamically-linked binary, or force "
862
 
              "LinuxThreads\n"
863
 
              "to be used with the LD_ASSUME_KERNEL environment variable. "
864
 
              "Please consult\n"
865
 
              "the documentation for your distribution on how to do that.\n"));
866
 
 
867
 
#ifdef HAVE_WRITE_CORE
868
 
  if (test_flags.test(TEST_CORE_ON_SIGNAL))
869
 
  {
870
 
    fprintf(stderr, _("Writing a core file\n"));
871
 
    fflush(stderr);
872
 
    write_core(sig);
873
 
  }
874
 
#endif
875
 
 
876
 
  exit(1);
877
 
}
878
 
 
879
686
#ifndef SA_RESETHAND
880
687
#define SA_RESETHAND 0
881
688
#endif
884
691
#endif
885
692
 
886
693
 
887
 
/**
888
 
  All global error messages are sent here where the first one is stored
889
 
  for the client.
890
 
*/
891
 
static void my_message_sql(uint32_t error, const char *str, myf MyFlags)
892
 
{
893
 
  Session *session;
894
 
  /*
895
 
    Put here following assertion when situation with EE_* error codes
896
 
    will be fixed
897
 
  */
898
 
  if ((session= current_session))
899
 
  {
900
 
    if (MyFlags & ME_FATALERROR)
901
 
      session->is_fatal_error= 1;
902
 
 
903
 
    /*
904
 
      TODO: There are two exceptions mechanism (Session and sp_rcontext),
905
 
      this could be improved by having a common stack of handlers.
906
 
    */
907
 
    if (session->handle_error(error, str,
908
 
                          DRIZZLE_ERROR::WARN_LEVEL_ERROR))
909
 
      return;;
910
 
 
911
 
    /*
912
 
      session->lex->current_select == 0 if lex structure is not inited
913
 
      (not query command (COM_QUERY))
914
 
    */
915
 
    if (! (session->lex->current_select &&
916
 
        session->lex->current_select->no_error && !session->is_fatal_error))
917
 
    {
918
 
      if (! session->main_da.is_error())            // Return only first message
919
 
      {
920
 
        if (error == 0)
921
 
          error= ER_UNKNOWN_ERROR;
922
 
        if (str == NULL)
923
 
          str= ER(error);
924
 
        session->main_da.set_error_status(error, str);
925
 
      }
926
 
    }
927
 
 
928
 
    if (!session->no_warnings_for_error && !session->is_fatal_error)
929
 
    {
930
 
      /*
931
 
        Suppress infinite recursion if there a memory allocation error
932
 
        inside push_warning.
933
 
      */
934
 
      session->no_warnings_for_error= true;
935
 
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, error, str);
936
 
      session->no_warnings_for_error= false;
937
 
    }
938
 
  }
939
 
  if (!session || MyFlags & ME_NOREFRESH)
940
 
    errmsg_printf(ERRMSG_LVL_ERROR, "%s: %s",internal::my_progname,str);
941
 
}
942
 
 
943
 
 
944
 
static const char *load_default_groups[]= {
945
 
DRIZZLE_CONFIG_NAME, "server", 0, 0};
946
 
 
947
 
static int show_starttime(drizzle_show_var *var, char *buff)
948
 
{
949
 
  var->type= SHOW_LONG;
950
 
  var->value= buff;
951
 
  *((long *)buff)= (long) (time(NULL) - server_start_time);
952
 
  return 0;
953
 
}
954
 
 
955
 
static int show_flushstatustime(drizzle_show_var *var, char *buff)
956
 
{
957
 
  var->type= SHOW_LONG;
958
 
  var->value= buff;
959
 
  *((long *)buff)= (long) (time(NULL) - flush_status_time);
960
 
  return 0;
961
 
}
962
 
 
963
 
static st_show_var_func_container show_starttime_cont= { &show_starttime };
964
 
 
965
 
static st_show_var_func_container show_flushstatustime_cont= { &show_flushstatustime };
966
 
 
967
 
/*
968
 
  Variables shown by SHOW STATUS in alphabetical order
969
 
*/
970
 
static drizzle_show_var com_status_vars[]= {
971
 
  {"admin_commands",       (char*) offsetof(system_status_var, com_other), SHOW_LONG_STATUS},
972
 
  {"alter_db",             (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_ALTER_DB]), SHOW_LONG_STATUS},
973
 
  {"alter_table",          (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_ALTER_TABLE]), SHOW_LONG_STATUS},
974
 
  {"analyze",              (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_ANALYZE]), SHOW_LONG_STATUS},
975
 
  {"begin",                (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_BEGIN]), SHOW_LONG_STATUS},
976
 
  {"change_db",            (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_CHANGE_DB]), SHOW_LONG_STATUS},
977
 
  {"check",                (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_CHECK]), SHOW_LONG_STATUS},
978
 
  {"checksum",             (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_CHECKSUM]), SHOW_LONG_STATUS},
979
 
  {"commit",               (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_COMMIT]), SHOW_LONG_STATUS},
980
 
  {"create_db",            (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_CREATE_DB]), SHOW_LONG_STATUS},
981
 
  {"create_index",         (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_CREATE_INDEX]), SHOW_LONG_STATUS},
982
 
  {"create_table",         (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_CREATE_TABLE]), SHOW_LONG_STATUS},
983
 
  {"delete",               (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_DELETE]), SHOW_LONG_STATUS},
984
 
  {"drop_db",              (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_DROP_DB]), SHOW_LONG_STATUS},
985
 
  {"drop_index",           (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_DROP_INDEX]), SHOW_LONG_STATUS},
986
 
  {"drop_table",           (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_DROP_TABLE]), SHOW_LONG_STATUS},
987
 
  {"empty_query",          (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_EMPTY_QUERY]), SHOW_LONG_STATUS},
988
 
  {"flush",                (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_FLUSH]), SHOW_LONG_STATUS},
989
 
  {"insert",               (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_INSERT]), SHOW_LONG_STATUS},
990
 
  {"insert_select",        (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_INSERT_SELECT]), SHOW_LONG_STATUS},
991
 
  {"kill",                 (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_KILL]), SHOW_LONG_STATUS},
992
 
  {"load",                 (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_LOAD]), SHOW_LONG_STATUS},
993
 
  {"release_savepoint",    (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_RELEASE_SAVEPOINT]), SHOW_LONG_STATUS},
994
 
  {"rename_table",         (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_RENAME_TABLE]), SHOW_LONG_STATUS},
995
 
  {"replace",              (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_REPLACE]), SHOW_LONG_STATUS},
996
 
  {"replace_select",       (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_REPLACE_SELECT]), SHOW_LONG_STATUS},
997
 
  {"rollback",             (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_ROLLBACK]), SHOW_LONG_STATUS},
998
 
  {"rollback_to_savepoint",(char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_ROLLBACK_TO_SAVEPOINT]), SHOW_LONG_STATUS},
999
 
  {"savepoint",            (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_SAVEPOINT]), SHOW_LONG_STATUS},
1000
 
  {"select",               (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_SELECT]), SHOW_LONG_STATUS},
1001
 
  {"set_option",           (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_SET_OPTION]), SHOW_LONG_STATUS},
1002
 
  {"show_create_db",       (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_SHOW_CREATE_DB]), SHOW_LONG_STATUS},
1003
 
  {"show_create_table",    (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_SHOW_CREATE]), SHOW_LONG_STATUS},
1004
 
  {"show_errors",          (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_SHOW_ERRORS]), SHOW_LONG_STATUS},
1005
 
  {"show_warnings",        (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_SHOW_WARNS]), SHOW_LONG_STATUS},
1006
 
  {"truncate",             (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_TRUNCATE]), SHOW_LONG_STATUS},
1007
 
  {"unlock_tables",        (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_UNLOCK_TABLES]), SHOW_LONG_STATUS},
1008
 
  {"update",               (char*) offsetof(system_status_var, com_stat[(uint32_t) SQLCOM_UPDATE]), SHOW_LONG_STATUS},
1009
 
  {NULL, NULL, SHOW_LONGLONG}
1010
 
};
1011
 
 
1012
 
static drizzle_show_var status_vars[]= {
1013
 
  {"Aborted_clients",          (char*) &aborted_threads,        SHOW_LONGLONG},
1014
 
  {"Aborted_connects",         (char*) &aborted_connects,       SHOW_LONGLONG},
1015
 
  {"Bytes_received",           (char*) offsetof(system_status_var, bytes_received), SHOW_LONGLONG_STATUS},
1016
 
  {"Bytes_sent",               (char*) offsetof(system_status_var, bytes_sent), SHOW_LONGLONG_STATUS},
1017
 
  {"Connections",              (char*) &global_thread_id, SHOW_INT_NOFLUSH},
1018
 
  {"Created_tmp_disk_tables",  (char*) offsetof(system_status_var, created_tmp_disk_tables), SHOW_LONG_STATUS},
1019
 
  {"Created_tmp_tables",       (char*) offsetof(system_status_var, created_tmp_tables), SHOW_LONG_STATUS},
1020
 
  {"Flush_commands",           (char*) &refresh_version,    SHOW_INT_NOFLUSH},
1021
 
  {"Handler_commit",           (char*) offsetof(system_status_var, ha_commit_count), SHOW_LONG_STATUS},
1022
 
  {"Handler_delete",           (char*) offsetof(system_status_var, ha_delete_count), SHOW_LONG_STATUS},
1023
 
  {"Handler_prepare",          (char*) offsetof(system_status_var, ha_prepare_count),  SHOW_LONG_STATUS},
1024
 
  {"Handler_read_first",       (char*) offsetof(system_status_var, ha_read_first_count), SHOW_LONG_STATUS},
1025
 
  {"Handler_read_key",         (char*) offsetof(system_status_var, ha_read_key_count), SHOW_LONG_STATUS},
1026
 
  {"Handler_read_next",        (char*) offsetof(system_status_var, ha_read_next_count), SHOW_LONG_STATUS},
1027
 
  {"Handler_read_prev",        (char*) offsetof(system_status_var, ha_read_prev_count), SHOW_LONG_STATUS},
1028
 
  {"Handler_read_rnd",         (char*) offsetof(system_status_var, ha_read_rnd_count), SHOW_LONG_STATUS},
1029
 
  {"Handler_read_rnd_next",    (char*) offsetof(system_status_var, ha_read_rnd_next_count), SHOW_LONG_STATUS},
1030
 
  {"Handler_rollback",         (char*) offsetof(system_status_var, ha_rollback_count), SHOW_LONG_STATUS},
1031
 
  {"Handler_savepoint",        (char*) offsetof(system_status_var, ha_savepoint_count), SHOW_LONG_STATUS},
1032
 
  {"Handler_savepoint_rollback",(char*) offsetof(system_status_var, ha_savepoint_rollback_count), SHOW_LONG_STATUS},
1033
 
  {"Handler_update",           (char*) offsetof(system_status_var, ha_update_count), SHOW_LONG_STATUS},
1034
 
  {"Handler_write",            (char*) offsetof(system_status_var, ha_write_count), SHOW_LONG_STATUS},
1035
 
  {"Key_blocks_not_flushed",   (char*) offsetof(KEY_CACHE, global_blocks_changed), SHOW_KEY_CACHE_LONG},
1036
 
  {"Key_blocks_unused",        (char*) offsetof(KEY_CACHE, blocks_unused), SHOW_KEY_CACHE_LONG},
1037
 
  {"Key_blocks_used",          (char*) offsetof(KEY_CACHE, blocks_used), SHOW_KEY_CACHE_LONG},
1038
 
  {"Key_read_requests",        (char*) offsetof(KEY_CACHE, global_cache_r_requests), SHOW_KEY_CACHE_LONGLONG},
1039
 
  {"Key_reads",                (char*) offsetof(KEY_CACHE, global_cache_read), SHOW_KEY_CACHE_LONGLONG},
1040
 
  {"Key_write_requests",       (char*) offsetof(KEY_CACHE, global_cache_w_requests), SHOW_KEY_CACHE_LONGLONG},
1041
 
  {"Key_writes",               (char*) offsetof(KEY_CACHE, global_cache_write), SHOW_KEY_CACHE_LONGLONG},
1042
 
  {"Last_query_cost",          (char*) offsetof(system_status_var, last_query_cost), SHOW_DOUBLE_STATUS},
1043
 
  {"Max_used_connections",     (char*) &max_used_connections,  SHOW_INT},
1044
 
  {"Questions",                (char*) offsetof(system_status_var, questions), SHOW_LONG_STATUS},
1045
 
  {"Select_full_join",         (char*) offsetof(system_status_var, select_full_join_count), SHOW_LONG_STATUS},
1046
 
  {"Select_full_range_join",   (char*) offsetof(system_status_var, select_full_range_join_count), SHOW_LONG_STATUS},
1047
 
  {"Select_range",             (char*) offsetof(system_status_var, select_range_count), SHOW_LONG_STATUS},
1048
 
  {"Select_range_check",       (char*) offsetof(system_status_var, select_range_check_count), SHOW_LONG_STATUS},
1049
 
  {"Select_scan",              (char*) offsetof(system_status_var, select_scan_count), SHOW_LONG_STATUS},
1050
 
  {"Slow_queries",             (char*) offsetof(system_status_var, long_query_count), SHOW_LONG_STATUS},
1051
 
  {"Sort_merge_passes",        (char*) offsetof(system_status_var, filesort_merge_passes), SHOW_LONG_STATUS},
1052
 
  {"Sort_range",               (char*) offsetof(system_status_var, filesort_range_count), SHOW_LONG_STATUS},
1053
 
  {"Sort_rows",                (char*) offsetof(system_status_var, filesort_rows), SHOW_LONG_STATUS},
1054
 
  {"Sort_scan",                (char*) offsetof(system_status_var, filesort_scan_count), SHOW_LONG_STATUS},
1055
 
  {"Table_locks_immediate",    (char*) &locks_immediate,        SHOW_INT},
1056
 
  {"Table_locks_waited",       (char*) &locks_waited,           SHOW_INT},
1057
 
  {"Threads_connected",        (char*) &connection_count,       SHOW_INT},
1058
 
  {"Uptime",                   (char*) &show_starttime_cont,         SHOW_FUNC},
1059
 
  {"Uptime_since_flush_status",(char*) &show_flushstatustime_cont,   SHOW_FUNC},
1060
 
  {NULL, NULL, SHOW_LONGLONG}
1061
 
};
1062
 
 
1063
 
static int init_common_variables(const char *conf_file_name, int argc,
1064
 
                                 char **argv, const char **groups)
 
694
 
 
695
 
 
696
const char *load_default_groups[]= 
 
697
{
 
698
  DRIZZLE_CONFIG_NAME, "server", 0, 0
 
699
};
 
700
 
 
701
int init_common_variables(const char *conf_file_name, int argc,
 
702
                          char **argv, const char **groups)
1065
703
{
1066
704
  time_t curr_time;
1067
705
  umask(((~internal::my_umask) & 0666));
1105
743
    strncpy(pidfile_name, glob_hostname, sizeof(pidfile_name)-5);
1106
744
  strcpy(internal::fn_ext(pidfile_name),".pid");                // Add proper extension
1107
745
 
1108
 
  /*
1109
 
    Add server status variables to the dynamic list of
1110
 
    status variables that is shown by SHOW STATUS.
1111
 
    Later, in plugin_init, new entries could be added to that list.
1112
 
  */
1113
 
  if (add_com_status_vars(com_status_vars))
1114
 
    return 1; // an error was already reported
1115
 
 
1116
 
  if (add_status_vars(status_vars))
1117
 
    return 1; // an error was already reported
1118
 
 
1119
746
  internal::load_defaults(conf_file_name, groups, &argc, &argv);
1120
747
  defaults_argv=argv;
1121
748
  defaults_argc=argc;
1178
805
}
1179
806
 
1180
807
 
1181
 
static int init_thread_environment()
 
808
int init_thread_environment()
1182
809
{
1183
810
   pthread_mutexattr_t attr; 
1184
811
   pthread_mutexattr_init(&attr);
1210
837
}
1211
838
 
1212
839
 
1213
 
static int init_server_components(plugin::Registry &plugins)
 
840
int init_server_components(module::Registry &plugins)
1214
841
{
1215
842
  /*
1216
843
    We need to call each of these following functions to ensure that
1235
862
  ha_init_errors();
1236
863
 
1237
864
  if (plugin_init(plugins, &defaults_argc, defaults_argv,
1238
 
                  ((opt_help) ? true : false)))
 
865
                  ((opt_help) ? true : false), long_options))
1239
866
  {
1240
867
    errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to initialize plugins."));
1241
868
    unireg_abort(1);
1242
869
  }
1243
870
 
 
871
 
1244
872
  if (opt_help || opt_help_extended)
1245
873
    unireg_abort(0);
1246
874
 
 
875
  po::parsed_options parsed= po::command_line_parser(defaults_argc,
 
876
                                                     defaults_argv).
 
877
    options(long_options).allow_unregistered().run();
 
878
 
 
879
  vector<string> unknown_options=
 
880
    po::collect_unrecognized(parsed.options, po::include_positional);
 
881
 
1247
882
  /* we do want to exit if there are any other unknown options */
1248
 
  if (defaults_argc > 1)
 
883
  /** @TODO: We should perhaps remove allowed_unregistered() and catch the
 
884
    exception here */
 
885
  if (unknown_options.size() > 0)
1249
886
  {
1250
 
    int ho_error;
1251
 
    char **tmp_argv= defaults_argv;
1252
 
    struct option no_opts[]=
1253
 
    {
1254
 
      {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
1255
 
    };
1256
 
    /*
1257
 
      We need to eat any 'loose' arguments first before we conclude
1258
 
      that there are unprocessed options.
1259
 
      But we need to preserve defaults_argv pointer intact for
1260
 
      internal::free_defaults() to work. Thus we use a copy here.
1261
 
    */
1262
 
    my_getopt_skip_unknown= 0;
1263
 
 
1264
 
    if ((ho_error= handle_options(&defaults_argc, &tmp_argv, no_opts,
1265
 
                                  drizzled_get_one_option)))
1266
 
      unireg_abort(ho_error);
1267
 
 
1268
 
    if (defaults_argc)
1269
 
    {
1270
 
      fprintf(stderr,
1271
 
              _("%s: Too many arguments (first extra is '%s').\n"
1272
 
                "Use --verbose --help to get a list of available options\n"),
1273
 
              internal::my_progname, *tmp_argv);
 
887
     fprintf(stderr,
 
888
            _("%s: Too many arguments (first extra is '%s').\n"
 
889
              "Use --verbose --help to get a list of available options\n"),
 
890
            internal::my_progname, unknown_options[0].c_str());
1274
891
      unireg_abort(1);
1275
 
    }
1276
892
  }
1277
893
 
 
894
  po::store(parsed, vm);
 
895
  po::notify(vm);
 
896
 
1278
897
  string scheduler_name;
1279
898
  if (opt_scheduler)
1280
899
  {
1283
902
  else
1284
903
  {
1285
904
    scheduler_name= opt_scheduler_default;
 
905
    opt_scheduler= opt_scheduler_default; 
1286
906
  }
1287
907
 
1288
908
  if (plugin::Scheduler::setPlugin(scheduler_name))
1445
1065
   NO_ARG, 0, 0, 0, 0, 0, 0},
1446
1066
  {"datadir", 'h',
1447
1067
   N_("Path to the database root."),
1448
 
   (char**) &drizzle_data_home,
1449
 
   (char**) &drizzle_data_home, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1068
   (char**) &data_home,
 
1069
   (char**) &data_home, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1450
1070
  {"default-storage-engine", OPT_STORAGE_ENGINE,
1451
1071
   N_("Set the default storage engine (table type) for tables."),
1452
1072
   (char**)&default_storage_engine_str, (char**)&default_storage_engine_str,
1765
1385
     puts("");
1766
1386
 
1767
1387
     /* Print out all the options including plugin supplied options */
1768
 
     my_print_help_inc_plugins(my_long_options);
 
1388
     my_print_help_inc_plugins(my_long_options, long_options);
1769
1389
  }
1770
1390
}
1771
1391
 
1792
1412
  drizzle_home[0]= pidfile_name[0]= 0;
1793
1413
  opt_tc_log_file= (char *)"tc.log";      // no hostname in tc_log file name !
1794
1414
  opt_secure_file_priv= 0;
1795
 
  segfaulted= 0;
1796
1415
  cleanup_done= 0;
1797
1416
  defaults_argc= 0;
1798
1417
  defaults_argv= 0;
1801
1420
  wake_thread=0;
1802
1421
  abort_loop= select_thread_in_use= false;
1803
1422
  ready_to_exit= shutdown_in_progress= 0;
1804
 
  aborted_threads= aborted_connects= 0;
1805
 
  max_used_connections= 0;
1806
1423
  drizzled_user= drizzled_chroot= 0;
1807
 
  memset(&global_status_var, 0, sizeof(global_status_var));
 
1424
  memset(&current_global_counters, 0, sizeof(current_global_counters));
1808
1425
  key_map_full.set();
1809
1426
 
1810
1427
  /* Character sets */
1817
1434
  drizzle_home_ptr= drizzle_home;
1818
1435
  pidfile_name_ptr= pidfile_name;
1819
1436
  language_ptr= language;
1820
 
  drizzle_data_home= drizzle_real_data_home;
 
1437
  data_home= data_home_real;
1821
1438
  session_startup_options= (OPTION_AUTO_IS_NULL | OPTION_SQL_NOTES);
1822
1439
  refresh_version= 1L;  /* Increments on each reload */
1823
1440
  global_thread_id= 1UL;
1825
1442
 
1826
1443
  /* Set directory paths */
1827
1444
  strncpy(language, LANGUAGE, sizeof(language)-1);
1828
 
  strncpy(drizzle_real_data_home, get_relative_path(LOCALSTATEDIR),
1829
 
          sizeof(drizzle_real_data_home)-1);
1830
 
  drizzle_data_home_buff[0]=FN_CURLIB;  // all paths are relative from here
1831
 
  drizzle_data_home_buff[1]=0;
1832
 
  drizzle_data_home_len= 2;
 
1445
  strncpy(data_home_real, get_relative_path(LOCALSTATEDIR),
 
1446
          sizeof(data_home_real)-1);
 
1447
  data_home_buff[0]=FN_CURLIB;  // all paths are relative from here
 
1448
  data_home_buff[1]=0;
 
1449
  data_home_len= 2;
1833
1450
 
1834
1451
  /* Variables in libraries */
1835
1452
  default_character_set_name= "utf8";
1844
1461
  max_system_variables.select_limit=    (uint64_t) HA_POS_ERROR;
1845
1462
  global_system_variables.max_join_size= (uint64_t) HA_POS_ERROR;
1846
1463
  max_system_variables.max_join_size=   (uint64_t) HA_POS_ERROR;
 
1464
  opt_scheduler_default= (char*) "multi_thread";
1847
1465
 
1848
1466
  /* Variables that depends on compile options */
1849
1467
#ifdef HAVE_BROKEN_REALPATH
1876
1494
      default_collation_name= 0;
1877
1495
    break;
1878
1496
  case 'h':
1879
 
    strncpy(drizzle_real_data_home,argument, sizeof(drizzle_real_data_home)-1);
 
1497
    strncpy(data_home_real,argument, sizeof(data_home_real)-1);
1880
1498
    /* Correct pointer set by my_getopt (for embedded library) */
1881
 
    drizzle_data_home= drizzle_real_data_home;
1882
 
    drizzle_data_home_len= strlen(drizzle_data_home);
 
1499
    data_home= data_home_real;
 
1500
    data_home_len= strlen(data_home);
1883
1501
    break;
1884
1502
  case 'u':
1885
1503
    if (!drizzled_user || !strcmp(drizzled_user, argument))
1971
1589
  /* Don't print warnings for --loose options during bootstrap */
1972
1590
  if (level == ERROR_LEVEL || global_system_variables.log_warnings)
1973
1591
  {
1974
 
    plugin::ErrorMessage::vprintf(current_session, ERROR_LEVEL, format, args);
 
1592
    plugin::ErrorMessage::vprintf(NULL, ERROR_LEVEL, format, args);
1975
1593
  }
1976
1594
  va_end(args);
1977
1595
}
2064
1682
    pos[0]= FN_LIBCHAR;
2065
1683
    pos[1]= 0;
2066
1684
  }
2067
 
  internal::convert_dirname(drizzle_real_data_home,drizzle_real_data_home,NULL);
2068
 
  (void) internal::fn_format(buff, drizzle_real_data_home, "", "",
 
1685
  internal::convert_dirname(data_home_real,data_home_real,NULL);
 
1686
  (void) internal::fn_format(buff, data_home_real, "", "",
2069
1687
                   (MY_RETURN_REAL_PATH|MY_RESOLVE_SYMLINKS));
2070
 
  (void) internal::unpack_dirname(drizzle_unpacked_real_data_home, buff);
 
1688
  (void) internal::unpack_dirname(data_home_real_unpacked, buff);
2071
1689
  internal::convert_dirname(language,language,NULL);
2072
1690
  (void) internal::my_load_path(drizzle_home, drizzle_home,""); // Resolve current dir
2073
 
  (void) internal::my_load_path(drizzle_real_data_home, drizzle_real_data_home,drizzle_home);
2074
 
  (void) internal::my_load_path(pidfile_name, pidfile_name,drizzle_real_data_home);
 
1691
  (void) internal::my_load_path(data_home_real, data_home_real,drizzle_home);
 
1692
  (void) internal::my_load_path(pidfile_name, pidfile_name,data_home_real);
2075
1693
 
2076
1694
  if (opt_plugin_dir_ptr == NULL)
2077
1695
  {
2098
1716
    {
2099
1717
      progdir.assign(progdir.substr(0, progdir.rfind(".libs/")));
2100
1718
    }
2101
 
    string testfile(progdir);
2102
 
    testfile.append("drizzled.o");
 
1719
    string testlofile(progdir);
 
1720
    testlofile.append("drizzled.lo");
 
1721
    string testofile(progdir);
 
1722
    testofile.append("drizzled.o");
2103
1723
    struct stat testfile_stat;
2104
 
    if (stat(testfile.c_str(), &testfile_stat))
 
1724
    if (stat(testlofile.c_str(), &testfile_stat) && stat(testofile.c_str(), &testfile_stat))
2105
1725
    {
2106
 
      /* drizzled.o doesn't exist - we are not in a source dir.
 
1726
      /* neither drizzled.lo or drizzled.o exist - we are not in a source dir.
2107
1727
       * Go on as usual
2108
1728
       */
2109
1729
      (void) internal::my_load_path(opt_plugin_dir, get_relative_path(PKGPLUGINDIR),
2135
1755
  internal::convert_dirname(buff,buff,NULL);
2136
1756
  (void) internal::my_load_path(language,language,buff);
2137
1757
 
 
1758
  if (not opt_help and not opt_help_extended)
2138
1759
  {
2139
1760
    char *tmp_string;
2140
1761
    struct stat buf;
2142
1763
    tmp_string= getenv("TMPDIR");
2143
1764
 
2144
1765
    if (opt_drizzle_tmpdir)
2145
 
      drizzle_tmpdir= strdup(opt_drizzle_tmpdir);
 
1766
    {
 
1767
      drizzle_tmpdir.append(opt_drizzle_tmpdir);
 
1768
    }
2146
1769
    else if (tmp_string == NULL)
2147
 
      drizzle_tmpdir= strdup(P_tmpdir);
 
1770
    {
 
1771
      drizzle_tmpdir.append(data_home);
 
1772
    }
2148
1773
    else
2149
 
      drizzle_tmpdir= strdup(tmp_string);
2150
 
 
2151
 
    assert(drizzle_tmpdir);
2152
 
 
2153
 
    if (stat(drizzle_tmpdir, &buf) || (S_ISDIR(buf.st_mode) == false))
2154
 
    {
 
1774
    {
 
1775
      drizzle_tmpdir.append(tmp_string);
 
1776
    }
 
1777
 
 
1778
    assert(drizzle_tmpdir.size());
 
1779
    if (stat(drizzle_tmpdir.c_str(), &buf) || (S_ISDIR(buf.st_mode) == false))
 
1780
    {
 
1781
      perror(drizzle_tmpdir.c_str());
 
1782
      exit(1);
 
1783
    }
 
1784
 
 
1785
    drizzle_tmpdir.append(FN_ROOTDIR);
 
1786
    drizzle_tmpdir.append(GLOBAL_TEMPORARY_EXT);
 
1787
 
 
1788
    if (mkdir(drizzle_tmpdir.c_str(), 0777) == -1)
 
1789
    {
 
1790
      if (errno != EEXIST)
 
1791
      {
 
1792
        perror(drizzle_tmpdir.c_str());
 
1793
        exit(1);
 
1794
      }
 
1795
    }
 
1796
 
 
1797
    if (stat(drizzle_tmpdir.c_str(), &buf) || (S_ISDIR(buf.st_mode) == false))
 
1798
    {
 
1799
      perror(drizzle_tmpdir.c_str());
2155
1800
      exit(1);
2156
1801
    }
2157
1802
  }
2172
1817
 
2173
1818
} /* namespace drizzled */
2174
1819
 
2175
 
using namespace drizzled;
2176
 
 
2177
 
 
2178
 
static void init_signals(void)
2179
 
{
2180
 
  sigset_t set;
2181
 
  struct sigaction sa;
2182
 
 
2183
 
  if (!(test_flags.test(TEST_NO_STACKTRACE) || 
2184
 
        test_flags.test(TEST_CORE_ON_SIGNAL)))
2185
 
  {
2186
 
    sa.sa_flags = SA_RESETHAND | SA_NODEFER;
2187
 
    sigemptyset(&sa.sa_mask);
2188
 
    sigprocmask(SIG_SETMASK,&sa.sa_mask,NULL);
2189
 
 
2190
 
    init_stacktrace();
2191
 
    sa.sa_handler=handle_segfault;
2192
 
    sigaction(SIGSEGV, &sa, NULL);
2193
 
    sigaction(SIGABRT, &sa, NULL);
2194
 
#ifdef SIGBUS
2195
 
    sigaction(SIGBUS, &sa, NULL);
2196
 
#endif
2197
 
    sigaction(SIGILL, &sa, NULL);
2198
 
    sigaction(SIGFPE, &sa, NULL);
2199
 
  }
2200
 
 
2201
 
  if (test_flags.test(TEST_CORE_ON_SIGNAL))
2202
 
  {
2203
 
    /* Change limits so that we will get a core file */
2204
 
    struct rlimit rl;
2205
 
    rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
2206
 
    if (setrlimit(RLIMIT_CORE, &rl) && global_system_variables.log_warnings)
2207
 
        errmsg_printf(ERRMSG_LVL_WARN,
2208
 
                      _("setrlimit could not change the size of core files "
2209
 
                        "to 'infinity';  We may not be able to generate a "
2210
 
                        "core file on signals"));
2211
 
  }
2212
 
  (void) sigemptyset(&set);
2213
 
  my_sigset(SIGPIPE,SIG_IGN);
2214
 
  sigaddset(&set,SIGPIPE);
2215
 
#ifndef IGNORE_SIGHUP_SIGQUIT
2216
 
  sigaddset(&set,SIGQUIT);
2217
 
  sigaddset(&set,SIGHUP);
2218
 
#endif
2219
 
  sigaddset(&set,SIGTERM);
2220
 
 
2221
 
  /* Fix signals if blocked by parents (can happen on Mac OS X) */
2222
 
  sigemptyset(&sa.sa_mask);
2223
 
  sa.sa_flags = 0;
2224
 
  sa.sa_handler = print_signal_warning;
2225
 
  sigaction(SIGTERM, &sa, (struct sigaction*) 0);
2226
 
  sa.sa_flags = 0;
2227
 
  sa.sa_handler = print_signal_warning;
2228
 
  sigaction(SIGHUP, &sa, (struct sigaction*) 0);
2229
 
#ifdef SIGTSTP
2230
 
  sigaddset(&set,SIGTSTP);
2231
 
#endif
2232
 
  if (test_flags.test(TEST_SIGINT))
2233
 
  {
2234
 
    my_sigset(thr_kill_signal, end_thread_signal);
2235
 
    // May be SIGINT
2236
 
    sigdelset(&set, thr_kill_signal);
2237
 
  }
2238
 
  else
2239
 
    sigaddset(&set,SIGINT);
2240
 
  sigprocmask(SIG_SETMASK,&set,NULL);
2241
 
  pthread_sigmask(SIG_SETMASK,&set,NULL);
2242
 
  return;;
2243
 
}
2244
 
 
2245
 
int main(int argc, char **argv)
2246
 
{
2247
 
#if defined(ENABLE_NLS)
2248
 
# if defined(HAVE_LOCALE_H)
2249
 
  setlocale(LC_ALL, "");
2250
 
# endif
2251
 
  bindtextdomain("drizzle", LOCALEDIR);
2252
 
  textdomain("drizzle");
2253
 
#endif
2254
 
 
2255
 
  plugin::Registry &plugins= plugin::Registry::singleton();
2256
 
  plugin::Client *client;
2257
 
  Session *session;
2258
 
 
2259
 
  MY_INIT(argv[0]);             // init my_sys library & pthreads
2260
 
  /* nothing should come before this line ^^^ */
2261
 
 
2262
 
  /* Set signal used to kill Drizzle */
2263
 
#if defined(SIGUSR2)
2264
 
  thr_kill_signal= internal::thd_lib_detected == THD_LIB_LT ? SIGINT : SIGUSR2;
2265
 
#else
2266
 
  thr_kill_signal= SIGINT;
2267
 
#endif
2268
 
 
2269
 
  if (init_common_variables(DRIZZLE_CONFIG_NAME,
2270
 
                            argc, argv, load_default_groups))
2271
 
    unireg_abort(1);                            // Will do exit
2272
 
 
2273
 
  init_signals();
2274
 
 
2275
 
 
2276
 
  select_thread=pthread_self();
2277
 
  select_thread_in_use=1;
2278
 
 
2279
 
  if (chdir(drizzle_real_data_home) && !opt_help)
2280
 
  {
2281
 
    errmsg_printf(ERRMSG_LVL_ERROR, _("Data directory %s does not exist\n"), drizzle_real_data_home);
2282
 
    unireg_abort(1);
2283
 
  }
2284
 
  drizzle_data_home= drizzle_data_home_buff;
2285
 
  drizzle_data_home[0]=FN_CURLIB;               // all paths are relative from here
2286
 
  drizzle_data_home[1]=0;
2287
 
  drizzle_data_home_len= 2;
2288
 
 
2289
 
  if ((user_info= check_user(drizzled_user)))
2290
 
  {
2291
 
    set_user(drizzled_user, user_info);
2292
 
  }
2293
 
 
2294
 
  if (server_id == 0)
2295
 
  {
2296
 
    server_id= 1;
2297
 
  }
2298
 
 
2299
 
  if (init_server_components(plugins))
2300
 
    unireg_abort(1);
2301
 
 
2302
 
  if (plugin::Listen::setup())
2303
 
    unireg_abort(1);
2304
 
 
2305
 
  /*
2306
 
    init signals & alarm
2307
 
    After this we can't quit by a simple unireg_abort
2308
 
  */
2309
 
  error_handler_hook= my_message_sql;
2310
 
 
2311
 
  assert(plugin::num_trx_monitored_objects > 0);
2312
 
  if (drizzle_rm_tmp_tables() ||
2313
 
      my_tz_init((Session *)0, default_tz_name))
2314
 
  {
2315
 
    abort_loop= true;
2316
 
    select_thread_in_use=0;
2317
 
    (void) pthread_kill(signal_thread, SIGTERM);
2318
 
 
2319
 
    (void) unlink(pidfile_name);        // Not needed anymore
2320
 
 
2321
 
    exit(1);
2322
 
  }
2323
 
 
2324
 
  init_status_vars();
2325
 
 
2326
 
  errmsg_printf(ERRMSG_LVL_INFO, _(ER(ER_STARTUP)), internal::my_progname,
2327
 
                PANDORA_RELEASE_VERSION, COMPILATION_COMMENT);
2328
 
 
2329
 
 
2330
 
  /* Listen for new connections and start new session for each connection
2331
 
     accepted. The listen.getClient() method will return NULL when the server
2332
 
     should be shutdown. */
2333
 
  while ((client= plugin::Listen::getClient()) != NULL)
2334
 
  {
2335
 
    if (!(session= new Session(client)))
2336
 
    {
2337
 
      delete client;
2338
 
      continue;
2339
 
    }
2340
 
 
2341
 
    /* If we error on creation we drop the connection and delete the session. */
2342
 
    if (session->schedule())
2343
 
      Session::unlink(session);
2344
 
  }
2345
 
 
2346
 
  /* (void) pthread_attr_destroy(&connection_attrib); */
2347
 
 
2348
 
 
2349
 
  (void) pthread_mutex_lock(&LOCK_thread_count);
2350
 
  select_thread_in_use=0;                       // For close_connections
2351
 
  (void) pthread_mutex_unlock(&LOCK_thread_count);
2352
 
  (void) pthread_cond_broadcast(&COND_thread_count);
2353
 
 
2354
 
  /* Wait until cleanup is done */
2355
 
  (void) pthread_mutex_lock(&LOCK_thread_count);
2356
 
  while (!ready_to_exit)
2357
 
    pthread_cond_wait(&COND_server_end,&LOCK_thread_count);
2358
 
  (void) pthread_mutex_unlock(&LOCK_thread_count);
2359
 
 
2360
 
  clean_up(1);
2361
 
  plugin::Registry::shutdown();
2362
 
  clean_up_mutexes();
2363
 
  internal::my_end();
2364
 
  return 0;
2365
 
}
2366